generaltranslation 1.1.11 → 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 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)
@@ -79,7 +85,7 @@ async function main() {
79
85
  main();
80
86
  ```
81
87
 
82
- ### async isSupportedLanguage(model, language)
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
- Coming soon!
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, 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.12",
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
  })