@leikeduntech/leiai-js 3.2.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.
Files changed (2) hide show
  1. package/build/index.js +56 -21
  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,
@@ -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;
@@ -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 && !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);
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 = "";
@@ -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
  }
@@ -844,7 +875,11 @@ ${message.content}`]);
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) {
847
- parentMessageItem = Object.assign(parentMessage.content.message, { content: parentMessage.text || parentMessage.content });
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;
848
883
  } else {
849
884
  parentMessageItem = parentMessage.content.message;
850
885
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leikeduntech/leiai-js",
3
- "version": "3.2.0",
3
+ "version": "3.3.0",
4
4
  "author": "liuhean",
5
5
  "repository": {
6
6
  "type": "git",