@leikeduntech/leiai-js 2.8.0 → 2.9.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
@@ -35,6 +35,10 @@ type SendMessageOptions = {
35
35
  abortSignal?: AbortSignal;
36
36
  completionParams?: Partial<Omit<openai.CreateChatCompletionRequest, 'messages' | 'n' | 'stream'>>;
37
37
  };
38
+ type PluginParams = {
39
+ plugin_res: object;
40
+ assistant_res: ChatMessage | any;
41
+ };
38
42
  type MessageActionType = 'next' | 'variant';
39
43
  type SendMessageBrowserOptions = {
40
44
  conversationId?: string;
@@ -51,7 +55,7 @@ interface ChatMessage {
51
55
  role?: Role;
52
56
  name?: string;
53
57
  delta?: string;
54
- detail?: openai.CreateChatCompletionResponse | CreateChatCompletionStreamResponse;
58
+ detail?: openai.CreateChatCompletionResponse | CreateChatCompletionStreamResponse | any;
55
59
  parentMessageId?: string;
56
60
  conversationId?: string;
57
61
  object?: string;
@@ -59,10 +63,11 @@ interface ChatMessage {
59
63
  sentence_id?: number;
60
64
  is_end?: boolean;
61
65
  is_truncated?: boolean;
62
- result?: string;
66
+ result?: string | object;
63
67
  need_clear_history?: boolean;
64
68
  usage?: object;
65
69
  tool_call_id?: string;
70
+ content?: string | object | any;
66
71
  }
67
72
  declare class ChatGPTError extends Error {
68
73
  statusCode?: number;
@@ -227,11 +232,13 @@ declare namespace openai {
227
232
  * ]}
228
233
  */
229
234
  input?: object;
235
+ tool_call_id?: string;
230
236
  }
231
237
  const ChatCompletionRequestMessageRoleEnum: {
232
238
  readonly System: 'system';
233
239
  readonly User: 'user';
234
240
  readonly Assistant: 'assistant';
241
+ readonly Tool: 'tool';
235
242
  };
236
243
  type ChatCompletionRequestMessageRoleEnum = (typeof ChatCompletionRequestMessageRoleEnum)[keyof typeof ChatCompletionRequestMessageRoleEnum];
237
244
  /**
@@ -460,6 +467,7 @@ declare class ChatGPTAPI {
460
467
  protected _getMessageById: GetMessageByIdFunction;
461
468
  protected _upsertMessage: UpsertMessageFunction;
462
469
  protected _messageStore: Keyv<ChatMessage>;
470
+ protected _DallE3FunData: any;
463
471
  /**
464
472
  * Creates a new client wrapper around OpenAI's chat completion API, mimicing the official ChatGPT webapp's functionality as closely as possible.
465
473
  *
@@ -477,10 +485,7 @@ declare class ChatGPTAPI {
477
485
  */
478
486
  constructor(opts: ChatGPTAPIOptions);
479
487
  pluginListMap(pluginList: any): any;
480
- DallE3Fun(): {
481
- pluginList: string;
482
- description: string;
483
- };
488
+ DallE3Fun(): any;
484
489
  /**
485
490
  * Sends a message to the OpenAI chat completions endpoint, waits for the response
486
491
  * to resolve, and returns the response.
@@ -503,7 +508,7 @@ declare class ChatGPTAPI {
503
508
  *
504
509
  * @returns The response from ChatGPT
505
510
  */
506
- sendMessage(text: string, opts?: SendMessageOptions): Promise<ChatMessage>;
511
+ sendMessage(text: string, opts: SendMessageOptions, pluginParams: PluginParams): Promise<ChatMessage>;
507
512
  get manufacturer(): string;
508
513
  set manufacturer(manufacturer: string);
509
514
  get maxModelTokens(): number;
@@ -516,7 +521,7 @@ declare class ChatGPTAPI {
516
521
  set apiKey(apiKey: string);
517
522
  get apiOrg(): string;
518
523
  set apiOrg(apiOrg: string);
519
- protected _buildMessages(text: string, opts: SendMessageOptions): Promise<{
524
+ protected _buildMessages(text: string, opts: SendMessageOptions, pluginData: ChatMessage): Promise<{
520
525
  messages: openai.ChatCompletionRequestMessage[];
521
526
  maxTokens: number;
522
527
  numTokens: number;
@@ -577,4 +582,4 @@ declare class ChatGPTUnofficialProxyAPI {
577
582
  sendMessage(text: string, opts?: SendMessageBrowserOptions): Promise<ChatMessage>;
578
583
  }
579
584
 
580
- export { ChatGPTAPI, ChatGPTAPIOptions, ChatGPTError, ChatGPTUnofficialProxyAPI, ChatMessage, ContentType, ConversationJSONBody, ConversationResponseEvent, CreateChatCompletionStreamResponse, CreateCompletionStreamResponseUsage, FetchFn, GetMessageByIdFunction, Message, MessageActionType, MessageContent, MessageMetadata, Prompt, PromptContent, Role, SendMessageBrowserOptions, SendMessageOptions, UpsertMessageFunction, openai };
585
+ export { ChatGPTAPI, ChatGPTAPIOptions, ChatGPTError, ChatGPTUnofficialProxyAPI, ChatMessage, ContentType, ConversationJSONBody, ConversationResponseEvent, CreateChatCompletionStreamResponse, CreateCompletionStreamResponseUsage, FetchFn, GetMessageByIdFunction, Message, MessageActionType, MessageContent, MessageMetadata, PluginParams, Prompt, PromptContent, Role, SendMessageBrowserOptions, SendMessageOptions, UpsertMessageFunction, openai };
package/build/index.js CHANGED
@@ -227,13 +227,16 @@ var ChatGPTAPI = class {
227
227
  }
228
228
  pluginListMap(pluginList) {
229
229
  const list = {
230
- "dall-e-3": "DallE3Fun"
230
+ "dall-e-3": {
231
+ name: "DallE3Fun",
232
+ description: "\u4F7F\u7528DALL-E3\u6A21\u578B\u6839\u636E\u7528\u6237\u63CF\u8FF0\u63D0\u793A\u7ED8\u56FE\u753B\u753B\u4F5C\u56FE"
233
+ }
231
234
  };
232
235
  return list[pluginList];
233
236
  }
234
237
  // DallE3文生图插件
235
238
  DallE3Fun() {
236
- return { pluginList: "dall-e-3", description: "\u4F7F\u7528DALL-E3\u6A21\u578B\u6839\u636E\u7528\u6237\u63CF\u8FF0\u63D0\u793A\u7ED8\u56FE\u753B\u753B\u4F5C\u56FE" };
239
+ return this._DallE3FunData;
237
240
  }
238
241
  /**
239
242
  * Sends a message to the OpenAI chat completions endpoint, waits for the response
@@ -257,7 +260,7 @@ var ChatGPTAPI = class {
257
260
  *
258
261
  * @returns The response from ChatGPT
259
262
  */
260
- async sendMessage(text, opts = {}) {
263
+ async sendMessage(text, opts = {}, pluginParams) {
261
264
  var _a;
262
265
  if (this._systemMessage === void 0 && ["openai", "azure"].indexOf((_a = this._manufacturer) == null ? void 0 : _a.toLowerCase()) > -1) {
263
266
  const currentDate = (/* @__PURE__ */ new Date()).toISOString().split("T")[0];
@@ -265,7 +268,12 @@ var ChatGPTAPI = class {
265
268
  Knowledge cutoff: 2021-09-01
266
269
  Current date: ${currentDate}`;
267
270
  }
268
- const {
271
+ let parentMsg;
272
+ if (pluginParams) {
273
+ parentMsg = await this._getMessageById(pluginParams.assistant_res.id);
274
+ opts.parentMessageId = parentMsg.id;
275
+ }
276
+ let {
269
277
  parentMessageId,
270
278
  messageId = uuidv4(),
271
279
  timeoutMs,
@@ -280,17 +288,42 @@ Current date: ${currentDate}`;
280
288
  abortController = new AbortController();
281
289
  abortSignal = abortController.signal;
282
290
  }
283
- const message = {
291
+ let message = {
284
292
  role: "user",
285
293
  id: messageId,
286
294
  conversationId,
287
295
  parentMessageId,
288
296
  text
289
297
  };
290
- const latestQuestion = message;
298
+ let latestQuestion = message, last_assistant_res_message = null, last_assistant_res = null, last_plugin_res = null;
299
+ if (pluginParams) {
300
+ last_assistant_res_message = pluginParams.assistant_res.detail.choices[0];
301
+ last_assistant_res = {
302
+ role: last_assistant_res_message.message.role,
303
+ id: parentMsg.id,
304
+ conversationId,
305
+ parentMessageId: parentMsg.parentMessageId,
306
+ text,
307
+ content: last_assistant_res_message
308
+ };
309
+ last_plugin_res = {
310
+ role: "tool",
311
+ id: messageId,
312
+ name: last_assistant_res_message.message.tool_calls[0].function.name,
313
+ conversationId,
314
+ parentMessageId: pluginParams.assistant_res.id,
315
+ text,
316
+ content: pluginParams.plugin_res,
317
+ tool_call_id: last_assistant_res_message.message.tool_calls[0].id
318
+ };
319
+ await this._upsertMessage(last_assistant_res);
320
+ latestQuestion = last_plugin_res;
321
+ this[`_${last_plugin_res.name}Data`] = pluginParams.plugin_res;
322
+ }
291
323
  const { messages, maxTokens, numTokens, errorMessage } = await this._buildMessages(
292
324
  text,
293
- opts
325
+ opts,
326
+ last_plugin_res
294
327
  );
295
328
  if (this._debug)
296
329
  console.log(`typeof errorMessage ${typeof errorMessage},numTokens:${numTokens}, _maxModelTokens:${this._maxModelTokens},errorMessage: ${errorMessage}`);
@@ -365,7 +398,7 @@ Current date: ${currentDate}`;
365
398
  type: "function",
366
399
  function: {
367
400
  "name": "DallE3Fun",
368
- "description": this.DallE3Fun().description,
401
+ "description": this.pluginListMap(pluginList).description,
369
402
  "parameters": {
370
403
  "type": "object",
371
404
  "properties": {
@@ -454,16 +487,12 @@ Current date: ${currentDate}`;
454
487
  signal: abortSignal,
455
488
  onMessage: (data) => {
456
489
  var _a3, _b2, _c2, _d2, _e2, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v;
457
- if (this._debug) {
458
- }
459
490
  if (data === "[DONE]") {
460
491
  result.text = result.text.trim();
461
492
  return resolve(result);
462
493
  }
463
494
  try {
464
495
  const response = JSON.parse(data);
465
- if (this._debug) {
466
- }
467
496
  if (this._manufacturer.toLowerCase() === "baidu") {
468
497
  if ((response == null ? void 0 : response.is_end) === true) {
469
498
  result.text += response.result.trim();
@@ -512,7 +541,6 @@ Current date: ${currentDate}`;
512
541
  result.text += result.delta;
513
542
  result.role = "assistant";
514
543
  if (response.choices[0].finish_reason === "tool_calls") {
515
- result.name = this.pluginListMap(pluginList);
516
544
  } else if (delta.role) {
517
545
  result.role = delta.role;
518
546
  }
@@ -575,7 +603,7 @@ Current date: ${currentDate}`;
575
603
  }
576
604
  const response = await res.json();
577
605
  if (this._debug) {
578
- console.log(response);
606
+ console.log(`row data ${typeof response} : `, response, response == null ? void 0 : response.choices);
579
607
  }
580
608
  if (this._manufacturer.toLowerCase() === "aliyun") {
581
609
  if (response == null ? void 0 : response.request_id) {
@@ -634,10 +662,11 @@ Current date: ${currentDate}`;
634
662
  } catch (err) {
635
663
  }
636
664
  }
637
- return Promise.all([
665
+ const pRes = Promise.all([
638
666
  this._upsertMessage(latestQuestion),
639
667
  this._upsertMessage(message2)
640
668
  ]).then(() => message2);
669
+ return pRes;
641
670
  });
642
671
  if (timeoutMs) {
643
672
  if (abortController) {
@@ -690,7 +719,8 @@ Current date: ${currentDate}`;
690
719
  set apiOrg(apiOrg) {
691
720
  this._apiOrg = apiOrg;
692
721
  }
693
- async _buildMessages(text, opts) {
722
+ async _buildMessages(text, opts, pluginData) {
723
+ var _a;
694
724
  const { systemMessage = this._systemMessage, completionParams } = opts;
695
725
  let { parentMessageId } = opts;
696
726
  let errorMessage = "";
@@ -719,12 +749,30 @@ Current date: ${currentDate}`;
719
749
  }
720
750
  }
721
751
  const systemMessageOffset = messages.length;
722
- const userMessage = ["baidu", "azure", "zhipu", "xunfei", "aliyun", "tencent"].indexOf(this._manufacturer.toLowerCase()) > -1 ? [{ role: "user", content: text }] : [{ role: "user", content: text, name: opts.name }];
723
- let nextMessages = text ? messages.concat(userMessage) : messages;
752
+ let userMessage = null;
753
+ if (["baidu", "azure", "zhipu", "xunfei", "aliyun", "tencent"].indexOf(this._manufacturer.toLowerCase()) > -1) {
754
+ userMessage = [{ role: "user", content: text }];
755
+ } else if (pluginData) {
756
+ userMessage = {
757
+ role: pluginData.role,
758
+ name: pluginData.name,
759
+ content: JSON.stringify(pluginData.content),
760
+ tool_call_id: pluginData.tool_call_id
761
+ };
762
+ } else {
763
+ userMessage = [{ role: "user", content: text, name: opts.name }];
764
+ }
765
+ let nextMessages = messages;
766
+ if (pluginData || text) {
767
+ nextMessages = nextMessages.concat(userMessage);
768
+ }
724
769
  let numTokens = 0;
725
770
  do {
726
771
  const prompt = nextMessages.reduce((prompt2, message) => {
727
772
  switch (message.role) {
773
+ case "tool":
774
+ return prompt2.concat([`Tool:
775
+ function call`]);
728
776
  case "system":
729
777
  return prompt2.concat([`Instructions:
730
778
  ${message.content}`]);
@@ -754,14 +802,20 @@ ${message.content}`]);
754
802
  break;
755
803
  }
756
804
  const parentMessageRole = parentMessage.role || "user";
757
- const parentMessageItem = ["baidu", "azure", "zhipu", "xunfei", "aliyun", "tencent"].indexOf(this._manufacturer.toLowerCase()) > -1 ? {
758
- role: parentMessageRole,
759
- content: parentMessage.text
760
- } : {
761
- role: parentMessageRole,
762
- content: parentMessage.text,
763
- name: parentMessage.name
764
- };
805
+ let parentMessageItem = null;
806
+ if (["baidu", "azure", "zhipu", "xunfei", "aliyun", "tencent"].indexOf(this._manufacturer.toLowerCase()) > -1) {
807
+ parentMessageItem = { role: parentMessageRole, content: parentMessage.text };
808
+ } else if (parentMessage.content && ((_a = parentMessage.content) == null ? void 0 : _a.finish_reason) === "tool_calls") {
809
+ parentMessageItem = parentMessage.content.message;
810
+ } else {
811
+ parentMessageItem = {
812
+ role: parentMessageRole,
813
+ content: parentMessage.text || parentMessage.content,
814
+ name: parentMessage.name
815
+ };
816
+ if (parentMessage.tool_call_id)
817
+ parentMessageItem.tool_call_id = parentMessage.tool_call_id;
818
+ }
765
819
  nextMessages = nextMessages.slice(0, systemMessageOffset).concat([
766
820
  parentMessageItem,
767
821
  ...nextMessages.slice(systemMessageOffset)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leikeduntech/leiai-js",
3
- "version": "2.8.0",
3
+ "version": "2.9.0",
4
4
  "author": "liuhean",
5
5
  "repository": {
6
6
  "type": "git",