generaltranslation 1.1.16 → 1.1.18

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/index.js CHANGED
@@ -4,7 +4,7 @@
4
4
  // ----- IMPORTS ----- //
5
5
 
6
6
  const { _getLanguageName, _getLanguageCode } = require('./codes/codes.js');
7
- const { _getModelList, _getModelLanguages, _isLanguageSupported } = require('./models/models.js');
7
+ const { _getModelList, _getModelInfo, _getModelLanguages, _isLanguageSupported } = require('./models/models.js');
8
8
  const { _translate } = require('./translate/translate.js');
9
9
 
10
10
  // ----- CORE CLASS ----- //
@@ -25,6 +25,7 @@ class GT {
25
25
 
26
26
  // Model information functions
27
27
  getModelList = _getModelList; // returns array of supported model names
28
+ getModelInfo = _getModelInfo; // returns model object
28
29
  getModelLanguages = _getModelLanguages; // e.g. 'mistral-7b' => ['en']
29
30
  isLanguageSupported = _isLanguageSupported; // e.g. ('mistral-7b', 'en') => true
30
31
 
@@ -32,7 +33,6 @@ class GT {
32
33
  getPrompt = async (prompt, code) => {
33
34
  return await _translate(prompt, code, this.defaultLanguage, this.apiKey);
34
35
  }
35
-
36
36
  // Translation (same as prompt internationalization)
37
37
  translate = async (content, code) => {
38
38
  return await _translate(content, code, this.defaultLanguage, this.apiKey);
package/models/models.js CHANGED
@@ -45,6 +45,7 @@ const _isLanguageSupported = async (model, code) => {
45
45
 
46
46
  module.exports = {
47
47
  _getModelList,
48
+ _getModelInfo,
48
49
  _getModelLanguages,
49
50
  _isLanguageSupported
50
51
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "generaltranslation",
3
- "version": "1.1.16",
3
+ "version": "1.1.18",
4
4
  "description": "A language toolkit for AI developers",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -7,7 +7,7 @@ const _shouldTranslate = item => typeof item?.translate === 'boolean' ? item.tra
7
7
  // Separates out text that shouldn't be translated.
8
8
  const _processContent = (content) => {
9
9
  const processed = [];
10
- const redacted = [];
10
+ const untranslated = [];
11
11
  if (Array.isArray(content)) {
12
12
  for (const item of content) {
13
13
  if (typeof item === 'string') {
@@ -18,7 +18,7 @@ const _processContent = (content) => {
18
18
  processed.push(item);
19
19
  } else {
20
20
  processed.push({text: '', translate: false});
21
- redacted.push(item);
21
+ untranslated.push(item);
22
22
  }
23
23
  }
24
24
  } else {
@@ -30,49 +30,49 @@ const _processContent = (content) => {
30
30
  processed.push(content);
31
31
  } else {
32
32
  processed.push({text: '', translate: false});
33
- redacted.push(content);
33
+ untranslated.push(content);
34
34
  }
35
35
  }
36
36
  return {
37
37
  processed: processed,
38
- redacted: redacted.length > 0 ? redacted : null
38
+ untranslated: untranslated.length > 0 ? untranslated : null
39
39
  }
40
40
  }
41
41
 
42
42
  // Build content string from array or single item
43
- const _constructContent = ({translated, redacted = null}) => {
44
- if (Array.isArray(translated)) {
43
+ const _constructContent = ({ content, untranslated = null}) => {
44
+ if (Array.isArray(content)) {
45
45
  let final = '';
46
- for (const item of translated) {
46
+ for (const item of content) {
47
47
  if (typeof item === 'string') final += item;
48
48
  else if (_shouldTranslate(item)) final += item?.text || '';
49
49
  else {
50
- if (redacted?.length > 0) {
51
- final += redacted?.shift().text || '';
50
+ if (untranslated?.length > 0) {
51
+ final += untranslated?.shift().text || '';
52
52
  } else {
53
53
  final += item?.text || '';
54
54
  }
55
55
  }
56
56
  }
57
57
  return final;
58
- } else if (typeof translated === 'string') {
59
- return translated;
58
+ } else if (typeof content === 'string') {
59
+ return content;
60
60
  } else {
61
- return translated?.text || '';
61
+ return content?.text || '';
62
62
  }
63
63
  }
64
64
 
65
65
  // Get a translation via General Translation API
66
66
  // Returns string
67
67
  const _translate = async (content, code, defaultLanguage, apiKey) => {
68
+ if (!apiKey) {
69
+ throw new Error('Missing API Key!')
70
+ }
71
+ if (code === defaultLanguage) {
72
+ return _constructContent({ content: content });
73
+ }
74
+ const { processed, untranslated } = _processContent(content);
68
75
  try {
69
- if (!apiKey) {
70
- throw new Error('Missing API Key!')
71
- }
72
- if (code === defaultLanguage) {
73
- return _constructContent(content);
74
- }
75
- const { processed, redacted } = _processContent(content);
76
76
  const response = await fetch('http://translate.gtx.dev', {
77
77
  method: 'POST',
78
78
  headers: {
@@ -90,11 +90,11 @@ const _translate = async (content, code, defaultLanguage, apiKey) => {
90
90
  throw new Error(`${result || response.status}`);
91
91
  } else {
92
92
  const result = await response.json();
93
- return _constructContent({translated: result, redacted: redacted});
93
+ return _constructContent({content: result, untranslated: untranslated });
94
94
  }
95
95
  } catch (error) {
96
96
  console.error(error)
97
- return _constructContent({ translated: content })
97
+ return _constructContent({ content: content })
98
98
  }
99
99
  }
100
100