import type { Tokenizers } from "@llamaindex/env"; import type { NodeWithScore } from "../Node.js"; import type { BaseEvent } from "../internal/type.js"; import type { BaseTool, JSONObject, ToolOutput, UUID } from "../types.js"; export type RetrievalStartEvent = BaseEvent<{ query: MessageContent; }>; export type RetrievalEndEvent = BaseEvent<{ query: MessageContent; nodes: NodeWithScore[]; }>; export type LLMStartEvent = BaseEvent<{ id: UUID; messages: ChatMessage[]; }>; export type LLMToolCallEvent = BaseEvent<{ toolCall: ToolCall; }>; export type LLMToolResultEvent = BaseEvent<{ toolCall: ToolCall; toolResult: ToolOutput; }>; export type LLMEndEvent = BaseEvent<{ id: UUID; response: ChatResponse; }>; export type LLMStreamEvent = BaseEvent<{ id: UUID; chunk: ChatResponseChunk; }>; /** * @internal */ export interface LLMChat { chat(params: LLMChatParamsStreaming | LLMChatParamsNonStreaming): Promise | AsyncIterable>>; } /** * Unified language model interface */ export 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; } export type MessageType = "user" | "assistant" | "system" | "memory"; export type TextChatMessage = { content: string; role: MessageType; options?: undefined | AdditionalMessageOptions; }; export type ChatMessage = { content: MessageContent; role: MessageType; options?: undefined | AdditionalMessageOptions; }; export 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; } export type ChatResponseChunk = { raw: object | null; delta: string; options?: undefined | AdditionalMessageOptions; }; export 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; } export type LLMMetadata = { model: string; temperature: number; topP: number; maxTokens?: number; contextWindow: number; tokenizer: Tokenizers | undefined; }; export interface LLMChatParamsBase { messages: ChatMessage[]; additionalChatOptions?: AdditionalChatOptions; tools?: BaseTool[]; } export interface LLMChatParamsStreaming extends LLMChatParamsBase { stream: true; } export interface LLMChatParamsNonStreaming extends LLMChatParamsBase { stream?: false; } export interface LLMCompletionParamsBase { prompt: MessageContent; } export interface LLMCompletionParamsStreaming extends LLMCompletionParamsBase { stream: true; } export interface LLMCompletionParamsNonStreaming extends LLMCompletionParamsBase { stream?: false | null; } export type MessageContentTextDetail = { type: "text"; text: string; }; export type MessageContentImageDetail = { type: "image_url"; image_url: { url: string; }; }; export type MessageContentDetail = MessageContentTextDetail | MessageContentImageDetail; /** * Extended type for the content of a message that allows for multi-modal messages. */ export type MessageContent = string | MessageContentDetail[]; export type ToolCall = { name: string; input: JSONObject; id: string; }; export type PartialToolCall = { name: string; id: string; input: string; }; export type ToolResult = { id: string; result: string; isError: boolean; }; export type ToolCallOptions = { toolCall: (ToolCall | PartialToolCall)[]; }; export type ToolResultOptions = { toolResult: ToolResult; }; export type ToolCallLLMMessageOptions = ToolResultOptions | ToolCallOptions | {};