@leikeduntech/leiai-js 3.4.5 → 3.5.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.d.ts CHANGED
@@ -185,6 +185,8 @@ declare namespace openai {
185
185
  data?: string;
186
186
  meta?: any;
187
187
  type?: string;
188
+ answer?: string;
189
+ source_info?: any;
188
190
  }
189
191
  /**
190
192
  *
@@ -346,6 +348,7 @@ declare namespace openai {
346
348
  user?: string;
347
349
  pluginList?: string | object;
348
350
  fileList?: Array<any>;
351
+ extParams?: any;
349
352
  }
350
353
  /**
351
354
  * @type CreateChatCompletionRequestStop
package/build/index.js CHANGED
@@ -261,13 +261,6 @@ var ChatGPTAPI = class {
261
261
  * @returns The response from ChatGPT
262
262
  */
263
263
  async sendMessage(text, opts = {}, pluginParams) {
264
- var _a;
265
- if (this._systemMessage === void 0 && ["openai", "azure"].indexOf((_a = this._manufacturer) == null ? void 0 : _a.toLowerCase()) > -1) {
266
- const currentDate = (/* @__PURE__ */ new Date()).toISOString().split("T")[0];
267
- this._systemMessage = `You are ChatGPT, a large language model trained by ${this._manufacturer}. Answer as concisely as possible.
268
- Knowledge cutoff: 2021-09-01
269
- Current date: ${currentDate}`;
270
- }
271
264
  let parentMsg;
272
265
  if (pluginParams) {
273
266
  parentMsg = await this._getMessageById(pluginParams.assistant_res.id);
@@ -355,7 +348,7 @@ Current date: ${currentDate}`;
355
348
  };
356
349
  const responseP = new Promise(
357
350
  async (resolve, reject) => {
358
- var _a2, _b, _c, _d, _e, _f, _g;
351
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
359
352
  let url = `${this._apiBaseUrl}/chat/completions`;
360
353
  const headers = {
361
354
  "Content-Type": "application/json",
@@ -380,6 +373,8 @@ Current date: ${currentDate}`;
380
373
  } else if (this._manufacturer.toLowerCase() === "xunfei") {
381
374
  url = "";
382
375
  delete headers.Authorization;
376
+ } else if (this._manufacturer.toLowerCase() === "chatdoc") {
377
+ url = this._apiBaseUrl;
383
378
  }
384
379
  let pluginList;
385
380
  if (completionParams.pluginList) {
@@ -477,9 +472,22 @@ Current date: ${currentDate}`;
477
472
  } else if (this._manufacturer.toLowerCase() === "baidu") {
478
473
  if (pluginList && pluginList.indexOf("zhishiku") > -1) {
479
474
  let query = messages.splice(messages.length - 1, 1);
480
- body = Object.assign(body, { query: (_a2 = query[0]) == null ? void 0 : _a2.content, history: messages });
475
+ body = Object.assign(body, { query: (_a = query[0]) == null ? void 0 : _a.content, history: messages });
481
476
  delete body.messages;
482
477
  }
478
+ } else if (this._manufacturer.toLowerCase() === "chatdoc") {
479
+ let query = messages.splice(messages.length - 1, 1);
480
+ body = Object.assign(body, { upload_id: (_b = completionParams == null ? void 0 : completionParams.extParams) == null ? void 0 : _b.upload_id, question: (_c = query[0]) == null ? void 0 : _c.content, history: messages });
481
+ if (body.extParams)
482
+ delete body.extParams;
483
+ if (body.model)
484
+ delete body.model;
485
+ if (body.max_tokens)
486
+ delete body.max_tokens;
487
+ if (body.temperature)
488
+ delete body.temperature;
489
+ if (body.messages)
490
+ delete body.messages;
483
491
  }
484
492
  if (this._apiOrg && this._manufacturer.toLowerCase() === "openai") {
485
493
  headers["OpenAI-Organization"] = this._apiOrg;
@@ -512,7 +520,7 @@ Current date: ${currentDate}`;
512
520
  onData({ content, start, end, seq }) {
513
521
  if (self._debug)
514
522
  console.log("onData", content, start, end, seq);
515
- result.id = `xunfei-${Math.floor(Math.random() * 1e13)}`;
523
+ result.id = `xunfei-${Math.floor(Math.random() * 1e7)}${(/* @__PURE__ */ new Date()).getTime()}`;
516
524
  result.delta = content;
517
525
  if (content)
518
526
  result.text += content;
@@ -537,7 +545,7 @@ Current date: ${currentDate}`;
537
545
  body: JSON.stringify(body),
538
546
  signal: abortSignal,
539
547
  onMessage: (data) => {
540
- var _a3, _b2, _c2, _d2, _e2, _f2, _g2, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v;
548
+ var _a2, _b2, _c2, _d2, _e2, _f2, _g2, _h2, _i2, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v;
541
549
  if (data === "[DONE]") {
542
550
  result.text = result.text.trim();
543
551
  return resolve(result);
@@ -554,7 +562,7 @@ Current date: ${currentDate}`;
554
562
  return resolve(result);
555
563
  }
556
564
  } else if (this._manufacturer.toLowerCase() === "azure") {
557
- if (((_a3 = response.choices[0]) == null ? void 0 : _a3.finish_reason) === "stop") {
565
+ if (((_a2 = response.choices[0]) == null ? void 0 : _a2.finish_reason) === "stop") {
558
566
  result.text = result.text.trim();
559
567
  return resolve(result);
560
568
  }
@@ -570,7 +578,13 @@ Current date: ${currentDate}`;
570
578
  }
571
579
  } else if (this._manufacturer.toLowerCase() === "tencent") {
572
580
  if (((_g2 = response.choices[0]) == null ? void 0 : _g2.finish_reason) === "stop") {
573
- result.text += (_i = (_h = response == null ? void 0 : response.choices[0]) == null ? void 0 : _h.delta) == null ? void 0 : _i.content.trim();
581
+ result.text += (_i2 = (_h2 = response == null ? void 0 : response.choices[0]) == null ? void 0 : _h2.delta) == null ? void 0 : _i2.content.trim();
582
+ return resolve(result);
583
+ }
584
+ } else if (this._manufacturer.toLowerCase() === "chatdoc") {
585
+ result.id = `chatdoc-${Math.floor(Math.random() * 1e7)}${(/* @__PURE__ */ new Date()).getTime()}`;
586
+ if (response == null ? void 0 : response.source_info) {
587
+ result.text += response == null ? void 0 : response.answer;
574
588
  return resolve(result);
575
589
  }
576
590
  }
@@ -629,6 +643,13 @@ Current date: ${currentDate}`;
629
643
  result.role = "assistant";
630
644
  result.detail = response;
631
645
  onProgress == null ? void 0 : onProgress(result);
646
+ } else if ((response == null ? void 0 : response.answer) && this._manufacturer.toLowerCase() === "chatdoc") {
647
+ result.delta = response.answer;
648
+ if (response == null ? void 0 : response.answer)
649
+ result.text += response.answer;
650
+ result.role = "assistant";
651
+ result.detail = response;
652
+ onProgress == null ? void 0 : onProgress(result);
632
653
  }
633
654
  } catch (err) {
634
655
  console.warn(`${this._manufacturer} stream SEE event unexpected error`, err);
@@ -661,7 +682,7 @@ Current date: ${currentDate}`;
661
682
  }
662
683
  const response = await res.json();
663
684
  if (this._debug) {
664
- console.log(`row data ${typeof response} : `, response, (response == null ? void 0 : response.choices) && ((_b = response == null ? void 0 : response.choices[0]) == null ? void 0 : _b.message));
685
+ console.log(`row data ${typeof response} : `, response, (response == null ? void 0 : response.choices) && ((_d = response == null ? void 0 : response.choices[0]) == null ? void 0 : _d.message));
665
686
  }
666
687
  if (this._manufacturer.toLowerCase() === "aliyun") {
667
688
  if (response == null ? void 0 : response.request_id) {
@@ -672,7 +693,7 @@ Current date: ${currentDate}`;
672
693
  result.id = response.id;
673
694
  }
674
695
  }
675
- if (((_c = response == null ? void 0 : response.choices) == null ? void 0 : _c.length) && ["openai", "azure"].indexOf(this._manufacturer.toLowerCase()) > -1) {
696
+ if (((_e = response == null ? void 0 : response.choices) == null ? void 0 : _e.length) && ["openai", "azure"].indexOf(this._manufacturer.toLowerCase()) > -1) {
676
697
  const message2 = response.choices[0].message;
677
698
  result.text = message2.content;
678
699
  if (message2.role) {
@@ -681,14 +702,14 @@ Current date: ${currentDate}`;
681
702
  } else if ((response == null ? void 0 : response.result) && this._manufacturer.toLowerCase() === "baidu") {
682
703
  result.text = response.result;
683
704
  result.role = "assistant";
684
- } else if (((_d = response == null ? void 0 : response.output) == null ? void 0 : _d.text) && this._manufacturer.toLowerCase() === "aliyun") {
685
- result.text = (_e = response == null ? void 0 : response.output) == null ? void 0 : _e.text;
705
+ } else if (((_f = response == null ? void 0 : response.output) == null ? void 0 : _f.text) && this._manufacturer.toLowerCase() === "aliyun") {
706
+ result.text = (_g = response == null ? void 0 : response.output) == null ? void 0 : _g.text;
686
707
  result.role = "assistant";
687
708
  } else {
688
709
  const res2 = response;
689
710
  return reject(
690
711
  new Error(
691
- `${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"}`
712
+ `${this._manufacturer} error: ${((_h = res2 == null ? void 0 : res2.detail) == null ? void 0 : _h.message) || ((_i = res2 == null ? void 0 : res2.detail) == null ? void 0 : _i.error_msg) || (res2 == null ? void 0 : res2.detail) || "unknown"}`
692
713
  )
693
714
  );
694
715
  }
@@ -705,7 +726,7 @@ Current date: ${currentDate}`;
705
726
  }
706
727
  }
707
728
  ).then(async (message2) => {
708
- var _a2, _b, _c, _d, _e;
729
+ var _a, _b, _c, _d, _e;
709
730
  if (this._debug)
710
731
  console.log("\u8DDF\u8E2A7", JSON.stringify(message2));
711
732
  if (message2.detail) {
@@ -714,7 +735,7 @@ Current date: ${currentDate}`;
714
735
  const promptTokens = numTokens;
715
736
  let completionTokens = 0;
716
737
  if (["baidu", "aliyun"].indexOf(this._manufacturer.toLowerCase()) > -1) {
717
- completionTokens = (_b = (_a2 = message2.detail) == null ? void 0 : _a2.usage) == null ? void 0 : _b.total_tokens;
738
+ completionTokens = (_b = (_a = message2.detail) == null ? void 0 : _a.usage) == null ? void 0 : _b.total_tokens;
718
739
  } else {
719
740
  completionTokens = await this._getTokenCount(message2.text);
720
741
  }
@@ -835,7 +856,7 @@ Current date: ${currentDate}`;
835
856
  }
836
857
  const systemMessageOffset = messages.length;
837
858
  let userMessage = null;
838
- if (["baidu", "zhipu", "xunfei", "aliyun", "tencent"].indexOf(this._manufacturer.toLowerCase()) > -1) {
859
+ if (["baidu", "zhipu", "xunfei", "aliyun", "tencent", "chatdoc"].indexOf(this._manufacturer.toLowerCase()) > -1) {
839
860
  userMessage = [{ role: "user", content: text }];
840
861
  } else if (pluginData) {
841
862
  userMessage = {
@@ -891,7 +912,7 @@ ${message.content}`]);
891
912
  }
892
913
  const parentMessageRole = parentMessage.role || "user";
893
914
  let parentMessageItem = null;
894
- if (["baidu", "zhipu", "xunfei", "aliyun", "tencent"].indexOf(this._manufacturer.toLowerCase()) > -1) {
915
+ if (["baidu", "zhipu", "xunfei", "aliyun", "tencent", "chatdoc"].indexOf(this._manufacturer.toLowerCase()) > -1) {
895
916
  parentMessageItem = { role: parentMessageRole, content: parentMessage.text };
896
917
  } else if (parentMessage.content && ((_a = parentMessage.content) == null ? void 0 : _a.finish_reason) === "tool_calls") {
897
918
  if (["azure"].indexOf(this._manufacturer.toLowerCase()) > -1) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leikeduntech/leiai-js",
3
- "version": "3.4.5",
3
+ "version": "3.5.0",
4
4
  "author": "liuhean",
5
5
  "repository": {
6
6
  "type": "git",