UNPKG

@zhengxs/wechaty-plugin-assistant

Version:
106 lines (104 loc) 3.26 kB
import { Tiktoken, TiktokenEncoding } from 'js-tiktoken'; import { default as Keyv } from 'keyv'; import { default as OpenAI } from 'openai'; export interface ChatLLMAPIOptions<ChatParams = Record<string, any>> { debug?: boolean; chatParams?: ChatParams; maxModelTokens?: number; maxResponseTokens?: number; messageStore?: Keyv; systemMessage?: string; userLabel?: string; assistantLabel?: string; encoding?: TiktokenEncoding; getMessageById?: GetMessageByIdFunction; upsertMessage?: UpsertMessageFunction; } export declare abstract class ChatLLMAPI<ChatParams = Record<string, any>> { protected debug: boolean; protected systemMessage?: string; protected chatParams: ChatParams; protected maxModelTokens: number; protected maxResponseTokens: number; protected userLabel: string; protected assistantLabel: string; protected getMessageById: GetMessageByIdFunction; protected upsertMessage: UpsertMessageFunction; protected messageStore: Keyv; protected tokenizer: Tiktoken; constructor(options: ChatLLMAPIOptions<ChatParams>); sendMessage(text: string, options?: SendMessageOptions<ChatParams>): Promise<ChatMessage>; protected abstract makeRequest(question: ChatMessage, answer: ChatMessage, options: SendMessageOptions<ChatParams>): Promise<void>; protected buildMessages(text: string, { parentMessageId, systemMessage, }: SendMessageOptions<ChatParams>): Promise<{ messages: OpenAI.Chat.Completions.ChatCompletionMessageParam[]; maxTokens: number; numTokens: number; }>; protected getTokenCount(text: string): Promise<number>; protected defaultGetMessageById(id: string): Promise<any>; protected defaultUpsertMessage(message: any): Promise<void>; } export type GetMessageByIdFunction = (id: string) => Promise<any>; export type UpsertMessageFunction = (message: ChatMessage) => Promise<void>; export interface MessageStoreOptions { messageStore?: Keyv; getMessageById?: GetMessageByIdFunction; upsertMessage?: UpsertMessageFunction; } export type Role = 'system' | 'user' | 'assistant'; export type ChatCompletionRequestMessage = { role: Role; content: string; name?: string; }; export interface ChatMessage { sessionId?: number | string; conversationId?: string | number; parentMessageId?: string; id: string; type: 'text' | 'image' | 'video' | 'audio'; text: string; files: string[]; role: Role; name?: string; needClearHistory?: boolean; error?: boolean; } export interface SendMessageOptions<CompletionParams = Record<string, any>> { /** * 会话 ID */ sessionId?: number | string; /** * 对话 ID */ conversationId?: string | number; /** * 上一条消息 ID */ parentMessageId?: string; /** * 当前消息 ID */ messageId?: string; /** * 系统消息 */ systemMessage?: string; /** * 其他对话参数 */ chatParams?: CompletionParams; /** * 中断信号 */ abortSignal?: any; /** * TODO 预留参数 */ onProcess?: () => void; /** * TODO 预留参数 */ stream?: boolean; }