UNPKG

@mi-gpt/chat

Version:
111 lines (108 loc) 2.83 kB
import { kDefaultChatConfig } from './chunk-APNEFITI.js'; import { randomUUID } from 'node:crypto'; import { OpenAI } from '@mi-gpt/openai'; import { StreamResponse } from '@mi-gpt/stream'; import { deepMerge } from '@mi-gpt/utils'; import { replaceVars, toUTC8Time } from '@mi-gpt/utils/string'; var _ChatBot = class { history = []; config = {}; init(config) { this.config = deepMerge(kDefaultChatConfig, config); OpenAI.init(config == null ? void 0 : config.openai); StreamResponse.init(config == null ? void 0 : config.stream); } dispose() { this.history = []; OpenAI.dispose(); } async chat(msg) { const answer = await OpenAI.chat({ requestId: msg.id, createParams: { messages: this._getMessages(msg), stream: false } }); if (answer) { this._addMessage({ id: randomUUID(), text: answer, timestamp: Date.now(), sender: "assistant" }); } return answer; } /** * 处理用户消息,返回流式响应 */ async chatWithStream(msg, onError) { const stream = new StreamResponse(); OpenAI.chat({ requestId: msg.id, createParams: { messages: this._getMessages(msg), stream: true }, onStream: (text) => { if (stream.status === "canceled") { return OpenAI.cancel(msg.id); } stream.write(text); }, onError: async (error) => { stream.cancel(); await (onError == null ? void 0 : onError(error)); } }).then((answer) => { if (answer) { stream.flush(); this._addMessage({ id: randomUUID(), text: answer, timestamp: Date.now(), sender: "assistant" }); } else { stream.cancel(); } }); return stream; } _getMessages(msg) { const { context } = this._addMessage(msg); const messages = this.history.map((m) => ({ role: m.sender, content: m.text })); if (this.config.prompt.system) { messages.unshift({ role: "system", content: replaceVars(this.config.prompt.system, context) }); } return messages; } _addMessage(msg) { const context = { msg: msg.text, time: toUTC8Time(new Date(msg.timestamp)), ...this.config.context.vars }; const message = { ...msg, text: replaceVars(this.config.prompt[msg.sender], context) }; const maxHistoryLength = Math.max(1, this.config.context.historyMaxLength); if (this.history.length === maxHistoryLength) { this.history.shift(); } if (this.history.length < maxHistoryLength) { this.history.push(message); } return { message, context }; } }; var ChatBot = new _ChatBot(); export { ChatBot, _ChatBot };