doomiaichat
Version:
Doomisoft OpenAI
185 lines (184 loc) • 11.1 kB
JavaScript
"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 });
// import { Configuration, OpenAIApi, ChatCompletionRequestMessage } from "azure-openai"
/**
* OpenAI
*/
const openaibase_1 = __importDefault(require("./openaibase"));
const openai_1 = __importDefault(require("openai"));
// import { ChatCompletionToolChoiceOption } from "openai/resources";
class OpenAIGpt extends openaibase_1.default {
/**
* 初始化OpenAI 的聊天对象Api
*/
createOpenAI(apiKey) {
return new openai_1.default({ apiKey });
}
/**
* 获得文字的向量
* @param text
*/
getTextEmbedding(text, axiosOption) {
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 response = yield this.aiApi.embeddings.create({
model: this.embeddingmodel,
input: text,
}, axiosOption);
return { successed: true, embedding: response.data.data }; //[0].embedding };
}
catch (error) {
return { successed: false, error };
}
});
}
/**
* 向OpenAI发送一个聊天请求
* @param {*} chatText
*/
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: any = await this.aiApi.createChatCompletion({
const response = yield this.aiApi.chat.completions.create({
model: (callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.model) || this.chatModel,
messages: message,
temperature: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.temperature) || this.temperature),
max_tokens: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.maxtoken) || this.maxtoken),
top_p: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.top_p) || this.top_p),
presence_penalty: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.presence_penalty) || this.presence_penalty),
frequency_penalty: 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?.enableToolCall === 1 && callChatOption?.tools) ? callChatOption.tools : undefined,
// tool_choice: callChatOption?.enableToolCall === 1 ? 'auto' : undefined,
}, axiosOption);
// console.log('response.data', response)
return { successed: true, message: response.choices, usage: response.usage };
}
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;
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);
try {
const response = yield this.aiApi.chat.completions.create({
model: (callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.model) || this.chatModel,
messages: message,
temperature: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.temperature) || this.temperature),
max_tokens: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.maxtoken) || this.maxtoken),
top_p: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.top_p) || this.top_p),
presence_penalty: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.presence_penalty) || this.presence_penalty),
frequency_penalty: 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,
tool_choice: (callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.enableToolCall) === 1 ? 'auto' : undefined,
stream: true
}, axiosOption);
let replytext = [];
let has_tool_calls = 0, currentIndex, previous_index = -1, tool_calls = []; // 使用数组来存储工具调用
try {
for (var _d = 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;
_d = false;
try {
const chunk = _c;
const [choice] = chunk.choices, { finish_reason: finishreason, index, usage } = choice, { content, tool_calls: toolCalls } = choice.delta;
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 {
replytext.push(content);
}
let output = { successed: true, requestid, segment: content, text: replytext.join(''), finish_reason: finishreason, index, usage, has_tool_calls: has_tool_calls, tool_calls: tool_calls };
if (attach)
output = Object.assign({}, output, attach);
this.emit(finishreason ? 'chatdone' : 'chattext', output);
}
finally {
_d = true;
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_d && !_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 = OpenAIGpt;