-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·448 lines (400 loc) · 14.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#!/usr/bin/env node
import dotenv from "dotenv";
import prettier from "prettier";
import { Configuration, OpenAIApi } from "openai";
import { encode } from "gpt-tokenizer";
import fs from "fs";
import chalk from "chalk";
dotenv.config();
const isVerbose = process.env.TRANSLATEGPT_VERBOSE === "true";
const queryMaxSafeguard = Number(process.env.TRANSLATEGPT_MAX_QUERIES);
const openAIModel = process.env.OPENAI_MODEL;
let translateGPTConfig;
import(process.env.TRANSLATEGPT_JS_PATH)
.then((module) => {
translateGPTConfig = module.config;
console.log(chalk.cyan("config: "), JSON.stringify(translateGPTConfig));
init();
})
.catch((error) => {
console.error(chalk.red("Error importing module:"), error);
});
const openAIConfig = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(openAIConfig);
const buildQueries = (mappedTranslateStrings) => {
const tokenLimit = 500;
const queries = [];
let buildingTokens = 0;
let buildingQuery = {};
const getTokenCount = (str) => encode(str).length;
Object.keys(mappedTranslateStrings).forEach((key) => {
if (buildingTokens >= tokenLimit) {
queries.push(JSON.stringify(buildingQuery));
buildingTokens = 0;
buildingQuery = {};
}
if (mappedTranslateStrings[key] === "") {
buildingQuery[key] = "";
buildingTokens += getTokenCount(key) + 4;
}
});
if (Object.keys(buildingQuery).length > 0) {
queries.push(JSON.stringify(buildingQuery));
}
return queries;
};
const generatePrompt = (query, language) => {
const prompt = [
{
role: "system",
content:
"The translations that will be asked for in this conversation will be used in the following way: " +
translateGPTConfig.context +
". The following prompt will give you instructions on what to translate and how",
},
{
role: "user",
content: `This JSON is used in a platform implementing i18n, return it with the empty value strings filled in with the translation of the keys into ${language}. Values in {{}} are used for interpolation, so they should be placed correctly but anything inside {{}} should be not be translated. JSON ONLY. NO DISCUSSION. DO NOT ALTER THE KEYS IN THE JSON. ALWAYS FILL IN ONLY THE EMPTY STRING VALUE WITH YOUR TRANSLATION: ${query} `,
},
];
// Only log the user prompt, since the system prompt doesn't change
console.log(chalk.blue("Prompt: "), prompt[1]);
console.log(
chalk.yellow("Translations are still being generated, please wait.")
);
return prompt;
};
const sendQuery = async (query, language) => {
try {
const completion = await openai.createChatCompletion({
model: openAIModel,
response_format: { type: "json_object" },
messages: generatePrompt(query, language),
temperature: 1.0,
});
const response = completion.data.choices[0].message.content;
console.log(chalk.blue("Query response: "), response);
return response;
} catch (error) {
if (error.response) {
console.error(
chalk.red(error.response.status, JSON.stringify(error.response.data))
);
} else {
console.error(
chalk.red(`Error with OpenAI API request: ${error.message}`)
);
}
}
};
// Merging new data from response into the building output, which should be a running list of translations, some with empty strings that will need to be filled
// Eventually, this building output should be a complete list of translations, and then we can stop running queries... but that's not happening rn!
const generateAppliedResponse = (response, buildingOutput) => {
let appliedResponse = buildingOutput;
let parsedResponse;
try {
const firstIndex = response.indexOf("{");
const lastIndex = response.lastIndexOf("}");
parsedResponse = JSON.parse(response.slice(firstIndex, lastIndex + 1));
console.log(chalk.blue("Parsed response: "), parsedResponse);
} catch {
console.log(
chalk.yellow(`Response parse error, retrying. Response: ${response}`)
);
return buildingOutput;
}
Object.keys(parsedResponse).forEach((key) => {
if (appliedResponse[key] === "") {
appliedResponse[key] = parsedResponse[key];
}
});
return appliedResponse;
};
// Returns a JSON object with the source translations that are missing in the output JSON
// Only adds to returned result if the key from source does not exist in output
const addMissingSourceTranslations = (
sourceJSON,
outputJSON,
sourceLanguage
) => {
console.log(chalk.cyan(`Adding translations from: ${sourceLanguage}`));
const addedTranslations = {};
if (sourceJSON && outputJSON) {
console.log(chalk.cyan("JSON from source and output found, merging."));
for (const [key, value] of Object.entries(sourceJSON)) {
if (!outputJSON[key]) {
addedTranslations[value] = "";
}
}
} else if (sourceJSON && !outputJSON) {
console.log(
chalk.cyan("Output file/JSON not found, adding all source translations.")
);
for (const [key, value] of Object.entries(sourceJSON)) {
addedTranslations[value] = "";
}
}
return addedTranslations;
};
const removeOrphanTranslations = (sourceJSON, outputJSON) => {
let didRemoveOrphans = false;
if (sourceJSON && outputJSON) {
console.log(chalk.cyan("Checking for any orphan translations in output (any keys that aren't found in source)"));
for (const [key, value] of Object.entries(outputJSON)) {
if (!sourceJSON[key]) {
console.log(
chalk.yellow(`Removing orphan translation: ${key} - ${value}`)
);
// This actually removes the key from the original outputJSON object passed into removeOrphanTranslations
delete outputJSON[key];
didRemoveOrphans = true;
}
}
}
return didRemoveOrphans;
}
async function translate(addedTranslations, language) {
let buildingOutput = { ...addedTranslations };
console.log(chalk.green("buildOut"), buildingOutput);
let isOutputBuilt = false;
let queryCount = 0;
while (!isOutputBuilt && queryCount < queryMaxSafeguard) {
const queries = buildQueries(buildingOutput);
console.log(chalk.green("Queries"), queries);
if (queries.length === 0) {
console.log(chalk.magenta(`Finished queries`));
isOutputBuilt = true;
return buildingOutput;
}
for (let query of queries) {
const queryResponse = await sendQuery(query, language.language);
queryCount++;
if (queryCount == queryMaxSafeguard) {
console.log(
chalk.red(
`Query count reached max safeguard of ${queryMaxSafeguard}, stopping queries.`
)
);
throw new Error(`Was not able to translate "${query}" into ${language.abbreviation} (or, ${language.language}), please try again or manually add the expected translation to the final language file. Final attempted response was: ${queryResponse}. This is probably due to ChatGPT hallucination`);
}
console.log(chalk.blue("QUERY COUNT: "), queryCount);
if (isValidInterpolations(query, queryResponse)) {
buildingOutput = generateAppliedResponse(queryResponse, buildingOutput);
}
console.log(chalk.green("Building output"), buildingOutput);
}
}
console.log(chalk.green("build output"), buildingOutput);
return buildingOutput;
}
const getInterpolations = (str) => {
const regex = /{{([^}]+)}}/g;
let interpolations;
try {
interpolations = (str.match(regex) || []).reduce((obj, match) => {
const key = match.replace(/{{|}}/g, "");
obj[key] = true;
return obj;
}, {});
} catch {
interpolations = null;
}
return interpolations;
};
const isValidInterpolations = (query, queryResponse) => {
const validInterpolations = getInterpolations(query);
const responseInterpolations = getInterpolations(queryResponse);
if (validInterpolations === null || responseInterpolations === null) {
console.log(chalk.red("Error interpolating query."));
return false;
} else {
const validKeys = Object.keys(validInterpolations);
for (const key of validKeys) {
if (!responseInterpolations[key]) {
return false;
}
}
return validKeys.length === Object.keys(responseInterpolations).length;
}
};
const mergeExistingTranslations = (result, outputFile) => {
if (fs.existsSync(outputFile)) {
const existingJsonData = fs.readFileSync(outputFile, "utf-8");
try {
const parsedExistingData = JSON.parse(existingJsonData);
console.log(
chalk.cyan(
"Destination file already exists, merging existing translations with new translations."
)
);
if (isVerbose) {
console.log(
chalk.cyan(
"Existing translations: ",
JSON.stringify(JSON.stringify(Object.entries(parsedExistingData)))
)
);
console.log(
chalk.cyan(
"New translations: ",
JSON.stringify(Object.entries(result))
)
);
}
const mergedTranslations = { ...parsedExistingData, ...result };
if (isVerbose) {
console.log(chalk.blue("Merged translations: "), mergedTranslations);
} else {
console.log(chalk.blue("Translations merged successfully."));
}
return mergedTranslations;
} catch {
console.log(chalk.cyan("Destination file currently empty."));
return result;
}
} else {
console.log(chalk.cyan("The file does not exist."));
return result;
}
};
const getFileJSON = (filePath) => {
if (fs.existsSync(filePath)) {
if (isVerbose) {
console.log(chalk.cyan(`File found: ${filePath}`));
}
try {
const existingJsonData = fs.readFileSync(filePath, "utf-8");
const parsedExistingData = JSON.parse(existingJsonData);
if (isVerbose) {
console.log(chalk.cyan(`File JSON parsed successfully.`));
}
return parsedExistingData;
} catch {
console.log(chalk.yellow(`Could not parse file JSON`));
}
} else {
console.log(chalk.cyan("The file does not exist."));
return null;
}
};
if (!openAIConfig.apiKey) {
console.log(
chalk.red(
"OpenAI API key not configured, please follow instructions in README.md"
)
);
process.exit(1);
}
const remapSource = (source, output) => {
if (Object.keys(output).length > 0) {
console.log(chalk.yellow("Remapping source keys to output values."));
if (isVerbose) {
console.log(chalk.blue("Source", JSON.stringify(source)));
}
console.log(chalk.blue("Output", JSON.stringify(output)));
const remap = {};
for (const [key, value] of Object.entries(source)) {
if (output[value]) {
remap[key] = output[value];
}
}
console.log(chalk.green("Remapped", JSON.stringify(remap)));
return remap;
} else {
console.log(chalk.yellow("No results received for remapping, skipping."));
return output;
}
};
const init = async () => {
for (const namespace of translateGPTConfig.namespaces) {
const outputDirectory = `${process.env.TRANSLATEGPT_OUTPUT_DIRECTORY}/${namespace}`;
if (!fs.existsSync(outputDirectory)) {
fs.mkdirSync(outputDirectory);
console.log(chalk.cyan("Folder created: "), outputDirectory);
}
console.log(chalk.cyan(`Output directory set to: `), outputDirectory);
for (const language of translateGPTConfig.languages) {
const sourceLanguageAbbreviation =
language.sourceLanguage ?? translateGPTConfig.sourceLanguage;
const sourceLanguageFile = `${outputDirectory}/${namespace}.${sourceLanguageAbbreviation.replace(
/\s/g,
"_"
)}.json`;
console.log(
chalk.cyan(`Source language file set to: `),
sourceLanguageFile
);
const sourceJSON = getFileJSON(sourceLanguageFile);
if (isVerbose) {
console.log(chalk.cyan(`Source JSON set to: `), sourceJSON);
}
const outputFile = `${outputDirectory}/${namespace}.${language.abbreviation.replace(
/\s/g,
"_"
)}.json`;
console.log(chalk.cyan(`Output file set to: `), outputFile);
const outputJSON = getFileJSON(outputFile);
if (isVerbose) {
console.log(chalk.cyan(`Output JSON set to: `), outputJSON);
}
const addedTranslations = addMissingSourceTranslations(
sourceJSON,
outputJSON,
language.sourceLanguage ?? translateGPTConfig.sourceLanguage
);
const didAddTranslations = Object.keys(addedTranslations).length > 0;
let result = outputJSON;
if (didAddTranslations) {
console.log(
chalk.yellow("addedTranslations after data parsing: "),
addedTranslations
);
result = await translate(addedTranslations, language);
console.log(chalk.green("result"), result);
result = remapSource(sourceJSON, result);
result = mergeExistingTranslations(result, outputFile);
}
// Remove any orphan translations (keys that exist in output but not in source)
const didRemoveOrphans = removeOrphanTranslations(sourceJSON, result);
// With no new translations or no orphans removed, there is no need to update any files
if (didAddTranslations || didRemoveOrphans) {
if (isVerbose) {
console.log(
chalk.cyan(
`Attempting to write file. Path: ${outputFile} | Result: ${JSON.stringify(
result
)}`
)
);
}
await new Promise((resolve, reject) => {
fs.writeFile(
outputFile,
prettier.format(JSON.stringify(result), { parser: "json" }),
(err) => {
if (err) {
console.error(err);
reject(err);
} else {
console.log(
chalk.cyan("File written successfully: "),
outputFile
);
resolve();
}
}
);
});
} else {
console.log(
chalk.cyan(
"No new translations or orphans found. File contents are the same as output, skipping file write for: "
),
outputFile
);
}
}
}
};