UNPKG

aichat-core

Version:

AI 流式聊天业务核心TS模型封装及业务实现模块

48 lines (47 loc) 2.36 kB
import OpenAI from "openai"; import McpTool from "./McpTool"; import { ChatCompletionMessageParam, ChatCompletionTool } from "openai/resources/index"; import LLMStreamChoiceDeltaTooCall from "./response/LLMStreamChoiceDeltaTooCall"; import McpClient from "./McpClient"; import { ChatCompletionNamedToolChoice } from "openai/resources/chat/index"; import LLMCallBackMessage from "./response/LLMCallBackMessage"; export default class LLMClient { static readonly CONTENT_THINKING = "thinking"; static readonly TYPE_FUNCTION = "function"; static readonly TYPE_STRING = "string"; static readonly REASON_STOP = "stop"; static readonly REASON_TOO_CALLS = "tool_calls"; static readonly ROLE_AI = "assistant"; static readonly ROLE_SYSTEM = "system"; static readonly ROLE_USER = "user"; static readonly ROLE_TOOL = "tool"; static readonly THINGKING_START_TAG = "<think>"; static readonly THINGKING_END_TAG = "</think>"; private static readonly TOOLS_CALLING_DESC; private static readonly TOOLS_CALLED_DESC; llm: OpenAI; mcpClient?: McpClient; modelName: string; mcpServer: string; tools: McpTool[]; initMcpState: boolean; thinkState: boolean; private logDebug; private toolChoice; constructor(mcpServer: string, mcpClientName: string, apiKey: string, baseUrl: string, modelName: string); initMcpServer(): Promise<void>; chatStreamLLMV2(messages: ChatCompletionMessageParam[], onContentCallBack: (callBackMessage: LLMCallBackMessage) => void, onCallToolsCallBack: (_toolCalls: LLMStreamChoiceDeltaTooCall[]) => void, onCallToolResultCallBack: (name: string, result: any) => void, callBackMessage: LLMCallBackMessage, controllerRef: AbortController | null, loop?: number): Promise<void>; private genLLMStream; private filterChoiceMessage; private dealFinishReason; private reBuildMessages; private buildMessageToolCall; private dealToolCalls; private dealTextContent; listTools(): McpTool[]; convert2OpenAiTools(tools: McpTool[]): ChatCompletionTool[]; getLogDebug(): boolean; setLogDebug(logDebug?: boolean): void; getToolChoice(): "required" | OpenAI.Chat.Completions.ChatCompletionNamedToolChoice | "none" | "auto"; setToolChoice(toolChoice: "none" | "auto" | "required" | ChatCompletionNamedToolChoice): void; }