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

Feat: Add support for multi languages #69

Closed
wants to merge 8 commits into from
7 changes: 7 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ const argv = cli({
alias: 'g',
default: 1,
},
lang: {
type: String,
description: 'ISO code language (2-chars) to use for generating the commit message',
alias: 'l',
default: 'en',
},
},

help: {
Expand Down Expand Up @@ -63,6 +69,7 @@ const argv = cli({
OPENAI_KEY,
staged.diff,
argv.flags.generate,
argv.flags.lang,
);
s.stop('Changes analyzed');

Expand Down
5 changes: 3 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,15 @@ export const getDetectedMessage = (files: string[]) => `Detected ${files.length.

const sanitizeMessage = (message: string) => message.trim().replace(/[\n\r]/g, '').replace(/(\w)\.$/, '$1');

const promptTemplate = 'Write an insightful but concise Git commit message in a complete sentence in imperative present tense for the following diff without prefacing it with anything:';
const getPrompt = (lang: string, diff: string) => `Write an insightful but concise Git commit message in a complete sentence in imperative present tense for the following diff without prefacing it with anything, the response must be in the lang ${lang}:\n${diff}`;

export const generateCommitMessage = async (
apiKey: string,
diff: string,
completions: number,
lang: string,
) => {
const prompt = `${promptTemplate}\n${diff}`;
const prompt = getPrompt(lang, diff);

// Accounting for GPT-3's input req of 4k tokens (approx 8k chars)
if (prompt.length > 8000) {
Expand Down