doomiaichat 6.0.4 → 6.0.5

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
@@ -93,16 +93,19 @@ class AzureAI extends openaibase_1.default {
93
93
  presencePenalty: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.presence_penalty) || this.presence_penalty),
94
94
  frequencyPenalty: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.frequency_penalty) || this.frequency_penalty),
95
95
  n: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.replyCounts) || 1) || 1,
96
- tools: callChatOption.tools,
97
- toolChoice: (callChatOption.tool_choice || 'none'),
96
+ tools: ((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.enableToolCall) === 1 && (callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.tools)) ? callChatOption.tools : undefined,
97
+ toolChoice: (callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.enableToolCall) === 1 ? 'auto' : undefined
98
98
  });
99
99
  const { promptTokens: prompt_tokens, completionTokens: completion_tokens, totalTokens: total_tokens } = response.usage;
100
100
  let rebuildChoice = [];
101
101
  for (const msg of response.choices) {
102
- ///, contentFilterResults: content_filter
103
102
  const { index, finishReason: finish_reason, message } = msg;
104
103
  rebuildChoice.push({ index, finish_reason, message });
105
104
  }
105
+ // if (response.data.choices[0].finish_reason === 'content_filter') {
106
+ // console.log('content_filter');
107
+ // return { successed: false, error: 'content_filter' };
108
+ // }
106
109
  return { successed: true, message: rebuildChoice, usage: { prompt_tokens, completion_tokens, total_tokens } };
107
110
  }
108
111
  catch (error) {
package/dist/openai.js CHANGED
@@ -81,8 +81,8 @@ class OpenAIGpt extends openaibase_1.default {
81
81
  presence_penalty: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.presence_penalty) || this.presence_penalty),
82
82
  frequency_penalty: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.frequency_penalty) || this.frequency_penalty),
83
83
  n: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.replyCounts) || 1) || 1,
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,
84
+ // tools: (callChatOption?.enableToolCall === 1 && callChatOption?.tools) ? callChatOption.tools : undefined,
85
+ // tool_choice: callChatOption?.enableToolCall === 1 ? 'auto' : undefined,
86
86
  }, axiosOption);
87
87
  return { successed: true, message: response.data.choices, usage: response.data.usage };
88
88
  }
@@ -106,17 +106,10 @@ class OpenAIGpt extends openaibase_1.default {
106
106
  if (!this.aiApi) {
107
107
  this.aiApi = this.createOpenAI(this.apiKey);
108
108
  }
109
- // const DATA_END_TAG = `"usage":null}`
110
- let message = typeof (chatText) == 'string' ?
111
- [{ role: 'user', content: chatText }] : chatText;
112
- //axiosOption = Object.assign({}, axiosOption || { timeout: 60000 }, { responseType: 'stream' })
109
+ let message = typeof (chatText) == 'string' ? [{ role: 'user', content: chatText }] : chatText;
113
110
  axiosOption = Object.assign({}, axiosOption || { timeout: 60000 });
114
111
  let requestid = Math.ceil(Math.random() * (new Date().getTime() * Math.random()) / 1000);
115
112
  try {
116
- // let finishreason: any = null, usage: any = null,errtxt = '';
117
- ///便于知道返回的requestid
118
- // console.log('model', callChatOption?.model,this.chatModel,)
119
- //const response: any = await this.aiApi.chat.completions.create({
120
113
  const response = yield this.aiApi.chat.completions.create({
121
114
  model: (callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.model) || this.chatModel,
122
115
  messages: message,
@@ -132,6 +125,7 @@ class OpenAIGpt extends openaibase_1.default {
132
125
  }, axiosOption);
133
126
  let replytext = [];
134
127
  try {
128
+ // let has_tool_calls = 0, currentIndex, previous_index = -1, tool_calls: any[] = [];// 使用数组来存储工具调用
135
129
  for (var _d = true, response_1 = __asyncValues(response), response_1_1; response_1_1 = yield response_1.next(), _a = response_1_1.done, !_a;) {
136
130
  _c = response_1_1.value;
137
131
  _d = false;
@@ -156,40 +150,6 @@ class OpenAIGpt extends openaibase_1.default {
156
150
  }
157
151
  finally { if (e_1) throw e_1.error; }
158
152
  }
159
- // response.data.on('data', (data: any) => {
160
- // const lines = data.toString().split('\n').filter((line: string) => line.trim() !== '');
161
- // ///已经返回了结束原因
162
- // if (finishreason) return;
163
- // let alltext = (errtxt +lines.join('')).split('data:');
164
- // errtxt = '';
165
- // for (const line of alltext) {
166
- // let txt = line.trim();
167
- // if (!txt) continue;
168
- // if (txt === '[DONE]') {
169
- // let output = { successed: true, requestid, text: replytext.join(''), finish_reason: 'stop', usage };
170
- // if (attach) output = Object.assign({}, output, attach);
171
- // this.emit('chatdone', output)
172
- // return; // Stream finished
173
- // }
174
- // try {
175
- // ///{ delta: { content: '$\\' }, index: 0, finish_reason: null }
176
- // ///发送出去
177
- // const parsed = JSON.parse(txt);
178
- // ///已经返回一个正确的了,可以重置这个变量了
179
- // finishreason = parsed.choices[0].finish_reason;
180
- // usage = parsed.usage;
181
- // let streamtext = parsed.choices[0].delta.content;
182
- // replytext.push(streamtext);
183
- // let output = { successed: true, requestid, segment: streamtext, text: replytext.join(''), finish_reason: finishreason, index: parsed.choices[0].index, usage };
184
- // if (attach) output = Object.assign({}, output, attach);
185
- // this.emit(finishreason ? 'chatdone' : 'chattext', output)
186
- // if (finishreason) return;
187
- // } catch (error) {
188
- // errtxt+=txt; ///这一段json没有结束,作为下一次的流过来时使用
189
- // this.emit('chaterror', { successed: false, requestid, error: 'JSON parse stream message', errtxt });
190
- // }
191
- // }
192
- // });
193
153
  return { successed: true, requestid };
194
154
  }
195
155
  catch (error) {
@@ -65,7 +65,7 @@ class OpenAIProxy extends openai_1.default {
65
65
  },
66
66
  responseType: 'stream',
67
67
  };
68
- // let lastResponse:any = null;
68
+ let unCompleteSegment = '';
69
69
  (0, axios_1.default)(opts)
70
70
  .then(res => {
71
71
  res.data.on('data', (chunk) => {
@@ -75,17 +75,24 @@ class OpenAIProxy extends openai_1.default {
75
75
  if (ERROR_RESPONSE.includes(streamText)) {
76
76
  return this.emit('requesterror', { successed: false, requestid, error: 'Request Remote OpenAI Error : ' + streamText });
77
77
  }
78
- const fullData = streamText.split('*&$');
78
+ const fullData = (unCompleteSegment + streamText).split('*&$');
79
+ unCompleteSegment = '';
79
80
  // console.log('fullData', fullData.length);
80
81
  for (const segment of fullData) {
81
82
  if (!segment)
82
83
  continue;
83
84
  try {
85
+ ////判断接收到的不是一个完整的JSON段了,则该段作为下一次的数据段
86
+ if (!segment.endsWith('}')) {
87
+ unCompleteSegment = segment;
88
+ break;
89
+ }
84
90
  const objData = Object.assign(JSON.parse(segment), attach);
85
91
  this.emit(objData.finish_reason ? 'chatdone' : 'chattext', objData);
86
92
  }
87
93
  catch (errParse) {
88
- this.emit('chaterror', { successed: false, requestid, error: 'JSON parse stream message' + errParse });
94
+ break;
95
+ //this.emit('chaterror', { successed: false, requestid, error: 'JSON parse stream message' + errParse });
89
96
  }
90
97
  }
91
98
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "doomiaichat",
3
- "version": "6.0.4",
3
+ "version": "6.0.5",
4
4
  "description": "Doomisoft OpenAI",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/src/azureai.ts CHANGED
@@ -3,7 +3,7 @@
3
3
  */
4
4
  import OpenAIBase from "./openaibase"
5
5
  import { AzureOpenAIPatameters, ChatReponse, EmbeddingResult, OpenAIApiParameters, request } from "./declare";
6
- import { OpenAIClient, AzureKeyCredential, ChatCompletionsNamedToolSelection } from "@azure/openai";
6
+ import { OpenAIClient, AzureKeyCredential } from "@azure/openai";
7
7
  export default class AzureAI extends OpenAIBase<OpenAIClient> {
8
8
 
9
9
  protected readonly azureSetting: AzureOpenAIPatameters;
@@ -80,16 +80,19 @@ export default class AzureAI extends OpenAIBase<OpenAIClient> {
80
80
  presencePenalty: Number(callChatOption?.presence_penalty || this.presence_penalty),
81
81
  frequencyPenalty: Number(callChatOption?.frequency_penalty || this.frequency_penalty),
82
82
  n: Number(callChatOption?.replyCounts || 1) || 1,
83
- tools: callChatOption.tools,
84
- toolChoice: (callChatOption.tool_choice || 'none') as ChatCompletionsNamedToolSelection,
83
+ tools: (callChatOption?.enableToolCall === 1 && callChatOption?.tools) ? callChatOption.tools : undefined,
84
+ toolChoice: callChatOption?.enableToolCall === 1 ? 'auto' : undefined
85
85
  });
86
86
  const { promptTokens: prompt_tokens, completionTokens: completion_tokens, totalTokens: total_tokens } = response.usage
87
87
  let rebuildChoice = [];
88
88
  for (const msg of response.choices) {
89
- ///, contentFilterResults: content_filter
90
89
  const { index, finishReason: finish_reason, message } = msg
91
90
  rebuildChoice.push({ index, finish_reason, message })
92
91
  }
92
+ // if (response.data.choices[0].finish_reason === 'content_filter') {
93
+ // console.log('content_filter');
94
+ // return { successed: false, error: 'content_filter' };
95
+ // }
93
96
  return { successed: true, message: rebuildChoice, usage: { prompt_tokens, completion_tokens, total_tokens } };
94
97
 
95
98
  } catch (error) {
package/src/openai.ts CHANGED
@@ -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?.enableToolCall === 1 && callChatOption?.tools) ? callChatOption.tools : undefined,
61
- tool_choice: callChatOption?.enableToolCall === 1 ? 'auto' : undefined,
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) {
@@ -78,17 +78,10 @@ export default class OpenAIGpt extends OpenAIBase<OpenAI> {
78
78
  if (!this.aiApi) {
79
79
  this.aiApi = this.createOpenAI(this.apiKey);
80
80
  }
81
- // const DATA_END_TAG = `"usage":null}`
82
- let message: Array<any> = typeof (chatText) == 'string' ?
83
- [{ role: 'user', content: chatText }] : chatText;
84
- //axiosOption = Object.assign({}, axiosOption || { timeout: 60000 }, { responseType: 'stream' })
81
+ let message: Array<any> = typeof (chatText) == 'string' ? [{ role: 'user', content: chatText }] : chatText;
85
82
  axiosOption = Object.assign({}, axiosOption || { timeout: 60000 })
86
83
  let requestid = Math.ceil(Math.random() * (new Date().getTime() * Math.random()) / 1000);
87
84
  try {
88
- // let finishreason: any = null, usage: any = null,errtxt = '';
89
- ///便于知道返回的requestid
90
- // console.log('model', callChatOption?.model,this.chatModel,)
91
- //const response: any = await this.aiApi.chat.completions.create({
92
85
  const response: any = await this.aiApi.chat.completions.create(
93
86
  {
94
87
  model: callChatOption?.model || this.chatModel,
@@ -104,6 +97,7 @@ export default class OpenAIGpt extends OpenAIBase<OpenAI> {
104
97
  stream:true
105
98
  }, axiosOption);
106
99
  let replytext: string[] = [];
100
+ // let has_tool_calls = 0, currentIndex, previous_index = -1, tool_calls: any[] = [];// 使用数组来存储工具调用
107
101
  for await (const chunk of response) {
108
102
  const [choice] = chunk.choices,
109
103
  { finish_reason:finishreason, index, usage } = choice,
@@ -113,40 +107,6 @@ export default class OpenAIGpt extends OpenAIBase<OpenAI> {
113
107
  if (attach) output = Object.assign({}, output, attach);
114
108
  this.emit(finishreason ? 'chatdone' : 'chattext', output)
115
109
  }
116
- // response.data.on('data', (data: any) => {
117
- // const lines = data.toString().split('\n').filter((line: string) => line.trim() !== '');
118
- // ///已经返回了结束原因
119
- // if (finishreason) return;
120
- // let alltext = (errtxt +lines.join('')).split('data:');
121
- // errtxt = '';
122
- // for (const line of alltext) {
123
- // let txt = line.trim();
124
- // if (!txt) continue;
125
- // if (txt === '[DONE]') {
126
- // let output = { successed: true, requestid, text: replytext.join(''), finish_reason: 'stop', usage };
127
- // if (attach) output = Object.assign({}, output, attach);
128
- // this.emit('chatdone', output)
129
- // return; // Stream finished
130
- // }
131
- // try {
132
- // ///{ delta: { content: '$\\' }, index: 0, finish_reason: null }
133
- // ///发送出去
134
- // const parsed = JSON.parse(txt);
135
- // ///已经返回一个正确的了,可以重置这个变量了
136
- // finishreason = parsed.choices[0].finish_reason;
137
- // usage = parsed.usage;
138
- // let streamtext = parsed.choices[0].delta.content;
139
- // replytext.push(streamtext);
140
- // let output = { successed: true, requestid, segment: streamtext, text: replytext.join(''), finish_reason: finishreason, index: parsed.choices[0].index, usage };
141
- // if (attach) output = Object.assign({}, output, attach);
142
- // this.emit(finishreason ? 'chatdone' : 'chattext', output)
143
- // if (finishreason) return;
144
- // } catch (error) {
145
- // errtxt+=txt; ///这一段json没有结束,作为下一次的流过来时使用
146
- // this.emit('chaterror', { successed: false, requestid, error: 'JSON parse stream message', errtxt });
147
- // }
148
- // }
149
- // });
150
110
  return { successed: true, requestid }
151
111
  } catch (error) {
152
112
  this.emit('requesterror', { successed: false, requestid, error: 'call axios faied ' + error });
@@ -56,7 +56,7 @@ export default class OpenAIProxy extends OpenAIGpt {
56
56
  },
57
57
  responseType: 'stream',
58
58
  }
59
- // let lastResponse:any = null;
59
+ let unCompleteSegment:string = '';
60
60
  axios(opts)
61
61
  .then(res => {
62
62
  res.data.on('data', (chunk:any) => {
@@ -66,15 +66,23 @@ export default class OpenAIProxy extends OpenAIGpt {
66
66
  if (ERROR_RESPONSE.includes(streamText)) {
67
67
  return this.emit('requesterror', { successed: false, requestid, error: 'Request Remote OpenAI Error : ' + streamText });
68
68
  }
69
- const fullData = streamText.split('*&$')
69
+ const fullData = (unCompleteSegment +streamText).split('*&$')
70
+ unCompleteSegment = '';
70
71
  // console.log('fullData', fullData.length);
71
72
  for (const segment of fullData){
72
73
  if (!segment) continue;
73
74
  try {
75
+ ////判断接收到的不是一个完整的JSON段了,则该段作为下一次的数据段
76
+ if (!segment.endsWith('}')) {
77
+ unCompleteSegment = segment;
78
+ break;
79
+ }
74
80
  const objData = Object.assign(JSON.parse(segment), attach);
75
81
  this.emit(objData.finish_reason?'chatdone':'chattext', objData);
76
82
  } catch (errParse) {
77
- this.emit('chaterror', { successed: false, requestid, error: 'JSON parse stream message' + errParse });
83
+
84
+ break;
85
+ //this.emit('chaterror', { successed: false, requestid, error: 'JSON parse stream message' + errParse });
78
86
  }
79
87
  }
80
88