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
@@ -1,108 +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
- const openai_1 = __importDefault(require("./openai"));
16
- const axios_1 = __importDefault(require("axios"));
17
- const ERROR_RESPONSE = ['[AUTHORIZATION NEEDED]', '[AUTHORIZATION ERROR]', '[BODY ERROR]', '[REQUEST ERROR]'];
18
- class OpenAIProxy extends openai_1.default {
19
- constructor(apiKey, proxyOption, apiOption = {}) {
20
- super(apiKey, apiOption);
21
- this.proxySetting = proxyOption;
22
- }
23
- /**
24
- * 重写chatRequest方法
25
- * @param chatText
26
- * @param callChatOption
27
- * @param axiosOption
28
- */
29
- chatRequest(chatText, _paramOption, axiosOption) {
30
- return __awaiter(this, void 0, void 0, function* () {
31
- const opts = Object.assign({ headers: {
32
- 'Content-Type': 'application/json',
33
- 'authorization': `Bearer ${this.apiKey}`
34
- }, method: 'post', url: this.proxySetting.serviceurl, data: {
35
- chatText,
36
- option: _paramOption
37
- } }, axiosOption);
38
- const requestResult = yield (0, axios_1.default)(opts);
39
- return requestResult.data;
40
- });
41
- }
42
- /**
43
- * 重写chatRequestInStream方法
44
- * @param chatText
45
- * @param callChatOption
46
- * @param attach
47
- * @param axiosOption
48
- */
49
- chatRequestInStream(chatText, callChatOption, attach, axiosOption) {
50
- return __awaiter(this, void 0, void 0, function* () {
51
- // const decoder = new TextDecoder();
52
- //overContent,
53
- let streamText, requestid = Math.ceil(Math.random() * (new Date().getTime() * Math.random()) / 1000);
54
- const opts = {
55
- headers: {
56
- 'Content-Type': 'application/json',
57
- 'authorization': `Bearer ${this.apiKey}`
58
- },
59
- method: 'post',
60
- url: this.proxySetting.serviceurl + '/stream',
61
- data: {
62
- messages: chatText,
63
- option: callChatOption,
64
- axiosOption
65
- },
66
- responseType: 'stream',
67
- };
68
- let unCompleteSegment = '';
69
- (0, axios_1.default)(opts)
70
- .then(res => {
71
- res.data.on('data', (chunk) => {
72
- streamText = chunk.toString(); //decoder.decode(chunk);
73
- if (streamText) {
74
- ///请求的响应发生了错误
75
- if (ERROR_RESPONSE.includes(streamText)) {
76
- return this.emit('requesterror', { successed: false, requestid, error: 'Request Remote OpenAI Error : ' + streamText });
77
- }
78
- const fullData = (unCompleteSegment + streamText).split('*&$');
79
- unCompleteSegment = '';
80
- // console.log('fullData', fullData.length);
81
- for (const segment of fullData) {
82
- if (!segment)
83
- continue;
84
- try {
85
- ////判断接收到的不是一个完整的JSON段了,则该段作为下一次的数据段
86
- if (!segment.endsWith('}')) {
87
- unCompleteSegment = segment;
88
- break;
89
- }
90
- const objData = Object.assign(JSON.parse(segment), attach);
91
- this.emit(objData.finish_reason ? 'chatdone' : 'chattext', objData);
92
- }
93
- catch (errParse) {
94
- break;
95
- //this.emit('chaterror', { successed: false, requestid, error: 'JSON parse stream message' + errParse });
96
- }
97
- }
98
- }
99
- return;
100
- });
101
- //res.data.on('end', () => { this.emit('chatdone', Object.assign(streamText, attach)); });
102
- }).catch(err => {
103
- this.emit('requesterror', { successed: false, requestid, error: 'Axios Error : ' + err });
104
- });
105
- });
106
- }
107
- }
108
- exports.default = OpenAIProxy;
@@ -1,18 +0,0 @@
1
- import { AzureOpenAIPatameters, StabilityOption, StabilityResult } from "./declare";
2
- import GptBase from "./gptbase";
3
- export default class StabilityAI extends GptBase {
4
- protected readonly apiKey: string;
5
- protected readonly apiSetting: AzureOpenAIPatameters;
6
- protected readonly apiOption: StabilityOption;
7
- /**
8
- *
9
- * @param apiKey 调用OpenAI 的key
10
- * @param azureOption 用作accesstoken的缓存
11
- * @param apiOption 用作accesstoken的缓存
12
- */
13
- constructor(apiKey: string, urlOption: AzureOpenAIPatameters, apiOption?: StabilityOption);
14
- /**
15
- * 请求Stable作画的接口
16
- */
17
- chatRequest(chatText: string, paramOption: StabilityOption, axiosOption?: any): Promise<StabilityResult>;
18
- }
@@ -1,75 +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
- const declare_1 = require("./declare");
16
- const gptbase_1 = __importDefault(require("./gptbase"));
17
- class StabilityAI extends gptbase_1.default {
18
- /**
19
- *
20
- * @param apiKey 调用OpenAI 的key
21
- * @param azureOption 用作accesstoken的缓存
22
- * @param apiOption 用作accesstoken的缓存
23
- */
24
- constructor(apiKey, urlOption, apiOption = {}) {
25
- super();
26
- this.apiKey = apiKey;
27
- this.apiSetting = urlOption;
28
- this.apiOption = apiOption;
29
- if (!this.apiSetting.endpoint.toLowerCase().startsWith('http')) {
30
- this.apiSetting.endpoint = 'https://' + this.apiSetting.endpoint;
31
- }
32
- }
33
- /**
34
- * 请求Stable作画的接口
35
- */
36
- chatRequest(chatText, paramOption, axiosOption = {}) {
37
- return __awaiter(this, void 0, void 0, function* () {
38
- if (!chatText)
39
- return { successed: false, error: { errcode: 2, errmsg: '缺失聊天的内容' } };
40
- axiosOption = Object.assign({}, axiosOption, {
41
- headers: {
42
- "Content-Type": "application/json",
43
- "Accept": "application/json",
44
- "Authorization": `Bearer ${this.apiKey}`
45
- }
46
- });
47
- try {
48
- let param = Object.assign(Object.assign({}, axiosOption), { method: "post", url: `${this.apiSetting.endpoint}/v1/generation/${this.apiSetting.engine}/text-to-image`, data: {
49
- text_prompts: [
50
- {
51
- text: chatText
52
- }
53
- ],
54
- cfg_scale: paramOption.cfg_scale || this.apiOption.cfg_scale || 7,
55
- clip_guidance_preset: paramOption.clip_guidance_preset || this.apiOption.clip_guidance_preset || "FAST_BLUE",
56
- height: paramOption.height || this.apiOption.height || 512,
57
- width: paramOption.width || this.apiOption.width || 512,
58
- samples: paramOption.samples || this.apiOption.samples || 1,
59
- steps: paramOption.steps || this.apiOption.steps || 30,
60
- } });
61
- const response = yield (0, declare_1.request)(param);
62
- if (response.successed) {
63
- let data = response.data;
64
- return { successed: true, type: 'image', data: data.artifacts, };
65
- }
66
- return Object.assign({ successed: false }, response.data);
67
- }
68
- catch (error) {
69
- console.log('result is error ', error);
70
- return { successed: false, error };
71
- }
72
- });
73
- }
74
- }
75
- exports.default = StabilityAI;
@@ -1,11 +0,0 @@
1
- import { AzureOpenAIPatameters, StabilityOption, StabilityResult } from "./declare";
2
- import GptBase from "./gptbase";
3
- export default class StabilityPlusAI extends GptBase {
4
- protected readonly apiKey: string;
5
- protected readonly apiOption: StabilityOption;
6
- constructor(apiKey: string, _urlOption: AzureOpenAIPatameters, apiOption?: StabilityOption);
7
- /**
8
- * 请求Stable作画的接口
9
- */
10
- chatRequest(chatText: string, paramOption: StabilityOption, axiosOption?: any): Promise<StabilityResult>;
11
- }
@@ -1,86 +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
- const declare_1 = require("./declare");
16
- const gptbase_1 = __importDefault(require("./gptbase"));
17
- class StabilityPlusAI extends gptbase_1.default {
18
- constructor(apiKey, _urlOption, apiOption = {}) {
19
- super();
20
- this.apiKey = apiKey;
21
- // this.apiSetting = urlOption;
22
- this.apiOption = apiOption;
23
- // if (!this.apiSetting.endpoint.toLowerCase().startsWith('http')) {
24
- // this.apiSetting.endpoint = 'https://' + this.apiSetting.endpoint;
25
- // }
26
- }
27
- /**
28
- * 请求Stable作画的接口
29
- */
30
- chatRequest(chatText, paramOption, axiosOption = {}) {
31
- return __awaiter(this, void 0, void 0, function* () {
32
- if (!chatText)
33
- return { successed: false, error: { errcode: 2, errmsg: '缺失聊天的内容' } };
34
- axiosOption = Object.assign({}, axiosOption, {
35
- headers: {
36
- "Content-Type": "application/json",
37
- "Accept": "application/json",
38
- }
39
- });
40
- try {
41
- const requestOption = Object.assign(Object.assign({}, axiosOption), { method: "POST", data: {
42
- "enable_hr": false,
43
- "denoising_strength": paramOption.denoising_strength || this.apiOption.denoising_strength || 0.5,
44
- "firstphase_width": 0,
45
- "firstphase_height": 0,
46
- "hr_scale": paramOption.hr_scale || this.apiOption.hr_scale || 2,
47
- "hr_upscaler": "string",
48
- "hr_second_pass_steps": 0,
49
- "hr_resize_x": 0,
50
- "hr_resize_y": 0,
51
- "prompt": chatText,
52
- "styles": ["string"],
53
- "seed": paramOption.seed || this.apiOption.seed || -1,
54
- "subseed": -1,
55
- "subseed_strength": 0,
56
- "seed_resize_from_h": -1,
57
- "seed_resize_from_w": -1,
58
- "sampler_name": paramOption.sampler || this.apiOption.sampler || "Euler a",
59
- "batch_size": 1,
60
- "n_iter": paramOption.samples || this.apiOption.samples || 1,
61
- "steps": paramOption.steps || this.apiOption.steps || 20,
62
- "cfg_scale": paramOption.cfg_scale || this.apiOption.cfg_scale || 7,
63
- "width": paramOption.width || this.apiOption.width || 512,
64
- "height": paramOption.height || this.apiOption.height || 512,
65
- "restore_faces": false,
66
- "tiling": false,
67
- "do_not_save_samples": false,
68
- "do_not_save_grid": false,
69
- "negative_prompt": paramOption.negative || ''
70
- }, url: `${paramOption.endpoint}/sdapi/v1/txt2img` });
71
- // console.log('stablity param', requestOption);
72
- const response = yield (0, declare_1.request)(requestOption);
73
- if (response.successed) {
74
- return { successed: true, type: 'image', data: response.data.images, };
75
- }
76
- // console.log('response result ', response.data)
77
- return Object.assign({ successed: false }, response.data);
78
- }
79
- catch (error) {
80
- console.log('result is error ', error);
81
- return { successed: false, error };
82
- }
83
- });
84
- }
85
- }
86
- exports.default = StabilityPlusAI;
package/src/aimp.ts DELETED
@@ -1,125 +0,0 @@
1
- /**
2
- * 接入AI平台的中间层
3
- */
4
- import axios from 'axios';
5
- import { request } from "./declare";
6
- import GptBase from "./gptbase"
7
- import { Readable } from 'stream';
8
- // import { ChatReponse } from './declare';
9
- export default class AIMiddlePlatform extends GptBase {
10
-
11
- protected apikey: string;
12
- protected agent: { endpoint: string, agentid: string };
13
- /**
14
- *
15
- * @param apikey 调用AI中台 的key
16
- * @param agent 智能体信息
17
- */
18
- constructor(apikey: string, agent: { endpoint: string, agentid: string }) {
19
- super();
20
- this.apikey = apikey;
21
- this.agent = agent;
22
- }
23
-
24
- /**
25
- * 非流式传输聊天请求
26
- * @param chatText
27
- * @param callChatOption
28
- * @param axiosOption
29
- */
30
- public async chatRequest(chatText: string | any, callChatOption: any, axiosOption: any = {}): Promise<any> {
31
- if (!chatText) {
32
- // this.emit('chaterror', { successed: false, error: 'no text in chat' });
33
- return { successed: false, error: 'no text in chat' };
34
- }
35
- const question = typeof chatText === 'object' ? chatText.text ?? chatText.content : chatText;
36
- axiosOption = Object.assign({}, axiosOption || { timeout: 60000 })
37
- const opts: any = {
38
- headers: {
39
- 'Content-Type': 'application/json',
40
- 'authorization': `Bearer ${this.apikey}`
41
- },
42
- method: 'post',
43
- url: `${this.agent.endpoint}/api/v1/agents/${this.agent.agentid}/completions`,
44
- data: {
45
- question,
46
- session_id: callChatOption.session_id,
47
- optional: callChatOption.optional,
48
- stream: false
49
- },
50
- ...axiosOption
51
- }
52
- const response = await request(opts);
53
- if (!response.successed || response.data.code) return { successed: false, error: 'failed' };
54
- const {answer:message,session_id} = response.data.data;
55
- // this.emit('chatdone', { successed: true, segment: message, text: message, finish_reason: 'stop', index: 0, session_id })
56
- return { successed: true, message:[{message:{content:message}}],session_id };
57
- }
58
- /**
59
- * 流式传输聊天请求
60
- * @param chatText
61
- * @param callChatOption
62
- * @param attach
63
- * @param axiosOption
64
- * @returns
65
- */
66
- override async chatRequestInStream(chatText: string | any, callChatOption: any, attach?: any, axiosOption?: any): Promise<any> {
67
- if (!chatText) this.emit('chaterror', { successed: false, error: 'no text in chat' });
68
- // console.log('Question===>', chatText)
69
- const question = typeof chatText === 'object' ? chatText.text??chatText.content : chatText;
70
- axiosOption = Object.assign({}, axiosOption || { timeout: 60000 })
71
- let requestid = Math.ceil(Math.random() * (new Date().getTime() * Math.random()) / 1000);
72
- try {
73
- const opts: any = {
74
- headers: {
75
- 'Content-Type': 'application/json',
76
- 'authorization': `Bearer ${this.apikey}`
77
- },
78
- method: 'post',
79
- url: `${this.agent.endpoint}/api/v1/agents/${this.agent.agentid}/completions`,
80
- data: {
81
- question,
82
- session_id: callChatOption.session_id,
83
- stream: true,
84
- optional: callChatOption.optional,
85
- },
86
- responseType: 'stream',
87
- ...axiosOption
88
- }
89
- const response = await axios(opts);
90
- const readableStream = Readable.from(response.data);
91
- let index = 0, session_id,fullanswer='',errorKeeped=[],chunks:any=[];
92
- for await (const chunk of readableStream) {
93
- ///可能接收到的数据不完整,导致JSON.parse失败
94
- let answerData = null, jsonStr = '';
95
- try{
96
- jsonStr = chunk.toString().split('data:')
97
- if (jsonStr.length) jsonStr = jsonStr[jsonStr.length-1]+''
98
- answerData = JSON.parse(errorKeeped.join('') + jsonStr);
99
- }catch(e){
100
- ////如果发生了JSON解析错误,则当前的数据不完整,留着拼凑下一回数据
101
- // console.log('json parse error===>', errorKeeped.join('') + jsonStr)
102
- errorKeeped.push(jsonStr)
103
- // console.log('After Push===>', errorKeeped.join('') )
104
- continue;
105
- }
106
- errorKeeped = [];
107
- const { answer, running_status, reference } = answerData.data;
108
- if (running_status === true) continue;
109
- const segment = answer ? answer.replace(fullanswer,''):''
110
- fullanswer = answer ?? fullanswer;
111
- if (!session_id) session_id = answerData.data.session_id;
112
- if (reference && reference.chunks && reference.chunks.length) chunks = chunks.concat(reference.chunks)
113
- const finished = answerData.data === true;
114
- let output = { successed: true, requestid, segment: segment, chunks, text: fullanswer, finish_reason: finished ? 'stop' : null, index: index++, session_id };
115
- if (attach) output = Object.assign({}, output, attach);
116
- this.emit(finished ? 'chatdone' : 'chattext', output)
117
- }
118
- return { successed: true, requestid }
119
- } catch (error) {
120
-
121
- // this.emit('requesterror', { successed: false, requestid, error: 'call axios faied ' + error });
122
- // return { successed: false, requestid }
123
- }
124
- }
125
- }
package/src/azureai.ts DELETED
@@ -1,180 +0,0 @@
1
- /**
2
- * 微软AZure OpenAI
3
- */
4
- import OpenAIBase from "./openaibase"
5
- import { AzureOpenAIPatameters, ChatReponse, EmbeddingResult, OpenAIApiParameters} from "./declare";
6
- import { OpenAIClient, AzureKeyCredential } from "@azure/openai";
7
- export default class AzureAI extends OpenAIBase<OpenAIClient> {
8
-
9
- protected readonly azureSetting: AzureOpenAIPatameters;
10
- constructor(apiKey: string, azureOption: AzureOpenAIPatameters, apiOption: OpenAIApiParameters = {}) {
11
- super(apiKey, apiOption);
12
- this.azureSetting = azureOption;
13
- if (!this.azureSetting.endpoint.toLowerCase().startsWith('https://')) {
14
- this.azureSetting.endpoint = 'https://' + this.azureSetting.endpoint;
15
- }
16
- }
17
- /**
18
- * 初始化OpenAI 的聊天对象Api
19
- */
20
- createOpenAI(apiKey: string): OpenAIClient {
21
- return new OpenAIClient(this.azureSetting.endpoint, new AzureKeyCredential(apiKey));
22
- }
23
-
24
- get EmbeddingUrl(): string {
25
- return `${this.azureSetting.endpoint}/openai/deployments/${this.embeddingmodel || 'openai-embedding-ada-002'}/embeddings?api-version=2022-12-01`
26
- }
27
-
28
- /**
29
- * 获得文字的向量
30
- * @param text
31
- */
32
- override async getTextEmbedding(text: string|string[], callOption: any = {}): Promise<EmbeddingResult> {
33
- if (!text) return { successed: false, error: { errcode: 2, errmsg: 'content required' } };
34
- if (!this.aiApi) this.aiApi = this.createOpenAI(this.apiKey);
35
- try{
36
- const result = await this.aiApi.getEmbeddings(this.embeddingmodel || 'openai-embedding-ada-002', typeof text === 'string' ? [text] : text, callOption)
37
- return { successed: true, embedding:result.data };
38
- }
39
- // if (!axiosOption.headers)
40
- // axiosOption.headers = { 'api-key': this.apiKey, 'Content-Type': 'application/json' };
41
- // else {
42
- // axiosOption.headers['api-key'] = this.apiKey;
43
- // axiosOption.headers['Content-Type'] = 'application/json';
44
- // }
45
-
46
- // try {
47
- // let param = {
48
- // ...axiosOption,
49
- // method: "post",
50
- // data: {
51
- // input: text
52
- // },
53
- // url: this.EmbeddingUrl
54
- // };
55
- // const response = await request(param)
56
- // if (response.successed && response.data) {
57
- // return { successed: true, embedding: response.data.data[0].embedding };
58
- // }
59
- // return { successed: false, ...response.data };
60
- catch (error) {
61
- return { successed: false, error };
62
- }
63
- }
64
-
65
- /**
66
- * 非流式聊天请求
67
- * @param _chatText
68
- * @param _paramOption
69
- * @param _axiosOption
70
- */
71
- public async chatRequest(chatText: string | Array<any>, callChatOption: OpenAIApiParameters, _axiosOption: any = {}): Promise<ChatReponse> {
72
- if (!chatText) return { successed: false, error: { errcode: 2, errmsg: '缺失聊天的内容' } };
73
- if (!this.aiApi) this.aiApi = this.createOpenAI(this.apiKey);
74
-
75
- let message: Array<any> = typeof (chatText) == 'string' ?
76
- [{ role: 'user', content: chatText }] : chatText;
77
- try {
78
- const response: any = await this.aiApi.getChatCompletions(
79
- callChatOption?.model || this.chatModel,
80
- message,
81
- {
82
- temperature: Number(callChatOption?.temperature || this.temperature),
83
- maxTokens: Number(callChatOption?.maxtoken || this.maxtoken),
84
- topP: Number(callChatOption?.top_p || this.top_p),
85
- presencePenalty: Number(callChatOption?.presence_penalty || this.presence_penalty),
86
- frequencyPenalty: Number(callChatOption?.frequency_penalty || this.frequency_penalty),
87
- n: Number(callChatOption?.replyCounts || 1) || 1,
88
- tools: (callChatOption?.enableToolCall === 1 && callChatOption?.tools) ? callChatOption.tools : undefined,
89
- toolChoice: callChatOption?.enableToolCall === 1 ? 'auto' : undefined
90
- });
91
- const { promptTokens: prompt_tokens, completionTokens: completion_tokens, totalTokens: total_tokens } = response.usage
92
- let rebuildChoice = [];
93
- for (const msg of response.choices) {
94
- const { index, finishReason: finish_reason, message } = msg
95
- rebuildChoice.push({ index, finish_reason, message })
96
- }
97
- // if (response.data.choices[0].finish_reason === 'content_filter') {
98
- // console.log('content_filter');
99
- // return { successed: false, error: 'content_filter' };
100
- // }
101
- return { successed: true, message: rebuildChoice, usage: { prompt_tokens, completion_tokens, total_tokens } };
102
-
103
- } catch (error) {
104
- console.log('result is error ', error)
105
- return { successed: false, error };
106
- }
107
-
108
- }
109
-
110
- /**
111
- * 流式的聊天模式
112
- * @param chatText
113
- * @param _paramOption
114
- * @param axiosOption
115
- */
116
- override async chatRequestInStream(chatText: string | Array<any>, callChatOption: OpenAIApiParameters, attach?: any, axiosOption?: any): Promise<any> {
117
- if (!chatText) this.emit('chaterror', { successed: false, error: 'no text in chat' });
118
- if (!this.aiApi) {
119
- this.aiApi = this.createOpenAI(this.apiKey);
120
- }
121
- let message: Array<any> = typeof (chatText) == 'string' ?[{ role: 'user', content: chatText }] : chatText;
122
- axiosOption = Object.assign({}, axiosOption || { timeout: 60000 })
123
- let requestid = Math.ceil(Math.random() * (new Date().getTime() * Math.random()) / 1000);
124
- let has_tool_calls=0,currentIndex, previous_index=-1, tool_calls:any[] = [];// 使用数组来存储工具调用
125
- try {
126
- const response: any = await this.aiApi.streamChatCompletions(
127
- callChatOption?.model || this.chatModel,
128
- message,
129
- {
130
- temperature: Number(callChatOption?.temperature || this.temperature),
131
- maxTokens: Number(callChatOption?.maxtoken || this.maxtoken),
132
- topP: Number(callChatOption?.top_p || this.top_p),
133
- presencePenalty: Number(callChatOption?.presence_penalty || this.presence_penalty),
134
- frequencyPenalty: Number(callChatOption?.frequency_penalty || this.frequency_penalty),
135
- n: Number(callChatOption?.replyCounts || 1) || 1,
136
- tools: (callChatOption?.enableToolCall===1 && callChatOption?.tools) ? callChatOption.tools : undefined,
137
- toolChoice: callChatOption?.enableToolCall === 1 ?'auto':undefined
138
- });
139
- //console.log('tools', callChatOption.enableToolCall, callChatOption.tools)
140
- let replytext: string[] = [];
141
- for await (const event of response) {
142
- for (const choice of event.choices) {
143
- const { finishReason: finishreason, index } = choice;
144
- const toolCalls = choice.delta?.toolCalls;
145
- ///存在了toolCalls
146
- if (toolCalls && toolCalls.length){
147
- currentIndex = toolCalls[0].index;
148
- has_tool_calls = 1;
149
- // 检查index是否发生变化
150
- //console.log('currentIndex,previous_index', currentIndex, previous_index)
151
- if (currentIndex !== previous_index) {
152
- tool_calls.push({
153
- id: toolCalls[0].id,
154
- type: 'function',
155
- function: {
156
- name: toolCalls[0].function.name,
157
- arguments: toolCalls[0].function.arguments
158
- }
159
- });
160
- // 更新previousIndex以供下次比较
161
- previous_index = currentIndex;
162
- } else {
163
- tool_calls[previous_index].function.arguments += toolCalls[0].function.arguments
164
- }
165
- }else{
166
- const content = choice.delta?.content;
167
- replytext.push(content);
168
- }
169
- let output = { successed: true, requestid, segment: choice.delta?.content, text: replytext.join(''), finish_reason: finishreason, index, has_tool_calls: has_tool_calls, tool_calls: tool_calls };
170
- if (attach) output = Object.assign({}, output, attach);
171
- this.emit(finishreason ? 'chatdone' : 'chattext', output)
172
- }
173
- }
174
- return { successed: true, requestid }
175
- } catch (error) {
176
- this.emit('requesterror', { successed: false, requestid, error: 'call axios faied ' +error });
177
- return { successed: false, requestid }
178
- }
179
- }
180
- }