Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add translation context for OpenAI #1130

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ test/e2e-fixtures-temp/
test/fixtures-temp/
test/fixture-scripts-out/
/.idea/
yarn.lock
package-lock.json
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,12 @@
],
"description": "%config.openai_api_model%"
},
"i18n-ally.translate.openai.appContext": {
"type": "string",
"default": "",
"description": "Detailed context to provide enhanced understanding for translations with OpenAI. ",
"editPresentation": "multilineText"
},
"i18n-ally.usage.scanningIgnore": {
"type": "array",
"items": {
Expand Down
5 changes: 5 additions & 0 deletions src/core/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,11 @@ export class Config {
return this.getConfig<string>('translate.openai.apiModel') ?? 'gpt-3.5-turbo'
}

static get openaiApiAppContext() {
return this.getConfig<string>('translate.openai.appContext') ?? ''
}


static get telemetry(): boolean {
return workspace.getConfiguration().get('telemetry.enableTelemetry') as boolean
}
Expand Down
11 changes: 7 additions & 4 deletions src/translators/engines/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default class OpenAITranslate extends TranslateEngine {
let apiRoot = this.apiRoot;
if (Config.openaiApiRoot) apiRoot = Config.openaiApiRoot.replace(/\/$/, "");
let model = Config.openaiApiModel;
let appContext = Config.openaiApiAppContext;

const response = await axios.post(
`${apiRoot}/v1/chat/completions`,
Expand All @@ -28,7 +29,7 @@ export default class OpenAITranslate extends TranslateEngine {
},
{
role: "user",
content: this.generateUserPrompts(options),
content: this.generateUserPrompts(options,appContext),
},
],
},
Expand Down Expand Up @@ -61,12 +62,14 @@ export default class OpenAITranslate extends TranslateEngine {
return r;
}

generateUserPrompts(options: TranslateOptions): string {
generateUserPrompts(options: TranslateOptions, appContext: string): string {
const sourceLang = options.from;
const targetLang = options.to;

let generatedUserPrompt = `translate from ${sourceLang} to ${targetLang}:\n\n${options.text}`;
const contextInstruction = appContext ? `Please consider the following context for a more accurate translation:\n\nContext Information: ${appContext}\n\n` : '';
let generatedUserPrompt = `${contextInstruction}Given the above context, translate the following from ${sourceLang} to ${targetLang}:\n\n${options.text}`;
Copy link
Collaborator

Choose a reason for hiding this comment

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

in case there is no context, old prompt should be used


return generatedUserPrompt;
}


}
Loading