doomiaichat 5.0.0 → 6.0.0

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.
package/dist/azureai.d.ts CHANGED
@@ -1,20 +1,34 @@
1
- import { OpenAIApi } from "azure-openai";
2
- import { AzureOpenAIPatameters, EmbeddingResult, OpenAIApiParameters } from "./declare";
3
- import OpenAIGpt from "./openai";
4
- export default class AzureAI extends OpenAIGpt {
1
+ /**
2
+ * 微软AZure OpenAI
3
+ */
4
+ import OpenAIBase from "./openaibase";
5
+ import { AzureOpenAIPatameters, ChatReponse, EmbeddingResult, OpenAIApiParameters } from "./declare";
6
+ import { OpenAIClient } from "@azure/openai";
7
+ export default class AzureAI extends OpenAIBase<OpenAIClient> {
5
8
  protected readonly azureSetting: AzureOpenAIPatameters;
6
9
  constructor(apiKey: string, azureOption: AzureOpenAIPatameters, apiOption?: OpenAIApiParameters);
7
10
  /**
8
11
  * 初始化OpenAI 的聊天对象Api
9
12
  */
10
- createOpenAI(apiKey: string): OpenAIApi;
11
- /**
12
- * ZAure OpenAI 最新的URL地址
13
- */
13
+ createOpenAI(apiKey: string): OpenAIClient;
14
14
  get EmbeddingUrl(): string;
15
15
  /**
16
16
  * 获得文字的向量
17
17
  * @param text
18
18
  */
19
19
  getTextEmbedding(text: string, axiosOption?: any): Promise<EmbeddingResult>;
20
+ /**
21
+ * 非流式聊天请求
22
+ * @param _chatText
23
+ * @param _paramOption
24
+ * @param _axiosOption
25
+ */
26
+ chatRequest(chatText: string | Array<any>, callChatOption: OpenAIApiParameters, _axiosOption?: any): Promise<ChatReponse>;
27
+ /**
28
+ * 流式的聊天模式
29
+ * @param chatText
30
+ * @param _paramOption
31
+ * @param axiosOption
32
+ */
33
+ chatRequestInStream(chatText: string | Array<any>, callChatOption: OpenAIApiParameters, attach?: any, axiosOption?: any): Promise<any>;
20
34
  }
package/dist/azureai.js CHANGED
@@ -8,15 +8,24 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
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
+ };
11
18
  var __importDefault = (this && this.__importDefault) || function (mod) {
12
19
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
20
  };
14
21
  Object.defineProperty(exports, "__esModule", { value: true });
15
- const azure_openai_1 = require("azure-openai");
22
+ /**
23
+ * 微软AZure OpenAI
24
+ */
25
+ const openaibase_1 = __importDefault(require("./openaibase"));
16
26
  const declare_1 = require("./declare");
17
- // import { Configuration, OpenAIApi, ChatCompletionRequestMessage } from "azure-openai"
18
- const openai_1 = __importDefault(require("./openai"));
19
- class AzureAI extends openai_1.default {
27
+ const openai_1 = require("@azure/openai");
28
+ class AzureAI extends openaibase_1.default {
20
29
  constructor(apiKey, azureOption, apiOption = {}) {
21
30
  super(apiKey, apiOption);
22
31
  this.azureSetting = azureOption;
@@ -28,19 +37,8 @@ class AzureAI extends openai_1.default {
28
37
  * 初始化OpenAI 的聊天对象Api
29
38
  */
30
39
  createOpenAI(apiKey) {
31
- return new azure_openai_1.OpenAIApi(new azure_openai_1.Configuration({ apiKey,
32
- azure: {
33
- apiKey,
34
- endpoint: this.azureSetting.endpoint,
35
- deploymentName: this.azureSetting.engine
36
- } }));
40
+ return new openai_1.OpenAIClient(this.azureSetting.endpoint, new openai_1.AzureKeyCredential(apiKey));
37
41
  }
38
- /**
39
- * ZAure OpenAI 最新的URL地址
40
- */
41
- // get BaseUrl(): string {
42
- // return `${this.azureSetting.endpoint}/openai/deployments/${this.azureSetting.engine}/chat/completions?api-version=${this.azureSetting.version || '2023-03-15-preview'}`
43
- // }
44
42
  get EmbeddingUrl() {
45
43
  return `${this.azureSetting.endpoint}/openai/deployments/${this.embeddingmodel || 'openai-embedding-ada-002'}/embeddings?api-version=2022-12-01`;
46
44
  }
@@ -63,7 +61,7 @@ class AzureAI extends openai_1.default {
63
61
  input: text
64
62
  }, url: this.EmbeddingUrl });
65
63
  const response = yield (0, declare_1.request)(param);
66
- if (response.data) {
64
+ if (response.successed && response.data) {
67
65
  return { successed: true, embedding: response.data.data[0].embedding };
68
66
  }
69
67
  return Object.assign({ successed: false }, response.data);
@@ -73,5 +71,111 @@ class AzureAI extends openai_1.default {
73
71
  }
74
72
  });
75
73
  }
74
+ /**
75
+ * 非流式聊天请求
76
+ * @param _chatText
77
+ * @param _paramOption
78
+ * @param _axiosOption
79
+ */
80
+ chatRequest(chatText, callChatOption, _axiosOption = {}) {
81
+ return __awaiter(this, void 0, void 0, function* () {
82
+ if (!chatText)
83
+ return { successed: false, error: { errcode: 2, errmsg: '缺失聊天的内容' } };
84
+ if (!this.aiApi)
85
+ this.aiApi = this.createOpenAI(this.apiKey);
86
+ let message = typeof (chatText) == 'string' ?
87
+ [{ role: 'user', content: chatText }] : chatText;
88
+ try {
89
+ const response = yield this.aiApi.getChatCompletions((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.model) || this.chatModel, message, {
90
+ temperature: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.temperature) || this.temperature),
91
+ maxTokens: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.maxtoken) || this.maxtoken),
92
+ topP: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.top_p) || this.top_p),
93
+ presencePenalty: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.presence_penalty) || this.presence_penalty),
94
+ frequencyPenalty: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.frequency_penalty) || this.frequency_penalty),
95
+ n: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.replyCounts) || 1) || 1,
96
+ tools: callChatOption.tools,
97
+ toolChoice: (callChatOption.tool_choice || 'none'),
98
+ });
99
+ const { promptTokens: prompt_tokens, completionTokens: completion_tokens, totalTokens: total_tokens } = response.usage;
100
+ let rebuildChoice = [];
101
+ for (const msg of response.choices) {
102
+ ///, contentFilterResults: content_filter
103
+ const { index, finishReason: finish_reason, message } = msg;
104
+ rebuildChoice.push({ index, finish_reason, message });
105
+ }
106
+ return { successed: true, message: rebuildChoice, usage: { prompt_tokens, completion_tokens, total_tokens } };
107
+ }
108
+ catch (error) {
109
+ console.log('result is error ', error);
110
+ return { successed: false, error };
111
+ }
112
+ });
113
+ }
114
+ /**
115
+ * 流式的聊天模式
116
+ * @param chatText
117
+ * @param _paramOption
118
+ * @param axiosOption
119
+ */
120
+ chatRequestInStream(chatText, callChatOption, attach, axiosOption) {
121
+ var _a, e_1, _b, _c;
122
+ var _d;
123
+ return __awaiter(this, void 0, void 0, function* () {
124
+ if (!chatText)
125
+ this.emit('chaterror', { successed: false, error: 'no text in chat' });
126
+ if (!this.aiApi) {
127
+ this.aiApi = this.createOpenAI(this.apiKey);
128
+ }
129
+ let message = typeof (chatText) == 'string' ? [{ role: 'user', content: chatText }] : chatText;
130
+ axiosOption = Object.assign({}, axiosOption || { timeout: 60000 });
131
+ let requestid = Math.ceil(Math.random() * (new Date().getTime() * Math.random()) / 1000);
132
+ try {
133
+ const response = yield this.aiApi.streamChatCompletions((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.model) || this.chatModel, message, {
134
+ temperature: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.temperature) || this.temperature),
135
+ maxTokens: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.maxtoken) || this.maxtoken),
136
+ topP: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.top_p) || this.top_p),
137
+ presencePenalty: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.presence_penalty) || this.presence_penalty),
138
+ frequencyPenalty: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.frequency_penalty) || this.frequency_penalty),
139
+ n: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.replyCounts) || 1) || 1
140
+ });
141
+ let replytext = [];
142
+ try {
143
+ for (var _e = true, response_1 = __asyncValues(response), response_1_1; response_1_1 = yield response_1.next(), _a = response_1_1.done, !_a;) {
144
+ _c = response_1_1.value;
145
+ _e = false;
146
+ try {
147
+ const event = _c;
148
+ for (const choice of event.choices) {
149
+ const { finishReason: finishreason, index } = choice;
150
+ const content = (_d = choice.delta) === null || _d === void 0 ? void 0 : _d.content;
151
+ if (!content)
152
+ continue;
153
+ replytext.push(content);
154
+ let output = { successed: true, requestid, segment: content, text: replytext.join(''), finish_reason: finishreason, index }; //, usage };
155
+ if (attach)
156
+ output = Object.assign({}, output, attach);
157
+ this.emit(finishreason ? 'chatdone' : 'chattext', output);
158
+ }
159
+ }
160
+ finally {
161
+ _e = true;
162
+ }
163
+ }
164
+ }
165
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
166
+ finally {
167
+ try {
168
+ if (!_e && !_a && (_b = response_1.return)) yield _b.call(response_1);
169
+ }
170
+ finally { if (e_1) throw e_1.error; }
171
+ }
172
+ return { successed: true, requestid };
173
+ }
174
+ catch (error) {
175
+ this.emit('requesterror', { successed: false, requestid, error: 'call axios faied ' + error });
176
+ return { successed: false, requestid };
177
+ }
178
+ });
179
+ }
76
180
  }
77
181
  exports.default = AzureAI;
package/dist/declare.d.ts CHANGED
@@ -31,11 +31,22 @@ export interface OpenAIApiParameters {
31
31
  'model'?: string;
32
32
  'maxtoken'?: number;
33
33
  'temperature'?: number;
34
+ 'top_p'?: number;
35
+ 'presence_penalty'?: number;
36
+ 'frequency_penalty'?: number;
34
37
  'replyCounts'?: number;
38
+ 'tools'?: Array<any>;
39
+ 'tool_choice'?: string;
35
40
  }
36
41
  /**
37
42
  * Azure 上的OpenAI的链接参数
38
43
  */
44
+ export interface ProxyPatameters {
45
+ 'serviceurl': string;
46
+ }
47
+ /**
48
+ * OpenAI Proxy 链接参数
49
+ */
39
50
  export interface AzureOpenAIPatameters {
40
51
  'endpoint': string;
41
52
  'engine': string;
@@ -81,3 +92,22 @@ export interface CacheProvider {
81
92
  */
82
93
  delete(key: string): void;
83
94
  }
95
+ export interface StabilityOption {
96
+ 'cfg_scale'?: number;
97
+ 'clip_guidance_preset'?: string;
98
+ 'height'?: number;
99
+ 'width'?: number;
100
+ 'samples'?: number;
101
+ 'seed'?: number;
102
+ 'steps'?: number;
103
+ 'sampler'?: string;
104
+ 'negative'?: string;
105
+ 'engine'?: string;
106
+ 'endpoint'?: string;
107
+ 'denoising_strength'?: number;
108
+ 'hr_scale'?: number;
109
+ }
110
+ export interface StabilityResult extends ApiResult {
111
+ 'data'?: any;
112
+ 'type'?: string;
113
+ }
@@ -5,6 +5,7 @@ import GptBase from './gptbase';
5
5
  */
6
6
  export declare const GptProviderEnum: {
7
7
  readonly OPENAI: "openai";
8
+ readonly OPENAIPROXY: "openaiproxy";
8
9
  readonly MICROSOFT: "microsoft";
9
10
  readonly BAIDU: "baidu";
10
11
  readonly GOOGLE: "google";
@@ -9,6 +9,7 @@ exports.createGpt = exports.GptProviderEnum = void 0;
9
9
  * 语音转文字服务商工厂
10
10
  */
11
11
  const openai_1 = __importDefault(require("./openai"));
12
+ const openaiproxy_1 = __importDefault(require("./openaiproxy"));
12
13
  const azureai_1 = __importDefault(require("./azureai"));
13
14
  const stabilityai_1 = __importDefault(require("./stabilityai"));
14
15
  const stabilityplusai_1 = __importDefault(require("./stabilityplusai"));
@@ -18,6 +19,7 @@ const baiduai_1 = __importDefault(require("./baiduai"));
18
19
  */
19
20
  exports.GptProviderEnum = {
20
21
  OPENAI: 'openai',
22
+ OPENAIPROXY: 'openaiproxy',
21
23
  MICROSOFT: 'microsoft',
22
24
  BAIDU: 'baidu',
23
25
  GOOGLE: 'google',
@@ -32,12 +34,14 @@ exports.GptProviderEnum = {
32
34
  * @returns
33
35
  */
34
36
  function createGpt(provider, apikey, setting) {
35
- let { model, maxtoken, temperature, endpoint, engine, version, embedding } = setting || {};
37
+ let { model, maxtoken, temperature, serviceurl, endpoint, engine, version, embedding, top_p, presence_penalty, frequency_penalty } = setting || {};
36
38
  switch (provider) {
37
39
  case exports.GptProviderEnum.OPENAI:
38
- return new openai_1.default(apikey + '', { model, maxtoken, temperature, embedding });
40
+ return new openai_1.default(apikey + '', { model, maxtoken, temperature, embedding, top_p, presence_penalty, frequency_penalty });
41
+ case exports.GptProviderEnum.OPENAIPROXY:
42
+ return new openaiproxy_1.default(apikey + '', { serviceurl }, { model, maxtoken, temperature, embedding, top_p, presence_penalty, frequency_penalty });
39
43
  case exports.GptProviderEnum.MICROSOFT:
40
- return new azureai_1.default(apikey + '', { endpoint, engine, version }, { model, maxtoken, temperature, embedding });
44
+ return new azureai_1.default(apikey + '', { endpoint, engine, version }, { model, maxtoken, temperature, embedding, top_p, presence_penalty, frequency_penalty });
41
45
  case exports.GptProviderEnum.BAIDU:
42
46
  let cred = typeof (apikey) === 'string' ? { apikey, securitykey: apikey } : apikey;
43
47
  return new baiduai_1.default(cred);
package/dist/openai.d.ts CHANGED
@@ -1,23 +1,14 @@
1
- import { OpenAIApi } from "azure-openai";
2
- import GptBase from "./gptbase";
1
+ /**
2
+ * OpenAI
3
+ */
4
+ import OpenAIBase from "./openaibase";
3
5
  import { OpenAIApiParameters, ChatReponse, EmbeddingResult } from './declare';
4
- export default class OpenAIGpt extends GptBase {
5
- protected readonly apiKey: string;
6
- protected aiApi: OpenAIApi | undefined;
7
- protected readonly chatModel: string;
8
- protected readonly maxtoken: number;
9
- protected readonly temperature: number;
10
- protected readonly embeddingmodel: string;
11
- /**
12
- *
13
- * @param apiKey 调用OpenAI 的key
14
- * @param apiOption
15
- */
16
- constructor(apiKey: string, apiOption?: OpenAIApiParameters);
6
+ import OpenAI from "openai";
7
+ export default class OpenAIGpt extends OpenAIBase<OpenAI> {
17
8
  /**
18
9
  * 初始化OpenAI 的聊天对象Api
19
10
  */
20
- createOpenAI(apiKey: string): OpenAIApi;
11
+ createOpenAI(apiKey: string): OpenAI;
21
12
  /**
22
13
  * 获得文字的向量
23
14
  * @param text
package/dist/openai.js CHANGED
@@ -8,31 +8,29 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
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
+ };
11
18
  var __importDefault = (this && this.__importDefault) || function (mod) {
12
19
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
20
  };
14
21
  Object.defineProperty(exports, "__esModule", { value: true });
15
- const azure_openai_1 = require("azure-openai");
16
- const gptbase_1 = __importDefault(require("./gptbase"));
17
- class OpenAIGpt extends gptbase_1.default {
18
- /**
19
- *
20
- * @param apiKey 调用OpenAI 的key
21
- * @param apiOption
22
- */
23
- constructor(apiKey, apiOption = {}) {
24
- super();
25
- this.apiKey = apiKey;
26
- this.chatModel = apiOption.model || 'gpt-3.5-turbo';
27
- this.maxtoken = apiOption.maxtoken || 2048;
28
- this.temperature = apiOption.temperature || 0.9;
29
- this.embeddingmodel = apiOption.embedding || 'text-embedding-ada-002';
30
- }
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
+ class OpenAIGpt extends openaibase_1.default {
31
29
  /**
32
30
  * 初始化OpenAI 的聊天对象Api
33
31
  */
34
32
  createOpenAI(apiKey) {
35
- return new azure_openai_1.OpenAIApi(new azure_openai_1.Configuration({ apiKey }));
33
+ return new openai_1.default({ apiKey });
36
34
  }
37
35
  /**
38
36
  * 获得文字的向量
@@ -46,7 +44,8 @@ class OpenAIGpt extends gptbase_1.default {
46
44
  this.aiApi = this.createOpenAI(this.apiKey);
47
45
  }
48
46
  try {
49
- const response = yield this.aiApi.createEmbedding({
47
+ //const response: any = await this.aiApi.createEmbedding({
48
+ const response = yield this.aiApi.embeddings.create({
50
49
  model: this.embeddingmodel,
51
50
  input: text,
52
51
  }, axiosOption);
@@ -66,27 +65,24 @@ class OpenAIGpt extends gptbase_1.default {
66
65
  return __awaiter(this, void 0, void 0, function* () {
67
66
  if (!chatText)
68
67
  return { successed: false, error: { errcode: 2, errmsg: '缺失聊天的内容' } };
69
- if (!this.aiApi) {
68
+ if (!this.aiApi)
70
69
  this.aiApi = this.createOpenAI(this.apiKey);
71
- //return { successed: false, error: { errcode: 1, errmsg: '聊天机器人无效' } };
72
- }
73
70
  let message = typeof (chatText) == 'string' ?
74
71
  [{ role: 'user', content: chatText }] : chatText;
75
- // console.log('message', message)
76
72
  try {
77
- const response = yield this.aiApi.createChatCompletion({
73
+ // const response: any = await this.aiApi.createChatCompletion({
74
+ const response = yield this.aiApi.chat.completions.create({
78
75
  model: (callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.model) || this.chatModel,
79
76
  messages: message,
80
77
  temperature: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.temperature) || this.temperature),
81
78
  max_tokens: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.maxtoken) || this.maxtoken),
82
- n: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.replyCounts) || 1) || 1
79
+ top_p: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.top_p) || this.top_p),
80
+ presence_penalty: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.presence_penalty) || this.presence_penalty),
81
+ frequency_penalty: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.frequency_penalty) || this.frequency_penalty),
82
+ n: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.replyCounts) || 1) || 1,
83
+ tools: callChatOption.tools,
84
+ tool_choice: (callChatOption.tool_choice || 'none'),
83
85
  }, axiosOption);
84
- // console.log('finish_reason==>', response.data.choices)
85
- ////输出的内容不合规
86
- if (response.data.choices[0].finish_reason === 'content_filter') {
87
- console.log('content_filter');
88
- return { successed: false, error: 'content_filter' };
89
- }
90
86
  return { successed: true, message: response.data.choices, usage: response.data.usage };
91
87
  }
92
88
  catch (error) {
@@ -102,6 +98,7 @@ class OpenAIGpt extends gptbase_1.default {
102
98
  * @param axiosOption
103
99
  */
104
100
  chatRequestInStream(chatText, callChatOption, attach, axiosOption) {
101
+ var _a, e_1, _b, _c;
105
102
  return __awaiter(this, void 0, void 0, function* () {
106
103
  if (!chatText)
107
104
  this.emit('chaterror', { successed: false, error: 'no text in chat' });
@@ -111,62 +108,87 @@ class OpenAIGpt extends gptbase_1.default {
111
108
  // const DATA_END_TAG = `"usage":null}`
112
109
  let message = typeof (chatText) == 'string' ?
113
110
  [{ role: 'user', content: chatText }] : chatText;
114
- axiosOption = Object.assign({}, axiosOption || { timeout: 60000 }, { responseType: 'stream' });
111
+ //axiosOption = Object.assign({}, axiosOption || { timeout: 60000 }, { responseType: 'stream' })
112
+ axiosOption = Object.assign({}, axiosOption || { timeout: 60000 });
115
113
  let requestid = Math.ceil(Math.random() * (new Date().getTime() * Math.random()) / 1000);
116
114
  try {
117
- let finishreason = null, usage = null, errtxt = '';
115
+ // let finishreason: any = null, usage: any = null,errtxt = '';
118
116
  ///便于知道返回的requestid
119
117
  // console.log('model', callChatOption?.model || this.chatModel,)
120
- const response = yield this.aiApi.createChatCompletion({
118
+ //const response: any = await this.aiApi.chat.completions.create({
119
+ const response = yield this.aiApi.chat.completions.create({
121
120
  model: (callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.model) || this.chatModel,
122
121
  messages: message,
123
- stream: true,
124
122
  temperature: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.temperature) || this.temperature),
125
- max_tokens: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.maxtoken) || this.maxtoken)
123
+ max_tokens: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.maxtoken) || this.maxtoken),
124
+ top_p: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.top_p) || this.top_p),
125
+ presence_penalty: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.presence_penalty) || this.presence_penalty),
126
+ frequency_penalty: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.frequency_penalty) || this.frequency_penalty),
127
+ n: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.replyCounts) || 1) || 1,
128
+ stream: true
126
129
  }, axiosOption);
127
130
  let replytext = [];
128
- response.data.on('data', (data) => {
129
- const lines = data.toString().split('\n').filter((line) => line.trim() !== '');
130
- ///已经返回了结束原因
131
- if (finishreason)
132
- return;
133
- // console.log('before add', lines.join('').split('data:'));
134
- let alltext = (errtxt + lines.join('')).split('data:');
135
- // console.log('alltext',alltext)
136
- errtxt = '';
137
- for (const line of alltext) {
138
- let txt = line.trim();
139
- if (!txt)
140
- continue;
141
- if (txt === '[DONE]') {
142
- let output = { successed: true, requestid, text: replytext.join(''), finish_reason: 'stop', usage };
143
- if (attach)
144
- output = Object.assign({}, output, attach);
145
- this.emit('chatdone', output);
146
- return; // Stream finished
147
- }
131
+ try {
132
+ for (var _d = true, response_1 = __asyncValues(response), response_1_1; response_1_1 = yield response_1.next(), _a = response_1_1.done, !_a;) {
133
+ _c = response_1_1.value;
134
+ _d = false;
148
135
  try {
149
- ///{ delta: { content: '$\\' }, index: 0, finish_reason: null }
150
- ///发送出去
151
- const parsed = JSON.parse(txt);
152
- ///已经返回一个正确的了,可以重置这个变量了
153
- finishreason = parsed.choices[0].finish_reason;
154
- usage = parsed.usage;
155
- let streamtext = parsed.choices[0].delta.content;
156
- replytext.push(streamtext);
157
- let output = { successed: true, requestid, text: replytext.join(''), finish_reason: finishreason, index: parsed.choices[0].index, usage };
136
+ const chunk = _c;
137
+ const [choice] = chunk.choices, { content, finishreason, index, usage } = choice.delta;
138
+ if (!content)
139
+ continue;
140
+ replytext.push(content);
141
+ let output = { successed: true, requestid, segment: content, text: replytext.join(''), finish_reason: finishreason, index, usage };
158
142
  if (attach)
159
143
  output = Object.assign({}, output, attach);
160
144
  this.emit(finishreason ? 'chatdone' : 'chattext', output);
161
- if (finishreason)
162
- return;
163
145
  }
164
- catch (error) {
165
- errtxt += txt; ///这一段json没有结束,作为下一次的流过来时使用
166
- this.emit('chaterror', { successed: false, requestid, error: 'JSON parse stream message', errtxt });
146
+ finally {
147
+ _d = true;
167
148
  }
168
149
  }
169
- });
150
+ }
151
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
152
+ finally {
153
+ try {
154
+ if (!_d && !_a && (_b = response_1.return)) yield _b.call(response_1);
155
+ }
156
+ finally { if (e_1) throw e_1.error; }
157
+ }
158
+ // response.data.on('data', (data: any) => {
159
+ // const lines = data.toString().split('\n').filter((line: string) => line.trim() !== '');
160
+ // ///已经返回了结束原因
161
+ // if (finishreason) return;
162
+ // let alltext = (errtxt +lines.join('')).split('data:');
163
+ // errtxt = '';
164
+ // for (const line of alltext) {
165
+ // let txt = line.trim();
166
+ // if (!txt) continue;
167
+ // if (txt === '[DONE]') {
168
+ // let output = { successed: true, requestid, text: replytext.join(''), finish_reason: 'stop', usage };
169
+ // if (attach) output = Object.assign({}, output, attach);
170
+ // this.emit('chatdone', output)
171
+ // return; // Stream finished
172
+ // }
173
+ // try {
174
+ // ///{ delta: { content: '$\\' }, index: 0, finish_reason: null }
175
+ // ///发送出去
176
+ // const parsed = JSON.parse(txt);
177
+ // ///已经返回一个正确的了,可以重置这个变量了
178
+ // finishreason = parsed.choices[0].finish_reason;
179
+ // usage = parsed.usage;
180
+ // let streamtext = parsed.choices[0].delta.content;
181
+ // replytext.push(streamtext);
182
+ // let output = { successed: true, requestid, segment: streamtext, text: replytext.join(''), finish_reason: finishreason, index: parsed.choices[0].index, usage };
183
+ // if (attach) output = Object.assign({}, output, attach);
184
+ // this.emit(finishreason ? 'chatdone' : 'chattext', output)
185
+ // if (finishreason) return;
186
+ // } catch (error) {
187
+ // errtxt+=txt; ///这一段json没有结束,作为下一次的流过来时使用
188
+ // this.emit('chaterror', { successed: false, requestid, error: 'JSON parse stream message', errtxt });
189
+ // }
190
+ // }
191
+ // });
170
192
  return { successed: true, requestid };
171
193
  }
172
194
  catch (error) {
@@ -0,0 +1,18 @@
1
+ import { OpenAIApiParameters } from "./declare";
2
+ import GptBase from "./gptbase";
3
+ export default abstract class OpenAIBase<T> extends GptBase {
4
+ protected readonly apiKey: string;
5
+ protected readonly chatModel: string;
6
+ protected readonly maxtoken: number;
7
+ protected readonly top_p: number;
8
+ protected readonly presence_penalty: number;
9
+ protected readonly frequency_penalty: number;
10
+ protected readonly temperature: number;
11
+ protected readonly embeddingmodel: string;
12
+ protected aiApi: T | undefined;
13
+ constructor(apiKey: string, apiOption?: OpenAIApiParameters);
14
+ /**
15
+ * 初始化OpenAI 的聊天对象Api
16
+ */
17
+ abstract createOpenAI(apiKey: string): T;
18
+ }
@@ -0,0 +1,20 @@
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;
@@ -0,0 +1,21 @@
1
+ import { ApiResult, OpenAIApiParameters, ProxyPatameters } from './declare';
2
+ import OpenAIGpt from './openai';
3
+ export default class OpenAIProxy extends OpenAIGpt {
4
+ protected readonly proxySetting: ProxyPatameters;
5
+ constructor(apiKey: string, proxyOption: ProxyPatameters, apiOption?: OpenAIApiParameters);
6
+ /**
7
+ * 重写chatRequest方法
8
+ * @param chatText
9
+ * @param callChatOption
10
+ * @param axiosOption
11
+ */
12
+ chatRequest(chatText: string | any[], _paramOption: any, axiosOption?: any): Promise<ApiResult>;
13
+ /**
14
+ * 重写chatRequestInStream方法
15
+ * @param chatText
16
+ * @param callChatOption
17
+ * @param attach
18
+ * @param axiosOption
19
+ */
20
+ chatRequestInStream(chatText: Array<any>, callChatOption: OpenAIApiParameters, attach?: any, axiosOption?: any): Promise<any>;
21
+ }