import { type Options as KYOptions } from 'ky'; import { type ChatParams, type ChatResponse, type ChatStreamResponse, type CompletionParams, type CompletionResponse, type EmbeddingParams, type EmbeddingResponse, type OpenAIClient } from 'openai-fetch'; import { type ChatModel } from './chat.js'; import { type CompletionModel } from './completion.js'; import { type EmbeddingModel } from './embedding.js'; import { type AbstractModel } from './model.js'; import { type SparseVectorModel } from './sparse-vector.js'; type InnerType = T extends ReadableStream ? U : never; /** All possible message types except Refusal, which we throw errors for instead. */ export type Msg = Msg.System | Msg.Developer | Msg.User | Msg.Assistant | Msg.Refusal | Msg.FuncCall | Msg.FuncResult | Msg.ToolCall | Msg.ToolResult; export declare namespace Msg { namespace Call { /** The name and arguments of a function that should be called, as generated by the model. */ type Function = { /** The arguments to call the function with, as generated by the model in JSON format. */ arguments: string; /** The name of the function to call. */ name: string; }; /** The tool calls generated by the model, such as function calls. */ type Tool = { /** The ID of the tool call. */ id: string; /** The type of the tool. Currently, only `function` is supported. */ type: 'function'; /** The function that the model called. */ function: Call.Function; }; } /** Message with text content for the system. */ type System = { role: 'system'; content: string; name?: string; }; /** * Developer-provided instructions that the model should follow. * Only supported by the `o1` and newer models. */ type Developer = { role: 'developer'; content: string; name?: string; }; /** Message with text content from the user. */ type User = { role: 'user'; name?: string; content: string; }; /** Message with text content from the assistant. */ type Assistant = { role: 'assistant'; name?: string; content: string; }; /** Message with a refusal reason and no content. */ type Refusal = { role: 'assistant'; refusal: string; content?: null; }; /** Message with arguments to call a function. */ type FuncCall = { role: 'assistant'; name?: string; content: null; function_call: Call.Function; }; /** Message with the result of a function call. */ type FuncResult = { role: 'function'; name: string; content: string; }; /** Message with arguments to call one or more tools. */ type ToolCall = { role: 'assistant'; name?: string; content: null; tool_calls: Call.Tool[]; }; /** Message with the result of a tool call. */ type ToolResult = { role: 'tool'; tool_call_id: string; content: string; }; } /** * Generic Model extended by provider specific implementations. */ export declare namespace Model { /** * Base model */ namespace Base { /** Client for making API calls. Extended by specific model clients. */ type Client = any; interface Config { model: string; } interface Run { [key: string]: any; requestOpts?: { signal?: AbortSignal; headers?: KYOptions['headers']; }; } interface Params extends Config, Run { } interface Response { cached: boolean; latency?: number; cost?: number; } type Model = AbstractModel; } /** * Chat Model */ namespace Chat { export type Client = { createChatCompletion: OpenAIClient['createChatCompletion']; streamChatCompletion: OpenAIClient['streamChatCompletion']; }; export interface Run extends Base.Run { messages: Msg[]; } export interface Config extends Base.Config { /** Handle new chunk from streaming requests. */ handleUpdate?: (chunk: string) => void; frequency_penalty?: ChatParams['frequency_penalty']; function_call?: ChatParams['function_call']; functions?: ChatParams['functions']; logit_bias?: ChatParams['logit_bias']; max_tokens?: ChatParams['max_tokens']; model: ChatParams['model']; presence_penalty?: ChatParams['presence_penalty']; response_format?: ChatParams['response_format']; seed?: ChatParams['seed']; stop?: ChatParams['stop']; temperature?: ChatParams['temperature']; tools?: ChatParams['tools']; tool_choice?: ChatParams['tool_choice']; top_p?: ChatParams['top_p']; } export interface Response extends Base.Response, ChatResponse { message: Msg; } /** Streaming response from the OpenAI API. */ type StreamResponse = ChatStreamResponse; /** A chunk recieved from a streaming response */ export type CompletionChunk = InnerType; export type ApiResponse = ChatResponse; export type Model = ChatModel; export {}; } /** * Completion model */ namespace Completion { type Client = { createCompletions: OpenAIClient['createCompletions']; }; interface Run extends Base.Run { prompt: string | string[] | number[] | number[][] | null; } interface Config extends Base.Config, Omit { model: CompletionParams['model']; } interface Response extends Base.Response, CompletionResponse { completion: string; } type ApiResponse = CompletionResponse; type Model = CompletionModel; } /** Generic metadata object. */ type Ctx = { [key: string]: any; }; /** * Embedding Model */ namespace Embedding { export type Client = { createEmbeddings: OpenAIClient['createEmbeddings']; }; export interface Run extends Base.Run { input: string[]; } /** API request batching options */ export interface BatchOptions { maxTokensPerBatch: number; maxBatchSize: number; } /** API request throttling options */ interface ThrottleOptions { maxRequestsPerMin: number; maxConcurrentRequests: number; } export interface Config extends Base.Config, Omit { model: EmbeddingParams['model']; batch?: Partial; throttle?: Partial; } export interface Response extends Base.Response, EmbeddingResponse { embeddings: number[][]; } export type ApiResponse = EmbeddingResponse; export type Model = EmbeddingModel; export {}; } /** * Event handlers for logging and debugging */ interface Events { onStart?: ((event: { timestamp: string; modelType: Type; modelProvider: Provider; params: Readonly; context: Readonly; }) => void | Promise)[]; onApiResponse?: ((event: { timestamp: string; modelType: Type; modelProvider: Provider; params: Readonly; response: Readonly; latency: number; context: Readonly; }) => void | Promise)[]; onComplete?: ((event: { timestamp: string; modelType: Type; modelProvider: Provider; params: Readonly; response: Readonly; context: Readonly; cached: boolean; }) => void | Promise)[]; onError?: ((event: { timestamp: string; modelType: Type; modelProvider: Provider; params: Readonly; error: unknown; context: Readonly; }) => void | Promise)[]; } /** * Generic interface for a model tokenizer */ interface ITokenizer { /** Tokenize a string into an array of integer tokens */ encode(text: string): Uint32Array; /** Decode an array of integer tokens into a string */ decode(tokens: number[] | Uint32Array): string; /** * Count the number of tokens in a string or message(s). * A single Msg is counted as a completion and an array as a prompt. * Strings are counted as is. */ countTokens(input?: string | Msg | Msg[]): number; /** Truncate a string to a maximum number of tokens */ truncate(args: { /** Text to truncate */ text: string; /** Maximum number of tokens to keep (inclusive) */ max: number; /** Truncate from the start or end of the text */ from?: 'start' | 'end'; }): string; } /** The provider of the model (eg: OpenAI) */ type Provider = (string & {}) | 'openai' | 'custom'; /** * Sparse vector model (SPLADE) */ namespace SparseVector { type Client = { createSparseVector: (params: { input: string; model: string; requestOpts?: { headers?: KYOptions['headers']; }; }, serviceUrl: string) => Promise; }; /** Sparse vector from SPLADE models. */ type Vector = { indices: number[]; values: number[]; }; interface Run extends Model.Base.Run { input: string[]; } interface Config extends Model.Base.Config { concurrency?: number; throttleLimit?: number; throttleInterval?: number; } interface Response extends Model.Base.Response { vectors: Vector[]; } type ApiResponse = Vector; type Model = SparseVectorModel; } /** The type of data returned by the model */ type Type = (string & {}) | 'base' | 'completion' | 'chat' | 'embedding' | 'sparse-vector'; } export {};