doomiaichat 7.1.29 → 7.1.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/dist/index.esm.js +16 -0
  2. package/dist/index.js +16 -28
  3. package/dist/{aimp.d.ts → types/aimp.d.ts} +1 -0
  4. package/dist/types/aimp.d.ts.map +1 -0
  5. package/dist/{azureai.d.ts → types/azureai.d.ts} +1 -0
  6. package/dist/types/azureai.d.ts.map +1 -0
  7. package/dist/{corzauthorization.d.ts → types/corzauthorization.d.ts} +1 -0
  8. package/dist/types/corzauthorization.d.ts.map +1 -0
  9. package/dist/{corzbot.d.ts → types/corzbot.d.ts} +1 -0
  10. package/dist/types/corzbot.d.ts.map +1 -0
  11. package/dist/{declare.d.ts → types/declare.d.ts} +2 -1
  12. package/dist/types/declare.d.ts.map +1 -0
  13. package/dist/{doubaoai.d.ts → types/doubaoai.d.ts} +1 -0
  14. package/dist/types/doubaoai.d.ts.map +1 -0
  15. package/dist/{gptbase.d.ts → types/gptbase.d.ts} +1 -1
  16. package/dist/types/gptbase.d.ts.map +1 -0
  17. package/dist/{gptprovider.d.ts → types/gptprovider.d.ts} +2 -7
  18. package/dist/types/gptprovider.d.ts.map +1 -0
  19. package/dist/{index.d.ts → types/index.d.ts} +1 -0
  20. package/dist/types/index.d.ts.map +1 -0
  21. package/dist/{openai.d.ts → types/openai.d.ts} +1 -0
  22. package/dist/types/openai.d.ts.map +1 -0
  23. package/dist/{openaibase.d.ts → types/openaibase.d.ts} +1 -0
  24. package/dist/types/openaibase.d.ts.map +1 -0
  25. package/dist/{openaiproxy.d.ts → types/openaiproxy.d.ts} +1 -0
  26. package/dist/types/openaiproxy.d.ts.map +1 -0
  27. package/package.json +16 -4
  28. package/dist/aimp.js +0 -162
  29. package/dist/azureai.js +0 -220
  30. package/dist/baiduai.d.ts +0 -28
  31. package/dist/baiduai.js +0 -92
  32. package/dist/corzauthorization.js +0 -79
  33. package/dist/corzbot.js +0 -490
  34. package/dist/declare.js +0 -51
  35. package/dist/deepseek.d.ts +0 -5
  36. package/dist/deepseek.js +0 -16
  37. package/dist/doubaoai.js +0 -149
  38. package/dist/gptbase.js +0 -58
  39. package/dist/gptprovider.js +0 -80
  40. package/dist/openai.js +0 -184
  41. package/dist/openaibase.js +0 -20
  42. package/dist/openaiprovider.d.ts +0 -20
  43. package/dist/openaiprovider.js +0 -43
  44. package/dist/openaiproxy.js +0 -108
  45. package/dist/stabilityai.d.ts +0 -18
  46. package/dist/stabilityai.js +0 -75
  47. package/dist/stabilityplusai.d.ts +0 -11
  48. package/dist/stabilityplusai.js +0 -86
  49. package/src/aimp.ts +0 -125
  50. package/src/azureai.ts +0 -180
  51. package/src/baiduai.ts +0 -86
  52. package/src/corzauthorization.ts +0 -59
  53. package/src/corzbot.ts +0 -434
  54. package/src/declare.ts +0 -152
  55. package/src/deepseek.ts +0 -11
  56. package/src/doubaoai.ts +0 -129
  57. package/src/gptbase.ts +0 -52
  58. package/src/gptprovider.ts +0 -74
  59. package/src/index.ts +0 -2
  60. package/src/openai.ts +0 -136
  61. package/src/openaibase.ts +0 -30
  62. package/src/openaiproxy.ts +0 -97
  63. package/src/stabilityai.ts +0 -67
  64. package/src/stabilityplusai.ts +0 -77
  65. package/tsconfig.json +0 -31
package/dist/declare.js DELETED
@@ -1,51 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.requestStream = exports.request = void 0;
16
- const axios_1 = __importDefault(require("axios"));
17
- /**
18
- * Axios远程请求封装
19
- * @param opts
20
- * @returns
21
- */
22
- function request(opts = {}) {
23
- return __awaiter(this, void 0, void 0, function* () {
24
- if (!opts.data)
25
- opts.data = opts.body;
26
- try {
27
- let result = yield (0, axios_1.default)(opts);
28
- return { successed: true, data: result.data };
29
- }
30
- catch (err) {
31
- return { successed: false, error: err, data: err };
32
- }
33
- });
34
- }
35
- exports.request = request;
36
- /**
37
- *
38
- * @param opts
39
- * @returns
40
- */
41
- function requestStream(opts = {}, processChunkData) {
42
- if (!opts.data)
43
- opts.data = opts.body;
44
- (0, axios_1.default)(opts)
45
- .then((res) => {
46
- res.data.on('data', (chunk) => {
47
- processChunkData(chunk);
48
- });
49
- });
50
- }
51
- exports.requestStream = requestStream;
@@ -1,5 +0,0 @@
1
- import OpenAI from 'openai';
2
- import OpenAIGpt from './openai';
3
- export default class DeepSeek extends OpenAIGpt {
4
- createOpenAI(apiKey: string): OpenAI;
5
- }
package/dist/deepseek.js DELETED
@@ -1,16 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const openai_1 = __importDefault(require("openai"));
7
- const openai_2 = __importDefault(require("./openai"));
8
- class DeepSeek extends openai_2.default {
9
- createOpenAI(apiKey) {
10
- return new openai_1.default({
11
- baseURL: 'https://api.deepseek.com',
12
- apiKey
13
- });
14
- }
15
- }
16
- exports.default = DeepSeek;
package/dist/doubaoai.js DELETED
@@ -1,149 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- /**
16
- * 火山方舟-豆包大模型引擎
17
- */
18
- const declare_1 = require("./declare");
19
- const gptbase_1 = __importDefault(require("./gptbase"));
20
- class DouBaoAI extends gptbase_1.default {
21
- /**
22
- * 构造函数
23
- */
24
- constructor(apiKey, apiOption = {}) {
25
- super();
26
- this.apiOption = {};
27
- this.apiKey = apiKey;
28
- this.apiOption = apiOption;
29
- }
30
- /**
31
- * 请求接口
32
- */
33
- chatRequest(chatText, callChatOption, axiosOption = {}) {
34
- return __awaiter(this, void 0, void 0, function* () {
35
- if (!chatText)
36
- return { successed: false, error: { errcode: 2, errmsg: '缺失聊天的内容' } };
37
- const callParams = this.assembleApiParams(chatText, false, callChatOption, axiosOption);
38
- try {
39
- const response = yield (0, declare_1.request)(callParams);
40
- if (response.successed && !response.data.code)
41
- return { successed: true, message: response.data.choices, usage: response.data.usage };
42
- return Object.assign({ successed: false }, response.data);
43
- }
44
- catch (error) {
45
- console.log('result is error ', error);
46
- return { successed: false, error };
47
- }
48
- });
49
- }
50
- /**
51
- * 组装最后的调用参数
52
- * @param callChatOption
53
- * @returns
54
- */
55
- assembleApiParams(chatText, streamCall = false, callChatOption, axiosOption = {}) {
56
- let messages = typeof (chatText) == 'string' ? [{ role: 'user', content: chatText }] : chatText;
57
- let params = {};
58
- if ((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.temperature) || this.apiOption.temperature)
59
- params.temperature = Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.temperature) || this.apiOption.temperature);
60
- params.max_tokens = Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.maxtoken) || this.apiOption.maxtoken);
61
- if ((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.top_p) || this.apiOption.top_p)
62
- params.top_p = Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.top_p) || this.apiOption.top_p);
63
- if ((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.presence_penalty) || this.apiOption.presence_penalty)
64
- params.presence_penalty = Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.presence_penalty) || this.apiOption.presence_penalty);
65
- if ((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.frequency_penalty) || this.apiOption.frequency_penalty)
66
- params.frequency_penalty = Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.frequency_penalty) || this.apiOption.frequency_penalty);
67
- if ((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.top_logprobs) || this.apiOption.top_logprobs) {
68
- params.logprobs = true;
69
- params.top_logprobs = Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.top_logprobs) || this.apiOption.top_logprobs);
70
- }
71
- params.tools = ((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.enableToolCall) === 1 && (callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.tools)) ? callChatOption.tools : undefined;
72
- params.tool_choice = (callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.enableToolCall) === 1 ? 'auto' : undefined;
73
- const axiosParams = Object.assign(Object.assign({}, axiosOption), { method: "post", headers: {
74
- 'Content-Type': 'application/json',
75
- 'authorization': `Bearer ${this.apiKey}`
76
- }, data: Object.assign(Object.assign({ model: (callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.model) || this.apiOption.model }, params), { messages, stream: streamCall }), url: 'https://ark.cn-beijing.volces.com/api/v3/chat/completions' });
77
- if (streamCall)
78
- axiosParams.responseType = 'stream';
79
- return axiosParams;
80
- }
81
- /**
82
- * 流式的聊天模式
83
- * @param chatText
84
- * @param _paramOption
85
- * @param axiosOption
86
- */
87
- chatRequestInStream(chatText, callChatOption, attach, axiosOption) {
88
- return __awaiter(this, void 0, void 0, function* () {
89
- if (!chatText)
90
- this.emit('chaterror', { successed: false, error: 'no text in chat' });
91
- axiosOption = Object.assign({}, axiosOption || { timeout: 10000 });
92
- const callParams = this.assembleApiParams(chatText, true, callChatOption, axiosOption);
93
- let requestid = Math.ceil(Math.random() * (new Date().getTime() * Math.random()) / 1000), replytext = [];
94
- try {
95
- (0, declare_1.requestStream)(callParams, (chunk) => {
96
- let streamText = chunk.toString().replace('[DONE]', '').replace(/[\r\n]+/gm, '');
97
- this.processChunkData(streamText.split(/data: /), requestid, replytext, attach);
98
- });
99
- return { successed: true, requestid };
100
- }
101
- catch (error) {
102
- this.emit('requesterror', { successed: false, requestid, error: 'call axios faied ' + error });
103
- return { successed: false, requestid };
104
- }
105
- });
106
- }
107
- /**
108
- * 处理每次流式返回的对话片段
109
- * @param chunks
110
- * @param requestid
111
- * @param replytext
112
- * @param attach
113
- */
114
- processChunkData(chunks, requestid, replytext, attach) {
115
- let has_tool_calls = 0, currentIndex, previous_index = -1, tool_calls = []; // 使用数组来存储工具调用
116
- for (const splitString of chunks) {
117
- if (!splitString)
118
- continue;
119
- const chunk = JSON.parse(splitString);
120
- const [choice] = chunk.choices, { finish_reason: finishreason, index, usage } = choice, { content, tool_calls: toolCalls } = choice.delta;
121
- if (toolCalls && toolCalls.length) {
122
- currentIndex = toolCalls[0].index;
123
- has_tool_calls = 1;
124
- if (currentIndex !== previous_index) {
125
- tool_calls.push({
126
- id: toolCalls[0].id,
127
- type: 'function',
128
- function: {
129
- name: toolCalls[0].function.name,
130
- arguments: toolCalls[0].function.arguments
131
- }
132
- });
133
- // 更新previousIndex以供下次比较
134
- previous_index = currentIndex;
135
- }
136
- else {
137
- tool_calls[previous_index].function.arguments += toolCalls[0].function.arguments;
138
- }
139
- }
140
- else
141
- replytext.push(content);
142
- 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 };
143
- if (attach)
144
- output = Object.assign({}, output, attach);
145
- this.emit(finishreason ? 'chatdone' : 'chattext', output);
146
- }
147
- }
148
- }
149
- exports.default = DouBaoAI;
package/dist/gptbase.js DELETED
@@ -1,58 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const events_1 = require("events");
13
- class GptBase extends events_1.EventEmitter {
14
- /**
15
- * 构造函数
16
- */
17
- constructor() {
18
- super();
19
- }
20
- /**
21
- * 获得文字的向量
22
- * @param text
23
- */
24
- getTextEmbedding(_text, _axiosOption) { return null; }
25
- /**
26
- * 创建一个会话主题id
27
- * @returns
28
- */
29
- createCoversation(_client) {
30
- return __awaiter(this, void 0, void 0, function* () {
31
- return null;
32
- });
33
- }
34
- /**
35
- * 设置智能体的变量
36
- * @returns
37
- */
38
- setVariables(_params) {
39
- return __awaiter(this, void 0, void 0, function* () {
40
- return { successed: true };
41
- });
42
- }
43
- /**
44
- * 获取智能体的变量
45
- * @returns
46
- */
47
- getVariables(_params) {
48
- return __awaiter(this, void 0, void 0, function* () { return { successed: false }; });
49
- }
50
- /**
51
- * 流式的聊天模式
52
- * @param chatText
53
- * @param _paramOption
54
- * @param axiosOption
55
- */
56
- chatRequestInStream(_chatText, _paramOption, _attach, _axiosOption) { return null; }
57
- }
58
- exports.default = GptBase;
@@ -1,80 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.createGpt = exports.GptProviderEnum = void 0;
7
- //ts check
8
- /**
9
- * 语音转文字服务商工厂
10
- */
11
- const openai_1 = __importDefault(require("./openai"));
12
- const deepseek_1 = __importDefault(require("./deepseek"));
13
- const corzbot_1 = __importDefault(require("./corzbot"));
14
- const openaiproxy_1 = __importDefault(require("./openaiproxy"));
15
- const azureai_1 = __importDefault(require("./azureai"));
16
- const stabilityai_1 = __importDefault(require("./stabilityai"));
17
- const stabilityplusai_1 = __importDefault(require("./stabilityplusai"));
18
- const baiduai_1 = __importDefault(require("./baiduai"));
19
- const aimp_1 = __importDefault(require("./aimp"));
20
- const doubaoai_1 = __importDefault(require("./doubaoai"));
21
- const corzauthorization_1 = require("./corzauthorization");
22
- // 扣子的身份认证应用
23
- const corzAuth = new Map();
24
- /**
25
- * OpenAI/NLP 的服务提供商 OpenAI,微软,百度文心(待接入),google(待接入)
26
- */
27
- exports.GptProviderEnum = {
28
- OPENAI: 'openai',
29
- OPENAIPROXY: 'openaiproxy',
30
- MICROSOFT: 'microsoft',
31
- AIMP: 'aimp',
32
- COZE: 'coze',
33
- BAIDU: 'baidu',
34
- DOUBAO: 'doubao',
35
- DEEPSEEK: 'deepseek',
36
- GOOGLE: 'google',
37
- STABILITY: 'stability',
38
- STABILITY2: 'stability2',
39
- };
40
- /**
41
- * 根据类型创建不同的TTS引擎对象
42
- * @param {*} provider
43
- * @param {*} apikey
44
- * @param {*} setting
45
- * @returns
46
- */
47
- function createGpt(provider, apikey, setting) {
48
- let { model, agentid, maxtoken, temperature, serviceurl, endpoint, engine, version, embedding, top_p, presence_penalty, frequency_penalty } = setting || {};
49
- switch (provider) {
50
- case exports.GptProviderEnum.OPENAI:
51
- return new openai_1.default(apikey + '', { model, maxtoken, temperature, embedding, top_p, presence_penalty, frequency_penalty });
52
- case exports.GptProviderEnum.DEEPSEEK:
53
- return new deepseek_1.default(apikey + '', { model, maxtoken, temperature, embedding, top_p, presence_penalty, frequency_penalty });
54
- case exports.GptProviderEnum.OPENAIPROXY:
55
- return new openaiproxy_1.default(apikey + '', { serviceurl }, { model, maxtoken, temperature, embedding, top_p, presence_penalty, frequency_penalty });
56
- case exports.GptProviderEnum.MICROSOFT:
57
- return new azureai_1.default(apikey + '', { endpoint, engine, version }, { model, maxtoken, temperature, embedding, top_p, presence_penalty, frequency_penalty });
58
- case exports.GptProviderEnum.BAIDU:
59
- let cred = typeof (apikey) === 'string' ? { apikey, securitykey: apikey } : apikey;
60
- return new baiduai_1.default(cred);
61
- case exports.GptProviderEnum.AIMP:
62
- return new aimp_1.default(apikey + '', { endpoint, agentid });
63
- case exports.GptProviderEnum.COZE:
64
- let authorizationProvider = corzAuth.get(apikey + '');
65
- if (!authorizationProvider) {
66
- authorizationProvider = new corzauthorization_1.CorzAuthorization(apikey + '', setting);
67
- corzAuth.set(apikey + '', authorizationProvider);
68
- }
69
- return new corzbot_1.default(authorizationProvider, setting);
70
- case exports.GptProviderEnum.DOUBAO:
71
- return new doubaoai_1.default(apikey + '', { model, maxtoken, temperature, top_p, presence_penalty, frequency_penalty });
72
- case exports.GptProviderEnum.STABILITY:
73
- return new stabilityai_1.default(apikey + '', { endpoint, engine }, setting);
74
- case exports.GptProviderEnum.STABILITY2:
75
- return new stabilityplusai_1.default(apikey + '', { endpoint, engine }, setting);
76
- default: return null;
77
- }
78
- }
79
- exports.createGpt = createGpt;
80
- ;
package/dist/openai.js DELETED
@@ -1,184 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __asyncValues = (this && this.__asyncValues) || function (o) {
12
- if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
13
- var m = o[Symbol.asyncIterator], i;
14
- 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);
15
- 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); }); }; }
16
- function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
17
- };
18
- var __importDefault = (this && this.__importDefault) || function (mod) {
19
- return (mod && mod.__esModule) ? mod : { "default": mod };
20
- };
21
- Object.defineProperty(exports, "__esModule", { value: true });
22
- // import { Configuration, OpenAIApi, ChatCompletionRequestMessage } from "azure-openai"
23
- /**
24
- * OpenAI
25
- */
26
- const openaibase_1 = __importDefault(require("./openaibase"));
27
- const openai_1 = __importDefault(require("openai"));
28
- // import { ChatCompletionToolChoiceOption } from "openai/resources";
29
- class OpenAIGpt extends openaibase_1.default {
30
- /**
31
- * 初始化OpenAI 的聊天对象Api
32
- */
33
- createOpenAI(apiKey) {
34
- return new openai_1.default({ apiKey });
35
- }
36
- /**
37
- * 获得文字的向量
38
- * @param text
39
- */
40
- getTextEmbedding(text, axiosOption) {
41
- return __awaiter(this, void 0, void 0, function* () {
42
- if (!text)
43
- return { successed: false, error: { errcode: 2, errmsg: 'content required' } };
44
- if (!this.aiApi) {
45
- this.aiApi = this.createOpenAI(this.apiKey);
46
- }
47
- try {
48
- const response = yield this.aiApi.embeddings.create({
49
- model: this.embeddingmodel,
50
- input: text,
51
- }, axiosOption);
52
- return { successed: true, embedding: response.data.data }; //[0].embedding };
53
- }
54
- catch (error) {
55
- return { successed: false, error };
56
- }
57
- });
58
- }
59
- /**
60
- * 向OpenAI发送一个聊天请求
61
- * @param {*} chatText
62
- */
63
- chatRequest(chatText, callChatOption, axiosOption = {}) {
64
- return __awaiter(this, void 0, void 0, function* () {
65
- if (!chatText)
66
- return { successed: false, error: { errcode: 2, errmsg: '缺失聊天的内容' } };
67
- if (!this.aiApi)
68
- this.aiApi = this.createOpenAI(this.apiKey);
69
- let message = typeof (chatText) == 'string' ?
70
- [{ role: 'user', content: chatText }] : chatText;
71
- try {
72
- // const response: any = await this.aiApi.createChatCompletion({
73
- const response = yield this.aiApi.chat.completions.create({
74
- model: (callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.model) || this.chatModel,
75
- messages: message,
76
- temperature: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.temperature) || this.temperature),
77
- max_tokens: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.maxtoken) || this.maxtoken),
78
- top_p: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.top_p) || this.top_p),
79
- presence_penalty: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.presence_penalty) || this.presence_penalty),
80
- frequency_penalty: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.frequency_penalty) || this.frequency_penalty),
81
- n: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.replyCounts) || 1) || 1,
82
- // tools: (callChatOption?.enableToolCall === 1 && callChatOption?.tools) ? callChatOption.tools : undefined,
83
- // tool_choice: callChatOption?.enableToolCall === 1 ? 'auto' : undefined,
84
- }, axiosOption);
85
- // console.log('response.data', response)
86
- return { successed: true, message: response.choices, usage: response.usage };
87
- }
88
- catch (error) {
89
- console.log('result is error ', error);
90
- return { successed: false, error };
91
- }
92
- });
93
- }
94
- /**
95
- * 流式的聊天模式
96
- * @param chatText
97
- * @param _paramOption
98
- * @param axiosOption
99
- */
100
- chatRequestInStream(chatText, callChatOption, attach, axiosOption) {
101
- var _a, e_1, _b, _c;
102
- return __awaiter(this, void 0, void 0, function* () {
103
- if (!chatText)
104
- this.emit('chaterror', { successed: false, error: 'no text in chat' });
105
- if (!this.aiApi) {
106
- this.aiApi = this.createOpenAI(this.apiKey);
107
- }
108
- let message = typeof (chatText) == 'string' ? [{ role: 'user', content: chatText }] : chatText;
109
- axiosOption = Object.assign({}, axiosOption || { timeout: 60000 });
110
- let requestid = Math.ceil(Math.random() * (new Date().getTime() * Math.random()) / 1000);
111
- try {
112
- const response = yield this.aiApi.chat.completions.create({
113
- model: (callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.model) || this.chatModel,
114
- messages: message,
115
- temperature: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.temperature) || this.temperature),
116
- max_tokens: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.maxtoken) || this.maxtoken),
117
- top_p: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.top_p) || this.top_p),
118
- presence_penalty: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.presence_penalty) || this.presence_penalty),
119
- frequency_penalty: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.frequency_penalty) || this.frequency_penalty),
120
- n: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.replyCounts) || 1) || 1,
121
- tools: ((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.enableToolCall) === 1 && (callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.tools)) ? callChatOption.tools : undefined,
122
- tool_choice: (callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.enableToolCall) === 1 ? 'auto' : undefined,
123
- stream: true
124
- }, axiosOption);
125
- let replytext = [];
126
- let has_tool_calls = 0, currentIndex, previous_index = -1, tool_calls = []; // 使用数组来存储工具调用
127
- try {
128
- for (var _d = true, response_1 = __asyncValues(response), response_1_1; response_1_1 = yield response_1.next(), _a = response_1_1.done, !_a;) {
129
- _c = response_1_1.value;
130
- _d = false;
131
- try {
132
- const chunk = _c;
133
- const [choice] = chunk.choices, { finish_reason: finishreason, index, usage } = choice, { content, tool_calls: toolCalls } = choice.delta;
134
- if (toolCalls && toolCalls.length) {
135
- currentIndex = toolCalls[0].index;
136
- has_tool_calls = 1;
137
- // 检查index是否发生变化
138
- //console.log('currentIndex,previous_index', currentIndex, previous_index)
139
- if (currentIndex !== previous_index) {
140
- tool_calls.push({
141
- id: toolCalls[0].id,
142
- type: 'function',
143
- function: {
144
- name: toolCalls[0].function.name,
145
- arguments: toolCalls[0].function.arguments
146
- }
147
- });
148
- // 更新previousIndex以供下次比较
149
- previous_index = currentIndex;
150
- }
151
- else {
152
- tool_calls[previous_index].function.arguments += toolCalls[0].function.arguments;
153
- }
154
- }
155
- else {
156
- replytext.push(content);
157
- }
158
- 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 };
159
- if (attach)
160
- output = Object.assign({}, output, attach);
161
- this.emit(finishreason ? 'chatdone' : 'chattext', output);
162
- }
163
- finally {
164
- _d = true;
165
- }
166
- }
167
- }
168
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
169
- finally {
170
- try {
171
- if (!_d && !_a && (_b = response_1.return)) yield _b.call(response_1);
172
- }
173
- finally { if (e_1) throw e_1.error; }
174
- }
175
- return { successed: true, requestid };
176
- }
177
- catch (error) {
178
- this.emit('requesterror', { successed: false, requestid, error: 'call axios faied ' + error });
179
- return { successed: false, requestid };
180
- }
181
- });
182
- }
183
- }
184
- exports.default = OpenAIGpt;
@@ -1,20 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const gptbase_1 = __importDefault(require("./gptbase"));
7
- class OpenAIBase extends gptbase_1.default {
8
- constructor(apiKey, apiOption = {}) {
9
- super();
10
- this.apiKey = apiKey;
11
- this.chatModel = apiOption.model || 'gpt-3.5-turbo';
12
- this.maxtoken = apiOption.maxtoken || 2048;
13
- this.top_p = apiOption.top_p || 0.95;
14
- this.temperature = apiOption.temperature || 0.9;
15
- this.presence_penalty = apiOption.presence_penalty || 0;
16
- this.frequency_penalty = apiOption.frequency_penalty || 0;
17
- this.embeddingmodel = apiOption.embedding || 'text-embedding-ada-002';
18
- }
19
- }
20
- exports.default = OpenAIBase;
@@ -1,20 +0,0 @@
1
- import { ApiCredential } from './baiduai';
2
- import GptBase from './gptbase';
3
- /**
4
- * OpenAI/NLP 的服务提供商 OpenAI,微软,百度文心(待接入),google(待接入)
5
- */
6
- export declare const AIProviderEnum: {
7
- readonly OPENAI: "openai";
8
- readonly MICROSOFT: "microsoft";
9
- readonly BAIDU: "baidu";
10
- readonly GOOGLE: "google";
11
- };
12
- export type AIProviderEnum = typeof AIProviderEnum[keyof typeof AIProviderEnum];
13
- /**
14
- * 根据类型创建不同的TTS引擎对象
15
- * @param {*} provider
16
- * @param {*} apikey
17
- * @param {*} setting
18
- * @returns
19
- */
20
- export declare function createAIInstance(provider: AIProviderEnum, apikey: string | ApiCredential, setting: any): GptBase | null;
@@ -1,43 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.createAIInstance = exports.AIProviderEnum = void 0;
7
- /**
8
- * 语音转文字服务商工厂
9
- */
10
- const openai_1 = __importDefault(require("./openai"));
11
- const azureai_1 = __importDefault(require("./azureai"));
12
- const baiduai_1 = __importDefault(require("./baiduai"));
13
- /**
14
- * OpenAI/NLP 的服务提供商 OpenAI,微软,百度文心(待接入),google(待接入)
15
- */
16
- exports.AIProviderEnum = {
17
- OPENAI: 'openai',
18
- MICROSOFT: 'microsoft',
19
- BAIDU: 'baidu',
20
- GOOGLE: 'google'
21
- };
22
- /**
23
- * 根据类型创建不同的TTS引擎对象
24
- * @param {*} provider
25
- * @param {*} apikey
26
- * @param {*} setting
27
- * @returns
28
- */
29
- function createAIInstance(provider, apikey, setting) {
30
- let { model, maxtoken, temperature, endpoint, engine, version } = setting || {};
31
- switch (provider) {
32
- case exports.AIProviderEnum.OPENAI:
33
- return new openai_1.default(apikey + '', { model, maxtoken, temperature });
34
- case exports.AIProviderEnum.MICROSOFT:
35
- return new azureai_1.default(apikey + '', { endpoint, engine, version }, { model, maxtoken, temperature });
36
- case exports.AIProviderEnum.BAIDU:
37
- let cred = typeof (apikey) === 'string' ? { apikey, securitykey: apikey } : apikey;
38
- return new baiduai_1.default(cred);
39
- default: return null;
40
- }
41
- }
42
- exports.createAIInstance = createAIInstance;
43
- ;