@yxw007/translate 0.0.20 → 0.1.1
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/README.md +58 -3
- package/README_zh-CN.md +59 -5
- package/dist/browser/index.cjs +113 -7
- package/dist/browser/index.cjs.map +1 -1
- package/dist/browser/index.esm.js +113 -8
- 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 +113 -7
- 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 +29 -7
- package/dist/node/index.cjs +113 -7
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.js +113 -8
- 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
|
+
// translate v0.1.1 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 {
|
|
@@ -9,8 +9,34 @@ class TranslationError extends Error {
|
|
|
9
9
|
Error.captureStackTrace(this, this.constructor);
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
const OPEN_AI_MODELS = [
|
|
13
|
+
"o1-preview",
|
|
14
|
+
"o1-preview-2024-09-12",
|
|
15
|
+
"o1-mini-2024-09-12",
|
|
16
|
+
"o1-mini",
|
|
17
|
+
"dall-e-2",
|
|
18
|
+
"gpt-3.5-turbo",
|
|
19
|
+
"gpt-3.5-turbo-0125",
|
|
20
|
+
"babbage-002",
|
|
21
|
+
"davinci-002",
|
|
22
|
+
"dall-e-3",
|
|
23
|
+
"text-embedding-3-large",
|
|
24
|
+
"gpt-3.5-turbo-16k",
|
|
25
|
+
"tts-1-hd-1106",
|
|
26
|
+
"text-embedding-ada-002",
|
|
27
|
+
"text-embedding-3-small",
|
|
28
|
+
"tts-1-hd",
|
|
29
|
+
"whisper-1",
|
|
30
|
+
"gpt-3.5-turbo-1106",
|
|
31
|
+
"gpt-3.5-turbo-instruct",
|
|
32
|
+
"gpt-4o-mini-2024-07-18",
|
|
33
|
+
"gpt-4o-mini",
|
|
34
|
+
"tts-1",
|
|
35
|
+
"tts-1-1106",
|
|
36
|
+
"gpt-3.5-turbo-instruct-0914",
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
function google(options) {
|
|
14
40
|
const base = "https://translate.googleapis.com/translate_a/single";
|
|
15
41
|
return {
|
|
16
42
|
name: "google",
|
|
@@ -1345,12 +1371,74 @@ function deepl$2(options) {
|
|
|
1345
1371
|
};
|
|
1346
1372
|
}
|
|
1347
1373
|
|
|
1374
|
+
function openai$1(options) {
|
|
1375
|
+
const { apiKey, model } = 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: `Translate the following texts from ${from} to ${to}:
|
|
1399
|
+
-$s$-
|
|
1400
|
+
${text.join("\n")}
|
|
1401
|
+
-$e$-
|
|
1402
|
+
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.
|
|
1403
|
+
Connect multiple text with newline character, keep the original order when return.
|
|
1404
|
+
`,
|
|
1405
|
+
};
|
|
1406
|
+
const res = await fetch(url, {
|
|
1407
|
+
method: "POST",
|
|
1408
|
+
headers: {
|
|
1409
|
+
"Content-Type": "application/json",
|
|
1410
|
+
Authorization: `Bearer ${apiKey}`,
|
|
1411
|
+
},
|
|
1412
|
+
body: JSON.stringify({
|
|
1413
|
+
model,
|
|
1414
|
+
messages: [{ role: "system", content: "You are a professional IT translator" }, prompt],
|
|
1415
|
+
max_tokens: 2000,
|
|
1416
|
+
}),
|
|
1417
|
+
});
|
|
1418
|
+
const bodyRes = await res.json();
|
|
1419
|
+
if (bodyRes.error) {
|
|
1420
|
+
throw new TranslationError(this.name, `Translate fail! message: ${bodyRes.error.message}`);
|
|
1421
|
+
}
|
|
1422
|
+
if (!bodyRes || !bodyRes.choices || bodyRes.choices.length === 0 || !bodyRes.choices[0]?.message?.content) {
|
|
1423
|
+
throw new TranslationError(this.name, "Translate fail ! translate's result is null or empty");
|
|
1424
|
+
}
|
|
1425
|
+
const content = bodyRes.choices[0].message.content;
|
|
1426
|
+
const translations = content
|
|
1427
|
+
.trim()
|
|
1428
|
+
.split("\n")
|
|
1429
|
+
.map((item) => item.trim());
|
|
1430
|
+
return translations;
|
|
1431
|
+
},
|
|
1432
|
+
};
|
|
1433
|
+
}
|
|
1434
|
+
|
|
1348
1435
|
const engines = {
|
|
1349
|
-
google
|
|
1436
|
+
google,
|
|
1350
1437
|
azure: azure$1,
|
|
1351
1438
|
amazon: amazon$1,
|
|
1352
1439
|
baidu: baidu$1,
|
|
1353
1440
|
deepl: deepl$2,
|
|
1441
|
+
openai: openai$1,
|
|
1354
1442
|
};
|
|
1355
1443
|
|
|
1356
1444
|
class Cache {
|
|
@@ -1578,7 +1666,7 @@ var azure = {
|
|
|
1578
1666
|
Zulu: "zu",
|
|
1579
1667
|
};
|
|
1580
1668
|
|
|
1581
|
-
var
|
|
1669
|
+
var openai = {
|
|
1582
1670
|
Abkhazian: "ab",
|
|
1583
1671
|
Acehnese: "ace",
|
|
1584
1672
|
"Acholi language": "ach",
|
|
@@ -2151,10 +2239,11 @@ var amazon = {
|
|
|
2151
2239
|
|
|
2152
2240
|
const originLanguages = {
|
|
2153
2241
|
azure: azure,
|
|
2154
|
-
google:
|
|
2242
|
+
google: openai,
|
|
2155
2243
|
baidu: baidu,
|
|
2156
2244
|
deepl: deepl$1,
|
|
2157
2245
|
amazon: amazon,
|
|
2246
|
+
openai: openai,
|
|
2158
2247
|
};
|
|
2159
2248
|
|
|
2160
2249
|
var deepl = {
|
|
@@ -2198,10 +2287,11 @@ var deepl = {
|
|
|
2198
2287
|
|
|
2199
2288
|
const targetLanguages = {
|
|
2200
2289
|
azure: azure,
|
|
2201
|
-
google:
|
|
2290
|
+
google: openai,
|
|
2202
2291
|
baidu: baidu,
|
|
2203
2292
|
deepl: deepl,
|
|
2204
2293
|
amazon: amazon,
|
|
2294
|
+
openai: openai,
|
|
2205
2295
|
};
|
|
2206
2296
|
|
|
2207
2297
|
function normalFromLanguage(from, engine) {
|
|
@@ -2255,13 +2345,28 @@ class Translator {
|
|
|
2255
2345
|
this.engines = new Map();
|
|
2256
2346
|
this.cache_time = cache_time;
|
|
2257
2347
|
}
|
|
2348
|
+
/**
|
|
2349
|
+
* This method is obsolete, please use the addEngine method
|
|
2350
|
+
* @param engine {@link Engine} instance
|
|
2351
|
+
* @deprecated Use {@link addEngine} instead.
|
|
2352
|
+
*/
|
|
2258
2353
|
use(engine) {
|
|
2354
|
+
this.addEngine(engine);
|
|
2355
|
+
}
|
|
2356
|
+
addEngine(engine) {
|
|
2259
2357
|
if (this.engines.has(engine.name)) {
|
|
2260
2358
|
logger.warn("Engine already exists");
|
|
2261
2359
|
return;
|
|
2262
2360
|
}
|
|
2263
2361
|
this.engines.set(engine.name, engine);
|
|
2264
2362
|
}
|
|
2363
|
+
removeEngine(engineName) {
|
|
2364
|
+
if (!engineName || !this.engines.has(engineName)) {
|
|
2365
|
+
logger.warn("Engine name is required or not found");
|
|
2366
|
+
return false;
|
|
2367
|
+
}
|
|
2368
|
+
this.engines.delete(engineName);
|
|
2369
|
+
}
|
|
2265
2370
|
async translate(text, options) {
|
|
2266
2371
|
const { engine = "google", cache_time = 60 * 1000 } = options;
|
|
2267
2372
|
let { from = "auto", to } = options;
|
|
@@ -2312,5 +2417,5 @@ var index = {
|
|
|
2312
2417
|
getLanguage,
|
|
2313
2418
|
};
|
|
2314
2419
|
|
|
2315
|
-
export { Cache, TranslationError, Translator, index as default, engines, getLanguage, translator };
|
|
2420
|
+
export { Cache, OPEN_AI_MODELS, TranslationError, Translator, index as default, engines, getLanguage, translator };
|
|
2316
2421
|
//# sourceMappingURL=index.esm.js.map
|