@leikeduntech/leiai-js 2.4.0 → 2.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.js +59 -20
- package/package.json +3 -2
package/build/index.js
CHANGED
|
@@ -108,6 +108,37 @@ 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 signTencentHunyuan(bodyData, url, keyList) {
|
|
119
|
+
const sortedObj = {};
|
|
120
|
+
Object.keys(bodyData).sort().forEach((key) => {
|
|
121
|
+
sortedObj[key] = bodyData[key];
|
|
122
|
+
});
|
|
123
|
+
let signStr = "";
|
|
124
|
+
for (let key in sortedObj) {
|
|
125
|
+
if (signStr) {
|
|
126
|
+
if (typeof sortedObj[key] === "object") {
|
|
127
|
+
signStr += `&${key}=${JSON.stringify(sortedObj[key])}`;
|
|
128
|
+
} else {
|
|
129
|
+
signStr += `&${key}=${sortedObj[key]}`;
|
|
130
|
+
}
|
|
131
|
+
} else {
|
|
132
|
+
signStr += `${key}=${sortedObj[key]}`;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
signStr = `${url.replace("https://", "")}?${signStr}`;
|
|
136
|
+
const hmac = CryptoJS.HmacSHA1(signStr, keyList[2]);
|
|
137
|
+
const signBase64 = CryptoJS.enc.Base64.stringify(hmac);
|
|
138
|
+
return signBase64;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// src/chatgpt-api.ts
|
|
111
142
|
var CHATGPT_MODEL = "gpt-3.5-turbo";
|
|
112
143
|
var USER_LABEL_DEFAULT = "User";
|
|
113
144
|
var ASSISTANT_LABEL_DEFAULT = "ChatGPT";
|
|
@@ -295,15 +326,23 @@ Current date: ${currentDate}`;
|
|
|
295
326
|
} else if (this._manufacturer.toLowerCase() === "zhipu") {
|
|
296
327
|
delete body.messages;
|
|
297
328
|
body = Object.assign(body, { prompt: messages });
|
|
329
|
+
} else if (this._manufacturer.toLowerCase() === "tencent") {
|
|
330
|
+
url = this._apiBaseUrl;
|
|
331
|
+
const timestamp = Math.ceil((/* @__PURE__ */ new Date()).getTime() / 1e3) + 1;
|
|
332
|
+
const keyList = this._apiKey.split(".");
|
|
333
|
+
body = Object.assign(body, { app_id: parseInt(keyList[0]), secret_id: keyList[1], timestamp, expired: timestamp + 24 * 60 * 60, stream: stream ? 1 : 0 });
|
|
334
|
+
delete body.model;
|
|
335
|
+
delete body.max_tokens;
|
|
336
|
+
headers["Authorization"] = signTencentHunyuan(body, url, keyList);
|
|
298
337
|
}
|
|
299
338
|
if (this._apiOrg && this._manufacturer.toLowerCase() === "openai") {
|
|
300
339
|
headers["OpenAI-Organization"] = this._apiOrg;
|
|
301
340
|
}
|
|
302
341
|
if (this._debug) {
|
|
303
|
-
console.log(`api url
|
|
304
|
-
console.log(`api header
|
|
342
|
+
console.log(`api url ${url}`);
|
|
343
|
+
console.log(`api header ${JSON.stringify(headers)}`);
|
|
305
344
|
console.log(`sendMessage (${numTokens} tokens) body: `, body);
|
|
306
|
-
console.log(`sendMessage (${numTokens} tokens) message : `, messages);
|
|
345
|
+
console.log(`sendMessage (${numTokens} tokens) message : `, messages, typeof messages);
|
|
307
346
|
}
|
|
308
347
|
if (this._manufacturer.toLowerCase() === "xunfei") {
|
|
309
348
|
const self = this;
|
|
@@ -352,7 +391,7 @@ Current date: ${currentDate}`;
|
|
|
352
391
|
body: JSON.stringify(body),
|
|
353
392
|
signal: abortSignal,
|
|
354
393
|
onMessage: (data) => {
|
|
355
|
-
var _a3, _b2, _c2, _d2, _e2, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s;
|
|
394
|
+
var _a3, _b2, _c2, _d2, _e2, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v;
|
|
356
395
|
if (this._debug) {
|
|
357
396
|
}
|
|
358
397
|
if (data === "[DONE]") {
|
|
@@ -361,6 +400,8 @@ Current date: ${currentDate}`;
|
|
|
361
400
|
}
|
|
362
401
|
try {
|
|
363
402
|
const response = JSON.parse(data);
|
|
403
|
+
if (this._debug) {
|
|
404
|
+
}
|
|
364
405
|
if (this._manufacturer.toLowerCase() === "baidu") {
|
|
365
406
|
if ((response == null ? void 0 : response.is_end) === true) {
|
|
366
407
|
result.text += response.result.trim();
|
|
@@ -382,6 +423,11 @@ Current date: ${currentDate}`;
|
|
|
382
423
|
result.text += response == null ? void 0 : response.data.trim();
|
|
383
424
|
return resolve(result);
|
|
384
425
|
}
|
|
426
|
+
} else if (this._manufacturer.toLowerCase() === "tencent") {
|
|
427
|
+
if (((_g = response.choices[0]) == null ? void 0 : _g.finish_reason) === "stop") {
|
|
428
|
+
result.text += (_i = (_h = response == null ? void 0 : response.choices[0]) == null ? void 0 : _h.delta) == null ? void 0 : _i.content.trim();
|
|
429
|
+
return resolve(result);
|
|
430
|
+
}
|
|
385
431
|
}
|
|
386
432
|
if (this._manufacturer.toLowerCase() === "aliyun") {
|
|
387
433
|
if (response == null ? void 0 : response.request_id) {
|
|
@@ -392,11 +438,12 @@ Current date: ${currentDate}`;
|
|
|
392
438
|
result.id = response.id;
|
|
393
439
|
}
|
|
394
440
|
}
|
|
395
|
-
if (((
|
|
441
|
+
if (((_j = response.choices) == null ? void 0 : _j.length) && ["openai", "azure", "tencent"].indexOf(this._manufacturer.toLowerCase()) > -1) {
|
|
396
442
|
const delta = response.choices[0].delta;
|
|
397
443
|
result.delta = delta.content;
|
|
398
444
|
if (delta == null ? void 0 : delta.content)
|
|
399
445
|
result.text += delta.content;
|
|
446
|
+
result.role = "assistant";
|
|
400
447
|
if (delta.role) {
|
|
401
448
|
result.role = delta.role;
|
|
402
449
|
}
|
|
@@ -410,17 +457,17 @@ Current date: ${currentDate}`;
|
|
|
410
457
|
result.detail = response;
|
|
411
458
|
onProgress == null ? void 0 : onProgress(result);
|
|
412
459
|
} else if ((response == null ? void 0 : response.output) && this._manufacturer.toLowerCase() === "aliyun") {
|
|
413
|
-
response.usage = Object.assign(response.usage, { prompt_tokens: (
|
|
460
|
+
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
461
|
result.delta = "";
|
|
415
|
-
if ((
|
|
416
|
-
result.text = (
|
|
462
|
+
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)
|
|
463
|
+
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
464
|
result.role = "assistant";
|
|
418
465
|
result.detail = response;
|
|
419
466
|
onProgress == null ? void 0 : onProgress(result);
|
|
420
467
|
} else if ((response == null ? void 0 : response.data) && this._manufacturer.toLowerCase() === "zhipu") {
|
|
421
468
|
if (response.event === "finish") {
|
|
422
|
-
if ((
|
|
423
|
-
response.usage = (
|
|
469
|
+
if ((_u = response == null ? void 0 : response.meta) == null ? void 0 : _u.usage) {
|
|
470
|
+
response.usage = (_v = response == null ? void 0 : response.meta) == null ? void 0 : _v.usage;
|
|
424
471
|
} else {
|
|
425
472
|
response.usage = { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 };
|
|
426
473
|
}
|
|
@@ -589,7 +636,7 @@ Current date: ${currentDate}`;
|
|
|
589
636
|
});
|
|
590
637
|
}
|
|
591
638
|
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 }];
|
|
639
|
+
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
640
|
let nextMessages = text ? messages.concat(userMessage) : messages;
|
|
594
641
|
let numTokens = 0;
|
|
595
642
|
do {
|
|
@@ -624,7 +671,7 @@ ${message.content}`]);
|
|
|
624
671
|
break;
|
|
625
672
|
}
|
|
626
673
|
const parentMessageRole = parentMessage.role || "user";
|
|
627
|
-
const parentMessageItem = ["baidu", "azure", "zhipu", "xunfei"].indexOf(this._manufacturer.toLowerCase()) > -1 ? {
|
|
674
|
+
const parentMessageItem = ["baidu", "azure", "zhipu", "xunfei", "aliyun", "tencent"].indexOf(this._manufacturer.toLowerCase()) > -1 ? {
|
|
628
675
|
role: parentMessageRole,
|
|
629
676
|
content: parentMessage.text
|
|
630
677
|
} : {
|
|
@@ -666,14 +713,6 @@ ${message.content}`]);
|
|
|
666
713
|
// src/chatgpt-unofficial-proxy-api.ts
|
|
667
714
|
import pTimeout2 from "p-timeout";
|
|
668
715
|
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
716
|
var ChatGPTUnofficialProxyAPI = class {
|
|
678
717
|
/**
|
|
679
718
|
* @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.0",
|
|
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": {
|