@yxw007/translate 0.1.1 → 0.1.3
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/dist/browser/index.cjs +91 -71
- package/dist/browser/index.cjs.map +1 -1
- package/dist/browser/index.esm.js +91 -71
- package/dist/browser/index.esm.js.map +1 -1
- package/dist/browser/index.esm.min.js +1 -1
- package/dist/browser/index.esm.min.js.map +1 -1
- package/dist/browser/index.min.cjs +1 -1
- package/dist/browser/index.min.cjs.map +1 -1
- package/dist/browser/index.umd.js +91 -71
- package/dist/browser/index.umd.js.map +1 -1
- package/dist/browser/index.umd.min.js +1 -1
- package/dist/browser/index.umd.min.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/node/index.cjs +91 -71
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.js +91 -71
- package/dist/node/index.js.map +1 -1
- package/dist/package.json +1 -1
- package/package.json +1 -1
package/dist/browser/index.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// translate v0.1.
|
|
1
|
+
// translate v0.1.3 Copyright (c) 2024 Potter<aa4790139@gmail.com> and contributors
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
4
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
@@ -1375,76 +1375,6 @@ function deepl$2(options) {
|
|
|
1375
1375
|
};
|
|
1376
1376
|
}
|
|
1377
1377
|
|
|
1378
|
-
function openai$1(options) {
|
|
1379
|
-
const { apiKey, model } = options;
|
|
1380
|
-
const name = "openai";
|
|
1381
|
-
const checkOptions = () => {
|
|
1382
|
-
if (!apiKey) {
|
|
1383
|
-
throw new TranslationError(name, `${name} apiKey is required`);
|
|
1384
|
-
}
|
|
1385
|
-
if (!OPEN_AI_MODELS.includes(model)) {
|
|
1386
|
-
throw new TranslationError(name, `${name} model=${model} is invalid`);
|
|
1387
|
-
}
|
|
1388
|
-
};
|
|
1389
|
-
checkOptions();
|
|
1390
|
-
const base = "https://api.openai.com/v1/chat/completions";
|
|
1391
|
-
return {
|
|
1392
|
-
name,
|
|
1393
|
-
async translate(text, opts) {
|
|
1394
|
-
checkOptions();
|
|
1395
|
-
const { from, to } = opts;
|
|
1396
|
-
const url = `${base}`;
|
|
1397
|
-
if (!Array.isArray(text)) {
|
|
1398
|
-
text = [text];
|
|
1399
|
-
}
|
|
1400
|
-
const prompt = {
|
|
1401
|
-
role: "user",
|
|
1402
|
-
content: `Translate the following texts from ${from} to ${to}:
|
|
1403
|
-
-$s$-
|
|
1404
|
-
${text.join("\n")}
|
|
1405
|
-
-$e$-
|
|
1406
|
-
Translated content is between the start marker -$s$- and the end marker -$e$-, do not return the start and end markers, only the translated text is returned.
|
|
1407
|
-
Connect multiple text with newline character, keep the original order when return.
|
|
1408
|
-
`,
|
|
1409
|
-
};
|
|
1410
|
-
const res = await fetch(url, {
|
|
1411
|
-
method: "POST",
|
|
1412
|
-
headers: {
|
|
1413
|
-
"Content-Type": "application/json",
|
|
1414
|
-
Authorization: `Bearer ${apiKey}`,
|
|
1415
|
-
},
|
|
1416
|
-
body: JSON.stringify({
|
|
1417
|
-
model,
|
|
1418
|
-
messages: [{ role: "system", content: "You are a professional IT translator" }, prompt],
|
|
1419
|
-
max_tokens: 2000,
|
|
1420
|
-
}),
|
|
1421
|
-
});
|
|
1422
|
-
const bodyRes = await res.json();
|
|
1423
|
-
if (bodyRes.error) {
|
|
1424
|
-
throw new TranslationError(this.name, `Translate fail! message: ${bodyRes.error.message}`);
|
|
1425
|
-
}
|
|
1426
|
-
if (!bodyRes || !bodyRes.choices || bodyRes.choices.length === 0 || !bodyRes.choices[0]?.message?.content) {
|
|
1427
|
-
throw new TranslationError(this.name, "Translate fail ! translate's result is null or empty");
|
|
1428
|
-
}
|
|
1429
|
-
const content = bodyRes.choices[0].message.content;
|
|
1430
|
-
const translations = content
|
|
1431
|
-
.trim()
|
|
1432
|
-
.split("\n")
|
|
1433
|
-
.map((item) => item.trim());
|
|
1434
|
-
return translations;
|
|
1435
|
-
},
|
|
1436
|
-
};
|
|
1437
|
-
}
|
|
1438
|
-
|
|
1439
|
-
const engines = {
|
|
1440
|
-
google,
|
|
1441
|
-
azure: azure$1,
|
|
1442
|
-
amazon: amazon$1,
|
|
1443
|
-
baidu: baidu$1,
|
|
1444
|
-
deepl: deepl$2,
|
|
1445
|
-
openai: openai$1,
|
|
1446
|
-
};
|
|
1447
|
-
|
|
1448
1378
|
class Cache {
|
|
1449
1379
|
cache;
|
|
1450
1380
|
constructor() {
|
|
@@ -1532,6 +1462,96 @@ function getErrorMessages(e, prefix = "Translate fail ! ") {
|
|
|
1532
1462
|
return prefix + e.message;
|
|
1533
1463
|
}
|
|
1534
1464
|
|
|
1465
|
+
const logger$1 = useLogger("openai");
|
|
1466
|
+
function openai$1(options) {
|
|
1467
|
+
const { apiKey, model, maxTokens = 2000, outputLog = false } = options;
|
|
1468
|
+
const name = "openai";
|
|
1469
|
+
const checkOptions = () => {
|
|
1470
|
+
if (!apiKey) {
|
|
1471
|
+
throw new TranslationError(name, `${name} apiKey is required`);
|
|
1472
|
+
}
|
|
1473
|
+
if (!OPEN_AI_MODELS.includes(model)) {
|
|
1474
|
+
throw new TranslationError(name, `${name} model=${model} is invalid`);
|
|
1475
|
+
}
|
|
1476
|
+
};
|
|
1477
|
+
checkOptions();
|
|
1478
|
+
const base = "https://api.openai.com/v1/chat/completions";
|
|
1479
|
+
return {
|
|
1480
|
+
name,
|
|
1481
|
+
async translate(text, opts) {
|
|
1482
|
+
checkOptions();
|
|
1483
|
+
const { from, to } = opts;
|
|
1484
|
+
const url = `${base}`;
|
|
1485
|
+
if (!Array.isArray(text)) {
|
|
1486
|
+
text = [text];
|
|
1487
|
+
}
|
|
1488
|
+
const prompt = {
|
|
1489
|
+
role: "user",
|
|
1490
|
+
content: `
|
|
1491
|
+
翻译要求:
|
|
1492
|
+
1.将每段文本从${from}翻译为${to}
|
|
1493
|
+
2.文本内容以换行符\n进行段落分割,并以段落分割顺序进行翻译
|
|
1494
|
+
3.仅翻译分割出的段落,其他任何不相关的内容都移除,比如:段落前后的空格、所有标点符号
|
|
1495
|
+
4.仅返回要翻译的文本内容,不要返回任何其他内容
|
|
1496
|
+
|
|
1497
|
+
|
|
1498
|
+
如何提取翻译文本:
|
|
1499
|
+
1.翻译从-$s$-字符标记开始至-$e$-字符标记结束,提取-$s$-至-$e$-之间的内容
|
|
1500
|
+
2.-$s$-和-$e$-这2个标记不要返回,只是用来标记翻译的起始和结束位置
|
|
1501
|
+
|
|
1502
|
+
-$s$-
|
|
1503
|
+
${text.join("\n")}
|
|
1504
|
+
-$e$-
|
|
1505
|
+
`,
|
|
1506
|
+
};
|
|
1507
|
+
if (outputLog) {
|
|
1508
|
+
logger$1.info("prompt:", prompt);
|
|
1509
|
+
}
|
|
1510
|
+
const res = await fetch(url, {
|
|
1511
|
+
method: "POST",
|
|
1512
|
+
headers: {
|
|
1513
|
+
"Content-Type": "application/json",
|
|
1514
|
+
Authorization: `Bearer ${apiKey}`,
|
|
1515
|
+
},
|
|
1516
|
+
body: JSON.stringify({
|
|
1517
|
+
model,
|
|
1518
|
+
messages: [{ role: "system", content: "You are a professional translator" }, prompt],
|
|
1519
|
+
max_tokens: maxTokens,
|
|
1520
|
+
temperature: 0,
|
|
1521
|
+
}),
|
|
1522
|
+
});
|
|
1523
|
+
const bodyRes = await res.json();
|
|
1524
|
+
if (bodyRes.error) {
|
|
1525
|
+
throw new TranslationError(this.name, `Translate fail! message: ${bodyRes.error.message}`);
|
|
1526
|
+
}
|
|
1527
|
+
if (!bodyRes || !bodyRes.choices || bodyRes.choices.length === 0 || !bodyRes.choices[0]?.message?.content) {
|
|
1528
|
+
throw new TranslationError(this.name, "Translate fail ! translate's result is null or empty");
|
|
1529
|
+
}
|
|
1530
|
+
const content = bodyRes.choices[0].message.content;
|
|
1531
|
+
const marks = ["-$s$-", "-$e$-"];
|
|
1532
|
+
const translations = content
|
|
1533
|
+
.trim()
|
|
1534
|
+
.split("\n")
|
|
1535
|
+
.map((item) => item.trim())
|
|
1536
|
+
.filter(Boolean)
|
|
1537
|
+
.filter((it) => !marks.includes(it));
|
|
1538
|
+
if (outputLog) {
|
|
1539
|
+
logger$1.info("translations:", translations);
|
|
1540
|
+
}
|
|
1541
|
+
return translations;
|
|
1542
|
+
},
|
|
1543
|
+
};
|
|
1544
|
+
}
|
|
1545
|
+
|
|
1546
|
+
const engines = {
|
|
1547
|
+
google,
|
|
1548
|
+
azure: azure$1,
|
|
1549
|
+
amazon: amazon$1,
|
|
1550
|
+
baidu: baidu$1,
|
|
1551
|
+
deepl: deepl$2,
|
|
1552
|
+
openai: openai$1,
|
|
1553
|
+
};
|
|
1554
|
+
|
|
1535
1555
|
var azure = {
|
|
1536
1556
|
Afrikaans: "af",
|
|
1537
1557
|
Albanian: "sq",
|