@leikeduntech/leiai-js 3.2.0 → 3.4.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.
Files changed (2) hide show
  1. package/build/index.js +74 -29
  2. 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: "tool",
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, _f;
358
+ var _a2, _b, _c, _d, _e, _f, _g;
355
359
  let url = `${this._apiBaseUrl}/chat/completions`;
356
360
  const headers = {
357
361
  "Content-Type": "application/json",
@@ -395,7 +399,10 @@ Current date: ${currentDate}`;
395
399
  if (["openai", "azure"].indexOf(this._manufacturer.toLowerCase()) > -1) {
396
400
  switch (pluginList) {
397
401
  case "dall-e-3":
398
- body = Object.assign(body, { tools: [{
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
- }], tool_choice: "auto" });
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;
@@ -450,10 +463,20 @@ Current date: ${currentDate}`;
450
463
  delete body.model;
451
464
  delete body.max_tokens;
452
465
  headers["Authorization"] = signTencentHunyuan(body, url, keyList);
466
+ } else if (this._manufacturer.toLowerCase() === "baidu") {
467
+ if (pluginList.indexOf("zhishiku") > -1) {
468
+ let query = messages.splice(messages.length - 1, 1);
469
+ body = Object.assign(body, { query: (_a2 = query[0]) == null ? void 0 : _a2.content, history: messages });
470
+ delete body.messages;
471
+ }
453
472
  }
454
473
  if (this._apiOrg && this._manufacturer.toLowerCase() === "openai") {
455
474
  headers["OpenAI-Organization"] = this._apiOrg;
456
475
  }
476
+ if (this._manufacturer.toLowerCase() !== "baidu" || pluginList.indexOf("zhishiku") < 0) {
477
+ if (typeof body.plugins !== void 0)
478
+ delete body.plugins;
479
+ }
457
480
  if (this._debug) {
458
481
  console.log(`api url ${url}`);
459
482
  console.log(`api header ${JSON.stringify(headers)}`);
@@ -507,7 +530,7 @@ Current date: ${currentDate}`;
507
530
  body: JSON.stringify(body),
508
531
  signal: abortSignal,
509
532
  onMessage: (data) => {
510
- var _a3, _b2, _c2, _d2, _e2, _f2, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v;
533
+ var _a3, _b2, _c2, _d2, _e2, _f2, _g2, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v;
511
534
  if (data === "[DONE]") {
512
535
  result.text = result.text.trim();
513
536
  return resolve(result);
@@ -536,7 +559,7 @@ Current date: ${currentDate}`;
536
559
  return resolve(result);
537
560
  }
538
561
  } else if (this._manufacturer.toLowerCase() === "tencent") {
539
- if (((_g = response.choices[0]) == null ? void 0 : _g.finish_reason) === "stop") {
562
+ if (((_g2 = response.choices[0]) == null ? void 0 : _g2.finish_reason) === "stop") {
540
563
  result.text += (_i = (_h = response == null ? void 0 : response.choices[0]) == null ? void 0 : _h.delta) == null ? void 0 : _i.content.trim();
541
564
  return resolve(result);
542
565
  }
@@ -628,7 +651,7 @@ Current date: ${currentDate}`;
628
651
  }
629
652
  const response = await res.json();
630
653
  if (this._debug) {
631
- console.log(`row data ${typeof response} : `, response, (_a2 = response == null ? void 0 : response.choices[0]) == null ? void 0 : _a2.message);
654
+ console.log(`row data ${typeof response} : `, response, (_b = response == null ? void 0 : response.choices[0]) == null ? void 0 : _b.message);
632
655
  }
633
656
  if (this._manufacturer.toLowerCase() === "aliyun") {
634
657
  if (response == null ? void 0 : response.request_id) {
@@ -639,7 +662,7 @@ Current date: ${currentDate}`;
639
662
  result.id = response.id;
640
663
  }
641
664
  }
642
- if (((_b = response == null ? void 0 : response.choices) == null ? void 0 : _b.length) && ["openai", "azure"].indexOf(this._manufacturer.toLowerCase()) > -1) {
665
+ if (((_c = response == null ? void 0 : response.choices) == null ? void 0 : _c.length) && ["openai", "azure"].indexOf(this._manufacturer.toLowerCase()) > -1) {
643
666
  const message2 = response.choices[0].message;
644
667
  result.text = message2.content;
645
668
  if (message2.role) {
@@ -648,14 +671,14 @@ Current date: ${currentDate}`;
648
671
  } else if ((response == null ? void 0 : response.result) && this._manufacturer.toLowerCase() === "baidu") {
649
672
  result.text = response.result;
650
673
  result.role = "assistant";
651
- } else if (((_c = response == null ? void 0 : response.output) == null ? void 0 : _c.text) && this._manufacturer.toLowerCase() === "aliyun") {
652
- result.text = (_d = response == null ? void 0 : response.output) == null ? void 0 : _d.text;
674
+ } else if (((_d = response == null ? void 0 : response.output) == null ? void 0 : _d.text) && this._manufacturer.toLowerCase() === "aliyun") {
675
+ result.text = (_e = response == null ? void 0 : response.output) == null ? void 0 : _e.text;
653
676
  result.role = "assistant";
654
677
  } else {
655
678
  const res2 = response;
656
679
  return reject(
657
680
  new Error(
658
- `${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"}`
681
+ `${this._manufacturer} error: ${((_f = res2 == null ? void 0 : res2.detail) == null ? void 0 : _f.message) || ((_g = res2 == null ? void 0 : res2.detail) == null ? void 0 : _g.error_msg) || (res2 == null ? void 0 : res2.detail) || "unknown"}`
659
682
  )
660
683
  );
661
684
  }
@@ -672,25 +695,40 @@ Current date: ${currentDate}`;
672
695
  }
673
696
  }
674
697
  ).then(async (message2) => {
675
- var _a2, _b;
698
+ var _a2, _b, _c, _d, _e;
676
699
  if (this._debug)
677
700
  console.log("\u8DDF\u8E2A7", JSON.stringify(message2));
678
- if (message2.detail && !message2.detail.usage) {
679
- try {
680
- const promptTokens = numTokens;
681
- let completionTokens = 0;
682
- if (["baidu", "aliyun"].indexOf(this._manufacturer.toLowerCase()) > -1) {
683
- completionTokens = (_b = (_a2 = message2.detail) == null ? void 0 : _a2.usage) == null ? void 0 : _b.total_tokens;
684
- } else {
685
- completionTokens = await this._getTokenCount(message2.text);
701
+ if (message2.detail) {
702
+ if (!message2.detail.usage) {
703
+ try {
704
+ const promptTokens = numTokens;
705
+ let completionTokens = 0;
706
+ if (["baidu", "aliyun"].indexOf(this._manufacturer.toLowerCase()) > -1) {
707
+ completionTokens = (_b = (_a2 = message2.detail) == null ? void 0 : _a2.usage) == null ? void 0 : _b.total_tokens;
708
+ } else {
709
+ completionTokens = await this._getTokenCount(message2.text);
710
+ }
711
+ message2.detail.usage = {
712
+ prompt_tokens: promptTokens,
713
+ completion_tokens: completionTokens,
714
+ total_tokens: promptTokens + completionTokens,
715
+ estimated: true
716
+ };
717
+ } catch (err) {
718
+ }
719
+ }
720
+ if (this._manufacturer.toLowerCase() === "azure") {
721
+ if (((_d = (_c = message2.detail) == null ? void 0 : _c.choices[0]) == null ? void 0 : _d.finish_reason) === "function_call") {
722
+ message2.detail.choices[0].finish_reason = "tool_calls";
723
+ message2.detail.choices[0].message = {
724
+ role: message2.detail.choices[0].message.role,
725
+ tool_calls: [{
726
+ id: `call_${uuidv4()}`,
727
+ type: "function",
728
+ function: (_e = message2.detail.choices[0].message) == null ? void 0 : _e.function_call
729
+ }]
730
+ };
686
731
  }
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
732
  }
695
733
  }
696
734
  const pRes = Promise.all([
@@ -757,7 +795,7 @@ Current date: ${currentDate}`;
757
795
  this._apiOrg = apiOrg;
758
796
  }
759
797
  async _buildMessages(text, opts, pluginData) {
760
- var _a;
798
+ var _a, _b;
761
799
  const { systemMessage = this._systemMessage, completionParams } = opts;
762
800
  let { parentMessageId } = opts;
763
801
  let errorMessage = "";
@@ -796,6 +834,9 @@ Current date: ${currentDate}`;
796
834
  content: JSON.stringify(pluginData.content),
797
835
  tool_call_id: pluginData.tool_call_id
798
836
  };
837
+ if (["azure"].indexOf(this._manufacturer.toLowerCase()) > -1) {
838
+ delete userMessage.tool_call_id;
839
+ }
799
840
  } else {
800
841
  userMessage = [{ role: "user", content: text, name: opts.name }];
801
842
  }
@@ -844,7 +885,11 @@ ${message.content}`]);
844
885
  parentMessageItem = { role: parentMessageRole, content: parentMessage.text };
845
886
  } else if (parentMessage.content && ((_a = parentMessage.content) == null ? void 0 : _a.finish_reason) === "tool_calls") {
846
887
  if (["azure"].indexOf(this._manufacturer.toLowerCase()) > -1) {
847
- parentMessageItem = Object.assign(parentMessage.content.message, { content: parentMessage.text || parentMessage.content });
888
+ parentMessageItem = Object.assign(parentMessage.content.message, {
889
+ function_call: (_b = parentMessage.content.message.tool_calls[0]) == null ? void 0 : _b.function,
890
+ content: parentMessage.text || parentMessage.content
891
+ });
892
+ delete parentMessage.content.message.tool_calls;
848
893
  } else {
849
894
  parentMessageItem = parentMessage.content.message;
850
895
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leikeduntech/leiai-js",
3
- "version": "3.2.0",
3
+ "version": "3.4.0",
4
4
  "author": "liuhean",
5
5
  "repository": {
6
6
  "type": "git",