UNPKG

@promptbook/remote-server

Version:

Promptbook: Create persistent AI agents that turn your company's scattered knowledge into action

103 lines (102 loc) 4.35 kB
import type OpenAI from 'openai'; import type { AvailableModel } from '../../execution/AvailableModel'; import type { CallChatModelStreamOptions, LlmExecutionTools } from '../../execution/LlmExecutionTools'; import type { ChatPromptResult, CompletionPromptResult, EmbeddingPromptResult, ImagePromptResult } from '../../execution/PromptResult'; import type { Usage } from '../../execution/Usage'; import type { Prompt } from '../../types/Prompt'; import type { string_markdown, string_markdown_text } from '../../types/string_markdown'; import type { string_model_name } from '../../types/string_model_name'; import type { string_title } from '../../types/string_title'; import { computeOpenAiUsage } from './computeOpenAiUsage'; import type { OpenAiCompatibleExecutionToolsNonProxiedOptions } from './OpenAiCompatibleExecutionToolsOptions'; /** * Execution Tools for calling OpenAI API or other OpenAI compatible provider * * @public exported from `@promptbook/openai` */ export declare abstract class OpenAiCompatibleExecutionTools implements LlmExecutionTools { protected readonly options: OpenAiCompatibleExecutionToolsNonProxiedOptions; /** * OpenAI client lifecycle and shared request execution behavior. */ private readonly requestManager; /** * Live/hardcoded model lookup shared by all provider variants. */ private readonly modelCatalog; /** * Completion, embedding, and image-generation prompt execution. */ private readonly nonChatPromptCaller; /** * Creates OpenAI compatible Execution Tools. * * @param options which are relevant are directly passed to the OpenAI compatible client */ constructor(options: OpenAiCompatibleExecutionToolsNonProxiedOptions); abstract get title(): string_title & string_markdown_text; abstract get description(): string_markdown; getClient(): Promise<OpenAI>; /** * Check the `options` passed to `constructor` */ checkConfiguration(): Promise<void>; /** * List all available OpenAI compatible models that can be used */ listModels(): Promise<ReadonlyArray<AvailableModel>>; /** * Calls OpenAI compatible API to use a chat model. */ callChatModel(prompt: Prompt): Promise<ChatPromptResult>; /** * Calls OpenAI compatible API to use a chat model with streaming. */ callChatModelStream(prompt: Prompt, onProgress: (chunk: ChatPromptResult) => void, _options?: CallChatModelStreamOptions): Promise<ChatPromptResult>; /** * Executes one OpenAI request under the shared rate limiter and network retry policy. */ private executeRateLimitedRequest; /** * Calls OpenAI API to use a complete model. */ callCompletionModel(prompt: Pick<Prompt, 'content' | 'parameters' | 'modelRequirements'>): Promise<CompletionPromptResult>; /** * Calls OpenAI compatible API to use a embedding model */ callEmbeddingModel(prompt: Pick<Prompt, 'content' | 'parameters' | 'modelRequirements'>): Promise<EmbeddingPromptResult>; /** * Calls OpenAI compatible API to use a image generation model */ callImageGenerationModel(prompt: Prompt): Promise<ImagePromptResult>; /** * Get the model that should be used as default */ protected getDefaultModel(defaultModelName: string_model_name): AvailableModel; /** * List all available models (non dynamically) * * Note: Purpose of this is to provide more information about models than standard listing from API */ protected abstract get HARDCODED_MODELS(): ReadonlyArray<AvailableModel>; /** * Computes the usage of the OpenAI API based on the response from OpenAI Compatible API */ protected abstract computeUsage(...args: Parameters<typeof computeOpenAiUsage>): Usage; /** * Default model for chat variant. */ protected abstract getDefaultChatModel(): AvailableModel; /** * Default model for completion variant. */ protected abstract getDefaultCompletionModel(): AvailableModel; /** * Default model for completion variant. */ protected abstract getDefaultEmbeddingModel(): AvailableModel; /** * Default model for image generation variant. */ protected abstract getDefaultImageGenerationModel(): AvailableModel; }