Skip to content

[WIP] Chatgpt api translator#9

Open
a0s wants to merge 2 commits into
translate-tools:masterfrom
a0s:chatgpt-api-translator
Open

[WIP] Chatgpt api translator#9
a0s wants to merge 2 commits into
translate-tools:masterfrom
a0s:chatgpt-api-translator

Conversation

@a0s
Copy link
Copy Markdown

@a0s a0s commented Feb 9, 2025

This is OpenAI API based translator module. Started at translate-tools/linguist#230

@vitonsky Any idea how to debug from inside eval() ? console.log and debugger; both are just not working.

@@ -0,0 +1,78 @@
class ChatGPTTranslator {
constructor(model = 'gpt-4o', openApiKey = "") {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use model gpt-4o-mini? It would save users money and it have quite good quality

Comment on lines +11 to +13
const prompt = (from === 'auto' || from === '')
? `Translate text to ${to}. Only return the translation without any additional text:\n\n${text}`
: `Translate text from ${from} to ${to}. Only return the translation without any additional text:\n\n${text}`;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's generate batch requests to translate array of texts with method translateBatch and then call this method with single text in translate method.

This approach may improve performance dramatically, since Linguist mostly call translateBatch method with as many texts as possible.

If we have 1k texts to translate, in current implementation user would sent 1k requests with prompt overhead, but if we will batch texts, linguist will send 50-200 texts in single request and eventually will sent only 5-20 requests.

Comment on lines +39 to +40
console.error("Translation request failed:", error);
return '';
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If exception has occurs, we must throw an error, to handle it properly. Otherwise user will see empty text in translations and will be confused

Comment on lines +50 to +52
getLengthLimit() {
return 20000;
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How you think is it optimal value?

Linguist uses this number to understand "can we put more text to current batch?" to translate as many texts as possible in single request.

How much chars ChatGPT can handle for single message?

Comment on lines +42 to +46
test('handles length limit correctly', () => {
const longText = 'a'.repeat(21000);
expect(translator.checkLimitExceeding(longText)).toBe(1);
expect(translator.checkLimitExceeding('short text')).toBe(0);
});
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method must return the number of chars that is out of limit. It is necessary for Linguist batch scheduler to efficiently group texts by its size.

You may check example here

checkLimitExceeding = (text) => {
const textLength = !Array.isArray(text)
? text.length
: text.reduce((len, text) => len + text.length, 0);
return textLength - this.getLengthLimit();
};

I think in this test a first assert should expect 1000 (that means we have to cut at least 1k chars) and second one is -19990 (that means we can add up to 19990 characters, and still fit in single request)

@vitonsky
Copy link
Copy Markdown
Member

vitonsky commented Feb 9, 2025

@vitonsky Any idea how to debug from inside eval() ? console.log and debugger; both are just not working.

You may use console log and maybe even debugger;, but code runs in extension context, not in web page context.

Try to open about:debugging#/runtime/this-firefox and click "Inspect" on Linguist extension if you use firefox or open chrome://extensions/ and toggle "Developer mode" then open and inspect Linguist service worker if you on chromium based browser.

There must be logs of custom translator module

@vitonsky vitonsky mentioned this pull request Feb 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants