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