@leikeduntech/leiai-js 4.1.0 → 4.1.2
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/build/index.js +163 -301
- package/package.json +1 -1
package/build/index.js
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
|
+
}) : x)(function(x) {
|
|
4
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
5
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
6
|
+
});
|
|
7
|
+
|
|
1
8
|
// src/chatgpt-api.ts
|
|
2
9
|
import { Spark } from "@leikeduntech/spark-nodejs";
|
|
3
10
|
import Keyv from "keyv";
|
|
@@ -113,46 +120,6 @@ var uuidv4Re = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9
|
|
|
113
120
|
function isValidUUIDv4(str) {
|
|
114
121
|
return str && uuidv4Re.test(str);
|
|
115
122
|
}
|
|
116
|
-
function convertArrayToString(arr) {
|
|
117
|
-
let result = "[";
|
|
118
|
-
for (let i = 0; i < arr.length; i++) {
|
|
119
|
-
const obj = arr[i];
|
|
120
|
-
const keys = Object.keys(obj);
|
|
121
|
-
result += "{";
|
|
122
|
-
for (let j = 0; j < keys.length; j++) {
|
|
123
|
-
const key = keys[j];
|
|
124
|
-
const value = obj[key];
|
|
125
|
-
result += '"' + key + '":' + (typeof value === "string" ? '"' + value + '"' : value) + ",";
|
|
126
|
-
}
|
|
127
|
-
result = result.slice(0, -1);
|
|
128
|
-
result += "},";
|
|
129
|
-
}
|
|
130
|
-
result = result.slice(0, -1);
|
|
131
|
-
result += "]";
|
|
132
|
-
return result;
|
|
133
|
-
}
|
|
134
|
-
function signTencentHunyuan(bodyData, url, keyList) {
|
|
135
|
-
const sortedObj = {};
|
|
136
|
-
Object.keys(bodyData).sort().forEach((key) => {
|
|
137
|
-
sortedObj[key] = bodyData[key];
|
|
138
|
-
});
|
|
139
|
-
let signStr = "";
|
|
140
|
-
for (let key in sortedObj) {
|
|
141
|
-
if (signStr) {
|
|
142
|
-
if (typeof sortedObj[key] === "object") {
|
|
143
|
-
signStr += `&${key}=${convertArrayToString(sortedObj[key])}`;
|
|
144
|
-
} else {
|
|
145
|
-
signStr += `&${key}=${sortedObj[key]}`;
|
|
146
|
-
}
|
|
147
|
-
} else {
|
|
148
|
-
signStr += `${key}=${sortedObj[key]}`;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
signStr = `${url.replace("https://", "")}?${signStr}`;
|
|
152
|
-
const hmac = CryptoJS.HmacSHA1(signStr, keyList[2]);
|
|
153
|
-
const signBase64 = CryptoJS.enc.Base64.stringify(hmac);
|
|
154
|
-
return signBase64;
|
|
155
|
-
}
|
|
156
123
|
function isPositiveNumber(str) {
|
|
157
124
|
return /^[1-9]\d*$/.test(str);
|
|
158
125
|
}
|
|
@@ -248,7 +215,7 @@ var ChatGPTAPI = class {
|
|
|
248
215
|
* @returns The response from ChatGPT
|
|
249
216
|
*/
|
|
250
217
|
async sendMessage(text, opts = {}, pluginParams) {
|
|
251
|
-
var _a, _b, _c, _d, _e, _f, _g;
|
|
218
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
252
219
|
let parentMsg;
|
|
253
220
|
if (pluginParams) {
|
|
254
221
|
parentMsg = await this._getMessageById(pluginParams.assistant_res.id);
|
|
@@ -326,6 +293,13 @@ var ChatGPTAPI = class {
|
|
|
326
293
|
if (this._manufacturer.toLowerCase() === "azure") {
|
|
327
294
|
r = "function";
|
|
328
295
|
}
|
|
296
|
+
let pluginContent = pluginParams.plugin_res;
|
|
297
|
+
if (Array.isArray(pluginContent) && pluginContent.length > 0) {
|
|
298
|
+
pluginContent = ((_h = pluginContent[0]) == null ? void 0 : _h.result) || pluginContent[0] || pluginContent;
|
|
299
|
+
}
|
|
300
|
+
if (typeof pluginContent !== "string") {
|
|
301
|
+
pluginContent = JSON.stringify(pluginContent);
|
|
302
|
+
}
|
|
329
303
|
last_plugin_res = {
|
|
330
304
|
role: r,
|
|
331
305
|
id: messageId,
|
|
@@ -333,7 +307,8 @@ var ChatGPTAPI = class {
|
|
|
333
307
|
conversationId,
|
|
334
308
|
parentMessageId: pluginParams.assistant_res.id,
|
|
335
309
|
text,
|
|
336
|
-
content
|
|
310
|
+
// ChatMessage.content 类型是 object,这里显式转为 any 以兼容字符串内容
|
|
311
|
+
content: pluginContent,
|
|
337
312
|
tool_call_id: toolCalls[0].id
|
|
338
313
|
};
|
|
339
314
|
await this._upsertMessage(last_assistant_res);
|
|
@@ -367,7 +342,7 @@ var ChatGPTAPI = class {
|
|
|
367
342
|
};
|
|
368
343
|
const responseP = new Promise(
|
|
369
344
|
async (resolve, reject) => {
|
|
370
|
-
var _a2, _b2, _c2, _d2, _e2, _f2, _g2
|
|
345
|
+
var _a2, _b2, _c2, _d2, _e2, _f2, _g2;
|
|
371
346
|
let url = `${this._apiBaseUrl}/chat/completions`;
|
|
372
347
|
const headers = {
|
|
373
348
|
"Content-Type": "application/json",
|
|
@@ -408,74 +383,7 @@ var ChatGPTAPI = class {
|
|
|
408
383
|
delete body.max_tokens;
|
|
409
384
|
}
|
|
410
385
|
if (typeof body.plugins !== void 0) delete body.plugins;
|
|
411
|
-
if (
|
|
412
|
-
if (body.tools && Array.isArray(body.tools) && body.tools.length > 0) {
|
|
413
|
-
if (this._manufacturer.toLowerCase() === "azure") {
|
|
414
|
-
body.functions = body.tools.map((tool) => {
|
|
415
|
-
if (tool.type === "function" && tool.function) {
|
|
416
|
-
return tool.function;
|
|
417
|
-
}
|
|
418
|
-
return tool;
|
|
419
|
-
});
|
|
420
|
-
delete body.tools;
|
|
421
|
-
if (body.tool_choice) {
|
|
422
|
-
if (typeof body.tool_choice === "string") {
|
|
423
|
-
body.function_call = body.tool_choice === "none" ? "none" : "auto";
|
|
424
|
-
} else if (body.tool_choice && typeof body.tool_choice === "object" && body.tool_choice.function) {
|
|
425
|
-
body.function_call = { name: body.tool_choice.function.name };
|
|
426
|
-
}
|
|
427
|
-
delete body.tool_choice;
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
if (["openai", "azure"].indexOf(this._manufacturer.toLowerCase()) === -1) {
|
|
433
|
-
if (body.tools) delete body.tools;
|
|
434
|
-
if (body.tool_choice) delete body.tool_choice;
|
|
435
|
-
if (body.functions) delete body.functions;
|
|
436
|
-
if (body.function_call) delete body.function_call;
|
|
437
|
-
if (body.parallel_tool_calls) delete body.parallel_tool_calls;
|
|
438
|
-
}
|
|
439
|
-
if (this._manufacturer.toLowerCase() === "aliyun") {
|
|
440
|
-
body = Object.assign(body, {
|
|
441
|
-
parameters: { result_format: "message" },
|
|
442
|
-
input: { messages }
|
|
443
|
-
});
|
|
444
|
-
delete body.messages;
|
|
445
|
-
} else if (this._manufacturer.toLowerCase() === "zhipu") {
|
|
446
|
-
if (completionParams.model === "glm-4") {
|
|
447
|
-
} else {
|
|
448
|
-
delete body.messages;
|
|
449
|
-
body = Object.assign(body, { prompt: messages });
|
|
450
|
-
}
|
|
451
|
-
} else if (this._manufacturer.toLowerCase() === "tencent") {
|
|
452
|
-
url = this._apiBaseUrl;
|
|
453
|
-
const timestamp = Math.ceil((/* @__PURE__ */ new Date()).getTime() / 1e3) + 1;
|
|
454
|
-
const keyList = this._apiKey.split(".");
|
|
455
|
-
body = Object.assign(body, {
|
|
456
|
-
app_id: parseInt(keyList[0]),
|
|
457
|
-
secret_id: keyList[1],
|
|
458
|
-
timestamp,
|
|
459
|
-
expired: timestamp + 24 * 60 * 60,
|
|
460
|
-
stream: stream ? 1 : 0
|
|
461
|
-
});
|
|
462
|
-
delete body.model;
|
|
463
|
-
delete body.max_tokens;
|
|
464
|
-
headers["Authorization"] = signTencentHunyuan(body, url, keyList);
|
|
465
|
-
} else if (this._manufacturer.toLowerCase() === "chatdoc") {
|
|
466
|
-
let query = messages.splice(messages.length - 1, 1);
|
|
467
|
-
body = Object.assign(body, {
|
|
468
|
-
upload_id: (_a2 = completionParams == null ? void 0 : completionParams.extParams) == null ? void 0 : _a2.upload_id,
|
|
469
|
-
question: (_b2 = query[0]) == null ? void 0 : _b2.content,
|
|
470
|
-
history: messages
|
|
471
|
-
});
|
|
472
|
-
if (body.extParams) delete body.extParams;
|
|
473
|
-
if (body.model) delete body.model;
|
|
474
|
-
if (body.max_tokens) delete body.max_tokens;
|
|
475
|
-
if (body.temperature) delete body.temperature;
|
|
476
|
-
if (body.messages) delete body.messages;
|
|
477
|
-
}
|
|
478
|
-
if (this._apiOrg && this._manufacturer.toLowerCase() === "openai") {
|
|
386
|
+
if (this._apiOrg) {
|
|
479
387
|
headers["OpenAI-Organization"] = this._apiOrg;
|
|
480
388
|
}
|
|
481
389
|
if (this._debug) {
|
|
@@ -488,6 +396,32 @@ var ChatGPTAPI = class {
|
|
|
488
396
|
typeof messages
|
|
489
397
|
);
|
|
490
398
|
}
|
|
399
|
+
if (pluginParams) {
|
|
400
|
+
const debugLog = {
|
|
401
|
+
location: "chatgpt-api.ts:430",
|
|
402
|
+
message: "Second sendMessage with pluginParams",
|
|
403
|
+
data: {
|
|
404
|
+
manufacturer: this._manufacturer,
|
|
405
|
+
messagesLength: messages.length,
|
|
406
|
+
messages: JSON.stringify(messages).substring(0, 2e3),
|
|
407
|
+
hasPluginRes: !!(pluginParams == null ? void 0 : pluginParams.plugin_res),
|
|
408
|
+
pluginResPreview: JSON.stringify(pluginParams == null ? void 0 : pluginParams.plugin_res).substring(0, 500),
|
|
409
|
+
bodyMessagesLength: (_a2 = body.messages) == null ? void 0 : _a2.length,
|
|
410
|
+
bodyMessages: JSON.stringify(body.messages).substring(0, 2e3),
|
|
411
|
+
stream,
|
|
412
|
+
url
|
|
413
|
+
},
|
|
414
|
+
timestamp: Date.now(),
|
|
415
|
+
sessionId: "debug-session",
|
|
416
|
+
runId: "run19",
|
|
417
|
+
hypothesisId: "T"
|
|
418
|
+
};
|
|
419
|
+
try {
|
|
420
|
+
const fs = __require("fs");
|
|
421
|
+
fs.appendFileSync("/Volumes/softdev2/gpt-project/LeiAI-Web-H5/.cursor/debug.log", JSON.stringify(debugLog) + "\n");
|
|
422
|
+
} catch (e) {
|
|
423
|
+
}
|
|
424
|
+
}
|
|
491
425
|
if (this._manufacturer.toLowerCase() === "xunfei") {
|
|
492
426
|
const self = this;
|
|
493
427
|
const keyList = this._apiKey.split(".");
|
|
@@ -539,8 +473,30 @@ var ChatGPTAPI = class {
|
|
|
539
473
|
body: JSON.stringify(body),
|
|
540
474
|
signal: abortSignal,
|
|
541
475
|
onMessage: (data) => {
|
|
542
|
-
var _a3, _b3, _c3, _d3, _e3, _f3, _g3, _h2, _i
|
|
476
|
+
var _a3, _b3, _c3, _d3, _e3, _f3, _g3, _h2, _i;
|
|
543
477
|
if (data === "[DONE]") {
|
|
478
|
+
if (pluginParams) {
|
|
479
|
+
const debugLog = {
|
|
480
|
+
location: "chatgpt-api.ts:520",
|
|
481
|
+
message: "Second sendMessage stream DONE",
|
|
482
|
+
data: {
|
|
483
|
+
manufacturer: this._manufacturer,
|
|
484
|
+
finalTextLength: result.text.length,
|
|
485
|
+
finalTextPreview: result.text.substring(0, 500),
|
|
486
|
+
hasDetail: !!result.detail,
|
|
487
|
+
resultKeys: Object.keys(result || {})
|
|
488
|
+
},
|
|
489
|
+
timestamp: Date.now(),
|
|
490
|
+
sessionId: "debug-session",
|
|
491
|
+
runId: "run19",
|
|
492
|
+
hypothesisId: "T"
|
|
493
|
+
};
|
|
494
|
+
try {
|
|
495
|
+
const fs = __require("fs");
|
|
496
|
+
fs.appendFileSync("/Volumes/softdev2/gpt-project/LeiAI-Web-H5/.cursor/debug.log", JSON.stringify(debugLog) + "\n");
|
|
497
|
+
} catch (e) {
|
|
498
|
+
}
|
|
499
|
+
}
|
|
544
500
|
result.text = result.text.trim();
|
|
545
501
|
return resolve(result);
|
|
546
502
|
}
|
|
@@ -553,134 +509,80 @@ var ChatGPTAPI = class {
|
|
|
553
509
|
response == null ? void 0 : response.choices
|
|
554
510
|
);
|
|
555
511
|
}
|
|
556
|
-
if (
|
|
557
|
-
if ((response == null ? void 0 : response.is_end) === true) {
|
|
558
|
-
result.text += response.result.trim();
|
|
559
|
-
result.detail = response;
|
|
560
|
-
return resolve(result);
|
|
561
|
-
}
|
|
562
|
-
} else if (this._manufacturer.toLowerCase() === "azure") {
|
|
563
|
-
if (((_a3 = response.choices[0]) == null ? void 0 : _a3.finish_reason) === "stop") {
|
|
564
|
-
result.text = result.text.trim();
|
|
565
|
-
return resolve(result);
|
|
566
|
-
}
|
|
567
|
-
} else if (this._manufacturer.toLowerCase() === "aliyun") {
|
|
568
|
-
if (["stop", "length"].indexOf(
|
|
569
|
-
(_c3 = (_b3 = response == null ? void 0 : response.output) == null ? void 0 : _b3.choices[0]) == null ? void 0 : _c3.finish_reason
|
|
570
|
-
) > -1) {
|
|
571
|
-
result.text = (_f3 = (_e3 = (_d3 = response == null ? void 0 : response.output) == null ? void 0 : _d3.choices[0]) == null ? void 0 : _e3.message) == null ? void 0 : _f3.content.trim();
|
|
572
|
-
return resolve(result);
|
|
573
|
-
}
|
|
574
|
-
} else if (this._manufacturer.toLowerCase() === "zhipu") {
|
|
575
|
-
if (completionParams.model === "glm-4") {
|
|
576
|
-
const gelResponse = JSON.parse(response.data);
|
|
577
|
-
if (((_h2 = (_g3 = gelResponse == null ? void 0 : gelResponse.choices) == null ? void 0 : _g3[0]) == null ? void 0 : _h2.finish_reason) === "stop") {
|
|
578
|
-
result.text = result.text.trim();
|
|
579
|
-
return resolve(result);
|
|
580
|
-
}
|
|
581
|
-
} else {
|
|
582
|
-
if ((response == null ? void 0 : response.event) === "finish") {
|
|
583
|
-
result.text += response == null ? void 0 : response.data.trim();
|
|
584
|
-
return resolve(result);
|
|
585
|
-
}
|
|
586
|
-
}
|
|
587
|
-
} else if (this._manufacturer.toLowerCase() === "tencent") {
|
|
588
|
-
if (((_i = response.choices[0]) == null ? void 0 : _i.finish_reason) === "stop") {
|
|
589
|
-
result.text += (_k = (_j = response == null ? void 0 : response.choices[0]) == null ? void 0 : _j.delta) == null ? void 0 : _k.content.trim();
|
|
590
|
-
return resolve(result);
|
|
591
|
-
}
|
|
592
|
-
} else if (this._manufacturer.toLowerCase() === "chatdoc") {
|
|
593
|
-
result.id = `chatdoc-${Math.floor(
|
|
594
|
-
Math.random() * 1e7
|
|
595
|
-
)}${(/* @__PURE__ */ new Date()).getTime()}`;
|
|
596
|
-
if (response == null ? void 0 : response.source_info) {
|
|
597
|
-
result.text += response == null ? void 0 : response.answer;
|
|
598
|
-
return resolve(result);
|
|
599
|
-
}
|
|
600
|
-
}
|
|
601
|
-
if (this._manufacturer.toLowerCase() === "aliyun") {
|
|
602
|
-
if (response == null ? void 0 : response.request_id) {
|
|
603
|
-
result.id = response.request_id;
|
|
604
|
-
}
|
|
605
|
-
} else {
|
|
606
|
-
if (response == null ? void 0 : response.id) {
|
|
607
|
-
result.id = response.id;
|
|
608
|
-
}
|
|
609
|
-
}
|
|
610
|
-
if (((_l = response.choices) == null ? void 0 : _l.length) && ["openai", "azure", "tencent", "anthropic"].indexOf(
|
|
611
|
-
this._manufacturer.toLowerCase()
|
|
612
|
-
) > -1) {
|
|
512
|
+
if (((_a3 = response.choices) == null ? void 0 : _a3.length) && ((_b3 = response.choices[0]) == null ? void 0 : _b3.delta) !== void 0) {
|
|
613
513
|
const delta = response.choices[0].delta;
|
|
514
|
+
const finishReason = response.choices[0].finish_reason;
|
|
614
515
|
result.delta = "";
|
|
615
|
-
if (
|
|
516
|
+
if (finishReason === "tool_calls") {
|
|
616
517
|
result.delta = text;
|
|
617
518
|
} else if (delta.content) {
|
|
618
519
|
result.delta = delta.content;
|
|
619
520
|
}
|
|
620
|
-
if (delta == null ? void 0 : delta.content)
|
|
521
|
+
if (delta == null ? void 0 : delta.content) {
|
|
522
|
+
result.text += result.delta;
|
|
523
|
+
}
|
|
621
524
|
result.role = "assistant";
|
|
622
|
-
if (
|
|
623
|
-
} else if (delta.role) {
|
|
525
|
+
if (finishReason !== "tool_calls" && delta.role) {
|
|
624
526
|
result.role = delta.role;
|
|
625
527
|
}
|
|
626
528
|
result.detail = response;
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
if (
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
result.delta = data2;
|
|
654
|
-
if (data2) {
|
|
655
|
-
result.text += data2;
|
|
656
|
-
}
|
|
657
|
-
result.role = "assistant";
|
|
658
|
-
result.detail = response;
|
|
659
|
-
onProgress == null ? void 0 : onProgress(result);
|
|
660
|
-
} else {
|
|
661
|
-
if (response.event === "finish") {
|
|
662
|
-
if ((_A = response == null ? void 0 : response.meta) == null ? void 0 : _A.usage) {
|
|
663
|
-
response.usage = (_B = response == null ? void 0 : response.meta) == null ? void 0 : _B.usage;
|
|
664
|
-
} else {
|
|
665
|
-
response.usage = {
|
|
666
|
-
prompt_tokens: 1,
|
|
667
|
-
completion_tokens: 1,
|
|
668
|
-
total_tokens: 2
|
|
669
|
-
};
|
|
670
|
-
}
|
|
529
|
+
if (response == null ? void 0 : response.id) {
|
|
530
|
+
result.id = response.id;
|
|
531
|
+
}
|
|
532
|
+
if (pluginParams) {
|
|
533
|
+
const debugLog = {
|
|
534
|
+
location: "chatgpt-api.ts:570",
|
|
535
|
+
message: "Second sendMessage onMessage (streaming)",
|
|
536
|
+
data: {
|
|
537
|
+
manufacturer: this._manufacturer,
|
|
538
|
+
hasDelta: !!delta,
|
|
539
|
+
deltaContent: delta == null ? void 0 : delta.content,
|
|
540
|
+
deltaContentLength: (_c3 = delta == null ? void 0 : delta.content) == null ? void 0 : _c3.length,
|
|
541
|
+
finishReason,
|
|
542
|
+
resultTextLength: result.text.length,
|
|
543
|
+
resultTextPreview: result.text.substring(0, 200),
|
|
544
|
+
responseChoicesLength: (_d3 = response.choices) == null ? void 0 : _d3.length
|
|
545
|
+
},
|
|
546
|
+
timestamp: Date.now(),
|
|
547
|
+
sessionId: "debug-session",
|
|
548
|
+
runId: "run19",
|
|
549
|
+
hypothesisId: "T"
|
|
550
|
+
};
|
|
551
|
+
try {
|
|
552
|
+
const fs = __require("fs");
|
|
553
|
+
fs.appendFileSync("/Volumes/softdev2/gpt-project/LeiAI-Web-H5/.cursor/debug.log", JSON.stringify(debugLog) + "\n");
|
|
554
|
+
} catch (e) {
|
|
671
555
|
}
|
|
672
|
-
result.delta = response.data;
|
|
673
|
-
if (response == null ? void 0 : response.data) result.text += response == null ? void 0 : response.data;
|
|
674
|
-
result.role = "assistant";
|
|
675
|
-
result.detail = response;
|
|
676
|
-
onProgress == null ? void 0 : onProgress(result);
|
|
677
556
|
}
|
|
678
|
-
} else if ((response == null ? void 0 : response.answer) && this._manufacturer.toLowerCase() === "chatdoc") {
|
|
679
|
-
result.delta = response.answer;
|
|
680
|
-
if (response == null ? void 0 : response.answer) result.text += response.answer;
|
|
681
|
-
result.role = "assistant";
|
|
682
|
-
result.detail = response;
|
|
683
557
|
onProgress == null ? void 0 : onProgress(result);
|
|
558
|
+
if (finishReason === "stop" || finishReason === "length") {
|
|
559
|
+
result.text = result.text.trim();
|
|
560
|
+
return resolve(result);
|
|
561
|
+
}
|
|
562
|
+
} else {
|
|
563
|
+
if (pluginParams) {
|
|
564
|
+
const debugLog = {
|
|
565
|
+
location: "chatgpt-api.ts:605",
|
|
566
|
+
message: "Second sendMessage onMessage (unexpected format)",
|
|
567
|
+
data: {
|
|
568
|
+
manufacturer: this._manufacturer,
|
|
569
|
+
responseStructure: JSON.stringify(response).substring(0, 1e3),
|
|
570
|
+
hasChoices: !!response.choices,
|
|
571
|
+
choicesLength: (_e3 = response.choices) == null ? void 0 : _e3.length,
|
|
572
|
+
firstChoiceDelta: (_g3 = (_f3 = response.choices) == null ? void 0 : _f3[0]) == null ? void 0 : _g3.delta,
|
|
573
|
+
firstChoiceFinishReason: (_i = (_h2 = response.choices) == null ? void 0 : _h2[0]) == null ? void 0 : _i.finish_reason
|
|
574
|
+
},
|
|
575
|
+
timestamp: Date.now(),
|
|
576
|
+
sessionId: "debug-session",
|
|
577
|
+
runId: "run19",
|
|
578
|
+
hypothesisId: "T"
|
|
579
|
+
};
|
|
580
|
+
try {
|
|
581
|
+
const fs = __require("fs");
|
|
582
|
+
fs.appendFileSync("/Volumes/softdev2/gpt-project/LeiAI-Web-H5/.cursor/debug.log", JSON.stringify(debugLog) + "\n");
|
|
583
|
+
} catch (e) {
|
|
584
|
+
}
|
|
585
|
+
}
|
|
684
586
|
}
|
|
685
587
|
} catch (err) {
|
|
686
588
|
console.warn(
|
|
@@ -717,35 +619,21 @@ var ChatGPTAPI = class {
|
|
|
717
619
|
console.log(
|
|
718
620
|
`row data ${typeof response} : `,
|
|
719
621
|
response,
|
|
720
|
-
(response == null ? void 0 : response.choices) && ((
|
|
622
|
+
(response == null ? void 0 : response.choices) && ((_b2 = response == null ? void 0 : response.choices[0]) == null ? void 0 : _b2.message)
|
|
721
623
|
);
|
|
722
624
|
}
|
|
723
|
-
if (
|
|
724
|
-
|
|
725
|
-
result.id = response.request_id;
|
|
726
|
-
}
|
|
727
|
-
} else {
|
|
728
|
-
if (response == null ? void 0 : response.id) {
|
|
729
|
-
result.id = response.id;
|
|
730
|
-
}
|
|
625
|
+
if (response == null ? void 0 : response.id) {
|
|
626
|
+
result.id = response.id;
|
|
731
627
|
}
|
|
732
|
-
if (((
|
|
628
|
+
if (((_c2 = response == null ? void 0 : response.choices) == null ? void 0 : _c2.length) && ((_d2 = response.choices[0]) == null ? void 0 : _d2.message)) {
|
|
733
629
|
const message2 = response.choices[0].message;
|
|
734
|
-
result.text = message2.content;
|
|
735
|
-
|
|
736
|
-
result.role = message2.role;
|
|
737
|
-
}
|
|
738
|
-
} else if ((response == null ? void 0 : response.result) && this._manufacturer.toLowerCase() === "baidu") {
|
|
739
|
-
result.text = response.result;
|
|
740
|
-
result.role = "assistant";
|
|
741
|
-
} else if (((_e2 = response == null ? void 0 : response.output) == null ? void 0 : _e2.text) && this._manufacturer.toLowerCase() === "aliyun") {
|
|
742
|
-
result.text = (_f2 = response == null ? void 0 : response.output) == null ? void 0 : _f2.text;
|
|
743
|
-
result.role = "assistant";
|
|
630
|
+
result.text = message2.content || "";
|
|
631
|
+
result.role = message2.role || "assistant";
|
|
744
632
|
} else {
|
|
745
633
|
const res2 = response;
|
|
746
634
|
return reject(
|
|
747
635
|
new Error(
|
|
748
|
-
|
|
636
|
+
`API error: ${((_e2 = res2 == null ? void 0 : res2.detail) == null ? void 0 : _e2.message) || ((_f2 = res2 == null ? void 0 : res2.detail) == null ? void 0 : _f2.error_msg) || ((_g2 = res2 == null ? void 0 : res2.error) == null ? void 0 : _g2.message) || (res2 == null ? void 0 : res2.detail) || "unknown"}`
|
|
749
637
|
)
|
|
750
638
|
);
|
|
751
639
|
}
|
|
@@ -859,7 +747,7 @@ var ChatGPTAPI = class {
|
|
|
859
747
|
this._apiOrg = apiOrg;
|
|
860
748
|
}
|
|
861
749
|
async _buildMessages(text, opts, pluginData) {
|
|
862
|
-
var _a
|
|
750
|
+
var _a;
|
|
863
751
|
const { systemMessage = this._systemMessage, completionParams } = opts;
|
|
864
752
|
let { parentMessageId, contextRestriction } = opts;
|
|
865
753
|
let errorMessage = "";
|
|
@@ -891,59 +779,55 @@ var ChatGPTAPI = class {
|
|
|
891
779
|
}
|
|
892
780
|
const systemMessageOffset = messages.length;
|
|
893
781
|
let userMessage = null;
|
|
894
|
-
if (
|
|
895
|
-
|
|
896
|
-
"
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
"tencent",
|
|
900
|
-
"chatdoc",
|
|
901
|
-
"anthropic"
|
|
902
|
-
].indexOf(this._manufacturer.toLowerCase()) > -1) {
|
|
903
|
-
userMessage = [{ role: "user", content: text }];
|
|
904
|
-
} else if (pluginData) {
|
|
782
|
+
if (pluginData) {
|
|
783
|
+
let pluginContent = pluginData.content;
|
|
784
|
+
if (typeof pluginContent !== "string") {
|
|
785
|
+
pluginContent = JSON.stringify(pluginContent);
|
|
786
|
+
}
|
|
905
787
|
userMessage = {
|
|
906
|
-
role: pluginData.role,
|
|
788
|
+
role: pluginData.role || "tool",
|
|
907
789
|
name: pluginData.name,
|
|
908
|
-
content:
|
|
790
|
+
content: pluginContent,
|
|
909
791
|
tool_call_id: pluginData.tool_call_id
|
|
910
792
|
};
|
|
911
|
-
if (["azure"].indexOf(this._manufacturer.toLowerCase()) > -1) {
|
|
912
|
-
delete userMessage.tool_call_id;
|
|
913
|
-
}
|
|
914
793
|
} else {
|
|
915
794
|
userMessage = [{ role: "user", content: text, name: opts.name }];
|
|
916
795
|
}
|
|
917
796
|
let nextMessages = messages;
|
|
918
797
|
if (pluginData || text) {
|
|
919
|
-
|
|
798
|
+
if (Array.isArray(userMessage)) {
|
|
799
|
+
nextMessages = nextMessages.concat(userMessage);
|
|
800
|
+
} else {
|
|
801
|
+
nextMessages = nextMessages.concat([userMessage]);
|
|
802
|
+
}
|
|
920
803
|
}
|
|
921
804
|
let numTokens = 0;
|
|
922
805
|
do {
|
|
923
806
|
const prompt = nextMessages.reduce((prompt2, message) => {
|
|
807
|
+
const content = typeof message === "object" && message.content !== void 0 ? typeof message.content === "string" ? message.content : JSON.stringify(message.content) : typeof message === "string" ? message : JSON.stringify(message);
|
|
924
808
|
switch (message.role) {
|
|
925
809
|
case "tool":
|
|
926
810
|
return prompt2.concat([`Tool:
|
|
927
811
|
function call`]);
|
|
928
812
|
case "system":
|
|
929
813
|
return prompt2.concat([`Instructions:
|
|
930
|
-
${
|
|
814
|
+
${content}`]);
|
|
931
815
|
case "user":
|
|
932
816
|
return prompt2.concat([`${userLabel}:
|
|
933
|
-
${
|
|
817
|
+
${content}`]);
|
|
934
818
|
default:
|
|
935
819
|
return prompt2.concat([`${assistantLabel}:
|
|
936
|
-
${
|
|
820
|
+
${content}`]);
|
|
937
821
|
}
|
|
938
822
|
}, []).join("\n\n");
|
|
939
823
|
const nextNumTokensEstimate = await this._getTokenCount(prompt);
|
|
940
824
|
const isValidPrompt = nextNumTokensEstimate <= maxNumTokens;
|
|
941
|
-
if (prompt && !isValidPrompt
|
|
825
|
+
if (prompt && !isValidPrompt) {
|
|
942
826
|
break;
|
|
943
827
|
}
|
|
944
828
|
messages = nextMessages;
|
|
945
829
|
numTokens = nextNumTokensEstimate;
|
|
946
|
-
if (!isValidPrompt
|
|
830
|
+
if (!isValidPrompt) {
|
|
947
831
|
break;
|
|
948
832
|
}
|
|
949
833
|
if (!parentMessageId) {
|
|
@@ -955,29 +839,8 @@ ${message.content}`]);
|
|
|
955
839
|
}
|
|
956
840
|
const parentMessageRole = parentMessage.role || "user";
|
|
957
841
|
let parentMessageItem = null;
|
|
958
|
-
if (
|
|
959
|
-
|
|
960
|
-
"zhipu",
|
|
961
|
-
"xunfei",
|
|
962
|
-
"aliyun",
|
|
963
|
-
"tencent",
|
|
964
|
-
"chatdoc",
|
|
965
|
-
"anthropic"
|
|
966
|
-
].indexOf(this._manufacturer.toLowerCase()) > -1) {
|
|
967
|
-
parentMessageItem = {
|
|
968
|
-
role: parentMessageRole,
|
|
969
|
-
content: parentMessage.text
|
|
970
|
-
};
|
|
971
|
-
} else if (parentMessage.content && ((_a = parentMessage.content) == null ? void 0 : _a.finish_reason) === "tool_calls") {
|
|
972
|
-
if (["azure"].indexOf(this._manufacturer.toLowerCase()) > -1) {
|
|
973
|
-
parentMessageItem = Object.assign(parentMessage.content.message, {
|
|
974
|
-
function_call: (_b = parentMessage.content.message.tool_calls[0]) == null ? void 0 : _b.function,
|
|
975
|
-
content: parentMessage.text || parentMessage.content
|
|
976
|
-
});
|
|
977
|
-
delete parentMessage.content.message.tool_calls;
|
|
978
|
-
} else {
|
|
979
|
-
parentMessageItem = parentMessage.content.message;
|
|
980
|
-
}
|
|
842
|
+
if (parentMessage.content && ((_a = parentMessage.content) == null ? void 0 : _a.finish_reason) === "tool_calls") {
|
|
843
|
+
parentMessageItem = parentMessage.content.message;
|
|
981
844
|
} else {
|
|
982
845
|
parentMessageItem = {
|
|
983
846
|
role: parentMessageRole,
|
|
@@ -993,14 +856,13 @@ ${message.content}`]);
|
|
|
993
856
|
} while (true);
|
|
994
857
|
let maxTokens = this._maxModelTokens;
|
|
995
858
|
if (isPositiveNumber(maxTokens)) {
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
} else if (numTokens > this._maxModelTokens) {
|
|
859
|
+
maxTokens = Math.max(
|
|
860
|
+
1,
|
|
861
|
+
Math.min(this._maxModelTokens - numTokens, this._maxResponseTokens)
|
|
862
|
+
);
|
|
863
|
+
if (numTokens > this._maxModelTokens) {
|
|
1002
864
|
maxTokens = this._maxModelTokens;
|
|
1003
|
-
errorMessage =
|
|
865
|
+
errorMessage = `\u5F53\u524D\u63D0\u95EE\u4E0A\u4E0B\u6587\u5185\u5BB9\u957F\u5EA6${numTokens}tokens\u8D85\u957F\uFF0C\u8BE5\u6A21\u578B\u6700\u5927\u63D0\u95EE\u957F\u5EA6\u4E3A${this._maxModelTokens}tokens\uFF0C\u8BF7\u5207\u6362\u5176\u4ED6\u589E\u5F3AAI\u6A21\u578B\u6216\u51CF\u5C11\u5B57\u6570\u6216\u8005\u5173\u95ED\u4E0A\u4E0B\u6587\u5386\u53F2\u63D0\u9AD8\u5355\u6B21\u63D0\u95EE\u957F\u5EA6\uFF01`;
|
|
1004
866
|
}
|
|
1005
867
|
}
|
|
1006
868
|
return { messages, maxTokens, numTokens, errorMessage, objText };
|