generaltranslation 1.1.4 → 1.1.6
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/{codes.js → codes/codes.js} +7 -7
- package/index.js +20 -8
- package/{models.js → models/models.js} +7 -7
- package/package.json +1 -1
- package/prompts/prompts.js +97 -0
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
// ----- IMPORTS ----- //
|
|
2
2
|
|
|
3
|
-
const CodeToLanguage = require('./
|
|
4
|
-
const LanguageToCode = require('./
|
|
3
|
+
const CodeToLanguage = require('./639-1/CodeToLanguage.json');
|
|
4
|
+
const LanguageToCode = require('./639-1/LanguageToCode.json');
|
|
5
5
|
|
|
6
6
|
// only for languages which have no two-letter code
|
|
7
|
-
const CodeToLanguageTriletter = require('./
|
|
8
|
-
const LanguageToCodeTriletter = require('./
|
|
7
|
+
const CodeToLanguageTriletter = require('./639-3/CodeToLanguageTriletter.json');
|
|
8
|
+
const LanguageToCodeTriletter = require('./639-3/LanguageToCodeTriletter.json');
|
|
9
9
|
|
|
10
10
|
// ----- LANGUAGE CODES ----- //
|
|
11
11
|
|
|
12
12
|
// Returns the name of a language from an ISO 639 code or an array of codes
|
|
13
13
|
const _mapCodeToLanguage = code => CodeToLanguage[code] || CodeToLanguageTriletter[code] || '';
|
|
14
|
-
const
|
|
14
|
+
const _getLanguageName = codes => {
|
|
15
15
|
return Array.isArray(codes) ? codes.map(_mapCodeToLanguage) : _mapCodeToLanguage(codes);
|
|
16
16
|
}
|
|
17
17
|
|
|
@@ -21,10 +21,10 @@ const _mapLanguageToCode = language => {
|
|
|
21
21
|
const lowerCaseLanguage = language.toLowerCase();
|
|
22
22
|
return LanguageToCode[lowerCaseLanguage] || LanguageToCodeTriletter[lowerCaseLanguage] || '';
|
|
23
23
|
}
|
|
24
|
-
const
|
|
24
|
+
const _getLanguageCode = languages => {
|
|
25
25
|
return Array.isArray(languages) ? languages.map(_mapLanguageToCode) : _mapLanguageToCode(languages);
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
module.exports = {
|
|
29
|
-
|
|
29
|
+
_getLanguageName, _getLanguageCode
|
|
30
30
|
}
|
package/index.js
CHANGED
|
@@ -3,9 +3,11 @@
|
|
|
3
3
|
|
|
4
4
|
// ----- IMPORTS ----- //
|
|
5
5
|
|
|
6
|
-
const {
|
|
6
|
+
const { _getLanguageName, _getLanguageCode } = require('./codes/codes.js');
|
|
7
|
+
const { _getModelList, _getModelLanguages, _isLanguageSupported } = require('./models/models.js');
|
|
8
|
+
const { _getPrompt } = require('./prompts/prompts.js');
|
|
7
9
|
|
|
8
|
-
|
|
10
|
+
// ----- CORE CLASS ----- //
|
|
9
11
|
|
|
10
12
|
class GT {
|
|
11
13
|
|
|
@@ -18,16 +20,26 @@ class GT {
|
|
|
18
20
|
}
|
|
19
21
|
|
|
20
22
|
// Language code functions
|
|
21
|
-
getLanguageName =
|
|
22
|
-
getLanguageCode =
|
|
23
|
+
getLanguageName = _getLanguageName; // e.g. 'en' => 'English'
|
|
24
|
+
getLanguageCode = _getLanguageCode; // e.g. 'English' => 'en'
|
|
23
25
|
|
|
24
26
|
// Model information functions
|
|
25
|
-
getModelList =
|
|
26
|
-
getModelLanguages =
|
|
27
|
-
isLanguageSupported =
|
|
27
|
+
getModelList = _getModelList; // returns array of supported model names
|
|
28
|
+
getModelLanguages = _getModelLanguages; // e.g. 'mistral-7b' => ['en']
|
|
29
|
+
isLanguageSupported = _isLanguageSupported; // e.g. ('mistral-7b', 'en') => true
|
|
30
|
+
|
|
31
|
+
// Prompt internationalization
|
|
32
|
+
getPrompt = async (prompt, language) => {
|
|
33
|
+
return await _getPrompt(prompt, language, this.defaultLanguage, this.apiKey);
|
|
34
|
+
}
|
|
28
35
|
|
|
29
36
|
}
|
|
30
37
|
|
|
31
38
|
// ----- EXPORTS ----- //
|
|
32
39
|
|
|
33
|
-
module.exports = GT;
|
|
40
|
+
module.exports = GT;
|
|
41
|
+
module.exports.getLanguageCode = _getLanguageCode;
|
|
42
|
+
module.exports.getLanguageName = _getLanguageName;
|
|
43
|
+
module.exports.getModelList = _getModelList;
|
|
44
|
+
module.exports.getModelLanguages = _getModelLanguages;
|
|
45
|
+
module.exports.isLanguageSupported = _isLanguageSupported;
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
// Get all models
|
|
4
4
|
// Returns an array of model names
|
|
5
|
-
const
|
|
5
|
+
const _getModelList = async () => {
|
|
6
6
|
try {
|
|
7
7
|
const result = await fetch(`https://models.gtx.dev/models`);
|
|
8
8
|
const models = await result.json();
|
|
@@ -30,21 +30,21 @@ const _getModelInfo = async model => {
|
|
|
30
30
|
|
|
31
31
|
// Get all languages known to be compatible with a given LLM
|
|
32
32
|
// Returns an array of languages codes, or [] if unknown
|
|
33
|
-
const
|
|
33
|
+
const _getModelLanguages = async model => {
|
|
34
34
|
const modelInfo = await _getModelInfo(model);
|
|
35
35
|
return modelInfo?.languages || [];
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
// Returns true if a model is known to be compatible with a language
|
|
39
39
|
// Returns false otherwise
|
|
40
|
-
const
|
|
40
|
+
const _isLanguageSupported = async (model, code) => {
|
|
41
41
|
if (!code) return false;
|
|
42
|
-
const modelLanguages = await
|
|
42
|
+
const modelLanguages = await _getModelLanguages(model);
|
|
43
43
|
return modelLanguages?.includes(code) ? true : false;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
module.exports = {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
47
|
+
_getModelList,
|
|
48
|
+
_getModelLanguages,
|
|
49
|
+
_isLanguageSupported
|
|
50
50
|
}
|
package/package.json
CHANGED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// ----- PROMPT INTERNATIONALIZATION ----- //
|
|
2
|
+
|
|
3
|
+
// Turn a prompt (which could be a string, array, or object) into a string
|
|
4
|
+
const _constructPrompt = (prompt, redacted = null) => {
|
|
5
|
+
if (Array.isArray(prompt)) {
|
|
6
|
+
let final = '';
|
|
7
|
+
for (const item of prompt) {
|
|
8
|
+
if (typeof item === 'string') final += item;
|
|
9
|
+
else if (_shouldTranslate(item)) final += item?.text || '';
|
|
10
|
+
else {
|
|
11
|
+
if (redacted?.length > 0) {
|
|
12
|
+
final += redacted?.shift().text || '';
|
|
13
|
+
} else {
|
|
14
|
+
final += item?.text || '';
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return final;
|
|
19
|
+
} else if (typeof prompt === 'string') {
|
|
20
|
+
return prompt;
|
|
21
|
+
} else {
|
|
22
|
+
return prompt?.text || '';
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const _shouldTranslate = item => typeof item?.translate === 'boolean' ? item.translate : true;
|
|
27
|
+
// Redacted items are not sent in the API call
|
|
28
|
+
const _processPrompt = (prompt) => {
|
|
29
|
+
const processed = [];
|
|
30
|
+
const redacted = [];
|
|
31
|
+
if (Array.isArray(prompt)) {
|
|
32
|
+
for (const item of prompt) {
|
|
33
|
+
if (typeof item === 'string') {
|
|
34
|
+
processed.push({
|
|
35
|
+
text: item
|
|
36
|
+
});
|
|
37
|
+
} else if (_shouldTranslate(item)) {
|
|
38
|
+
processed.push(item);
|
|
39
|
+
} else {
|
|
40
|
+
processed.push({text: '', translate: false});
|
|
41
|
+
redacted.push(item);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
processed: processed,
|
|
46
|
+
redacted: redacted.length > 0 ? redacted : null
|
|
47
|
+
}
|
|
48
|
+
} else {
|
|
49
|
+
if (typeof prompt === 'string') {
|
|
50
|
+
processed.push({
|
|
51
|
+
text: prompt
|
|
52
|
+
});
|
|
53
|
+
} else if (_shouldTranslate(prompt)) {
|
|
54
|
+
processed.push(prompt);
|
|
55
|
+
} else {
|
|
56
|
+
processed.push({text: '', translate: false});
|
|
57
|
+
redacted.push(prompt);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
processed: processed,
|
|
62
|
+
redacted: redacted.length > 0 ? redacted : null
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const _getPrompt = async (prompt, language, defaultLanguage, apiKey) => {
|
|
67
|
+
if (!apiKey) {
|
|
68
|
+
throw new Error('Missing API Key!')
|
|
69
|
+
}
|
|
70
|
+
if (language === defaultLanguage) {
|
|
71
|
+
return _constructPrompt(prompt);
|
|
72
|
+
}
|
|
73
|
+
const { processed, redacted } = _processPrompt(prompt);
|
|
74
|
+
const response = await fetch('https://api.gtx.dev/prompt', {
|
|
75
|
+
method: 'POST',
|
|
76
|
+
headers: {
|
|
77
|
+
'Content-Type': 'application/json',
|
|
78
|
+
'gtx-api-key': apiKey
|
|
79
|
+
},
|
|
80
|
+
body: JSON.stringify({
|
|
81
|
+
prompt: processed,
|
|
82
|
+
language: language,
|
|
83
|
+
defaultLanguage: defaultLanguage
|
|
84
|
+
})
|
|
85
|
+
})
|
|
86
|
+
if (!response.ok) {
|
|
87
|
+
const result = await response.text();
|
|
88
|
+
throw new Error(`${result || response.status}`);
|
|
89
|
+
} else {
|
|
90
|
+
const result = await response.json();
|
|
91
|
+
return _constructPrompt(result, redacted);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
module.exports = {
|
|
96
|
+
_getPrompt
|
|
97
|
+
}
|