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