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