@leikeduntech/leiai-js 4.0.0 → 4.0.2
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/LICENSE.txt +5 -0
- package/build/index.d.ts +1 -1
- package/build/index.js +30 -60
- package/package.json +28 -27
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
Proprietary License
|
|
2
|
+
|
|
3
|
+
This software is proprietary and confidential. Unauthorized copying of this file, via any medium, is strictly prohibited.
|
|
4
|
+
The usage of this software is governed by the terms set out in the license agreement between the user and the software provider.
|
|
5
|
+
© 2023 Shanghai Leikedun Technology Co., Ltd. All rights reserved.
|
package/build/index.d.ts
CHANGED
|
@@ -585,4 +585,4 @@ declare class ChatGPTUnofficialProxyAPI {
|
|
|
585
585
|
sendMessage(text: string, opts?: SendMessageBrowserOptions): Promise<ChatMessage>;
|
|
586
586
|
}
|
|
587
587
|
|
|
588
|
-
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 };
|
|
588
|
+
export { ChatGPTAPI, type ChatGPTAPIOptions, ChatGPTError, ChatGPTUnofficialProxyAPI, type ChatMessage, type ContentType, type ConversationJSONBody, type ConversationResponseEvent, type CreateChatCompletionStreamResponse, type CreateCompletionStreamResponseUsage, type FetchFn, type GetMessageByIdFunction, type Message, type MessageActionType, type MessageContent, type MessageMetadata, type PluginParams, type Prompt, type PromptContent, type Role, type SendMessageBrowserOptions, type SendMessageOptions, type UpsertMessageFunction, openai };
|
package/build/index.js
CHANGED
|
@@ -288,8 +288,7 @@ var ChatGPTAPI = class {
|
|
|
288
288
|
parentMessageId,
|
|
289
289
|
text
|
|
290
290
|
};
|
|
291
|
-
if (this._debug)
|
|
292
|
-
console.log("\u8DDF\u8E2A1, \u8D85\u65F6\u65F6\u95F4ms", timeoutMs);
|
|
291
|
+
if (this._debug) console.log("\u8DDF\u8E2A1, \u8D85\u65F6\u65F6\u95F4ms", timeoutMs);
|
|
293
292
|
let latestQuestion = message, last_assistant_res_message = null, last_assistant_res = null, last_plugin_res = null;
|
|
294
293
|
if (pluginParams) {
|
|
295
294
|
last_assistant_res_message = pluginParams.assistant_res.detail.choices[0];
|
|
@@ -318,8 +317,7 @@ var ChatGPTAPI = class {
|
|
|
318
317
|
await this._upsertMessage(last_assistant_res);
|
|
319
318
|
latestQuestion = last_plugin_res;
|
|
320
319
|
}
|
|
321
|
-
if (this._debug)
|
|
322
|
-
console.log("\u8DDF\u8E2A2");
|
|
320
|
+
if (this._debug) console.log("\u8DDF\u8E2A2");
|
|
323
321
|
const { messages, maxTokens, numTokens, errorMessage, objText } = await this._buildMessages(text, opts, last_plugin_res);
|
|
324
322
|
latestQuestion.text = objText;
|
|
325
323
|
if (this._debug)
|
|
@@ -358,13 +356,11 @@ var ChatGPTAPI = class {
|
|
|
358
356
|
delete headers.Authorization;
|
|
359
357
|
} else if (this._manufacturer.toLowerCase() === "aliyun") {
|
|
360
358
|
url = this._apiBaseUrl;
|
|
361
|
-
if (stream)
|
|
362
|
-
headers["X-DashScope-SSE"] = "enable";
|
|
359
|
+
if (stream) headers["X-DashScope-SSE"] = "enable";
|
|
363
360
|
} else if (this._manufacturer.toLowerCase() === "zhipu") {
|
|
364
361
|
url = this._apiBaseUrl;
|
|
365
362
|
headers["Authorization"] = this._apiKey;
|
|
366
|
-
if (stream)
|
|
367
|
-
headers["accept"] = "text/event-stream";
|
|
363
|
+
if (stream) headers["accept"] = "text/event-stream";
|
|
368
364
|
} else if (this._manufacturer.toLowerCase() === "azure") {
|
|
369
365
|
url = this._apiBaseUrl;
|
|
370
366
|
headers["api-key"] = this._apiKey;
|
|
@@ -396,15 +392,11 @@ var ChatGPTAPI = class {
|
|
|
396
392
|
delete body.max_tokens;
|
|
397
393
|
}
|
|
398
394
|
if (this._manufacturer.toLowerCase() === "baidu" && typeof pluginList === "string" && pluginList.indexOf("zhishiku") > -1) {
|
|
399
|
-
if (body.model)
|
|
400
|
-
|
|
401
|
-
if (body.
|
|
402
|
-
delete body.max_tokens;
|
|
403
|
-
if (body.temperature)
|
|
404
|
-
delete body.temperature;
|
|
395
|
+
if (body.model) delete body.model;
|
|
396
|
+
if (body.max_tokens) delete body.max_tokens;
|
|
397
|
+
if (body.temperature) delete body.temperature;
|
|
405
398
|
} else {
|
|
406
|
-
if (typeof body.plugins !== void 0)
|
|
407
|
-
delete body.plugins;
|
|
399
|
+
if (typeof body.plugins !== void 0) delete body.plugins;
|
|
408
400
|
}
|
|
409
401
|
if (["openai", "azure"].indexOf(this._manufacturer.toLowerCase()) > -1) {
|
|
410
402
|
switch (pluginList) {
|
|
@@ -507,16 +499,11 @@ var ChatGPTAPI = class {
|
|
|
507
499
|
question: (_c = query[0]) == null ? void 0 : _c.content,
|
|
508
500
|
history: messages
|
|
509
501
|
});
|
|
510
|
-
if (body.extParams)
|
|
511
|
-
|
|
512
|
-
if (body.
|
|
513
|
-
|
|
514
|
-
if (body.
|
|
515
|
-
delete body.max_tokens;
|
|
516
|
-
if (body.temperature)
|
|
517
|
-
delete body.temperature;
|
|
518
|
-
if (body.messages)
|
|
519
|
-
delete body.messages;
|
|
502
|
+
if (body.extParams) delete body.extParams;
|
|
503
|
+
if (body.model) delete body.model;
|
|
504
|
+
if (body.max_tokens) delete body.max_tokens;
|
|
505
|
+
if (body.temperature) delete body.temperature;
|
|
506
|
+
if (body.messages) delete body.messages;
|
|
520
507
|
}
|
|
521
508
|
if (this._apiOrg && this._manufacturer.toLowerCase() === "openai") {
|
|
522
509
|
headers["OpenAI-Organization"] = this._apiOrg;
|
|
@@ -542,23 +529,19 @@ var ChatGPTAPI = class {
|
|
|
542
529
|
useHistory: !!parentMessageId,
|
|
543
530
|
chatId: ""
|
|
544
531
|
};
|
|
545
|
-
if (message == null ? void 0 : message.conversationId)
|
|
546
|
-
|
|
547
|
-
if (self._debug)
|
|
548
|
-
console.log("spark options ", options);
|
|
532
|
+
if (message == null ? void 0 : message.conversationId) options.chatId = message == null ? void 0 : message.conversationId;
|
|
533
|
+
if (self._debug) console.log("spark options ", options);
|
|
549
534
|
const sparkIns = new Spark(options);
|
|
550
535
|
const url2 = sparkIns.chat({
|
|
551
536
|
content: messages && JSON.stringify(messages),
|
|
552
537
|
// onData 表示分段拿到返回结果
|
|
553
538
|
onData({ content, start, end, seq }) {
|
|
554
|
-
if (self._debug)
|
|
555
|
-
console.log("onData", content, start, end, seq);
|
|
539
|
+
if (self._debug) console.log("onData", content, start, end, seq);
|
|
556
540
|
result.id = `xunfei-${Math.floor(
|
|
557
541
|
Math.random() * 1e7
|
|
558
542
|
)}${(/* @__PURE__ */ new Date()).getTime()}`;
|
|
559
543
|
result.delta = content;
|
|
560
|
-
if (content)
|
|
561
|
-
result.text += content;
|
|
544
|
+
if (content) result.text += content;
|
|
562
545
|
result.role = "assistant";
|
|
563
546
|
onProgress == null ? void 0 : onProgress(result);
|
|
564
547
|
},
|
|
@@ -664,8 +647,7 @@ var ChatGPTAPI = class {
|
|
|
664
647
|
} else if (delta.content) {
|
|
665
648
|
result.delta = delta.content;
|
|
666
649
|
}
|
|
667
|
-
if (delta == null ? void 0 : delta.content)
|
|
668
|
-
result.text += result.delta;
|
|
650
|
+
if (delta == null ? void 0 : delta.content) result.text += result.delta;
|
|
669
651
|
result.role = "assistant";
|
|
670
652
|
if (response.choices[0].finish_reason === "tool_calls") {
|
|
671
653
|
} else if (delta.role) {
|
|
@@ -675,8 +657,7 @@ var ChatGPTAPI = class {
|
|
|
675
657
|
onProgress == null ? void 0 : onProgress(result);
|
|
676
658
|
} else if ((response == null ? void 0 : response.result) && this._manufacturer.toLowerCase() === "baidu") {
|
|
677
659
|
result.delta = response.result;
|
|
678
|
-
if (response == null ? void 0 : response.result)
|
|
679
|
-
result.text += response.result;
|
|
660
|
+
if (response == null ? void 0 : response.result) result.text += response.result;
|
|
680
661
|
result.role = "assistant";
|
|
681
662
|
result.detail = response;
|
|
682
663
|
onProgress == null ? void 0 : onProgress(result);
|
|
@@ -719,16 +700,14 @@ var ChatGPTAPI = class {
|
|
|
719
700
|
}
|
|
720
701
|
}
|
|
721
702
|
result.delta = response.data;
|
|
722
|
-
if (response == null ? void 0 : response.data)
|
|
723
|
-
result.text += response == null ? void 0 : response.data;
|
|
703
|
+
if (response == null ? void 0 : response.data) result.text += response == null ? void 0 : response.data;
|
|
724
704
|
result.role = "assistant";
|
|
725
705
|
result.detail = response;
|
|
726
706
|
onProgress == null ? void 0 : onProgress(result);
|
|
727
707
|
}
|
|
728
708
|
} else if ((response == null ? void 0 : response.answer) && this._manufacturer.toLowerCase() === "chatdoc") {
|
|
729
709
|
result.delta = response.answer;
|
|
730
|
-
if (response == null ? void 0 : response.answer)
|
|
731
|
-
result.text += response.answer;
|
|
710
|
+
if (response == null ? void 0 : response.answer) result.text += response.answer;
|
|
732
711
|
result.role = "assistant";
|
|
733
712
|
result.detail = response;
|
|
734
713
|
onProgress == null ? void 0 : onProgress(result);
|
|
@@ -747,16 +726,14 @@ var ChatGPTAPI = class {
|
|
|
747
726
|
).catch(reject);
|
|
748
727
|
} else {
|
|
749
728
|
try {
|
|
750
|
-
if (this._debug)
|
|
751
|
-
console.log("\u8DDF\u8E2A3");
|
|
729
|
+
if (this._debug) console.log("\u8DDF\u8E2A3");
|
|
752
730
|
const res = await this._fetch(url, {
|
|
753
731
|
method: "POST",
|
|
754
732
|
headers,
|
|
755
733
|
body: JSON.stringify(body),
|
|
756
734
|
signal: abortSignal
|
|
757
735
|
});
|
|
758
|
-
if (this._debug)
|
|
759
|
-
console.log("\u8DDF\u8E2A4");
|
|
736
|
+
if (this._debug) console.log("\u8DDF\u8E2A4");
|
|
760
737
|
if (!res.ok) {
|
|
761
738
|
const reason = await res.text();
|
|
762
739
|
const msg = `${this._manufacturer} error ${res.status || res.statusText}: ${reason}`;
|
|
@@ -803,12 +780,10 @@ var ChatGPTAPI = class {
|
|
|
803
780
|
);
|
|
804
781
|
}
|
|
805
782
|
result.detail = response;
|
|
806
|
-
if (this._debug)
|
|
807
|
-
console.log("\u8DDF\u8E2A5");
|
|
783
|
+
if (this._debug) console.log("\u8DDF\u8E2A5");
|
|
808
784
|
return resolve(result);
|
|
809
785
|
} catch (err) {
|
|
810
|
-
if (this._debug)
|
|
811
|
-
console.log("\u8DDF\u8E2A6");
|
|
786
|
+
if (this._debug) console.log("\u8DDF\u8E2A6");
|
|
812
787
|
return reject(err);
|
|
813
788
|
}
|
|
814
789
|
}
|
|
@@ -816,8 +791,7 @@ var ChatGPTAPI = class {
|
|
|
816
791
|
}
|
|
817
792
|
).then(async (message2) => {
|
|
818
793
|
var _a, _b, _c, _d, _e;
|
|
819
|
-
if (this._debug)
|
|
820
|
-
console.log("\u8DDF\u8E2A7", JSON.stringify(message2));
|
|
794
|
+
if (this._debug) console.log("\u8DDF\u8E2A7", JSON.stringify(message2));
|
|
821
795
|
if (message2.detail) {
|
|
822
796
|
if (!message2.detail.usage) {
|
|
823
797
|
try {
|
|
@@ -859,11 +833,9 @@ var ChatGPTAPI = class {
|
|
|
859
833
|
]).then(() => message2);
|
|
860
834
|
return pRes;
|
|
861
835
|
});
|
|
862
|
-
if (this._debug)
|
|
863
|
-
console.log("\u8DDF\u8E2A8");
|
|
836
|
+
if (this._debug) console.log("\u8DDF\u8E2A8");
|
|
864
837
|
if (timeoutMs) {
|
|
865
|
-
if (this._debug)
|
|
866
|
-
console.log("\u8DDF\u8E2A9");
|
|
838
|
+
if (this._debug) console.log("\u8DDF\u8E2A9");
|
|
867
839
|
if (abortController) {
|
|
868
840
|
;
|
|
869
841
|
responseP.cancel = () => {
|
|
@@ -876,8 +848,7 @@ var ChatGPTAPI = class {
|
|
|
876
848
|
message: `${this._manufacturer} timed out waiting for response`
|
|
877
849
|
});
|
|
878
850
|
} else {
|
|
879
|
-
if (this._debug)
|
|
880
|
-
console.log("\u8DDF\u8E2A10");
|
|
851
|
+
if (this._debug) console.log("\u8DDF\u8E2A10");
|
|
881
852
|
return responseP;
|
|
882
853
|
}
|
|
883
854
|
}
|
|
@@ -1047,8 +1018,7 @@ ${message.content}`]);
|
|
|
1047
1018
|
parentMessageItem.tool_call_id = parentMessage.tool_call_id;
|
|
1048
1019
|
}
|
|
1049
1020
|
nextMessages = nextMessages.slice(0, systemMessageOffset).concat([parentMessageItem, ...nextMessages.slice(systemMessageOffset)]);
|
|
1050
|
-
if (nextMessages.length >= contextRestriction)
|
|
1051
|
-
break;
|
|
1021
|
+
if (nextMessages.length >= contextRestriction) break;
|
|
1052
1022
|
parentMessageId = parentMessage.parentMessageId;
|
|
1053
1023
|
} while (true);
|
|
1054
1024
|
let maxTokens = this._maxModelTokens;
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leikeduntech/leiai-js",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.2",
|
|
4
4
|
"author": "liuhean",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git+https://github.com/liuhean2021/leiai-js.git"
|
|
8
8
|
},
|
|
9
|
-
"license": "
|
|
9
|
+
"license": "Proprietary",
|
|
10
10
|
"type": "module",
|
|
11
11
|
"source": "./src/index.ts",
|
|
12
12
|
"types": "./build/index.d.ts",
|
|
@@ -32,7 +32,8 @@
|
|
|
32
32
|
},
|
|
33
33
|
"files": [
|
|
34
34
|
"build",
|
|
35
|
-
"bin"
|
|
35
|
+
"bin",
|
|
36
|
+
"LICENSE.txt"
|
|
36
37
|
],
|
|
37
38
|
"bin": {
|
|
38
39
|
"leiai-js": "bin/cli.js"
|
|
@@ -43,33 +44,33 @@
|
|
|
43
44
|
"dependencies": {
|
|
44
45
|
"@leikeduntech/spark-nodejs": "1.1.0",
|
|
45
46
|
"cac": "^6.7.14",
|
|
46
|
-
"conf": "^
|
|
47
|
-
"crypto-js": "^4.
|
|
48
|
-
"eventsource-parser": "^1.
|
|
49
|
-
"js-tiktoken": "^1.0.
|
|
50
|
-
"keyv": "^4.5.
|
|
51
|
-
"p-timeout": "^6.1.
|
|
52
|
-
"quick-lru": "^
|
|
53
|
-
"read-pkg-up": "^
|
|
54
|
-
"uuid": "^
|
|
47
|
+
"conf": "^13.0.1",
|
|
48
|
+
"crypto-js": "^4.2.0",
|
|
49
|
+
"eventsource-parser": "^1.1.2",
|
|
50
|
+
"js-tiktoken": "^1.0.12",
|
|
51
|
+
"keyv": "^4.5.4",
|
|
52
|
+
"p-timeout": "^6.1.2",
|
|
53
|
+
"quick-lru": "^7.0.0",
|
|
54
|
+
"read-pkg-up": "^10.1.0",
|
|
55
|
+
"uuid": "^10.0.0"
|
|
55
56
|
},
|
|
56
57
|
"devDependencies": {
|
|
57
|
-
"@keyv/redis": "^2.5
|
|
58
|
-
"@trivago/prettier-plugin-sort-imports": "^4.
|
|
59
|
-
"@types/node": "^
|
|
60
|
-
"@types/uuid": "^
|
|
61
|
-
"del-cli": "^5.
|
|
62
|
-
"dotenv-safe": "^
|
|
63
|
-
"husky": "^
|
|
64
|
-
"lint-staged": "^
|
|
58
|
+
"@keyv/redis": "^2.8.5",
|
|
59
|
+
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
|
|
60
|
+
"@types/node": "^20.14.9",
|
|
61
|
+
"@types/uuid": "^10.0.0",
|
|
62
|
+
"del-cli": "^5.1.0",
|
|
63
|
+
"dotenv-safe": "^9.1.0",
|
|
64
|
+
"husky": "^9.0.11",
|
|
65
|
+
"lint-staged": "^15.2.7",
|
|
65
66
|
"npm-run-all": "^4.1.5",
|
|
66
|
-
"ora": "^
|
|
67
|
-
"prettier": "^
|
|
68
|
-
"tsup": "^
|
|
69
|
-
"tsx": "^
|
|
70
|
-
"typedoc": "^0.
|
|
71
|
-
"typedoc-plugin-markdown": "^
|
|
72
|
-
"typescript": "^5.
|
|
67
|
+
"ora": "^8.0.1",
|
|
68
|
+
"prettier": "^3.3.2",
|
|
69
|
+
"tsup": "^8.1.0",
|
|
70
|
+
"tsx": "^4.15.7",
|
|
71
|
+
"typedoc": "^0.26.2",
|
|
72
|
+
"typedoc-plugin-markdown": "^4.1.0",
|
|
73
|
+
"typescript": "^5.5.2"
|
|
73
74
|
},
|
|
74
75
|
"lint-staged": {
|
|
75
76
|
"*.{ts,tsx}": [
|