generaltranslation 1.1.11 → 1.1.13

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 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)
@@ -58,12 +64,8 @@ We continually benchmark AI models and add new models as they are released. That
58
64
  Get the latest list of models for which there is data. Returns an array of model names.
59
65
 
60
66
  ```
61
- async function main() {
62
- const models = await gt.getModelList();
63
- console.log(models) // ['gpt-4', ... ]
64
- }
65
-
66
- main();
67
+ const models = await gt.getModelList();
68
+ console.log(models) // ['gpt-4', ... ]
67
69
  ```
68
70
 
69
71
  ### async getModelLanguages(model)
@@ -71,27 +73,81 @@ main();
71
73
  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
74
 
73
75
  ```
74
- async function main() {
75
- const languages = await gt.getModelLanguages('mixtral-8x7b')
76
- console.log(languages) // ['en', 'fr', 'de', 'es', 'it']
77
- }
78
-
79
- main();
76
+ const languages = await gt.getModelLanguages('mixtral-8x7b')
77
+ console.log(languages) // ['en', 'fr', 'de', 'es', 'it']
80
78
  ```
81
79
 
82
- ### async isSupportedLanguage(model, language)
80
+ ### async isSupportedLanguage(model, code)
83
81
 
84
82
  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
83
 
86
84
  ```
87
- async function main() {
88
- const supported = await gt.isSupportedLanguage('gpt-4', 'fr')
89
- console.log(supported) // true
90
- }
91
-
92
- main();
85
+ const supported = await gt.isSupportedLanguage('gpt-4', 'fr')
86
+ console.log(supported) // true
93
87
  ```
94
88
 
95
89
  ## API
96
90
 
97
- Coming soon!
91
+ For these functions, you need to sign up for an API key at <a href='https://generaltranslation.com' target='_blank'>generaltranslation.com</a>.
92
+
93
+ There's a small, free allowance to let you test out the API without payment information.
94
+
95
+ Add the API key to your code like this:
96
+
97
+ ```
98
+ import GT from 'generaltranslation'
99
+
100
+ const gt = new GT({
101
+ apiKey: process.env.GT_API_KEY // looks like 'gtx-XXX'
102
+ });
103
+ ```
104
+
105
+ ### async gt.getPrompt(prompt, code)
106
+
107
+ 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.
108
+
109
+ Just wrap `gt.getPrompt` around your prompt and go.
110
+
111
+ All of the following are valid:
112
+
113
+ ```
114
+ const translatedPrompt = await gt.getPrompt('Tell me a story', 'es');
115
+ ```
116
+
117
+ ```
118
+ const first = 'Tell me a story ';
119
+ const second = 'about a cat'
120
+
121
+ const translatedPrompt = await gt.getPrompt([
122
+ first, second
123
+ ], 'es');
124
+ ```
125
+
126
+ 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:
127
+
128
+ ```
129
+ const prompt = 'Tell me a story about ';
130
+ const input = 'gatos con espadas'
131
+
132
+ const translatedPrompt = await gt.getPrompt([
133
+ prompt, { text: input, translate: false }
134
+ ], 'es');
135
+ ```
136
+
137
+ For type consistency, you can also make everything in the prompt parameter an object:
138
+
139
+ ```
140
+ const prompt = 'Tell me a story about ';
141
+ const input = 'gatos con espadas'
142
+
143
+ const translatedPrompt = await gt.getPrompt([
144
+ { text: prompt },
145
+ { text: input, translate: false }
146
+ ], 'es');
147
+ ```
148
+
149
+ This also works:
150
+
151
+ ```
152
+ const translatedPrompt = await gt.getPrompt({ text: 'Tell me a story' }, 'es');
153
+ ```
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, language) => {
33
- return await _getPrompt(prompt, language, this.defaultLanguage, this.apiKey);
32
+ getPrompt = async (prompt, code) => {
33
+ return await _getPrompt(prompt, code, this.defaultLanguage, this.apiKey);
34
34
  }
35
35
 
36
36
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "generaltranslation",
3
- "version": "1.1.11",
3
+ "version": "1.1.13",
4
4
  "description": "A language toolkit for AI developers",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -64,12 +64,12 @@ 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, language, defaultLanguage, apiKey) => {
67
+ const _getPrompt = async (prompt, code, defaultLanguage, apiKey) => {
68
68
  try {
69
69
  if (!apiKey) {
70
70
  throw new Error('Missing API Key!')
71
71
  }
72
- if (language === defaultLanguage) {
72
+ if (code === defaultLanguage) {
73
73
  return _constructPrompt(prompt);
74
74
  }
75
75
  const { processed, redacted } = _processPrompt(prompt);
@@ -81,7 +81,7 @@ const _getPrompt = async (prompt, language, defaultLanguage, apiKey) => {
81
81
  },
82
82
  body: JSON.stringify({
83
83
  prompt: processed,
84
- language: language,
84
+ language: code,
85
85
  defaultLanguage: defaultLanguage
86
86
  })
87
87
  })