From 58ce61eab8948f3f107f8f32da57c85d2cd282e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Guti=C3=A9rrez?= Date: Thu, 9 Mar 2023 05:13:58 -0600 Subject: [PATCH] feat: language support via `locale` config (#96) Co-authored-by: hiroki osame --- src/commands/aicommits.ts | 2 ++ src/commands/prepare-commit-msg-hook.ts | 3 ++- src/utils/config.ts | 6 ++++++ src/utils/openai.ts | 5 +++-- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/commands/aicommits.ts b/src/commands/aicommits.ts index 2ed074c..0313474 100644 --- a/src/commands/aicommits.ts +++ b/src/commands/aicommits.ts @@ -36,6 +36,7 @@ export default async ( const config = await getConfig(); const OPENAI_KEY = process.env.OPENAI_KEY ?? process.env.OPENAI_API_KEY ?? config.OPENAI_KEY; + const locale = config.locale ?? 'en'; if (!OPENAI_KEY) { throw new KnownError('Please set your OpenAI API key via `aicommits config set OPENAI_KEY=`'); } @@ -44,6 +45,7 @@ export default async ( s.start('The AI is analyzing your changes'); const messages = await generateCommitMessage( OPENAI_KEY, + locale, staged.diff, generate, ); diff --git a/src/commands/prepare-commit-msg-hook.ts b/src/commands/prepare-commit-msg-hook.ts index ba05292..6f0cdf2 100644 --- a/src/commands/prepare-commit-msg-hook.ts +++ b/src/commands/prepare-commit-msg-hook.ts @@ -30,7 +30,7 @@ export default () => (async () => { intro(bgCyan(black(' aicommits '))); - const { OPENAI_KEY, generate } = await getConfig(); + const { OPENAI_KEY, locale = 'en', generate } = await getConfig(); if (!OPENAI_KEY) { throw new KnownError('Please set your OpenAI API key in ~/.aicommits'); } @@ -39,6 +39,7 @@ export default () => (async () => { s.start('The AI is analyzing your changes'); const messages = await generateCommitMessage( OPENAI_KEY, + locale, staged!.diff, generate || 1, ); diff --git a/src/utils/config.ts b/src/utils/config.ts index 7befa72..b52ebde 100644 --- a/src/utils/config.ts +++ b/src/utils/config.ts @@ -26,6 +26,12 @@ const configParsers = { return key; }, + locale(key: string) { + parseAssert('locale', key, 'Cannot be empty'); + parseAssert('locale', /^[a-z-]+$/i.test(key), 'Must be a valid locale (letters and dashes/underscores). You can consult the list of codes in: https://wikipedia.org/wiki/List_of_ISO_639-1_codes'); + + return key; + }, generate(key: string) { parseAssert('generate', key, 'Cannot be empty'); parseAssert('generate', /^\d+$/.test(key), 'Must be an integer'); diff --git a/src/utils/openai.ts b/src/utils/openai.ts index 1c3191f..4e58cae 100644 --- a/src/utils/openai.ts +++ b/src/utils/openai.ts @@ -58,17 +58,18 @@ const sanitizeMessage = (message: string) => message.trim().replace(/[\n\r]/g, ' const deduplicateMessages = (array: string[]) => Array.from(new Set(array)); -const promptTemplate = 'Write an insightful but concise Git commit message in a complete sentence in present tense for the following diff without prefacing it with anything:'; +const getPrompt = (locale: string, diff: string) => `Write an insightful but concise Git commit message in a complete sentence in present tense for the following diff without prefacing it with anything, the response must be in the language ${locale}:\n${diff}`; const model = 'text-davinci-003'; const encoder = encodingForModel(model); export const generateCommitMessage = async ( apiKey: string, + locale: string, diff: string, completions: number, ) => { - const prompt = `${promptTemplate}\n${diff}`; + const prompt = getPrompt(locale, diff); /** * text-davinci-003 has a token limit of 4000