@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.
@@ -1,4 +1,4 @@
1
- // translate v0.0.20 Copyright (c) 2024 Potter<aa4790139@gmail.com> and contributors
1
+ // translate v0.1.1 Copyright (c) 2024 Potter<aa4790139@gmail.com> and contributors
2
2
  (function (global, factory) {
3
3
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@aws-sdk/client-translate')) :
4
4
  typeof define === 'function' && define.amd ? define(['exports', '@aws-sdk/client-translate'], factory) :
@@ -13,8 +13,34 @@
13
13
  Error.captureStackTrace(this, this.constructor);
14
14
  }
15
15
  }
16
-
17
- function google$1(options) {
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",
@@ -1349,12 +1375,74 @@
1349
1375
  };
1350
1376
  }
1351
1377
 
1378
+ function openai$1(options) {
1379
+ const { apiKey, model } = options;
1380
+ const name = "openai";
1381
+ const checkOptions = () => {
1382
+ if (!apiKey) {
1383
+ throw new TranslationError(name, `${name} apiKey is required`);
1384
+ }
1385
+ if (!OPEN_AI_MODELS.includes(model)) {
1386
+ throw new TranslationError(name, `${name} model=${model} is invalid`);
1387
+ }
1388
+ };
1389
+ checkOptions();
1390
+ const base = "https://api.openai.com/v1/chat/completions";
1391
+ return {
1392
+ name,
1393
+ async translate(text, opts) {
1394
+ checkOptions();
1395
+ const { from, to } = opts;
1396
+ const url = `${base}`;
1397
+ if (!Array.isArray(text)) {
1398
+ text = [text];
1399
+ }
1400
+ const prompt = {
1401
+ role: "user",
1402
+ content: `Translate the following texts from ${from} to ${to}:
1403
+ -$s$-
1404
+ ${text.join("\n")}
1405
+ -$e$-
1406
+ 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.
1407
+ Connect multiple text with newline character, keep the original order when return.
1408
+ `,
1409
+ };
1410
+ const res = await fetch(url, {
1411
+ method: "POST",
1412
+ headers: {
1413
+ "Content-Type": "application/json",
1414
+ Authorization: `Bearer ${apiKey}`,
1415
+ },
1416
+ body: JSON.stringify({
1417
+ model,
1418
+ messages: [{ role: "system", content: "You are a professional IT translator" }, prompt],
1419
+ max_tokens: 2000,
1420
+ }),
1421
+ });
1422
+ const bodyRes = await res.json();
1423
+ if (bodyRes.error) {
1424
+ throw new TranslationError(this.name, `Translate fail! message: ${bodyRes.error.message}`);
1425
+ }
1426
+ if (!bodyRes || !bodyRes.choices || bodyRes.choices.length === 0 || !bodyRes.choices[0]?.message?.content) {
1427
+ throw new TranslationError(this.name, "Translate fail ! translate's result is null or empty");
1428
+ }
1429
+ const content = bodyRes.choices[0].message.content;
1430
+ const translations = content
1431
+ .trim()
1432
+ .split("\n")
1433
+ .map((item) => item.trim());
1434
+ return translations;
1435
+ },
1436
+ };
1437
+ }
1438
+
1352
1439
  const engines = {
1353
- google: google$1,
1440
+ google,
1354
1441
  azure: azure$1,
1355
1442
  amazon: amazon$1,
1356
1443
  baidu: baidu$1,
1357
1444
  deepl: deepl$2,
1445
+ openai: openai$1,
1358
1446
  };
1359
1447
 
1360
1448
  class Cache {
@@ -1582,7 +1670,7 @@
1582
1670
  Zulu: "zu",
1583
1671
  };
1584
1672
 
1585
- var google = {
1673
+ var openai = {
1586
1674
  Abkhazian: "ab",
1587
1675
  Acehnese: "ace",
1588
1676
  "Acholi language": "ach",
@@ -2155,10 +2243,11 @@
2155
2243
 
2156
2244
  const originLanguages = {
2157
2245
  azure: azure,
2158
- google: google,
2246
+ google: openai,
2159
2247
  baidu: baidu,
2160
2248
  deepl: deepl$1,
2161
2249
  amazon: amazon,
2250
+ openai: openai,
2162
2251
  };
2163
2252
 
2164
2253
  var deepl = {
@@ -2202,10 +2291,11 @@
2202
2291
 
2203
2292
  const targetLanguages = {
2204
2293
  azure: azure,
2205
- google: google,
2294
+ google: openai,
2206
2295
  baidu: baidu,
2207
2296
  deepl: deepl,
2208
2297
  amazon: amazon,
2298
+ openai: openai,
2209
2299
  };
2210
2300
 
2211
2301
  function normalFromLanguage(from, engine) {
@@ -2259,13 +2349,28 @@
2259
2349
  this.engines = new Map();
2260
2350
  this.cache_time = cache_time;
2261
2351
  }
2352
+ /**
2353
+ * This method is obsolete, please use the addEngine method
2354
+ * @param engine {@link Engine} instance
2355
+ * @deprecated Use {@link addEngine} instead.
2356
+ */
2262
2357
  use(engine) {
2358
+ this.addEngine(engine);
2359
+ }
2360
+ addEngine(engine) {
2263
2361
  if (this.engines.has(engine.name)) {
2264
2362
  logger.warn("Engine already exists");
2265
2363
  return;
2266
2364
  }
2267
2365
  this.engines.set(engine.name, engine);
2268
2366
  }
2367
+ removeEngine(engineName) {
2368
+ if (!engineName || !this.engines.has(engineName)) {
2369
+ logger.warn("Engine name is required or not found");
2370
+ return false;
2371
+ }
2372
+ this.engines.delete(engineName);
2373
+ }
2269
2374
  async translate(text, options) {
2270
2375
  const { engine = "google", cache_time = 60 * 1000 } = options;
2271
2376
  let { from = "auto", to } = options;
@@ -2317,6 +2422,7 @@
2317
2422
  };
2318
2423
 
2319
2424
  exports.Cache = Cache;
2425
+ exports.OPEN_AI_MODELS = OPEN_AI_MODELS;
2320
2426
  exports.TranslationError = TranslationError;
2321
2427
  exports.Translator = Translator;
2322
2428
  exports.default = index;