@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
package/dist/node/index.js
CHANGED
|
@@ -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",
|
|
@@ -1311,12 +1337,74 @@ function deepl$2(options) {
|
|
|
1311
1337
|
};
|
|
1312
1338
|
}
|
|
1313
1339
|
|
|
1340
|
+
function openai$1(options) {
|
|
1341
|
+
const { apiKey, model } = 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: `Translate the following texts from ${from} to ${to}:
|
|
1365
|
+
-$s$-
|
|
1366
|
+
${text.join("\n")}
|
|
1367
|
+
-$e$-
|
|
1368
|
+
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.
|
|
1369
|
+
Connect multiple text with newline character, keep the original order when return.
|
|
1370
|
+
`,
|
|
1371
|
+
};
|
|
1372
|
+
const res = await fetch(url, {
|
|
1373
|
+
method: "POST",
|
|
1374
|
+
headers: {
|
|
1375
|
+
"Content-Type": "application/json",
|
|
1376
|
+
Authorization: `Bearer ${apiKey}`,
|
|
1377
|
+
},
|
|
1378
|
+
body: JSON.stringify({
|
|
1379
|
+
model,
|
|
1380
|
+
messages: [{ role: "system", content: "You are a professional IT translator" }, prompt],
|
|
1381
|
+
max_tokens: 2000,
|
|
1382
|
+
}),
|
|
1383
|
+
});
|
|
1384
|
+
const bodyRes = await res.json();
|
|
1385
|
+
if (bodyRes.error) {
|
|
1386
|
+
throw new TranslationError(this.name, `Translate fail! message: ${bodyRes.error.message}`);
|
|
1387
|
+
}
|
|
1388
|
+
if (!bodyRes || !bodyRes.choices || bodyRes.choices.length === 0 || !bodyRes.choices[0]?.message?.content) {
|
|
1389
|
+
throw new TranslationError(this.name, "Translate fail ! translate's result is null or empty");
|
|
1390
|
+
}
|
|
1391
|
+
const content = bodyRes.choices[0].message.content;
|
|
1392
|
+
const translations = content
|
|
1393
|
+
.trim()
|
|
1394
|
+
.split("\n")
|
|
1395
|
+
.map((item) => item.trim());
|
|
1396
|
+
return translations;
|
|
1397
|
+
},
|
|
1398
|
+
};
|
|
1399
|
+
}
|
|
1400
|
+
|
|
1314
1401
|
const engines = {
|
|
1315
|
-
google
|
|
1402
|
+
google,
|
|
1316
1403
|
azure: azure$1,
|
|
1317
1404
|
amazon: amazon$1,
|
|
1318
1405
|
baidu: baidu$1,
|
|
1319
1406
|
deepl: deepl$2,
|
|
1407
|
+
openai: openai$1,
|
|
1320
1408
|
};
|
|
1321
1409
|
|
|
1322
1410
|
class Cache {
|
|
@@ -1544,7 +1632,7 @@ var azure = {
|
|
|
1544
1632
|
Zulu: "zu",
|
|
1545
1633
|
};
|
|
1546
1634
|
|
|
1547
|
-
var
|
|
1635
|
+
var openai = {
|
|
1548
1636
|
Abkhazian: "ab",
|
|
1549
1637
|
Acehnese: "ace",
|
|
1550
1638
|
"Acholi language": "ach",
|
|
@@ -2117,10 +2205,11 @@ var amazon = {
|
|
|
2117
2205
|
|
|
2118
2206
|
const originLanguages = {
|
|
2119
2207
|
azure: azure,
|
|
2120
|
-
google:
|
|
2208
|
+
google: openai,
|
|
2121
2209
|
baidu: baidu,
|
|
2122
2210
|
deepl: deepl$1,
|
|
2123
2211
|
amazon: amazon,
|
|
2212
|
+
openai: openai,
|
|
2124
2213
|
};
|
|
2125
2214
|
|
|
2126
2215
|
var deepl = {
|
|
@@ -2164,10 +2253,11 @@ var deepl = {
|
|
|
2164
2253
|
|
|
2165
2254
|
const targetLanguages = {
|
|
2166
2255
|
azure: azure,
|
|
2167
|
-
google:
|
|
2256
|
+
google: openai,
|
|
2168
2257
|
baidu: baidu,
|
|
2169
2258
|
deepl: deepl,
|
|
2170
2259
|
amazon: amazon,
|
|
2260
|
+
openai: openai,
|
|
2171
2261
|
};
|
|
2172
2262
|
|
|
2173
2263
|
function normalFromLanguage(from, engine) {
|
|
@@ -2221,13 +2311,28 @@ class Translator {
|
|
|
2221
2311
|
this.engines = new Map();
|
|
2222
2312
|
this.cache_time = cache_time;
|
|
2223
2313
|
}
|
|
2314
|
+
/**
|
|
2315
|
+
* This method is obsolete, please use the addEngine method
|
|
2316
|
+
* @param engine {@link Engine} instance
|
|
2317
|
+
* @deprecated Use {@link addEngine} instead.
|
|
2318
|
+
*/
|
|
2224
2319
|
use(engine) {
|
|
2320
|
+
this.addEngine(engine);
|
|
2321
|
+
}
|
|
2322
|
+
addEngine(engine) {
|
|
2225
2323
|
if (this.engines.has(engine.name)) {
|
|
2226
2324
|
logger.warn("Engine already exists");
|
|
2227
2325
|
return;
|
|
2228
2326
|
}
|
|
2229
2327
|
this.engines.set(engine.name, engine);
|
|
2230
2328
|
}
|
|
2329
|
+
removeEngine(engineName) {
|
|
2330
|
+
if (!engineName || !this.engines.has(engineName)) {
|
|
2331
|
+
logger.warn("Engine name is required or not found");
|
|
2332
|
+
return false;
|
|
2333
|
+
}
|
|
2334
|
+
this.engines.delete(engineName);
|
|
2335
|
+
}
|
|
2231
2336
|
async translate(text, options) {
|
|
2232
2337
|
const { engine = "google", cache_time = 60 * 1000 } = options;
|
|
2233
2338
|
let { from = "auto", to } = options;
|
|
@@ -2278,5 +2383,5 @@ var index = {
|
|
|
2278
2383
|
getLanguage,
|
|
2279
2384
|
};
|
|
2280
2385
|
|
|
2281
|
-
export { Cache, TranslationError, Translator, index as default, engines, getLanguage, translator };
|
|
2386
|
+
export { Cache, OPEN_AI_MODELS, TranslationError, Translator, index as default, engines, getLanguage, translator };
|
|
2282
2387
|
//# sourceMappingURL=index.js.map
|