doomiaichat 6.0.1 → 6.0.4

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.js CHANGED
@@ -119,7 +119,7 @@ class AzureAI extends openaibase_1.default {
119
119
  */
120
120
  chatRequestInStream(chatText, callChatOption, attach, axiosOption) {
121
121
  var _a, e_1, _b, _c;
122
- var _d;
122
+ var _d, _e, _f;
123
123
  return __awaiter(this, void 0, void 0, function* () {
124
124
  if (!chatText)
125
125
  this.emit('chaterror', { successed: false, error: 'no text in chat' });
@@ -129,6 +129,7 @@ class AzureAI extends openaibase_1.default {
129
129
  let message = typeof (chatText) == 'string' ? [{ role: 'user', content: chatText }] : chatText;
130
130
  axiosOption = Object.assign({}, axiosOption || { timeout: 60000 });
131
131
  let requestid = Math.ceil(Math.random() * (new Date().getTime() * Math.random()) / 1000);
132
+ let has_tool_calls = 0, currentIndex, previous_index = -1, tool_calls = []; // 使用数组来存储工具调用
132
133
  try {
133
134
  const response = yield this.aiApi.streamChatCompletions((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.model) || this.chatModel, message, {
134
135
  temperature: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.temperature) || this.temperature),
@@ -136,34 +137,62 @@ class AzureAI extends openaibase_1.default {
136
137
  topP: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.top_p) || this.top_p),
137
138
  presencePenalty: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.presence_penalty) || this.presence_penalty),
138
139
  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
+ n: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.replyCounts) || 1) || 1,
141
+ tools: ((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.enableToolCall) === 1 && (callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.tools)) ? callChatOption.tools : undefined,
142
+ toolChoice: (callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.enableToolCall) === 1 ? 'auto' : undefined
140
143
  });
144
+ //console.log('tools', callChatOption.enableToolCall, callChatOption.tools)
141
145
  let replytext = [];
142
146
  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;) {
147
+ for (var _g = true, response_1 = __asyncValues(response), response_1_1; response_1_1 = yield response_1.next(), _a = response_1_1.done, !_a;) {
144
148
  _c = response_1_1.value;
145
- _e = false;
149
+ _g = false;
146
150
  try {
147
151
  const event = _c;
148
152
  for (const choice of event.choices) {
149
153
  const { finishReason: finishreason, index } = choice;
150
- const content = (_d = choice.delta) === null || _d === void 0 ? void 0 : _d.content;
151
- replytext.push(content);
152
- let output = { successed: true, requestid, segment: content, text: replytext.join(''), finish_reason: finishreason, index }; //, usage };
154
+ const toolCalls = (_d = choice.delta) === null || _d === void 0 ? void 0 : _d.toolCalls;
155
+ ///存在了toolCalls
156
+ if (toolCalls && toolCalls.length) {
157
+ currentIndex = toolCalls[0].index;
158
+ has_tool_calls = 1;
159
+ // 检查index是否发生变化
160
+ console.log('currentIndex,previous_index', currentIndex, previous_index);
161
+ if (currentIndex !== previous_index) {
162
+ tool_calls.push({
163
+ id: toolCalls[0].id,
164
+ type: 'function',
165
+ function: {
166
+ name: toolCalls[0].function.name,
167
+ arguments: toolCalls[0].function.arguments
168
+ }
169
+ });
170
+ // 更新previousIndex以供下次比较
171
+ previous_index = currentIndex;
172
+ }
173
+ else {
174
+ tool_calls[previous_index].function.arguments += toolCalls[0].function.arguments;
175
+ }
176
+ }
177
+ else {
178
+ const content = (_e = choice.delta) === null || _e === void 0 ? void 0 : _e.content;
179
+ replytext.push(content);
180
+ }
181
+ let output = { successed: true, requestid, segment: (_f = choice.delta) === null || _f === void 0 ? void 0 : _f.content, text: replytext.join(''), finish_reason: finishreason, index, has_tool_calls: has_tool_calls, tool_calls: tool_calls };
153
182
  if (attach)
154
183
  output = Object.assign({}, output, attach);
155
184
  this.emit(finishreason ? 'chatdone' : 'chattext', output);
156
185
  }
157
186
  }
158
187
  finally {
159
- _e = true;
188
+ _g = true;
160
189
  }
161
190
  }
162
191
  }
163
192
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
164
193
  finally {
165
194
  try {
166
- if (!_e && !_a && (_b = response_1.return)) yield _b.call(response_1);
195
+ if (!_g && !_a && (_b = response_1.return)) yield _b.call(response_1);
167
196
  }
168
197
  finally { if (e_1) throw e_1.error; }
169
198
  }
package/dist/declare.d.ts CHANGED
@@ -37,6 +37,7 @@ export interface OpenAIApiParameters {
37
37
  'replyCounts'?: number;
38
38
  'tools'?: Array<any>;
39
39
  'tool_choice'?: string;
40
+ 'enableToolCall'?: number;
40
41
  }
41
42
  /**
42
43
  * Azure 上的OpenAI的链接参数
package/dist/openai.js CHANGED
@@ -25,6 +25,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
25
25
  */
26
26
  const openaibase_1 = __importDefault(require("./openaibase"));
27
27
  const openai_1 = __importDefault(require("openai"));
28
+ // import { ChatCompletionToolChoiceOption } from "openai/resources";
28
29
  class OpenAIGpt extends openaibase_1.default {
29
30
  /**
30
31
  * 初始化OpenAI 的聊天对象Api
@@ -80,8 +81,8 @@ class OpenAIGpt extends openaibase_1.default {
80
81
  presence_penalty: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.presence_penalty) || this.presence_penalty),
81
82
  frequency_penalty: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.frequency_penalty) || this.frequency_penalty),
82
83
  n: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.replyCounts) || 1) || 1,
83
- tools: callChatOption.tools,
84
- tool_choice: (callChatOption.tool_choice || 'none'),
84
+ tools: ((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.enableToolCall) === 1 && (callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.tools)) ? callChatOption.tools : undefined,
85
+ tool_choice: (callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.enableToolCall) === 1 ? 'auto' : undefined,
85
86
  }, axiosOption);
86
87
  return { successed: true, message: response.data.choices, usage: response.data.usage };
87
88
  }
@@ -114,7 +115,7 @@ class OpenAIGpt extends openaibase_1.default {
114
115
  try {
115
116
  // let finishreason: any = null, usage: any = null,errtxt = '';
116
117
  ///便于知道返回的requestid
117
- // console.log('model', callChatOption?.model || this.chatModel,)
118
+ // console.log('model', callChatOption?.model,this.chatModel,)
118
119
  //const response: any = await this.aiApi.chat.completions.create({
119
120
  const response = yield this.aiApi.chat.completions.create({
120
121
  model: (callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.model) || this.chatModel,
@@ -125,6 +126,8 @@ class OpenAIGpt extends openaibase_1.default {
125
126
  presence_penalty: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.presence_penalty) || this.presence_penalty),
126
127
  frequency_penalty: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.frequency_penalty) || this.frequency_penalty),
127
128
  n: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.replyCounts) || 1) || 1,
129
+ tools: ((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.enableToolCall) === 1 && (callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.tools)) ? callChatOption.tools : undefined,
130
+ tool_choice: (callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.enableToolCall) === 1 ? 'auto' : undefined,
128
131
  stream: true
129
132
  }, axiosOption);
130
133
  let replytext = [];
@@ -134,7 +137,7 @@ class OpenAIGpt extends openaibase_1.default {
134
137
  _d = false;
135
138
  try {
136
139
  const chunk = _c;
137
- const [choice] = chunk.choices, { content, finishreason, index, usage } = choice.delta;
140
+ const [choice] = chunk.choices, { finish_reason: finishreason, index, usage } = choice, { content } = choice.delta;
138
141
  replytext.push(content);
139
142
  let output = { successed: true, requestid, segment: content, text: replytext.join(''), finish_reason: finishreason, index, usage };
140
143
  if (attach)
@@ -65,6 +65,7 @@ class OpenAIProxy extends openai_1.default {
65
65
  },
66
66
  responseType: 'stream',
67
67
  };
68
+ // let lastResponse:any = null;
68
69
  (0, axios_1.default)(opts)
69
70
  .then(res => {
70
71
  res.data.on('data', (chunk) => {
@@ -74,19 +75,17 @@ class OpenAIProxy extends openai_1.default {
74
75
  if (ERROR_RESPONSE.includes(streamText)) {
75
76
  return this.emit('requesterror', { successed: false, requestid, error: 'Request Remote OpenAI Error : ' + streamText });
76
77
  }
77
- ///已经全部结束了
78
- if (streamText === '[END]') {
79
- return this.emit('chatdone', streamText || {});
80
- }
81
- else {
82
- ///持续的文字输出中
78
+ const fullData = streamText.split('*&$');
79
+ // console.log('fullData', fullData.length);
80
+ for (const segment of fullData) {
81
+ if (!segment)
82
+ continue;
83
83
  try {
84
- streamText = JSON.parse(streamText);
85
- // overContent = JSON.parse(streamText);
86
- return this.emit('chattext', Object.assign(streamText, attach));
84
+ const objData = Object.assign(JSON.parse(segment), attach);
85
+ this.emit(objData.finish_reason ? 'chatdone' : 'chattext', objData);
87
86
  }
88
87
  catch (errParse) {
89
- return this.emit('chaterror', { successed: false, requestid, error: 'JSON parse stream message', streamText });
88
+ this.emit('chaterror', { successed: false, requestid, error: 'JSON parse stream message' + errParse });
90
89
  }
91
90
  }
92
91
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "doomiaichat",
3
- "version": "6.0.1",
3
+ "version": "6.0.4",
4
4
  "description": "Doomisoft OpenAI",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -16,8 +16,8 @@
16
16
  "typescript": "^4.9.5"
17
17
  },
18
18
  "dependencies": {
19
- "@azure/openai": "^1.0.0-beta.10",
19
+ "@azure/openai": "^1.0.0-beta.11",
20
20
  "axios": "^1.3.4",
21
- "openai": "^4.24.1"
21
+ "openai": "^4.29.0"
22
22
  }
23
23
  }
package/src/azureai.ts CHANGED
@@ -113,6 +113,7 @@ export default class AzureAI extends OpenAIBase<OpenAIClient> {
113
113
  let message: Array<any> = typeof (chatText) == 'string' ?[{ role: 'user', content: chatText }] : chatText;
114
114
  axiosOption = Object.assign({}, axiosOption || { timeout: 60000 })
115
115
  let requestid = Math.ceil(Math.random() * (new Date().getTime() * Math.random()) / 1000);
116
+ let has_tool_calls=0,currentIndex, previous_index=-1, tool_calls:any[] = [];// 使用数组来存储工具调用
116
117
  try {
117
118
  const response: any = await this.aiApi.streamChatCompletions(
118
119
  callChatOption?.model || this.chatModel,
@@ -123,22 +124,48 @@ export default class AzureAI extends OpenAIBase<OpenAIClient> {
123
124
  topP: Number(callChatOption?.top_p || this.top_p),
124
125
  presencePenalty: Number(callChatOption?.presence_penalty || this.presence_penalty),
125
126
  frequencyPenalty: Number(callChatOption?.frequency_penalty || this.frequency_penalty),
126
- n: Number(callChatOption?.replyCounts || 1) || 1
127
+ n: Number(callChatOption?.replyCounts || 1) || 1,
128
+ tools: (callChatOption?.enableToolCall===1 && callChatOption?.tools) ? callChatOption.tools : undefined,
129
+ toolChoice: callChatOption?.enableToolCall === 1 ?'auto':undefined
127
130
  });
131
+ //console.log('tools', callChatOption.enableToolCall, callChatOption.tools)
128
132
  let replytext: string[] = [];
129
133
  for await (const event of response) {
130
134
  for (const choice of event.choices) {
131
135
  const { finishReason: finishreason, index } = choice;
132
- const content = choice.delta?.content;
133
- replytext.push(content);
134
- let output = { successed: true, requestid, segment: content, text: replytext.join(''), finish_reason: finishreason, index };//, usage };
136
+ const toolCalls = choice.delta?.toolCalls;
137
+ ///存在了toolCalls
138
+ if (toolCalls && toolCalls.length){
139
+ currentIndex = toolCalls[0].index;
140
+ has_tool_calls = 1;
141
+ // 检查index是否发生变化
142
+ console.log('currentIndex,previous_index', currentIndex, previous_index)
143
+ if (currentIndex !== previous_index) {
144
+ tool_calls.push({
145
+ id: toolCalls[0].id,
146
+ type: 'function',
147
+ function: {
148
+ name: toolCalls[0].function.name,
149
+ arguments: toolCalls[0].function.arguments
150
+ }
151
+ });
152
+ // 更新previousIndex以供下次比较
153
+ previous_index = currentIndex;
154
+ } else {
155
+ tool_calls[previous_index].function.arguments += toolCalls[0].function.arguments
156
+ }
157
+ }else{
158
+ const content = choice.delta?.content;
159
+ replytext.push(content);
160
+ }
161
+ 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 };
135
162
  if (attach) output = Object.assign({}, output, attach);
136
163
  this.emit(finishreason ? 'chatdone' : 'chattext', output)
137
164
  }
138
165
  }
139
166
  return { successed: true, requestid }
140
167
  } catch (error) {
141
- this.emit('requesterror', { successed: false, requestid, error: 'call axios faied ' + error });
168
+ this.emit('requesterror', { successed: false, requestid, error: 'call axios faied ' +error });
142
169
  return { successed: false, requestid }
143
170
  }
144
171
  }
package/src/declare.ts CHANGED
@@ -39,7 +39,8 @@ export interface OpenAIApiParameters {
39
39
  'frequency_penalty'?: number;
40
40
  'replyCounts'?: number; ///返回多少答案
41
41
  'tools'?:Array<any>,
42
- 'tool_choice'?: string
42
+ 'tool_choice'?: string,
43
+ 'enableToolCall'?: number ///是否允许调用toolfunction
43
44
  }
44
45
  /**
45
46
  * Azure 上的OpenAI的链接参数
package/src/openai.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  import OpenAIBase from "./openaibase"
6
6
  import { OpenAIApiParameters, ChatReponse, EmbeddingResult } from './declare'
7
7
  import OpenAI from "openai";
8
- import { ChatCompletionToolChoiceOption } from "openai/resources";
8
+ // import { ChatCompletionToolChoiceOption } from "openai/resources";
9
9
  export default class OpenAIGpt extends OpenAIBase<OpenAI> {
10
10
  /**
11
11
  * 初始化OpenAI 的聊天对象Api
@@ -57,8 +57,8 @@ export default class OpenAIGpt extends OpenAIBase<OpenAI> {
57
57
  presence_penalty: Number(callChatOption?.presence_penalty || this.presence_penalty),
58
58
  frequency_penalty: Number(callChatOption?.frequency_penalty || this.frequency_penalty),
59
59
  n: Number(callChatOption?.replyCounts || 1) || 1,
60
- tools: callChatOption.tools,
61
- tool_choice: (callChatOption.tool_choice || 'none') as ChatCompletionToolChoiceOption,
60
+ tools: (callChatOption?.enableToolCall === 1 && callChatOption?.tools) ? callChatOption.tools : undefined,
61
+ tool_choice: callChatOption?.enableToolCall === 1 ? 'auto' : undefined,
62
62
  }, axiosOption);
63
63
  return { successed: true, message: response.data.choices, usage: response.data.usage };
64
64
  } catch (error) {
@@ -87,7 +87,7 @@ export default class OpenAIGpt extends OpenAIBase<OpenAI> {
87
87
  try {
88
88
  // let finishreason: any = null, usage: any = null,errtxt = '';
89
89
  ///便于知道返回的requestid
90
- // console.log('model', callChatOption?.model || this.chatModel,)
90
+ // console.log('model', callChatOption?.model,this.chatModel,)
91
91
  //const response: any = await this.aiApi.chat.completions.create({
92
92
  const response: any = await this.aiApi.chat.completions.create(
93
93
  {
@@ -99,11 +99,15 @@ export default class OpenAIGpt extends OpenAIBase<OpenAI> {
99
99
  presence_penalty: Number(callChatOption?.presence_penalty || this.presence_penalty),
100
100
  frequency_penalty: Number(callChatOption?.frequency_penalty || this.frequency_penalty),
101
101
  n: Number(callChatOption?.replyCounts || 1) || 1,
102
+ tools: (callChatOption?.enableToolCall === 1 && callChatOption?.tools) ? callChatOption.tools : undefined,
103
+ tool_choice: callChatOption?.enableToolCall === 1 ? 'auto' : undefined,
102
104
  stream:true
103
105
  }, axiosOption);
104
106
  let replytext: string[] = [];
105
107
  for await (const chunk of response) {
106
- const [choice] = chunk.choices, { content, finishreason, index, usage } = choice.delta;
108
+ const [choice] = chunk.choices,
109
+ { finish_reason:finishreason, index, usage } = choice,
110
+ { content} = choice.delta;
107
111
  replytext.push(content);
108
112
  let output = { successed: true, requestid, segment: content, text: replytext.join(''), finish_reason: finishreason, index, usage };
109
113
  if (attach) output = Object.assign({}, output, attach);
@@ -56,6 +56,7 @@ export default class OpenAIProxy extends OpenAIGpt {
56
56
  },
57
57
  responseType: 'stream',
58
58
  }
59
+ // let lastResponse:any = null;
59
60
  axios(opts)
60
61
  .then(res => {
61
62
  res.data.on('data', (chunk:any) => {
@@ -65,19 +66,18 @@ export default class OpenAIProxy extends OpenAIGpt {
65
66
  if (ERROR_RESPONSE.includes(streamText)) {
66
67
  return this.emit('requesterror', { successed: false, requestid, error: 'Request Remote OpenAI Error : ' + streamText });
67
68
  }
68
- ///已经全部结束了
69
- if (streamText==='[END]') {
70
- return this.emit('chatdone', streamText || {})
71
- } else{
72
- ///持续的文字输出中
73
- try{
74
- streamText = JSON.parse(streamText);
75
- // overContent = JSON.parse(streamText);
76
- return this.emit('chattext', Object.assign(streamText, attach));
77
- }catch(errParse){
78
- return this.emit('chaterror', { successed: false, requestid, error: 'JSON parse stream message', streamText });
69
+ const fullData = streamText.split('*&$')
70
+ // console.log('fullData', fullData.length);
71
+ for (const segment of fullData){
72
+ if (!segment) continue;
73
+ try {
74
+ const objData = Object.assign(JSON.parse(segment), attach);
75
+ this.emit(objData.finish_reason?'chatdone':'chattext', objData);
76
+ } catch (errParse) {
77
+ this.emit('chaterror', { successed: false, requestid, error: 'JSON parse stream message' + errParse });
79
78
  }
80
79
  }
80
+
81
81
  }
82
82
  return;
83
83
  });