doomiaichat 6.0.3 → 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 +6 -4
- package/dist/openai.js +4 -44
- package/dist/openaiproxy.js +16 -10
- package/package.json +1 -1
- package/src/azureai.ts +7 -5
- package/src/openai.ts +4 -44
- package/src/openaiproxy.ts +19 -11
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
|
|
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) {
|
|
@@ -152,7 +155,6 @@ class AzureAI extends openaibase_1.default {
|
|
|
152
155
|
for (const choice of event.choices) {
|
|
153
156
|
const { finishReason: finishreason, index } = choice;
|
|
154
157
|
const toolCalls = (_d = choice.delta) === null || _d === void 0 ? void 0 : _d.toolCalls;
|
|
155
|
-
console.log('toolCalls', toolCalls);
|
|
156
158
|
///存在了toolCalls
|
|
157
159
|
if (toolCalls && toolCalls.length) {
|
|
158
160
|
currentIndex = toolCalls[0].index;
|
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: (
|
|
85
|
-
tool_choice:
|
|
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
|
-
|
|
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) {
|
package/dist/openaiproxy.js
CHANGED
|
@@ -65,6 +65,7 @@ class OpenAIProxy extends openai_1.default {
|
|
|
65
65
|
},
|
|
66
66
|
responseType: 'stream',
|
|
67
67
|
};
|
|
68
|
+
let unCompleteSegment = '';
|
|
68
69
|
(0, axios_1.default)(opts)
|
|
69
70
|
.then(res => {
|
|
70
71
|
res.data.on('data', (chunk) => {
|
|
@@ -74,19 +75,24 @@ 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
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
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;
|
|
83
84
|
try {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
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);
|
|
87
92
|
}
|
|
88
93
|
catch (errParse) {
|
|
89
|
-
|
|
94
|
+
break;
|
|
95
|
+
//this.emit('chaterror', { successed: false, requestid, error: 'JSON parse stream message' + errParse });
|
|
90
96
|
}
|
|
91
97
|
}
|
|
92
98
|
}
|
package/package.json
CHANGED
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
|
|
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:
|
|
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) {
|
|
@@ -134,7 +137,6 @@ export default class AzureAI extends OpenAIBase<OpenAIClient> {
|
|
|
134
137
|
for (const choice of event.choices) {
|
|
135
138
|
const { finishReason: finishreason, index } = choice;
|
|
136
139
|
const toolCalls = choice.delta?.toolCalls;
|
|
137
|
-
console.log('toolCalls', toolCalls);
|
|
138
140
|
///存在了toolCalls
|
|
139
141
|
if (toolCalls && toolCalls.length){
|
|
140
142
|
currentIndex = toolCalls[0].index;
|
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
|
-
|
|
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 });
|
package/src/openaiproxy.ts
CHANGED
|
@@ -56,6 +56,7 @@ export default class OpenAIProxy extends OpenAIGpt {
|
|
|
56
56
|
},
|
|
57
57
|
responseType: 'stream',
|
|
58
58
|
}
|
|
59
|
+
let unCompleteSegment:string = '';
|
|
59
60
|
axios(opts)
|
|
60
61
|
.then(res => {
|
|
61
62
|
res.data.on('data', (chunk:any) => {
|
|
@@ -65,19 +66,26 @@ 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
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
try{
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
69
|
+
const fullData = (unCompleteSegment +streamText).split('*&$')
|
|
70
|
+
unCompleteSegment = '';
|
|
71
|
+
// console.log('fullData', fullData.length);
|
|
72
|
+
for (const segment of fullData){
|
|
73
|
+
if (!segment) continue;
|
|
74
|
+
try {
|
|
75
|
+
////判断接收到的不是一个完整的JSON段了,则该段作为下一次的数据段
|
|
76
|
+
if (!segment.endsWith('}')) {
|
|
77
|
+
unCompleteSegment = segment;
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
const objData = Object.assign(JSON.parse(segment), attach);
|
|
81
|
+
this.emit(objData.finish_reason?'chatdone':'chattext', objData);
|
|
82
|
+
} catch (errParse) {
|
|
83
|
+
|
|
84
|
+
break;
|
|
85
|
+
//this.emit('chaterror', { successed: false, requestid, error: 'JSON parse stream message' + errParse });
|
|
79
86
|
}
|
|
80
87
|
}
|
|
88
|
+
|
|
81
89
|
}
|
|
82
90
|
return;
|
|
83
91
|
});
|