@leikeduntech/leiai-js 3.1.0 → 3.3.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/build/index.js +72 -33
- package/package.json +1 -1
package/build/index.js
CHANGED
|
@@ -308,8 +308,12 @@ Current date: ${currentDate}`;
|
|
|
308
308
|
text,
|
|
309
309
|
content: last_assistant_res_message
|
|
310
310
|
};
|
|
311
|
+
let r = "tool";
|
|
312
|
+
if (this._manufacturer.toLowerCase() === "azure") {
|
|
313
|
+
r = "function";
|
|
314
|
+
}
|
|
311
315
|
last_plugin_res = {
|
|
312
|
-
role:
|
|
316
|
+
role: r,
|
|
313
317
|
id: messageId,
|
|
314
318
|
name: last_assistant_res_message.message.tool_calls[0].function.name,
|
|
315
319
|
conversationId,
|
|
@@ -351,7 +355,7 @@ Current date: ${currentDate}`;
|
|
|
351
355
|
};
|
|
352
356
|
const responseP = new Promise(
|
|
353
357
|
async (resolve, reject) => {
|
|
354
|
-
var _a2, _b, _c, _d, _e;
|
|
358
|
+
var _a2, _b, _c, _d, _e, _f;
|
|
355
359
|
let url = `${this._apiBaseUrl}/chat/completions`;
|
|
356
360
|
const headers = {
|
|
357
361
|
"Content-Type": "application/json",
|
|
@@ -392,10 +396,13 @@ Current date: ${currentDate}`;
|
|
|
392
396
|
messages,
|
|
393
397
|
stream
|
|
394
398
|
};
|
|
395
|
-
if (["openai"].indexOf(this._manufacturer.toLowerCase()) > -1) {
|
|
399
|
+
if (["openai", "azure"].indexOf(this._manufacturer.toLowerCase()) > -1) {
|
|
396
400
|
switch (pluginList) {
|
|
397
401
|
case "dall-e-3":
|
|
398
|
-
|
|
402
|
+
let tools_key = "tools", tool_choice_key = "tool_choice", funArr = null;
|
|
403
|
+
if (this._manufacturer.toLowerCase() === "azure")
|
|
404
|
+
tools_key = "functions", tool_choice_key = "function_call";
|
|
405
|
+
funArr = [{
|
|
399
406
|
type: "function",
|
|
400
407
|
function: {
|
|
401
408
|
"name": "DallE2Fun",
|
|
@@ -431,7 +438,13 @@ Current date: ${currentDate}`;
|
|
|
431
438
|
"required": ["prompt"]
|
|
432
439
|
}
|
|
433
440
|
}
|
|
434
|
-
}]
|
|
441
|
+
}];
|
|
442
|
+
if (this._manufacturer.toLowerCase() === "azure") {
|
|
443
|
+
funArr = funArr.map((item) => {
|
|
444
|
+
return item.function;
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
body = Object.assign(body, { [tools_key]: funArr, [tool_choice_key]: "auto" });
|
|
435
448
|
break;
|
|
436
449
|
default:
|
|
437
450
|
break;
|
|
@@ -507,7 +520,7 @@ Current date: ${currentDate}`;
|
|
|
507
520
|
body: JSON.stringify(body),
|
|
508
521
|
signal: abortSignal,
|
|
509
522
|
onMessage: (data) => {
|
|
510
|
-
var _a3, _b2, _c2, _d2, _e2,
|
|
523
|
+
var _a3, _b2, _c2, _d2, _e2, _f2, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v;
|
|
511
524
|
if (data === "[DONE]") {
|
|
512
525
|
result.text = result.text.trim();
|
|
513
526
|
return resolve(result);
|
|
@@ -527,7 +540,7 @@ Current date: ${currentDate}`;
|
|
|
527
540
|
}
|
|
528
541
|
} else if (this._manufacturer.toLowerCase() === "aliyun") {
|
|
529
542
|
if (["stop", "length"].indexOf((_c2 = (_b2 = response == null ? void 0 : response.output) == null ? void 0 : _b2.choices[0]) == null ? void 0 : _c2.finish_reason) > -1) {
|
|
530
|
-
result.text = (
|
|
543
|
+
result.text = (_f2 = (_e2 = (_d2 = response == null ? void 0 : response.output) == null ? void 0 : _d2.choices[0]) == null ? void 0 : _e2.message) == null ? void 0 : _f2.content.trim();
|
|
531
544
|
return resolve(result);
|
|
532
545
|
}
|
|
533
546
|
} else if (this._manufacturer.toLowerCase() === "zhipu") {
|
|
@@ -628,7 +641,7 @@ Current date: ${currentDate}`;
|
|
|
628
641
|
}
|
|
629
642
|
const response = await res.json();
|
|
630
643
|
if (this._debug) {
|
|
631
|
-
console.log(`row data ${typeof response} : `, response, response == null ? void 0 : response.choices);
|
|
644
|
+
console.log(`row data ${typeof response} : `, response, (_a2 = response == null ? void 0 : response.choices[0]) == null ? void 0 : _a2.message);
|
|
632
645
|
}
|
|
633
646
|
if (this._manufacturer.toLowerCase() === "aliyun") {
|
|
634
647
|
if (response == null ? void 0 : response.request_id) {
|
|
@@ -639,7 +652,7 @@ Current date: ${currentDate}`;
|
|
|
639
652
|
result.id = response.id;
|
|
640
653
|
}
|
|
641
654
|
}
|
|
642
|
-
if (((
|
|
655
|
+
if (((_b = response == null ? void 0 : response.choices) == null ? void 0 : _b.length) && ["openai", "azure"].indexOf(this._manufacturer.toLowerCase()) > -1) {
|
|
643
656
|
const message2 = response.choices[0].message;
|
|
644
657
|
result.text = message2.content;
|
|
645
658
|
if (message2.role) {
|
|
@@ -648,14 +661,14 @@ Current date: ${currentDate}`;
|
|
|
648
661
|
} else if ((response == null ? void 0 : response.result) && this._manufacturer.toLowerCase() === "baidu") {
|
|
649
662
|
result.text = response.result;
|
|
650
663
|
result.role = "assistant";
|
|
651
|
-
} else if (((
|
|
652
|
-
result.text = (
|
|
664
|
+
} else if (((_c = response == null ? void 0 : response.output) == null ? void 0 : _c.text) && this._manufacturer.toLowerCase() === "aliyun") {
|
|
665
|
+
result.text = (_d = response == null ? void 0 : response.output) == null ? void 0 : _d.text;
|
|
653
666
|
result.role = "assistant";
|
|
654
667
|
} else {
|
|
655
668
|
const res2 = response;
|
|
656
669
|
return reject(
|
|
657
670
|
new Error(
|
|
658
|
-
`${this._manufacturer} error: ${((
|
|
671
|
+
`${this._manufacturer} error: ${((_e = res2 == null ? void 0 : res2.detail) == null ? void 0 : _e.message) || ((_f = res2 == null ? void 0 : res2.detail) == null ? void 0 : _f.error_msg) || (res2 == null ? void 0 : res2.detail) || "unknown"}`
|
|
659
672
|
)
|
|
660
673
|
);
|
|
661
674
|
}
|
|
@@ -672,25 +685,40 @@ Current date: ${currentDate}`;
|
|
|
672
685
|
}
|
|
673
686
|
}
|
|
674
687
|
).then(async (message2) => {
|
|
675
|
-
var _a2, _b;
|
|
688
|
+
var _a2, _b, _c, _d, _e;
|
|
676
689
|
if (this._debug)
|
|
677
690
|
console.log("\u8DDF\u8E2A7", JSON.stringify(message2));
|
|
678
|
-
if (message2.detail
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
691
|
+
if (message2.detail) {
|
|
692
|
+
if (!message2.detail.usage) {
|
|
693
|
+
try {
|
|
694
|
+
const promptTokens = numTokens;
|
|
695
|
+
let completionTokens = 0;
|
|
696
|
+
if (["baidu", "aliyun"].indexOf(this._manufacturer.toLowerCase()) > -1) {
|
|
697
|
+
completionTokens = (_b = (_a2 = message2.detail) == null ? void 0 : _a2.usage) == null ? void 0 : _b.total_tokens;
|
|
698
|
+
} else {
|
|
699
|
+
completionTokens = await this._getTokenCount(message2.text);
|
|
700
|
+
}
|
|
701
|
+
message2.detail.usage = {
|
|
702
|
+
prompt_tokens: promptTokens,
|
|
703
|
+
completion_tokens: completionTokens,
|
|
704
|
+
total_tokens: promptTokens + completionTokens,
|
|
705
|
+
estimated: true
|
|
706
|
+
};
|
|
707
|
+
} catch (err) {
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
if (this._manufacturer.toLowerCase() === "azure") {
|
|
711
|
+
if (((_d = (_c = message2.detail) == null ? void 0 : _c.choices[0]) == null ? void 0 : _d.finish_reason) === "function_call") {
|
|
712
|
+
message2.detail.choices[0].finish_reason = "tool_calls";
|
|
713
|
+
message2.detail.choices[0].message = {
|
|
714
|
+
role: message2.detail.choices[0].message.role,
|
|
715
|
+
tool_calls: [{
|
|
716
|
+
id: `call_${uuidv4()}`,
|
|
717
|
+
type: "function",
|
|
718
|
+
function: (_e = message2.detail.choices[0].message) == null ? void 0 : _e.function_call
|
|
719
|
+
}]
|
|
720
|
+
};
|
|
686
721
|
}
|
|
687
|
-
message2.detail.usage = {
|
|
688
|
-
prompt_tokens: promptTokens,
|
|
689
|
-
completion_tokens: completionTokens,
|
|
690
|
-
total_tokens: promptTokens + completionTokens,
|
|
691
|
-
estimated: true
|
|
692
|
-
};
|
|
693
|
-
} catch (err) {
|
|
694
722
|
}
|
|
695
723
|
}
|
|
696
724
|
const pRes = Promise.all([
|
|
@@ -757,7 +785,7 @@ Current date: ${currentDate}`;
|
|
|
757
785
|
this._apiOrg = apiOrg;
|
|
758
786
|
}
|
|
759
787
|
async _buildMessages(text, opts, pluginData) {
|
|
760
|
-
var _a;
|
|
788
|
+
var _a, _b;
|
|
761
789
|
const { systemMessage = this._systemMessage, completionParams } = opts;
|
|
762
790
|
let { parentMessageId } = opts;
|
|
763
791
|
let errorMessage = "";
|
|
@@ -781,13 +809,13 @@ Current date: ${currentDate}`;
|
|
|
781
809
|
}
|
|
782
810
|
});
|
|
783
811
|
});
|
|
784
|
-
if (["openai"].indexOf(this._manufacturer.toLowerCase()) > -1) {
|
|
812
|
+
if (["openai", "azure"].indexOf(this._manufacturer.toLowerCase()) > -1) {
|
|
785
813
|
text = objText;
|
|
786
814
|
}
|
|
787
815
|
}
|
|
788
816
|
const systemMessageOffset = messages.length;
|
|
789
817
|
let userMessage = null;
|
|
790
|
-
if (["baidu", "
|
|
818
|
+
if (["baidu", "zhipu", "xunfei", "aliyun", "tencent"].indexOf(this._manufacturer.toLowerCase()) > -1) {
|
|
791
819
|
userMessage = [{ role: "user", content: text }];
|
|
792
820
|
} else if (pluginData) {
|
|
793
821
|
userMessage = {
|
|
@@ -796,6 +824,9 @@ Current date: ${currentDate}`;
|
|
|
796
824
|
content: JSON.stringify(pluginData.content),
|
|
797
825
|
tool_call_id: pluginData.tool_call_id
|
|
798
826
|
};
|
|
827
|
+
if (["azure"].indexOf(this._manufacturer.toLowerCase()) > -1) {
|
|
828
|
+
delete userMessage.tool_call_id;
|
|
829
|
+
}
|
|
799
830
|
} else {
|
|
800
831
|
userMessage = [{ role: "user", content: text, name: opts.name }];
|
|
801
832
|
}
|
|
@@ -840,10 +871,18 @@ ${message.content}`]);
|
|
|
840
871
|
}
|
|
841
872
|
const parentMessageRole = parentMessage.role || "user";
|
|
842
873
|
let parentMessageItem = null;
|
|
843
|
-
if (["baidu", "
|
|
874
|
+
if (["baidu", "zhipu", "xunfei", "aliyun", "tencent"].indexOf(this._manufacturer.toLowerCase()) > -1) {
|
|
844
875
|
parentMessageItem = { role: parentMessageRole, content: parentMessage.text };
|
|
845
876
|
} else if (parentMessage.content && ((_a = parentMessage.content) == null ? void 0 : _a.finish_reason) === "tool_calls") {
|
|
846
|
-
|
|
877
|
+
if (["azure"].indexOf(this._manufacturer.toLowerCase()) > -1) {
|
|
878
|
+
parentMessageItem = Object.assign(parentMessage.content.message, {
|
|
879
|
+
function_call: (_b = parentMessage.content.message.tool_calls[0]) == null ? void 0 : _b.function,
|
|
880
|
+
content: parentMessage.text || parentMessage.content
|
|
881
|
+
});
|
|
882
|
+
delete parentMessage.content.message.tool_calls;
|
|
883
|
+
} else {
|
|
884
|
+
parentMessageItem = parentMessage.content.message;
|
|
885
|
+
}
|
|
847
886
|
} else {
|
|
848
887
|
parentMessageItem = {
|
|
849
888
|
role: parentMessageRole,
|