gptrans 1.4.8 → 1.5.0
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/db/gptrans_en.json +9 -0
- package/demo/case_4.js +2 -2
- package/index.js +40 -34
- package/isoAssoc.js +6 -2
- package/package.json +1 -1
- package/prompt/translate.md +2 -0
package/demo/case_4.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import GPTrans from '../index.js';
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
// Case 2: Translate from Spanish
|
|
4
|
+
// Case 2: Translate from Spanish to English
|
|
5
5
|
const model = new GPTrans({
|
|
6
6
|
from: 'es',
|
|
7
|
-
target: '
|
|
7
|
+
target: 'en',
|
|
8
8
|
});
|
|
9
9
|
|
|
10
10
|
await model.preload();
|
package/index.js
CHANGED
|
@@ -9,7 +9,16 @@ class GPTrans {
|
|
|
9
9
|
|
|
10
10
|
static get mmix() {
|
|
11
11
|
if (!this.#mmixInstance) {
|
|
12
|
-
const mmix = new ModelMix(
|
|
12
|
+
const mmix = new ModelMix({
|
|
13
|
+
config: {
|
|
14
|
+
max_history: 1,
|
|
15
|
+
debug: false,
|
|
16
|
+
bottleneck: {
|
|
17
|
+
minTime: 15000,
|
|
18
|
+
maxConcurrent: 1
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
});
|
|
13
22
|
|
|
14
23
|
mmix.attach(new MixOpenAI());
|
|
15
24
|
mmix.attach(new MixAnthropic());
|
|
@@ -23,7 +32,7 @@ class GPTrans {
|
|
|
23
32
|
return isLanguageAvailable(langCode);
|
|
24
33
|
}
|
|
25
34
|
|
|
26
|
-
constructor({ from = 'en-US', target = 'es-AR', model = 'claude-3-7-sonnet-20250219', batchThreshold =
|
|
35
|
+
constructor({ from = 'en-US', target = 'es-AR', model = 'claude-3-7-sonnet-20250219', batchThreshold = 1500, debounceTimeout = 500, promptFile = null, context = '' }) {
|
|
27
36
|
|
|
28
37
|
try {
|
|
29
38
|
dotenv.config();
|
|
@@ -50,13 +59,6 @@ class GPTrans {
|
|
|
50
59
|
this.promptFile = promptFile ?? new URL('./prompt/translate.md', import.meta.url).pathname;
|
|
51
60
|
this.context = context;
|
|
52
61
|
this.modelConfig = {
|
|
53
|
-
config: {
|
|
54
|
-
max_history: 1,
|
|
55
|
-
debug: false,
|
|
56
|
-
bottleneck: {
|
|
57
|
-
maxConcurrent: 2,
|
|
58
|
-
}
|
|
59
|
-
},
|
|
60
62
|
options: {
|
|
61
63
|
max_tokens: batchThreshold,
|
|
62
64
|
temperature: 0
|
|
@@ -85,6 +87,11 @@ class GPTrans {
|
|
|
85
87
|
}
|
|
86
88
|
|
|
87
89
|
get(key, text) {
|
|
90
|
+
|
|
91
|
+
if (!text || !text.trim()) {
|
|
92
|
+
return text;
|
|
93
|
+
}
|
|
94
|
+
|
|
88
95
|
const contextHash = this._hash(this.context);
|
|
89
96
|
const translation = this.dbTarget.get(contextHash, key);
|
|
90
97
|
|
|
@@ -129,7 +136,11 @@ class GPTrans {
|
|
|
129
136
|
|
|
130
137
|
// Clear pending translations and character count before awaiting translation
|
|
131
138
|
this.pendingTranslations.clear();
|
|
139
|
+
|
|
132
140
|
this.modelConfig.options.max_tokens = this.pendingCharCount + 1000;
|
|
141
|
+
const minTime = Math.floor((60000 / (8000 / this.pendingCharCount)) * 1.4);
|
|
142
|
+
GPTrans.mmix.limiter.updateSettings({ minTime });
|
|
143
|
+
|
|
133
144
|
this.pendingCharCount = 0;
|
|
134
145
|
|
|
135
146
|
const textsToTranslate = batch.map(([_, text]) => text).join('\n---\n');
|
|
@@ -139,6 +150,14 @@ class GPTrans {
|
|
|
139
150
|
|
|
140
151
|
const contextHash = this._hash(context);
|
|
141
152
|
batch.forEach(([key], index) => {
|
|
153
|
+
|
|
154
|
+
if (!translatedTexts[index]) {
|
|
155
|
+
console.log(translations);
|
|
156
|
+
console.error(`No translation found for ${key}`);
|
|
157
|
+
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
|
|
142
161
|
this.dbTarget.set(contextHash, key, translatedTexts[index].trim());
|
|
143
162
|
});
|
|
144
163
|
|
|
@@ -188,38 +207,25 @@ class GPTrans {
|
|
|
188
207
|
return stringHash(input).toString(36);
|
|
189
208
|
}
|
|
190
209
|
|
|
191
|
-
async preload(
|
|
192
|
-
|
|
193
|
-
// Create new GPTrans instance for the target language
|
|
194
|
-
const translator = new GPTrans({
|
|
195
|
-
from,
|
|
196
|
-
target,
|
|
197
|
-
model,
|
|
198
|
-
batchThreshold,
|
|
199
|
-
debounceTimeout,
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
// Process all entries in batches
|
|
210
|
+
async preload() {
|
|
203
211
|
for (const [context, pairs] of this.dbFrom.entries()) {
|
|
204
|
-
|
|
212
|
+
this.setContext(context);
|
|
205
213
|
for (const [key, text] of Object.entries(pairs)) {
|
|
206
|
-
|
|
214
|
+
this.get(key, text);
|
|
207
215
|
}
|
|
208
216
|
}
|
|
209
217
|
|
|
210
218
|
// Wait for any pending translations to complete
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
});
|
|
220
|
-
}
|
|
219
|
+
await new Promise(resolve => {
|
|
220
|
+
const checkInterval = setInterval(() => {
|
|
221
|
+
if (this.dbFrom.keys().length === this.dbTarget.keys().length) {
|
|
222
|
+
clearInterval(checkInterval);
|
|
223
|
+
resolve();
|
|
224
|
+
}
|
|
225
|
+
}, 100);
|
|
226
|
+
});
|
|
221
227
|
|
|
222
|
-
return
|
|
228
|
+
return this;
|
|
223
229
|
}
|
|
224
230
|
}
|
|
225
231
|
|
package/isoAssoc.js
CHANGED
|
@@ -180,10 +180,14 @@ export function isoAssoc(iso, prefix = '') {
|
|
|
180
180
|
|
|
181
181
|
const parts = iso.toLowerCase().split('-');
|
|
182
182
|
const lang = parts[0];
|
|
183
|
-
|
|
183
|
+
let country = parts.length > 1 ? parts[1] : null;
|
|
184
184
|
|
|
185
|
-
|
|
185
|
+
if (lang === 'en' && !country) {
|
|
186
|
+
country = 'us';
|
|
187
|
+
}
|
|
186
188
|
|
|
189
|
+
let denonym = country ? countryDenonym[country] : 'Neutral';
|
|
190
|
+
|
|
187
191
|
if (lang === 'zh' && !country) {
|
|
188
192
|
denonym = 'Simplified';
|
|
189
193
|
}
|
package/package.json
CHANGED