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