@leikeduntech/leiai-js 2.8.0 → 3.0.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 +12 -9
- package/build/index.js +123 -32
- package/package.json +1 -1
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
|
/**
|
|
@@ -477,10 +484,6 @@ declare class ChatGPTAPI {
|
|
|
477
484
|
*/
|
|
478
485
|
constructor(opts: ChatGPTAPIOptions);
|
|
479
486
|
pluginListMap(pluginList: any): any;
|
|
480
|
-
DallE3Fun(): {
|
|
481
|
-
pluginList: string;
|
|
482
|
-
description: string;
|
|
483
|
-
};
|
|
484
487
|
/**
|
|
485
488
|
* Sends a message to the OpenAI chat completions endpoint, waits for the response
|
|
486
489
|
* to resolve, and returns the response.
|
|
@@ -503,7 +506,7 @@ declare class ChatGPTAPI {
|
|
|
503
506
|
*
|
|
504
507
|
* @returns The response from ChatGPT
|
|
505
508
|
*/
|
|
506
|
-
sendMessage(text: string, opts
|
|
509
|
+
sendMessage(text: string, opts: SendMessageOptions, pluginParams: PluginParams): Promise<ChatMessage>;
|
|
507
510
|
get manufacturer(): string;
|
|
508
511
|
set manufacturer(manufacturer: string);
|
|
509
512
|
get maxModelTokens(): number;
|
|
@@ -516,7 +519,7 @@ declare class ChatGPTAPI {
|
|
|
516
519
|
set apiKey(apiKey: string);
|
|
517
520
|
get apiOrg(): string;
|
|
518
521
|
set apiOrg(apiOrg: string);
|
|
519
|
-
protected _buildMessages(text: string, opts: SendMessageOptions): Promise<{
|
|
522
|
+
protected _buildMessages(text: string, opts: SendMessageOptions, pluginData: ChatMessage): Promise<{
|
|
520
523
|
messages: openai.ChatCompletionRequestMessage[];
|
|
521
524
|
maxTokens: number;
|
|
522
525
|
numTokens: number;
|
|
@@ -577,4 +580,4 @@ declare class ChatGPTUnofficialProxyAPI {
|
|
|
577
580
|
sendMessage(text: string, opts?: SendMessageBrowserOptions): Promise<ChatMessage>;
|
|
578
581
|
}
|
|
579
582
|
|
|
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 };
|
|
583
|
+
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,14 +227,17 @@ var ChatGPTAPI = class {
|
|
|
227
227
|
}
|
|
228
228
|
pluginListMap(pluginList) {
|
|
229
229
|
const list = {
|
|
230
|
-
"dall-e-3":
|
|
230
|
+
"dall-e-3": {
|
|
231
|
+
name: "DallE3Fun",
|
|
232
|
+
description: "\u4F7F\u7528DALL-E3\u6A21\u578B\u6839\u636E\u7528\u6237\u63CF\u8FF0\u63D0\u793A\u521B\u4F5C\u4E00\u5F20\u65B0\u7684\u56FE\u7247"
|
|
233
|
+
},
|
|
234
|
+
"dall-e-2": {
|
|
235
|
+
name: "DallE2Fun",
|
|
236
|
+
description: "\u4F7F\u7528DALL-E2\u6A21\u578B\u57FA\u4E8E\u7528\u6237\u63D0\u4F9B\u7684\u56FE\u7247\u5730\u5740url\u548C\u63D0\u793A\u8BCD\u91CD\u65B0\u4FEE\u6539\u56FE\u7247\u7F16\u8F91\u56FE\u7247\u66FF\u6362\u56FE\u7247\u5220\u9664\u56FE\u7247\u90E8\u4EFD\u5185\u5BB9"
|
|
237
|
+
}
|
|
231
238
|
};
|
|
232
239
|
return list[pluginList];
|
|
233
240
|
}
|
|
234
|
-
// DallE3文生图插件
|
|
235
|
-
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" };
|
|
237
|
-
}
|
|
238
241
|
/**
|
|
239
242
|
* Sends a message to the OpenAI chat completions endpoint, waits for the response
|
|
240
243
|
* to resolve, and returns 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
|
-
|
|
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,45 @@ Current date: ${currentDate}`;
|
|
|
280
288
|
abortController = new AbortController();
|
|
281
289
|
abortSignal = abortController.signal;
|
|
282
290
|
}
|
|
283
|
-
|
|
291
|
+
let message = {
|
|
284
292
|
role: "user",
|
|
285
293
|
id: messageId,
|
|
286
294
|
conversationId,
|
|
287
295
|
parentMessageId,
|
|
288
296
|
text
|
|
289
297
|
};
|
|
290
|
-
|
|
298
|
+
if (this._debug)
|
|
299
|
+
console.log("\u8DDF\u8E2A1, \u8D85\u65F6\u65F6\u95F4ms", timeoutMs);
|
|
300
|
+
let latestQuestion = message, last_assistant_res_message = null, last_assistant_res = null, last_plugin_res = null;
|
|
301
|
+
if (pluginParams) {
|
|
302
|
+
last_assistant_res_message = pluginParams.assistant_res.detail.choices[0];
|
|
303
|
+
last_assistant_res = {
|
|
304
|
+
role: last_assistant_res_message.message.role,
|
|
305
|
+
id: parentMsg.id,
|
|
306
|
+
conversationId,
|
|
307
|
+
parentMessageId: parentMsg.parentMessageId,
|
|
308
|
+
text,
|
|
309
|
+
content: last_assistant_res_message
|
|
310
|
+
};
|
|
311
|
+
last_plugin_res = {
|
|
312
|
+
role: "tool",
|
|
313
|
+
id: messageId,
|
|
314
|
+
name: last_assistant_res_message.message.tool_calls[0].function.name,
|
|
315
|
+
conversationId,
|
|
316
|
+
parentMessageId: pluginParams.assistant_res.id,
|
|
317
|
+
text,
|
|
318
|
+
content: pluginParams.plugin_res,
|
|
319
|
+
tool_call_id: last_assistant_res_message.message.tool_calls[0].id
|
|
320
|
+
};
|
|
321
|
+
await this._upsertMessage(last_assistant_res);
|
|
322
|
+
latestQuestion = last_plugin_res;
|
|
323
|
+
}
|
|
324
|
+
if (this._debug)
|
|
325
|
+
console.log("\u8DDF\u8E2A2");
|
|
291
326
|
const { messages, maxTokens, numTokens, errorMessage } = await this._buildMessages(
|
|
292
327
|
text,
|
|
293
|
-
opts
|
|
328
|
+
opts,
|
|
329
|
+
last_plugin_res
|
|
294
330
|
);
|
|
295
331
|
if (this._debug)
|
|
296
332
|
console.log(`typeof errorMessage ${typeof errorMessage},numTokens:${numTokens}, _maxModelTokens:${this._maxModelTokens},errorMessage: ${errorMessage}`);
|
|
@@ -358,20 +394,38 @@ Current date: ${currentDate}`;
|
|
|
358
394
|
};
|
|
359
395
|
if (["openai"].indexOf(this._manufacturer.toLowerCase()) > -1) {
|
|
360
396
|
switch (pluginList) {
|
|
361
|
-
case "plugin1":
|
|
362
|
-
break;
|
|
363
397
|
case "dall-e-3":
|
|
364
398
|
body = Object.assign(body, { tools: [{
|
|
399
|
+
type: "function",
|
|
400
|
+
function: {
|
|
401
|
+
"name": "DallE2Fun",
|
|
402
|
+
"description": this.pluginListMap(pluginList).description,
|
|
403
|
+
"parameters": {
|
|
404
|
+
"type": "object",
|
|
405
|
+
"properties": {
|
|
406
|
+
"prompt": {
|
|
407
|
+
"type": "string",
|
|
408
|
+
"description": "\u7528\u6237\u5BF9\u9700\u8981\u7F16\u8F91\u7684\u56FE\u7247\u4FEE\u6539\u7684\u63D0\u793A\u5185\u5BB9"
|
|
409
|
+
},
|
|
410
|
+
"image_url": {
|
|
411
|
+
"type": "string",
|
|
412
|
+
"description": "\u4ECE\u5BF9\u8BDD\u5386\u53F2\u8BB0\u5F55\u548C\u7528\u6237\u63D0\u793A\u8BCD\u91CC\u5339\u914D\u6700\u8FD1\u4E00\u6B21\u51FA\u73B0\u7684\u4EE5http\u5F00\u5934\u7684\u56FE\u7247\u7684\u94FE\u63A5\u5730\u5740"
|
|
413
|
+
}
|
|
414
|
+
},
|
|
415
|
+
"required": ["prompt", "image_url"]
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}, {
|
|
365
419
|
type: "function",
|
|
366
420
|
function: {
|
|
367
421
|
"name": "DallE3Fun",
|
|
368
|
-
"description": this.
|
|
422
|
+
"description": this.pluginListMap(pluginList).description,
|
|
369
423
|
"parameters": {
|
|
370
424
|
"type": "object",
|
|
371
425
|
"properties": {
|
|
372
426
|
"prompt": {
|
|
373
427
|
"type": "string",
|
|
374
|
-
"description": "\
|
|
428
|
+
"description": "\u7ED8\u56FE\u7684\u63D0\u793A\u8BCD"
|
|
375
429
|
}
|
|
376
430
|
},
|
|
377
431
|
"required": ["prompt"]
|
|
@@ -454,16 +508,12 @@ Current date: ${currentDate}`;
|
|
|
454
508
|
signal: abortSignal,
|
|
455
509
|
onMessage: (data) => {
|
|
456
510
|
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
511
|
if (data === "[DONE]") {
|
|
460
512
|
result.text = result.text.trim();
|
|
461
513
|
return resolve(result);
|
|
462
514
|
}
|
|
463
515
|
try {
|
|
464
516
|
const response = JSON.parse(data);
|
|
465
|
-
if (this._debug) {
|
|
466
|
-
}
|
|
467
517
|
if (this._manufacturer.toLowerCase() === "baidu") {
|
|
468
518
|
if ((response == null ? void 0 : response.is_end) === true) {
|
|
469
519
|
result.text += response.result.trim();
|
|
@@ -512,7 +562,6 @@ Current date: ${currentDate}`;
|
|
|
512
562
|
result.text += result.delta;
|
|
513
563
|
result.role = "assistant";
|
|
514
564
|
if (response.choices[0].finish_reason === "tool_calls") {
|
|
515
|
-
result.name = this.pluginListMap(pluginList);
|
|
516
565
|
} else if (delta.role) {
|
|
517
566
|
result.role = delta.role;
|
|
518
567
|
}
|
|
@@ -559,12 +608,16 @@ Current date: ${currentDate}`;
|
|
|
559
608
|
).catch(reject);
|
|
560
609
|
} else {
|
|
561
610
|
try {
|
|
611
|
+
if (this._debug)
|
|
612
|
+
console.log("\u8DDF\u8E2A3");
|
|
562
613
|
const res = await this._fetch(url, {
|
|
563
614
|
method: "POST",
|
|
564
615
|
headers,
|
|
565
616
|
body: JSON.stringify(body),
|
|
566
617
|
signal: abortSignal
|
|
567
618
|
});
|
|
619
|
+
if (this._debug)
|
|
620
|
+
console.log("\u8DDF\u8E2A4");
|
|
568
621
|
if (!res.ok) {
|
|
569
622
|
const reason = await res.text();
|
|
570
623
|
const msg = `${this._manufacturer} error ${res.status || res.statusText}: ${reason}`;
|
|
@@ -575,7 +628,7 @@ Current date: ${currentDate}`;
|
|
|
575
628
|
}
|
|
576
629
|
const response = await res.json();
|
|
577
630
|
if (this._debug) {
|
|
578
|
-
console.log(response);
|
|
631
|
+
console.log(`row data ${typeof response} : `, response, response == null ? void 0 : response.choices);
|
|
579
632
|
}
|
|
580
633
|
if (this._manufacturer.toLowerCase() === "aliyun") {
|
|
581
634
|
if (response == null ? void 0 : response.request_id) {
|
|
@@ -607,8 +660,12 @@ Current date: ${currentDate}`;
|
|
|
607
660
|
);
|
|
608
661
|
}
|
|
609
662
|
result.detail = response;
|
|
663
|
+
if (this._debug)
|
|
664
|
+
console.log("\u8DDF\u8E2A5");
|
|
610
665
|
return resolve(result);
|
|
611
666
|
} catch (err) {
|
|
667
|
+
if (this._debug)
|
|
668
|
+
console.log("\u8DDF\u8E2A6");
|
|
612
669
|
return reject(err);
|
|
613
670
|
}
|
|
614
671
|
}
|
|
@@ -616,6 +673,8 @@ Current date: ${currentDate}`;
|
|
|
616
673
|
}
|
|
617
674
|
).then(async (message2) => {
|
|
618
675
|
var _a2, _b;
|
|
676
|
+
if (this._debug)
|
|
677
|
+
console.log("\u8DDF\u8E2A7", JSON.stringify(message2));
|
|
619
678
|
if (message2.detail && !message2.detail.usage) {
|
|
620
679
|
try {
|
|
621
680
|
const promptTokens = numTokens;
|
|
@@ -634,12 +693,17 @@ Current date: ${currentDate}`;
|
|
|
634
693
|
} catch (err) {
|
|
635
694
|
}
|
|
636
695
|
}
|
|
637
|
-
|
|
696
|
+
const pRes = Promise.all([
|
|
638
697
|
this._upsertMessage(latestQuestion),
|
|
639
698
|
this._upsertMessage(message2)
|
|
640
699
|
]).then(() => message2);
|
|
700
|
+
return pRes;
|
|
641
701
|
});
|
|
702
|
+
if (this._debug)
|
|
703
|
+
console.log("\u8DDF\u8E2A8");
|
|
642
704
|
if (timeoutMs) {
|
|
705
|
+
if (this._debug)
|
|
706
|
+
console.log("\u8DDF\u8E2A9");
|
|
643
707
|
if (abortController) {
|
|
644
708
|
;
|
|
645
709
|
responseP.cancel = () => {
|
|
@@ -651,6 +715,8 @@ Current date: ${currentDate}`;
|
|
|
651
715
|
message: `${this._manufacturer} timed out waiting for response`
|
|
652
716
|
});
|
|
653
717
|
} else {
|
|
718
|
+
if (this._debug)
|
|
719
|
+
console.log("\u8DDF\u8E2A10");
|
|
654
720
|
return responseP;
|
|
655
721
|
}
|
|
656
722
|
}
|
|
@@ -690,7 +756,8 @@ Current date: ${currentDate}`;
|
|
|
690
756
|
set apiOrg(apiOrg) {
|
|
691
757
|
this._apiOrg = apiOrg;
|
|
692
758
|
}
|
|
693
|
-
async _buildMessages(text, opts) {
|
|
759
|
+
async _buildMessages(text, opts, pluginData) {
|
|
760
|
+
var _a;
|
|
694
761
|
const { systemMessage = this._systemMessage, completionParams } = opts;
|
|
695
762
|
let { parentMessageId } = opts;
|
|
696
763
|
let errorMessage = "";
|
|
@@ -719,12 +786,30 @@ Current date: ${currentDate}`;
|
|
|
719
786
|
}
|
|
720
787
|
}
|
|
721
788
|
const systemMessageOffset = messages.length;
|
|
722
|
-
|
|
723
|
-
|
|
789
|
+
let userMessage = null;
|
|
790
|
+
if (["baidu", "azure", "zhipu", "xunfei", "aliyun", "tencent"].indexOf(this._manufacturer.toLowerCase()) > -1) {
|
|
791
|
+
userMessage = [{ role: "user", content: text }];
|
|
792
|
+
} else if (pluginData) {
|
|
793
|
+
userMessage = {
|
|
794
|
+
role: pluginData.role,
|
|
795
|
+
name: pluginData.name,
|
|
796
|
+
content: JSON.stringify(pluginData.content),
|
|
797
|
+
tool_call_id: pluginData.tool_call_id
|
|
798
|
+
};
|
|
799
|
+
} else {
|
|
800
|
+
userMessage = [{ role: "user", content: text, name: opts.name }];
|
|
801
|
+
}
|
|
802
|
+
let nextMessages = messages;
|
|
803
|
+
if (pluginData || text) {
|
|
804
|
+
nextMessages = nextMessages.concat(userMessage);
|
|
805
|
+
}
|
|
724
806
|
let numTokens = 0;
|
|
725
807
|
do {
|
|
726
808
|
const prompt = nextMessages.reduce((prompt2, message) => {
|
|
727
809
|
switch (message.role) {
|
|
810
|
+
case "tool":
|
|
811
|
+
return prompt2.concat([`Tool:
|
|
812
|
+
function call`]);
|
|
728
813
|
case "system":
|
|
729
814
|
return prompt2.concat([`Instructions:
|
|
730
815
|
${message.content}`]);
|
|
@@ -754,14 +839,20 @@ ${message.content}`]);
|
|
|
754
839
|
break;
|
|
755
840
|
}
|
|
756
841
|
const parentMessageRole = parentMessage.role || "user";
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
content: parentMessage.text
|
|
760
|
-
} : {
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
842
|
+
let parentMessageItem = null;
|
|
843
|
+
if (["baidu", "azure", "zhipu", "xunfei", "aliyun", "tencent"].indexOf(this._manufacturer.toLowerCase()) > -1) {
|
|
844
|
+
parentMessageItem = { role: parentMessageRole, content: parentMessage.text };
|
|
845
|
+
} else if (parentMessage.content && ((_a = parentMessage.content) == null ? void 0 : _a.finish_reason) === "tool_calls") {
|
|
846
|
+
parentMessageItem = parentMessage.content.message;
|
|
847
|
+
} else {
|
|
848
|
+
parentMessageItem = {
|
|
849
|
+
role: parentMessageRole,
|
|
850
|
+
content: parentMessage.text || parentMessage.content,
|
|
851
|
+
name: parentMessage.name
|
|
852
|
+
};
|
|
853
|
+
if (parentMessage.tool_call_id)
|
|
854
|
+
parentMessageItem.tool_call_id = parentMessage.tool_call_id;
|
|
855
|
+
}
|
|
765
856
|
nextMessages = nextMessages.slice(0, systemMessageOffset).concat([
|
|
766
857
|
parentMessageItem,
|
|
767
858
|
...nextMessages.slice(systemMessageOffset)
|