import { ZodSchema, ZodInfer } from '../../../zod/dist/index.js'; import { Logger } from '@llamaindex/env'; import { Tokenizers } from '@llamaindex/env/tokenizers'; import { JSONSchemaType } from 'ajv'; import { JSONObject, JSONValue } from '../../../global/dist/index.js'; /** * @internal */ interface LLMChat { chat(params: LLMChatParamsStreaming | LLMChatParamsNonStreaming): Promise | AsyncIterable>>; } /** * Unified language model interface */ interface LLM extends LLMChat { metadata: LLMMetadata; /** * Get a chat response from the LLM */ chat(params: LLMChatParamsStreaming): Promise>; chat(params: LLMChatParamsNonStreaming): Promise>; /** * Get a prompt completion from the LLM */ complete(params: LLMCompletionParamsStreaming): Promise>; complete(params: LLMCompletionParamsNonStreaming): Promise; } type MessageType = "user" | "assistant" | "system" | "memory" | "developer"; type ChatMessage = { content: MessageContent; role: MessageType; options?: undefined | AdditionalMessageOptions; }; interface ChatResponse { message: ChatMessage; /** * Raw response from the LLM * * If LLM response an iterable of chunks, this will be an array of those chunks */ raw: object | null; } type ChatResponseChunk = { raw: object | null; delta: string; options?: undefined | AdditionalMessageOptions; }; interface ExecResponse { newMessages: ChatMessage[]; toolCalls: ToolCall[]; object?: O | undefined; } interface ExecStreamResponse { stream: AsyncIterable>; newMessages(): ChatMessage[]; toolCalls: ToolCall[]; object?: O | undefined; } interface CompletionResponse { text: string; /** * Raw response from the LLM * * It's possible that this is `null` if the LLM response an iterable of chunks */ raw: object | null; } type LLMMetadata = { model: string; temperature: number; topP: number; maxTokens?: number | undefined; contextWindow: number; tokenizer: Tokenizers | undefined; structuredOutput: boolean; }; interface LLMChatParamsBase { messages: ChatMessage[]; additionalChatOptions?: AdditionalChatOptions | undefined; tools?: BaseTool[] | undefined; responseFormat?: Schema | object | undefined; logger?: Logger | undefined; } interface LLMChatParamsStreaming extends LLMChatParamsBase { stream: true; } interface LLMChatParamsNonStreaming extends LLMChatParamsBase { stream?: false; } interface LLMCompletionParamsBase { prompt: MessageContent; responseFormat?: ZodSchema | object; } interface LLMCompletionParamsStreaming extends LLMCompletionParamsBase { stream: true; } interface LLMCompletionParamsNonStreaming extends LLMCompletionParamsBase { stream?: false | null | undefined; } type MessageContentTextDetail = { type: "text"; text: string; }; type MessageContentImageDetail = { type: "image_url"; image_url: { url: string; }; detail?: "high" | "low" | "auto"; }; type MessageContentAudioDetail = { type: "audio"; data: string; mimeType: string; }; type MessageContentVideoDetail = { type: "video"; data: string; mimeType: string; }; type MessageContentImageDataDetail = { type: "image"; data: string; mimeType: string; }; type MessageContentFileDetail = { type: "file"; data: string; mimeType: string; }; type MessageContentDetail = MessageContentTextDetail | MessageContentImageDetail | MessageContentAudioDetail | MessageContentVideoDetail | MessageContentImageDataDetail | MessageContentFileDetail; /** * Extended type for the content of a message that allows for multi-modal messages. */ type MessageContent = string | MessageContentDetail[]; type ToolCall = { name: string; input: JSONObject; id: string; }; type PartialToolCall = { name: string; id: string; input: string; }; type ToolResult = { id: string; result: string; isError: boolean; }; type ToolCallOptions = { toolCall: (ToolCall | PartialToolCall)[]; }; type ToolResultOptions = { toolResult: ToolResult; }; type ToolCallLLMMessageOptions = ToolResultOptions | ToolCallOptions | object; type Known = { [key: string]: Known; } | [Known, ...Known[]] | Known[] | number | string | boolean | null; type ToolMetadata = Record> = { description: string; name: string; /** * OpenAI uses JSON Schema to describe the parameters that a tool can take. * @link https://json-schema.org/understanding-json-schema */ parameters?: Parameters; }; /** * Simple Tool interface. Likely to change. */ interface BaseTool { /** * This could be undefined if the implementation is not provided, * which might be the case when communicating with a llm. * * @return {JSONValue | Promise} The output of the tool. */ call?: (input: Input) => JSONValue | Promise; metadata: Input extends Known ? ToolMetadata> : ToolMetadata; } declare abstract class BaseLLM implements LLM { abstract metadata: LLMMetadata; complete(params: LLMCompletionParamsStreaming): Promise>; complete(params: LLMCompletionParamsNonStreaming): Promise; abstract chat(params: LLMChatParamsStreaming): Promise>>; abstract chat(params: LLMChatParamsNonStreaming): Promise>; exec(params: LLMChatParamsStreaming): Promise>>; exec(params: LLMChatParamsNonStreaming): Promise>>; streamExec(params: LLMChatParamsStreaming): Promise>>; } declare abstract class ToolCallLLM extends BaseLLM { abstract supportToolCall: boolean; } declare class MockLLM extends ToolCallLLM { metadata: LLMMetadata; options: { timeBetweenToken: number; responseMessage: string; mockToolCallResponse?: { toolCalls: ToolCall[]; responseMessage?: string; }; }; supportToolCall: boolean; constructor(options?: { timeBetweenToken?: number; responseMessage?: string; metadata?: LLMMetadata; mockToolCallResponse?: { toolCalls: ToolCall[]; responseMessage?: string; }; }); chat(params: LLMChatParamsStreaming): Promise>; chat(params: LLMChatParamsNonStreaming): Promise>; complete(params: LLMCompletionParamsStreaming): Promise>; complete(params: LLMCompletionParamsNonStreaming): Promise; } export { MockLLM };