UNPKG

doomiaichat

Version:

Doomisoft OpenAI

221 lines (220 loc) 13.5 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __asyncValues = (this && this.__asyncValues) || function (o) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var m = o[Symbol.asyncIterator], i; return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /** * 微软AZure OpenAI */ const openaibase_1 = __importDefault(require("./openaibase")); const openai_1 = require("@azure/openai"); class AzureAI extends openaibase_1.default { constructor(apiKey, azureOption, apiOption = {}) { super(apiKey, apiOption); this.azureSetting = azureOption; if (!this.azureSetting.endpoint.toLowerCase().startsWith('https://')) { this.azureSetting.endpoint = 'https://' + this.azureSetting.endpoint; } } /** * 初始化OpenAI 的聊天对象Api */ createOpenAI(apiKey) { return new openai_1.OpenAIClient(this.azureSetting.endpoint, new openai_1.AzureKeyCredential(apiKey)); } get EmbeddingUrl() { return `${this.azureSetting.endpoint}/openai/deployments/${this.embeddingmodel || 'openai-embedding-ada-002'}/embeddings?api-version=2022-12-01`; } /** * 获得文字的向量 * @param text */ getTextEmbedding(text, callOption = {}) { return __awaiter(this, void 0, void 0, function* () { if (!text) return { successed: false, error: { errcode: 2, errmsg: 'content required' } }; if (!this.aiApi) this.aiApi = this.createOpenAI(this.apiKey); try { const result = yield this.aiApi.getEmbeddings(this.embeddingmodel || 'openai-embedding-ada-002', typeof text === 'string' ? [text] : text, callOption); return { successed: true, embedding: result.data }; } // if (!axiosOption.headers) // axiosOption.headers = { 'api-key': this.apiKey, 'Content-Type': 'application/json' }; // else { // axiosOption.headers['api-key'] = this.apiKey; // axiosOption.headers['Content-Type'] = 'application/json'; // } // try { // let param = { // ...axiosOption, // method: "post", // data: { // input: text // }, // url: this.EmbeddingUrl // }; // const response = await request(param) // if (response.successed && response.data) { // return { successed: true, embedding: response.data.data[0].embedding }; // } // return { successed: false, ...response.data }; catch (error) { return { successed: false, error }; } }); } /** * 非流式聊天请求 * @param _chatText * @param _paramOption * @param _axiosOption */ chatRequest(chatText, callChatOption, _axiosOption = {}) { return __awaiter(this, void 0, void 0, function* () { if (!chatText) return { successed: false, error: { errcode: 2, errmsg: '缺失聊天的内容' } }; if (!this.aiApi) this.aiApi = this.createOpenAI(this.apiKey); let message = typeof (chatText) == 'string' ? [{ role: 'user', content: chatText }] : chatText; try { const response = yield this.aiApi.getChatCompletions((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.model) || this.chatModel, message, { temperature: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.temperature) || this.temperature), maxTokens: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.maxtoken) || this.maxtoken), topP: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.top_p) || this.top_p), presencePenalty: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.presence_penalty) || this.presence_penalty), frequencyPenalty: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.frequency_penalty) || this.frequency_penalty), n: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.replyCounts) || 1) || 1, tools: ((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.enableToolCall) === 1 && (callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.tools)) ? callChatOption.tools : undefined, toolChoice: (callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.enableToolCall) === 1 ? 'auto' : undefined }); const { promptTokens: prompt_tokens, completionTokens: completion_tokens, totalTokens: total_tokens } = response.usage; let rebuildChoice = []; for (const msg of response.choices) { const { index, finishReason: finish_reason, message } = msg; rebuildChoice.push({ index, finish_reason, message }); } // if (response.data.choices[0].finish_reason === 'content_filter') { // console.log('content_filter'); // return { successed: false, error: 'content_filter' }; // } return { successed: true, message: rebuildChoice, usage: { prompt_tokens, completion_tokens, total_tokens } }; } catch (error) { console.log('result is error ', error); return { successed: false, error }; } }); } /** * 流式的聊天模式 * @param chatText * @param _paramOption * @param axiosOption */ chatRequestInStream(chatText, callChatOption, attach, axiosOption) { var _a, e_1, _b, _c; var _d, _e, _f; return __awaiter(this, void 0, void 0, function* () { if (!chatText) this.emit('chaterror', { successed: false, error: 'no text in chat' }); if (!this.aiApi) { this.aiApi = this.createOpenAI(this.apiKey); } let message = typeof (chatText) == 'string' ? [{ role: 'user', content: chatText }] : chatText; axiosOption = Object.assign({}, axiosOption || { timeout: 60000 }); let requestid = Math.ceil(Math.random() * (new Date().getTime() * Math.random()) / 1000); let has_tool_calls = 0, currentIndex, previous_index = -1, tool_calls = []; // 使用数组来存储工具调用 try { const response = yield this.aiApi.streamChatCompletions((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.model) || this.chatModel, message, { temperature: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.temperature) || this.temperature), maxTokens: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.maxtoken) || this.maxtoken), topP: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.top_p) || this.top_p), presencePenalty: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.presence_penalty) || this.presence_penalty), frequencyPenalty: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.frequency_penalty) || this.frequency_penalty), n: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.replyCounts) || 1) || 1, tools: ((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.enableToolCall) === 1 && (callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.tools)) ? callChatOption.tools : undefined, toolChoice: (callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.enableToolCall) === 1 ? 'auto' : undefined }); //console.log('tools', callChatOption.enableToolCall, callChatOption.tools) let replytext = []; try { for (var _g = true, response_1 = __asyncValues(response), response_1_1; response_1_1 = yield response_1.next(), _a = response_1_1.done, !_a;) { _c = response_1_1.value; _g = false; try { const event = _c; for (const choice of event.choices) { const { finishReason: finishreason, index } = choice; const toolCalls = (_d = choice.delta) === null || _d === void 0 ? void 0 : _d.toolCalls; ///存在了toolCalls if (toolCalls && toolCalls.length) { currentIndex = toolCalls[0].index; has_tool_calls = 1; // 检查index是否发生变化 //console.log('currentIndex,previous_index', currentIndex, previous_index) if (currentIndex !== previous_index) { tool_calls.push({ id: toolCalls[0].id, type: 'function', function: { name: toolCalls[0].function.name, arguments: toolCalls[0].function.arguments } }); // 更新previousIndex以供下次比较 previous_index = currentIndex; } else { tool_calls[previous_index].function.arguments += toolCalls[0].function.arguments; } } else { const content = (_e = choice.delta) === null || _e === void 0 ? void 0 : _e.content; replytext.push(content); } let output = { successed: true, requestid, segment: (_f = choice.delta) === null || _f === void 0 ? void 0 : _f.content, text: replytext.join(''), finish_reason: finishreason, index, has_tool_calls: has_tool_calls, tool_calls: tool_calls }; if (attach) output = Object.assign({}, output, attach); this.emit(finishreason ? 'chatdone' : 'chattext', output); } } finally { _g = true; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (!_g && !_a && (_b = response_1.return)) yield _b.call(response_1); } finally { if (e_1) throw e_1.error; } } return { successed: true, requestid }; } catch (error) { this.emit('requesterror', { successed: false, requestid, error: 'call axios faied ' + error }); return { successed: false, requestid }; } }); } } exports.default = AzureAI;