Skip to content

Commit

Permalink
feat: add prompt param to use customized prompt template
Browse files Browse the repository at this point in the history
  • Loading branch information
irene-chou committed Mar 7, 2023
1 parent 5fe127d commit 17d0d4d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
26 changes: 21 additions & 5 deletions src/commands/aicommits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { KnownError, handleCliError } from '../utils/error.js';

export default async (
generate: number,
prompt: string,
rawArgv: string[],
) => (async () => {
intro(bgCyan(black(' aicommits ')));
Expand Down Expand Up @@ -46,9 +47,23 @@ export default async (
OPENAI_KEY,
staged.diff,
generate,
prompt,
);
s.stop('Changes analyzed');

if (!prompt) {
commitMessageProcess(messages, rawArgv);
} else {
customizedProcess(messages);
}
})().catch((error) => {
outro(`${red('✖')} ${error.message}`);
handleCliError(error);
process.exit(1);
});

// a new function to generate commit messages
const commitMessageProcess = async (messages: string[], rawArgv: string[]) => {
let message: string;
if (messages.length === 1) {
[message] = messages;
Expand Down Expand Up @@ -77,8 +92,9 @@ export default async (
await execa('git', ['commit', '-m', message, ...rawArgv]);

outro(`${green('✔')} Successfully committed!`);
})().catch((error) => {
outro(`${red('✖')} ${error.message}`);
handleCliError(error);
process.exit(1);
});
};

const customizedProcess = async (messages: string[]) => {
const message = messages.length === 1 ? messages[0] : messages.join('\n');
outro(`Response message:\n\n ${message}\n`);
};
3 changes: 2 additions & 1 deletion src/utils/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ 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 promptDefaultTemplate = '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 model = 'text-davinci-003';
const encoder = encodingForModel(model);
Expand All @@ -67,6 +67,7 @@ export const generateCommitMessage = async (
apiKey: string,
diff: string,
completions: number,
promptTemplate: string = promptDefaultTemplate,
) => {
const prompt = `${promptTemplate}\n${diff}`;

Expand Down

0 comments on commit 17d0d4d

Please sign in to comment.