generaltranslation 1.1.10 → 1.1.12
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 +70 -6
- package/index.js +2 -2
- package/models/models.js +2 -2
- package/package.json +1 -1
- package/prompts/prompts.js +30 -25
package/README.md
CHANGED
|
@@ -8,13 +8,13 @@ Note: this package is in active development.
|
|
|
8
8
|
|
|
9
9
|
## Getting Started
|
|
10
10
|
|
|
11
|
-
In your terminal
|
|
11
|
+
<b>In your terminal:</b>
|
|
12
12
|
|
|
13
13
|
```
|
|
14
14
|
npm i generaltranslation
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
In your code
|
|
17
|
+
<b>In your code:</b>
|
|
18
18
|
|
|
19
19
|
```
|
|
20
20
|
import GT from 'generaltranslation'
|
|
@@ -23,6 +23,12 @@ import GT from 'generaltranslation'
|
|
|
23
23
|
const gt = new GT()
|
|
24
24
|
```
|
|
25
25
|
|
|
26
|
+
or, import functions directly:
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
import { getLanguageName } from 'generaltranslation'
|
|
30
|
+
```
|
|
31
|
+
|
|
26
32
|
## Convert between languages and ISO-639 codes
|
|
27
33
|
|
|
28
34
|
### getLanguageName(codes)
|
|
@@ -49,7 +55,7 @@ const codes = gt.getLanguageCodes(['French', 'Spanish'])
|
|
|
49
55
|
console.log(codes) // ['fr', 'es']
|
|
50
56
|
```
|
|
51
57
|
|
|
52
|
-
## Which languages do AI models
|
|
58
|
+
## Which languages do AI models understand?
|
|
53
59
|
|
|
54
60
|
We continually benchmark AI models and add new models as they are released. That means these functions have to be <code>async</code>. This information is provided as a public service. It's completely free and requires no API key.
|
|
55
61
|
|
|
@@ -68,7 +74,7 @@ main();
|
|
|
68
74
|
|
|
69
75
|
### async getModelLanguages(model)
|
|
70
76
|
|
|
71
|
-
Get all languages known to be compatible with a given AI model. Returns an array of languages codes, or
|
|
77
|
+
Get all languages known to be compatible with a given AI model. Returns an array of languages codes, or null if the model is unknown.
|
|
72
78
|
|
|
73
79
|
```
|
|
74
80
|
async function main() {
|
|
@@ -79,7 +85,7 @@ async function main() {
|
|
|
79
85
|
main();
|
|
80
86
|
```
|
|
81
87
|
|
|
82
|
-
### async isSupportedLanguage(model,
|
|
88
|
+
### async isSupportedLanguage(model, code)
|
|
83
89
|
|
|
84
90
|
Returns true if a model is known to be compatible with a given language, represented by an ISO-639 language code. Returns false otherwise.
|
|
85
91
|
|
|
@@ -94,4 +100,62 @@ main();
|
|
|
94
100
|
|
|
95
101
|
## API
|
|
96
102
|
|
|
97
|
-
|
|
103
|
+
For these functions, you need to sign up for an API key at <a href='https://generaltranslation.com' target='_blank'>generaltranslation.com</a>.
|
|
104
|
+
|
|
105
|
+
Add the API key to your code like this:
|
|
106
|
+
|
|
107
|
+
```
|
|
108
|
+
import GT from 'generaltranslation'
|
|
109
|
+
|
|
110
|
+
const gt = new GT({
|
|
111
|
+
apiKey: process.env.GT_API_KEY // looks like 'gtx-XXX'
|
|
112
|
+
});
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### async gt.getPrompt(prompt, code)
|
|
116
|
+
|
|
117
|
+
Translates prompt into the language represented by an ISO-639 language code. Designed for translating prompts into other languages, to internationalize responses from AI models.
|
|
118
|
+
|
|
119
|
+
Just wrap `gt.getPrompt` around your prompt and go. All of the following are valid:
|
|
120
|
+
|
|
121
|
+
```
|
|
122
|
+
const translatedPrompt = await gt.getPrompt('Tell me a story', 'es');
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
```
|
|
126
|
+
const first = 'Tell me a story ';
|
|
127
|
+
const second = 'about a cat'
|
|
128
|
+
|
|
129
|
+
const translatedPrompt = await gt.getPrompt([
|
|
130
|
+
first, second
|
|
131
|
+
], 'es');
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
To mark text that shouldn't be translated, wrap it in `{ text: "", translate: false }`. Items marked as `translate: false` are never sent to our API. For example:
|
|
135
|
+
|
|
136
|
+
```
|
|
137
|
+
const prompt = 'Tell me a story about ';
|
|
138
|
+
const input = 'gatos con espadas'
|
|
139
|
+
|
|
140
|
+
const translatedPrompt = await gt.getPrompt([
|
|
141
|
+
prompt, { text: input, translate: false }
|
|
142
|
+
], 'es');
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
For type consistency, you can also make everything in the prompt parameter an object:
|
|
146
|
+
|
|
147
|
+
```
|
|
148
|
+
const prompt = 'Tell me a story about ';
|
|
149
|
+
const input = 'gatos con espadas'
|
|
150
|
+
|
|
151
|
+
const translatedPrompt = await gt.getPrompt([
|
|
152
|
+
{ text: prompt },
|
|
153
|
+
{ text: input, translate: false }
|
|
154
|
+
], 'es');
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
This also works:
|
|
158
|
+
|
|
159
|
+
```
|
|
160
|
+
const translatedPrompt = await gt.getPrompt({ text: 'Tell me a story' }, 'es');
|
|
161
|
+
```
|
package/index.js
CHANGED
|
@@ -29,8 +29,8 @@ class GT {
|
|
|
29
29
|
isLanguageSupported = _isLanguageSupported; // e.g. ('mistral-7b', 'en') => true
|
|
30
30
|
|
|
31
31
|
// Prompt internationalization
|
|
32
|
-
getPrompt = async (prompt,
|
|
33
|
-
return await _getPrompt(prompt,
|
|
32
|
+
getPrompt = async (prompt, code) => {
|
|
33
|
+
return await _getPrompt(prompt, code, this.defaultLanguage, this.apiKey);
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
}
|
package/models/models.js
CHANGED
|
@@ -29,10 +29,10 @@ const _getModelInfo = async model => {
|
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
// Get all languages known to be compatible with a given LLM
|
|
32
|
-
// Returns an array of languages codes, or
|
|
32
|
+
// Returns an array of languages codes, or null if unknown
|
|
33
33
|
const _getModelLanguages = async model => {
|
|
34
34
|
const modelInfo = await _getModelInfo(model);
|
|
35
|
-
return modelInfo?.languages
|
|
35
|
+
return modelInfo?.languages;
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
// Returns true if a model is known to be compatible with a language
|
package/package.json
CHANGED
package/prompts/prompts.js
CHANGED
|
@@ -64,32 +64,37 @@ const _constructPrompt = ({translated, redacted = null}) => {
|
|
|
64
64
|
|
|
65
65
|
// Get a translated prompt via General Translation API
|
|
66
66
|
// Returns prompt string
|
|
67
|
-
const _getPrompt = async (prompt,
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
67
|
+
const _getPrompt = async (prompt, code, defaultLanguage, apiKey) => {
|
|
68
|
+
try {
|
|
69
|
+
if (!apiKey) {
|
|
70
|
+
throw new Error('Missing API Key!')
|
|
71
|
+
}
|
|
72
|
+
if (code === defaultLanguage) {
|
|
73
|
+
return _constructPrompt(prompt);
|
|
74
|
+
}
|
|
75
|
+
const { processed, redacted } = _processPrompt(prompt);
|
|
76
|
+
const response = await fetch('http://prompts.gtx.dev/internationalize', {
|
|
77
|
+
method: 'POST',
|
|
78
|
+
headers: {
|
|
79
|
+
'Content-Type': 'application/json',
|
|
80
|
+
'gtx-api-key': apiKey
|
|
81
|
+
},
|
|
82
|
+
body: JSON.stringify({
|
|
83
|
+
prompt: processed,
|
|
84
|
+
language: code,
|
|
85
|
+
defaultLanguage: defaultLanguage
|
|
86
|
+
})
|
|
85
87
|
})
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
88
|
+
if (!response.ok) {
|
|
89
|
+
const result = await response.text();
|
|
90
|
+
throw new Error(`${result || response.status}`);
|
|
91
|
+
} else {
|
|
92
|
+
const result = await response.json();
|
|
93
|
+
return _constructPrompt({translated: result, redacted: redacted});
|
|
94
|
+
}
|
|
95
|
+
} catch (error) {
|
|
96
|
+
console.error(error)
|
|
97
|
+
return _constructPrompt({ translated: prompt })
|
|
93
98
|
}
|
|
94
99
|
}
|
|
95
100
|
|