Skip to content

Commit 3578a86

Browse files
committed
Optimized responses error handling and translation logic for cost considerations
1 parent 74a4cf5 commit 3578a86

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

i18n/controllers/recurTranslate.ts

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ const ignoredTags = [
2525
"SCHEMEINLINE",
2626
"SCHEME",
2727
"LONG_PAGE",
28-
"LABEL"
28+
"LABEL",
29+
"HISTORY"
2930
];
3031

3132
const MAXLEN = Number(process.env.MAX_LEN) || 3000;
3233

3334
// change to true to avoid calling openai api, useful for troubleshooting
3435
// chunking logic
35-
const troubleshoot = true;
36+
const troubleshoot = false;
3637

3738
// Centralized logging to prevent duplicate messages
3839
const errorMessages = new Set();
@@ -412,33 +413,46 @@ async function recursivelyTranslate(
412413
try {
413414
await ai.beta.threads.messages.create(thread.id, {
414415
role: "user",
415-
content: `Translate this content to ${language}.
416-
IMPORTANT: You MUST search the uploaded reference file for any technical terms and use EXACTLY the translations specified there.
417-
If a term exists in the reference file, use that translation without deviation.
418-
Do not modify XML tags, attributes of XML tags and structure. Do not say anything else.
419-
Content to translate:
420-
<TRANSLATE> ${chunk} </TRANSLATE>`
416+
content: `<TRANSLATE> ${chunk} </TRANSLATE>`
421417
});
422418

423419
const run = await ai.beta.threads.runs.createAndPoll(thread.id, {
424-
assistant_id: assistant_id
420+
assistant_id: assistant_id,
421+
truncation_strategy: {
422+
type: "last_messages",
423+
last_messages: Number(process.env.CONTEXT) || 3
424+
}
425425
});
426426

427427
const messages = await ai.beta.threads.messages.list(thread.id, {
428-
run_id: run.id
428+
run_id: run.id,
429+
limit: Number(process.env.CONTEXT || 3)
429430
});
430431

431-
const message = messages.data.pop()!;
432-
const messageContent = message?.content[0];
432+
const message = messages.data.pop();
433+
434+
if (message) {
435+
if (message.status === "incomplete") {
436+
throw new Error(
437+
"AI Error: incomplete response: " + message.incomplete_details
438+
);
439+
}
440+
441+
if (message.content[0].type !== "text") {
442+
throw new Error("AI Error: Unexpected response format");
443+
}
433444

434-
if (!messageContent) throw new Error(`undefined AI response`);
435-
if (messageContent.type !== "text") {
436-
throw new Error(
437-
`Unexpected message content type: ${messageContent.type}`
438-
);
445+
if (
446+
run.usage &&
447+
run.usage.total_tokens > Number(process.env.TOKEN_WARNING || 10000)
448+
) {
449+
console.warn("warning: high token usage", run.usage);
450+
}
451+
} else {
452+
throw new Error("AI Error: Undefined Response");
439453
}
440454

441-
const text = messageContent.text;
455+
const text = message.content[0].text;
442456

443457
const safeText = escapeXML(text.value);
444458
const textStream = Readable.from("<WRAPPER>" + safeText + "</WRAPPER>");

0 commit comments

Comments
 (0)