generaltranslation 1.1.21 → 1.1.23

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
@@ -102,23 +102,23 @@ const gt = new GT({
102
102
  });
103
103
  ```
104
104
 
105
- ### async getPrompt(prompt, language)
105
+ ### async translatePrompt(prompt, language)
106
106
 
107
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
108
 
109
- Just wrap `getPrompt` around your prompt and go.
109
+ Just wrap `translatePrompt` around your prompt and go.
110
110
 
111
111
  All of the following are valid:
112
112
 
113
113
  ```
114
- const translatedPrompt = await gt.getPrompt('Tell me a story', 'es');
114
+ const translatedPrompt = await gt.translatePrompt('Tell me a story', 'es');
115
115
  ```
116
116
 
117
117
  ```
118
118
  const first = 'Tell me a story ';
119
119
  const second = 'about a cat'
120
120
 
121
- const translatedPrompt = await gt.getPrompt([
121
+ const translatedPrompt = await gt.translatePrompt([
122
122
  first, second
123
123
  ], 'es');
124
124
  ```
@@ -129,7 +129,7 @@ To mark text that shouldn't be translated, wrap it in `{ text: "", translate: fa
129
129
  const prompt = 'Tell me a story about ';
130
130
  const input = 'gatos con espadas'
131
131
 
132
- const translatedPrompt = await gt.getPrompt([
132
+ const translatedPrompt = await gt.translatePrompt([
133
133
  prompt, { text: input, translate: false }
134
134
  ], 'es');
135
135
  ```
@@ -140,7 +140,7 @@ For type consistency, you can also make everything in the prompt parameter an ob
140
140
  const prompt = 'Tell me a story about ';
141
141
  const input = 'gatos con espadas'
142
142
 
143
- const translatedPrompt = await gt.getPrompt([
143
+ const translatedPrompt = await gt.translatePrompt([
144
144
  { text: prompt },
145
145
  { text: input, translate: false }
146
146
  ], 'es');
@@ -149,5 +149,5 @@ const translatedPrompt = await gt.getPrompt([
149
149
  This also works:
150
150
 
151
151
  ```
152
- const translatedPrompt = await gt.getPrompt({ text: 'Tell me a story' }, 'es');
152
+ const translatedPrompt = await gt.translatePrompt({ text: 'Tell me a story' }, 'es');
153
153
  ```
package/index.js CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  const { _getLanguageName, _getLanguageCode } = require('./codes/codes.js');
7
7
  const { _getModelList, _getModelInfo, _getAllModelInfo, _getModelLanguages, _isLanguageSupported, _getModelsByDeveloper, _getModelsByLanguage, } = require('./models/models.js');
8
- const { _translate } = require('./translate/translate.js');
8
+ const { _translatePrompt } = require('./prompts/translate.js');
9
9
 
10
10
  // ----- CORE CLASS ----- //
11
11
 
@@ -13,10 +13,12 @@ class GT {
13
13
 
14
14
  constructor({
15
15
  apiKey = '',
16
- defaultLanguage = 'en'
16
+ defaultLanguage = 'en',
17
+ baseURL = 'https://translate.gtx.dev'
17
18
  } = {}) {
18
19
  this.apiKey = apiKey;
19
20
  this.defaultLanguage = defaultLanguage;
21
+ this.baseURL = baseURL;
20
22
  }
21
23
 
22
24
  // Language code functions
@@ -32,13 +34,11 @@ class GT {
32
34
  getModelsByLanguage = _getModelsByLanguage; // returns array of model names
33
35
  getModelsByDeveloper = _getModelsByDeveloper; // returns array of model names
34
36
 
35
- // Prompt internationalization
36
- getPrompt = async (prompt, language) => {
37
- return await _translate(prompt, language, this.defaultLanguage, this.apiKey);
38
- }
39
- // Translation (same as prompt internationalization)
40
- translate = async (content, language) => {
41
- return await _translate(content, language, this.defaultLanguage, this.apiKey);
37
+ // Prompt I18N
38
+ translatePrompt = async (prompt, language) => {
39
+ return await _translatePrompt({
40
+ content: prompt, language: language, context: this
41
+ });
42
42
  }
43
43
 
44
44
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "generaltranslation",
3
- "version": "1.1.21",
3
+ "version": "1.1.23",
4
4
  "description": "A language toolkit for AI developers",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -5,7 +5,7 @@ const _shouldTranslate = item => typeof item?.translate === 'boolean' ? item.tra
5
5
 
6
6
  // Pre-processes content to send to the API
7
7
  // Separates out text that shouldn't be translated.
8
- const _processContent = (content) => {
8
+ const _processContent = ({ content }) => {
9
9
  const processed = [];
10
10
  const untranslated = [];
11
11
  if (Array.isArray(content)) {
@@ -64,16 +64,24 @@ const _constructContent = ({ content, untranslated = null}) => {
64
64
 
65
65
  // Get a translation via General Translation API
66
66
  // Returns string
67
- const _translate = async (content, language, defaultLanguage, apiKey) => {
67
+ const _translatePrompt = async ({
68
+ content, language, context
69
+ }) => {
70
+
71
+ const apiKey = context?.apiKey;
68
72
  if (!apiKey) {
69
73
  throw new Error('Missing API Key!')
70
- }
74
+ };
75
+
76
+ const defaultLanguage = context?.defaultLanguage;
71
77
  if (language === defaultLanguage) {
72
78
  return _constructContent({ content: content });
73
- }
74
- const { processed, untranslated } = _processContent(content);
79
+ };
80
+
81
+ const { processed, untranslated } = _processContent({ content });
82
+
75
83
  try {
76
- const response = await fetch('http://translate.gtx.dev', {
84
+ const response = await fetch(`${context?.baseURL}/prompt`, {
77
85
  method: 'POST',
78
86
  headers: {
79
87
  'Content-Type': 'application/json',
@@ -96,8 +104,9 @@ const _translate = async (content, language, defaultLanguage, apiKey) => {
96
104
  console.error(error)
97
105
  return _constructContent({ content: content })
98
106
  }
107
+
99
108
  }
100
109
 
101
110
  module.exports = {
102
- _translate
111
+ _translatePrompt
103
112
  }