@yxw007/translate 0.0.19 → 0.1.0
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 +60 -3
- package/README_zh-CN.md +61 -5
- package/dist/browser/index.cjs +116 -8
- package/dist/browser/index.cjs.map +1 -1
- package/dist/browser/index.esm.js +116 -9
- 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 +116 -8
- 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 +116 -8
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.js +116 -9
- package/dist/node/index.js.map +1 -1
- package/dist/package.json +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -23,12 +23,21 @@ interface BaiduEngineOption extends BaseEngineOption {
|
|
|
23
23
|
}
|
|
24
24
|
declare function baidu(options: BaiduEngineOption): Engine;
|
|
25
25
|
|
|
26
|
+
interface DeeplEngineOption {
|
|
27
|
+
key: string;
|
|
28
|
+
}
|
|
29
|
+
declare function deepl(options: DeeplEngineOption): {
|
|
30
|
+
name: string;
|
|
31
|
+
translate<T extends Engines>(text: string | string[], opts: EngineTranslateOptions<T>): Promise<string[]>;
|
|
32
|
+
};
|
|
33
|
+
|
|
26
34
|
declare const engines: {
|
|
27
35
|
readonly google: typeof google;
|
|
28
36
|
readonly azure: typeof azure;
|
|
29
37
|
readonly amazon: typeof amazon;
|
|
30
38
|
readonly baidu: typeof baidu;
|
|
31
39
|
readonly deepl: typeof deepl;
|
|
40
|
+
readonly openai: typeof openai;
|
|
32
41
|
};
|
|
33
42
|
|
|
34
43
|
declare const _default$6: {
|
|
@@ -748,6 +757,7 @@ type originLanguageMapNames = {
|
|
|
748
757
|
google: keyof typeof _default$5;
|
|
749
758
|
baidu: keyof typeof _default$4;
|
|
750
759
|
deepl: keyof typeof _default$3;
|
|
760
|
+
openai: keyof typeof _default$5;
|
|
751
761
|
};
|
|
752
762
|
type originLanguageMapValues = {
|
|
753
763
|
amazon: ValuesOf<typeof _default$2>;
|
|
@@ -755,6 +765,7 @@ type originLanguageMapValues = {
|
|
|
755
765
|
google: ValuesOf<typeof _default$5>;
|
|
756
766
|
baidu: ValuesOf<typeof _default$4>;
|
|
757
767
|
deepl: ValuesOf<typeof _default$3>;
|
|
768
|
+
openai: ValuesOf<typeof _default$5>;
|
|
758
769
|
};
|
|
759
770
|
|
|
760
771
|
declare const _default$1: {
|
|
@@ -817,6 +828,7 @@ type targetLanguageMapNames = {
|
|
|
817
828
|
google: keyof typeof _default$5;
|
|
818
829
|
baidu: keyof typeof _default$4;
|
|
819
830
|
deepl: keyof typeof _default$1;
|
|
831
|
+
openai: keyof typeof _default$5;
|
|
820
832
|
};
|
|
821
833
|
type targetLanguageMapValues = {
|
|
822
834
|
amazon: ValuesOf<typeof _default$2>;
|
|
@@ -824,6 +836,7 @@ type targetLanguageMapValues = {
|
|
|
824
836
|
google: ValuesOf<typeof _default$5>;
|
|
825
837
|
baidu: ValuesOf<typeof _default$4>;
|
|
826
838
|
deepl: ValuesOf<typeof _default$1>;
|
|
839
|
+
openai: ValuesOf<typeof _default$5>;
|
|
827
840
|
};
|
|
828
841
|
|
|
829
842
|
type KeysOfTarget<T extends keyof targetLanguageMapNames> = targetLanguageMapNames[T];
|
|
@@ -2267,19 +2280,27 @@ declare class TranslationError extends Error {
|
|
|
2267
2280
|
constructor(region: string, message: string);
|
|
2268
2281
|
}
|
|
2269
2282
|
|
|
2270
|
-
|
|
2271
|
-
|
|
2283
|
+
declare const OPEN_AI_MODELS: readonly ["o1-preview", "o1-preview-2024-09-12", "o1-mini-2024-09-12", "o1-mini", "dall-e-2", "gpt-3.5-turbo", "gpt-3.5-turbo-0125", "babbage-002", "davinci-002", "dall-e-3", "text-embedding-3-large", "gpt-3.5-turbo-16k", "tts-1-hd-1106", "text-embedding-ada-002", "text-embedding-3-small", "tts-1-hd", "whisper-1", "gpt-3.5-turbo-1106", "gpt-3.5-turbo-instruct", "gpt-4o-mini-2024-07-18", "gpt-4o-mini", "tts-1", "tts-1-1106", "gpt-3.5-turbo-instruct-0914"];
|
|
2284
|
+
type OpenAIModel = (typeof OPEN_AI_MODELS)[number];
|
|
2285
|
+
|
|
2286
|
+
interface OpenAIEngineOption extends BaseEngineOption {
|
|
2287
|
+
apiKey: string;
|
|
2288
|
+
model: OpenAIModel;
|
|
2272
2289
|
}
|
|
2273
|
-
declare function
|
|
2274
|
-
name: string;
|
|
2275
|
-
translate<T extends Engines>(text: string | string[], opts: EngineTranslateOptions<T>): Promise<string[]>;
|
|
2276
|
-
};
|
|
2290
|
+
declare function openai(options: OpenAIEngineOption): Engine;
|
|
2277
2291
|
|
|
2278
2292
|
declare class Translator {
|
|
2279
2293
|
private engines;
|
|
2280
2294
|
private cache_time;
|
|
2281
2295
|
constructor(cache_time?: number);
|
|
2296
|
+
/**
|
|
2297
|
+
* This method is obsolete, please use the addEngine method
|
|
2298
|
+
* @param engine {@link Engine} instance
|
|
2299
|
+
* @deprecated Use {@link addEngine} instead.
|
|
2300
|
+
*/
|
|
2282
2301
|
use(engine: Engine): void;
|
|
2302
|
+
addEngine(engine: Engine): void;
|
|
2303
|
+
removeEngine(engineName: string): false | undefined;
|
|
2283
2304
|
translate<T extends Engines>(text: string | string[], options: TranslateOptions<T>): Promise<string[]>;
|
|
2284
2305
|
}
|
|
2285
2306
|
declare const translator: Translator;
|
|
@@ -2290,6 +2311,7 @@ declare const _default: {
|
|
|
2290
2311
|
readonly amazon: typeof amazon;
|
|
2291
2312
|
readonly baidu: typeof baidu;
|
|
2292
2313
|
readonly deepl: typeof deepl;
|
|
2314
|
+
readonly openai: typeof openai;
|
|
2293
2315
|
};
|
|
2294
2316
|
translator: Translator;
|
|
2295
2317
|
Translator: typeof Translator;
|
|
@@ -2297,5 +2319,5 @@ declare const _default: {
|
|
|
2297
2319
|
getLanguage: typeof getLanguage;
|
|
2298
2320
|
};
|
|
2299
2321
|
|
|
2300
|
-
export { type BaseEngineOption, Cache, type CacheRecord, type Engine, type EngineTranslateOptions, type Engines, type FromLanguage, type ToLanguage, type TranslateOptions, TranslationError, Translator, type ValuesOf, _default as default, engines, getLanguage, translator };
|
|
2322
|
+
export { type BaseEngineOption, Cache, type CacheRecord, type Engine, type EngineTranslateOptions, type Engines, type FromLanguage, OPEN_AI_MODELS, type OpenAIModel, type ToLanguage, type TranslateOptions, TranslationError, Translator, type ValuesOf, _default as default, engines, getLanguage, translator };
|
|
2301
2323
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/node/index.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// translate v0.0
|
|
1
|
+
// translate v0.1.0 Copyright (c) 2024 Potter<aa4790139@gmail.com> and contributors
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
4
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
@@ -13,8 +13,34 @@ class TranslationError extends Error {
|
|
|
13
13
|
Error.captureStackTrace(this, this.constructor);
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
const OPEN_AI_MODELS = [
|
|
17
|
+
"o1-preview",
|
|
18
|
+
"o1-preview-2024-09-12",
|
|
19
|
+
"o1-mini-2024-09-12",
|
|
20
|
+
"o1-mini",
|
|
21
|
+
"dall-e-2",
|
|
22
|
+
"gpt-3.5-turbo",
|
|
23
|
+
"gpt-3.5-turbo-0125",
|
|
24
|
+
"babbage-002",
|
|
25
|
+
"davinci-002",
|
|
26
|
+
"dall-e-3",
|
|
27
|
+
"text-embedding-3-large",
|
|
28
|
+
"gpt-3.5-turbo-16k",
|
|
29
|
+
"tts-1-hd-1106",
|
|
30
|
+
"text-embedding-ada-002",
|
|
31
|
+
"text-embedding-3-small",
|
|
32
|
+
"tts-1-hd",
|
|
33
|
+
"whisper-1",
|
|
34
|
+
"gpt-3.5-turbo-1106",
|
|
35
|
+
"gpt-3.5-turbo-instruct",
|
|
36
|
+
"gpt-4o-mini-2024-07-18",
|
|
37
|
+
"gpt-4o-mini",
|
|
38
|
+
"tts-1",
|
|
39
|
+
"tts-1-1106",
|
|
40
|
+
"gpt-3.5-turbo-instruct-0914",
|
|
41
|
+
];
|
|
42
|
+
|
|
43
|
+
function google(options) {
|
|
18
44
|
const base = "https://translate.googleapis.com/translate_a/single";
|
|
19
45
|
return {
|
|
20
46
|
name: "google",
|
|
@@ -1315,12 +1341,76 @@ function deepl$2(options) {
|
|
|
1315
1341
|
};
|
|
1316
1342
|
}
|
|
1317
1343
|
|
|
1344
|
+
function openai$1(options) {
|
|
1345
|
+
const { apiKey, model } = 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: `Translate the following texts from ${from} to ${to}:
|
|
1369
|
+
-$s$-
|
|
1370
|
+
${text.join("\n")}
|
|
1371
|
+
-$e$-
|
|
1372
|
+
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.
|
|
1373
|
+
Connect multiple text with newline character, keep the original order when return.
|
|
1374
|
+
`,
|
|
1375
|
+
};
|
|
1376
|
+
console.log("prompt:", prompt.content);
|
|
1377
|
+
const res = await fetch(url, {
|
|
1378
|
+
method: "POST",
|
|
1379
|
+
headers: {
|
|
1380
|
+
"Content-Type": "application/json",
|
|
1381
|
+
Authorization: `Bearer ${apiKey}`,
|
|
1382
|
+
},
|
|
1383
|
+
body: JSON.stringify({
|
|
1384
|
+
model,
|
|
1385
|
+
messages: [{ role: "system", content: "You are a professional IT translator" }, prompt],
|
|
1386
|
+
max_tokens: 2000,
|
|
1387
|
+
}),
|
|
1388
|
+
});
|
|
1389
|
+
const bodyRes = await res.json();
|
|
1390
|
+
if (bodyRes.error) {
|
|
1391
|
+
throw new TranslationError(this.name, `Translate fail! message: ${bodyRes.error.message}`);
|
|
1392
|
+
}
|
|
1393
|
+
if (!bodyRes || !bodyRes.choices || bodyRes.choices.length === 0 || !bodyRes.choices[0]?.message?.content) {
|
|
1394
|
+
throw new TranslationError(this.name, "Translate fail ! translate's result is null or empty");
|
|
1395
|
+
}
|
|
1396
|
+
const content = bodyRes.choices[0].message.content;
|
|
1397
|
+
const translations = content
|
|
1398
|
+
.trim()
|
|
1399
|
+
.split("\n")
|
|
1400
|
+
.map((item) => item.trim());
|
|
1401
|
+
console.log("translations:", translations);
|
|
1402
|
+
return translations;
|
|
1403
|
+
},
|
|
1404
|
+
};
|
|
1405
|
+
}
|
|
1406
|
+
|
|
1318
1407
|
const engines = {
|
|
1319
|
-
google
|
|
1408
|
+
google,
|
|
1320
1409
|
azure: azure$1,
|
|
1321
1410
|
amazon: amazon$1,
|
|
1322
1411
|
baidu: baidu$1,
|
|
1323
1412
|
deepl: deepl$2,
|
|
1413
|
+
openai: openai$1,
|
|
1324
1414
|
};
|
|
1325
1415
|
|
|
1326
1416
|
class Cache {
|
|
@@ -1405,7 +1495,7 @@ function getGapLine() {
|
|
|
1405
1495
|
}
|
|
1406
1496
|
function getErrorMessages(e, prefix = "Translate fail ! ") {
|
|
1407
1497
|
if (e instanceof TypeError) {
|
|
1408
|
-
return prefix + (e.cause
|
|
1498
|
+
return prefix + (e.cause?.message ?? e.message);
|
|
1409
1499
|
}
|
|
1410
1500
|
return prefix + e.message;
|
|
1411
1501
|
}
|
|
@@ -1548,7 +1638,7 @@ var azure = {
|
|
|
1548
1638
|
Zulu: "zu",
|
|
1549
1639
|
};
|
|
1550
1640
|
|
|
1551
|
-
var
|
|
1641
|
+
var openai = {
|
|
1552
1642
|
Abkhazian: "ab",
|
|
1553
1643
|
Acehnese: "ace",
|
|
1554
1644
|
"Acholi language": "ach",
|
|
@@ -2121,10 +2211,11 @@ var amazon = {
|
|
|
2121
2211
|
|
|
2122
2212
|
const originLanguages = {
|
|
2123
2213
|
azure: azure,
|
|
2124
|
-
google:
|
|
2214
|
+
google: openai,
|
|
2125
2215
|
baidu: baidu,
|
|
2126
2216
|
deepl: deepl$1,
|
|
2127
2217
|
amazon: amazon,
|
|
2218
|
+
openai: openai,
|
|
2128
2219
|
};
|
|
2129
2220
|
|
|
2130
2221
|
var deepl = {
|
|
@@ -2168,10 +2259,11 @@ var deepl = {
|
|
|
2168
2259
|
|
|
2169
2260
|
const targetLanguages = {
|
|
2170
2261
|
azure: azure,
|
|
2171
|
-
google:
|
|
2262
|
+
google: openai,
|
|
2172
2263
|
baidu: baidu,
|
|
2173
2264
|
deepl: deepl,
|
|
2174
2265
|
amazon: amazon,
|
|
2266
|
+
openai: openai,
|
|
2175
2267
|
};
|
|
2176
2268
|
|
|
2177
2269
|
function normalFromLanguage(from, engine) {
|
|
@@ -2225,13 +2317,28 @@ class Translator {
|
|
|
2225
2317
|
this.engines = new Map();
|
|
2226
2318
|
this.cache_time = cache_time;
|
|
2227
2319
|
}
|
|
2320
|
+
/**
|
|
2321
|
+
* This method is obsolete, please use the addEngine method
|
|
2322
|
+
* @param engine {@link Engine} instance
|
|
2323
|
+
* @deprecated Use {@link addEngine} instead.
|
|
2324
|
+
*/
|
|
2228
2325
|
use(engine) {
|
|
2326
|
+
this.addEngine(engine);
|
|
2327
|
+
}
|
|
2328
|
+
addEngine(engine) {
|
|
2229
2329
|
if (this.engines.has(engine.name)) {
|
|
2230
2330
|
logger.warn("Engine already exists");
|
|
2231
2331
|
return;
|
|
2232
2332
|
}
|
|
2233
2333
|
this.engines.set(engine.name, engine);
|
|
2234
2334
|
}
|
|
2335
|
+
removeEngine(engineName) {
|
|
2336
|
+
if (!engineName || !this.engines.has(engineName)) {
|
|
2337
|
+
logger.warn("Engine name is required or not found");
|
|
2338
|
+
return false;
|
|
2339
|
+
}
|
|
2340
|
+
this.engines.delete(engineName);
|
|
2341
|
+
}
|
|
2235
2342
|
async translate(text, options) {
|
|
2236
2343
|
const { engine = "google", cache_time = 60 * 1000 } = options;
|
|
2237
2344
|
let { from = "auto", to } = options;
|
|
@@ -2283,6 +2390,7 @@ var index = {
|
|
|
2283
2390
|
};
|
|
2284
2391
|
|
|
2285
2392
|
exports.Cache = Cache;
|
|
2393
|
+
exports.OPEN_AI_MODELS = OPEN_AI_MODELS;
|
|
2286
2394
|
exports.TranslationError = TranslationError;
|
|
2287
2395
|
exports.Translator = Translator;
|
|
2288
2396
|
exports.default = index;
|