0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-03-10 22:22:45 -05:00

fix(cli): polish translate prompts (#3955)

This commit is contained in:
wangsijie 2023-06-08 11:23:31 +09:00 committed by GitHub
parent ec6e266705
commit b4857dbf7b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 22 deletions

View file

@ -8,7 +8,7 @@ import { z } from 'zod';
import { consoleLog, getProxy } from '../../utils.js';
import { getTranslationPrompt } from './prompts.js';
import { getTranslationPromptMessages } from './prompts.js';
export const createOpenaiApi = () => {
const proxy = getProxy();
@ -51,16 +51,11 @@ export const translate = async ({
.post('chat/completions', {
json: {
model: 'gpt-3.5-turbo',
messages: [
{
role: 'user',
content: getTranslationPrompt({
sourceFileContent,
targetLanguage,
extraPrompt,
}),
},
],
messages: getTranslationPromptMessages({
sourceFileContent,
targetLanguage,
extraPrompt,
}),
},
})
.json(),

View file

@ -13,19 +13,20 @@ type GetTranslationPromptProperties = {
* Remember to check the token limit before adding more prompt.
* Tokens can be counted in https://platform.openai.com/tokenizer
*/
export const getTranslationPrompt = ({
export const getTranslationPromptMessages = ({
sourceFileContent,
targetLanguage,
extraPrompt,
}: GetTranslationPromptProperties) => `Given the following code snippet:
\`\`\`ts
${sourceFileContent}
\`\`\`
only translate object values to ${
languages[targetLanguage]
}, keep all object keys original, output ts code only, the code format should be strictly consistent, and should not contains the given code snippet.
}: GetTranslationPromptProperties) => [
{
role: 'assistant',
content: `You are a translate assistant of a Typescript engenieer, when you receive a code snippet that contains an object, translate those values that are marked with comment "// UNTRANSLATED" into the language ${
languages[targetLanguage]
}, keep all object keys original, output ts code only, the code format should be strictly consistent, and should not contain the given code snippet. ${conditionalString(
extraPrompt
)}
Take zh-cn as an example, if the input is:
Take Chinese as an example, if the input is:
\`\`\`ts
import others from './others.js';
@ -48,5 +49,12 @@ const translation = {
};
export default translation;
\`\`\`
${conditionalString(extraPrompt)}`;
\`\`\``,
},
{
role: 'user',
content: `\`\`\`ts
${sourceFileContent}
\`\`\``,
},
];