@leikeduntech/leiai-js 2.4.0 → 2.5.1
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.js +77 -20
- package/package.json +3 -2
package/build/index.js
CHANGED
|
@@ -108,6 +108,55 @@ async function fetchSSE(url, options, fetch2 = fetch, manufacturer = "OpenAI") {
|
|
|
108
108
|
|
|
109
109
|
// src/chatgpt-api.ts
|
|
110
110
|
import { Spark } from "@leikeduntech/spark-nodejs";
|
|
111
|
+
|
|
112
|
+
// src/utils.ts
|
|
113
|
+
import CryptoJS from "crypto-js";
|
|
114
|
+
var uuidv4Re = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
115
|
+
function isValidUUIDv4(str) {
|
|
116
|
+
return str && uuidv4Re.test(str);
|
|
117
|
+
}
|
|
118
|
+
function convertArrayToString(arr) {
|
|
119
|
+
let result = "[";
|
|
120
|
+
for (let i = 0; i < arr.length; i++) {
|
|
121
|
+
const obj = arr[i];
|
|
122
|
+
const keys = Object.keys(obj);
|
|
123
|
+
result += "{";
|
|
124
|
+
for (let j = 0; j < keys.length; j++) {
|
|
125
|
+
const key = keys[j];
|
|
126
|
+
const value = obj[key];
|
|
127
|
+
result += '"' + key + '":' + (typeof value === "string" ? '"' + value + '"' : value) + ",";
|
|
128
|
+
}
|
|
129
|
+
result = result.slice(0, -1);
|
|
130
|
+
result += "},";
|
|
131
|
+
}
|
|
132
|
+
result = result.slice(0, -1);
|
|
133
|
+
result += "]";
|
|
134
|
+
return result;
|
|
135
|
+
}
|
|
136
|
+
function signTencentHunyuan(bodyData, url, keyList) {
|
|
137
|
+
const sortedObj = {};
|
|
138
|
+
Object.keys(bodyData).sort().forEach((key) => {
|
|
139
|
+
sortedObj[key] = bodyData[key];
|
|
140
|
+
});
|
|
141
|
+
let signStr = "";
|
|
142
|
+
for (let key in sortedObj) {
|
|
143
|
+
if (signStr) {
|
|
144
|
+
if (typeof sortedObj[key] === "object") {
|
|
145
|
+
signStr += `&${key}=${convertArrayToString(sortedObj[key])}`;
|
|
146
|
+
} else {
|
|
147
|
+
signStr += `&${key}=${sortedObj[key]}`;
|
|
148
|
+
}
|
|
149
|
+
} else {
|
|
150
|
+
signStr += `${key}=${sortedObj[key]}`;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
signStr = `${url.replace("https://", "")}?${signStr}`;
|
|
154
|
+
const hmac = CryptoJS.HmacSHA1(signStr, keyList[2]);
|
|
155
|
+
const signBase64 = CryptoJS.enc.Base64.stringify(hmac);
|
|
156
|
+
return signBase64;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// src/chatgpt-api.ts
|
|
111
160
|
var CHATGPT_MODEL = "gpt-3.5-turbo";
|
|
112
161
|
var USER_LABEL_DEFAULT = "User";
|
|
113
162
|
var ASSISTANT_LABEL_DEFAULT = "ChatGPT";
|
|
@@ -295,15 +344,23 @@ Current date: ${currentDate}`;
|
|
|
295
344
|
} else if (this._manufacturer.toLowerCase() === "zhipu") {
|
|
296
345
|
delete body.messages;
|
|
297
346
|
body = Object.assign(body, { prompt: messages });
|
|
347
|
+
} else if (this._manufacturer.toLowerCase() === "tencent") {
|
|
348
|
+
url = this._apiBaseUrl;
|
|
349
|
+
const timestamp = Math.ceil((/* @__PURE__ */ new Date()).getTime() / 1e3) + 1;
|
|
350
|
+
const keyList = this._apiKey.split(".");
|
|
351
|
+
body = Object.assign(body, { app_id: parseInt(keyList[0]), secret_id: keyList[1], timestamp, expired: timestamp + 24 * 60 * 60, stream: stream ? 1 : 0 });
|
|
352
|
+
delete body.model;
|
|
353
|
+
delete body.max_tokens;
|
|
354
|
+
headers["Authorization"] = signTencentHunyuan(body, url, keyList);
|
|
298
355
|
}
|
|
299
356
|
if (this._apiOrg && this._manufacturer.toLowerCase() === "openai") {
|
|
300
357
|
headers["OpenAI-Organization"] = this._apiOrg;
|
|
301
358
|
}
|
|
302
359
|
if (this._debug) {
|
|
303
|
-
console.log(`api url
|
|
304
|
-
console.log(`api header
|
|
360
|
+
console.log(`api url ${url}`);
|
|
361
|
+
console.log(`api header ${JSON.stringify(headers)}`);
|
|
305
362
|
console.log(`sendMessage (${numTokens} tokens) body: `, body);
|
|
306
|
-
console.log(`sendMessage (${numTokens} tokens) message : `, messages);
|
|
363
|
+
console.log(`sendMessage (${numTokens} tokens) message : `, messages, typeof messages);
|
|
307
364
|
}
|
|
308
365
|
if (this._manufacturer.toLowerCase() === "xunfei") {
|
|
309
366
|
const self = this;
|
|
@@ -352,7 +409,7 @@ Current date: ${currentDate}`;
|
|
|
352
409
|
body: JSON.stringify(body),
|
|
353
410
|
signal: abortSignal,
|
|
354
411
|
onMessage: (data) => {
|
|
355
|
-
var _a3, _b2, _c2, _d2, _e2, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s;
|
|
412
|
+
var _a3, _b2, _c2, _d2, _e2, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v;
|
|
356
413
|
if (this._debug) {
|
|
357
414
|
}
|
|
358
415
|
if (data === "[DONE]") {
|
|
@@ -361,6 +418,8 @@ Current date: ${currentDate}`;
|
|
|
361
418
|
}
|
|
362
419
|
try {
|
|
363
420
|
const response = JSON.parse(data);
|
|
421
|
+
if (this._debug) {
|
|
422
|
+
}
|
|
364
423
|
if (this._manufacturer.toLowerCase() === "baidu") {
|
|
365
424
|
if ((response == null ? void 0 : response.is_end) === true) {
|
|
366
425
|
result.text += response.result.trim();
|
|
@@ -382,6 +441,11 @@ Current date: ${currentDate}`;
|
|
|
382
441
|
result.text += response == null ? void 0 : response.data.trim();
|
|
383
442
|
return resolve(result);
|
|
384
443
|
}
|
|
444
|
+
} else if (this._manufacturer.toLowerCase() === "tencent") {
|
|
445
|
+
if (((_g = response.choices[0]) == null ? void 0 : _g.finish_reason) === "stop") {
|
|
446
|
+
result.text += (_i = (_h = response == null ? void 0 : response.choices[0]) == null ? void 0 : _h.delta) == null ? void 0 : _i.content.trim();
|
|
447
|
+
return resolve(result);
|
|
448
|
+
}
|
|
385
449
|
}
|
|
386
450
|
if (this._manufacturer.toLowerCase() === "aliyun") {
|
|
387
451
|
if (response == null ? void 0 : response.request_id) {
|
|
@@ -392,11 +456,12 @@ Current date: ${currentDate}`;
|
|
|
392
456
|
result.id = response.id;
|
|
393
457
|
}
|
|
394
458
|
}
|
|
395
|
-
if (((
|
|
459
|
+
if (((_j = response.choices) == null ? void 0 : _j.length) && ["openai", "azure", "tencent"].indexOf(this._manufacturer.toLowerCase()) > -1) {
|
|
396
460
|
const delta = response.choices[0].delta;
|
|
397
461
|
result.delta = delta.content;
|
|
398
462
|
if (delta == null ? void 0 : delta.content)
|
|
399
463
|
result.text += delta.content;
|
|
464
|
+
result.role = "assistant";
|
|
400
465
|
if (delta.role) {
|
|
401
466
|
result.role = delta.role;
|
|
402
467
|
}
|
|
@@ -410,17 +475,17 @@ Current date: ${currentDate}`;
|
|
|
410
475
|
result.detail = response;
|
|
411
476
|
onProgress == null ? void 0 : onProgress(result);
|
|
412
477
|
} else if ((response == null ? void 0 : response.output) && this._manufacturer.toLowerCase() === "aliyun") {
|
|
413
|
-
response.usage = Object.assign(response.usage, { prompt_tokens: (
|
|
478
|
+
response.usage = Object.assign(response.usage, { prompt_tokens: (_k = response.usage) == null ? void 0 : _k.input_tokens, completion_tokens: (_l = response.usage) == null ? void 0 : _l.output_tokens, total_tokens: ((_m = response.usage) == null ? void 0 : _m.input_tokens) + ((_n = response.usage) == null ? void 0 : _n.output_tokens) });
|
|
414
479
|
result.delta = "";
|
|
415
|
-
if ((
|
|
416
|
-
result.text = (
|
|
480
|
+
if ((_q = (_p = (_o = response == null ? void 0 : response.output) == null ? void 0 : _o.choices[0]) == null ? void 0 : _p.message) == null ? void 0 : _q.content)
|
|
481
|
+
result.text = (_t = (_s = (_r = response == null ? void 0 : response.output) == null ? void 0 : _r.choices[0]) == null ? void 0 : _s.message) == null ? void 0 : _t.content;
|
|
417
482
|
result.role = "assistant";
|
|
418
483
|
result.detail = response;
|
|
419
484
|
onProgress == null ? void 0 : onProgress(result);
|
|
420
485
|
} else if ((response == null ? void 0 : response.data) && this._manufacturer.toLowerCase() === "zhipu") {
|
|
421
486
|
if (response.event === "finish") {
|
|
422
|
-
if ((
|
|
423
|
-
response.usage = (
|
|
487
|
+
if ((_u = response == null ? void 0 : response.meta) == null ? void 0 : _u.usage) {
|
|
488
|
+
response.usage = (_v = response == null ? void 0 : response.meta) == null ? void 0 : _v.usage;
|
|
424
489
|
} else {
|
|
425
490
|
response.usage = { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 };
|
|
426
491
|
}
|
|
@@ -589,7 +654,7 @@ Current date: ${currentDate}`;
|
|
|
589
654
|
});
|
|
590
655
|
}
|
|
591
656
|
const systemMessageOffset = messages.length;
|
|
592
|
-
const userMessage = ["baidu", "azure", "zhipu", "xunfei", "aliyun"].indexOf(this._manufacturer.toLowerCase()) > -1 ? [{ role: "user", content: text }] : [{ role: "user", content: text, name: opts.name }];
|
|
657
|
+
const userMessage = ["baidu", "azure", "zhipu", "xunfei", "aliyun", "tencent"].indexOf(this._manufacturer.toLowerCase()) > -1 ? [{ role: "user", content: text }] : [{ role: "user", content: text, name: opts.name }];
|
|
593
658
|
let nextMessages = text ? messages.concat(userMessage) : messages;
|
|
594
659
|
let numTokens = 0;
|
|
595
660
|
do {
|
|
@@ -624,7 +689,7 @@ ${message.content}`]);
|
|
|
624
689
|
break;
|
|
625
690
|
}
|
|
626
691
|
const parentMessageRole = parentMessage.role || "user";
|
|
627
|
-
const parentMessageItem = ["baidu", "azure", "zhipu", "xunfei"].indexOf(this._manufacturer.toLowerCase()) > -1 ? {
|
|
692
|
+
const parentMessageItem = ["baidu", "azure", "zhipu", "xunfei", "aliyun", "tencent"].indexOf(this._manufacturer.toLowerCase()) > -1 ? {
|
|
628
693
|
role: parentMessageRole,
|
|
629
694
|
content: parentMessage.text
|
|
630
695
|
} : {
|
|
@@ -666,14 +731,6 @@ ${message.content}`]);
|
|
|
666
731
|
// src/chatgpt-unofficial-proxy-api.ts
|
|
667
732
|
import pTimeout2 from "p-timeout";
|
|
668
733
|
import { v4 as uuidv42 } from "uuid";
|
|
669
|
-
|
|
670
|
-
// src/utils.ts
|
|
671
|
-
var uuidv4Re = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
672
|
-
function isValidUUIDv4(str) {
|
|
673
|
-
return str && uuidv4Re.test(str);
|
|
674
|
-
}
|
|
675
|
-
|
|
676
|
-
// src/chatgpt-unofficial-proxy-api.ts
|
|
677
734
|
var ChatGPTUnofficialProxyAPI = class {
|
|
678
735
|
/**
|
|
679
736
|
* @param fetch - Optional override for the `fetch` implementation to use. Defaults to the global `fetch` function.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leikeduntech/leiai-js",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.5.1",
|
|
4
4
|
"author": "liuhean",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -41,15 +41,16 @@
|
|
|
41
41
|
"node": ">=14"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
+
"@leikeduntech/spark-nodejs": "0.3.3",
|
|
44
45
|
"cac": "^6.7.14",
|
|
45
46
|
"conf": "^11.0.1",
|
|
47
|
+
"crypto-js": "^4.1.1",
|
|
46
48
|
"eventsource-parser": "^1.0.0",
|
|
47
49
|
"js-tiktoken": "^1.0.5",
|
|
48
50
|
"keyv": "^4.5.2",
|
|
49
51
|
"p-timeout": "^6.1.1",
|
|
50
52
|
"quick-lru": "^6.1.1",
|
|
51
53
|
"read-pkg-up": "^9.1.0",
|
|
52
|
-
"@leikeduntech/spark-nodejs": "0.3.3",
|
|
53
54
|
"uuid": "^9.0.0"
|
|
54
55
|
},
|
|
55
56
|
"devDependencies": {
|