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 maximum length flag (#91) #120

Closed
wants to merge 2 commits into from
Closed
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
7 changes: 7 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ cli(
alias: 'g',
default: 1,
},
length: {
type: Number,
description: 'Maximum length of the generated message',
alias: 'l',
default: 50,
},
},

commands: [
Expand All @@ -45,6 +51,7 @@ cli(
aicommits(
argv.flags.generate,
rawArgv,
argv.flags.length,
);
}
},
Expand Down
2 changes: 2 additions & 0 deletions src/commands/aicommits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { generateCommitMessage } from '../utils/openai.js';
export default async (
generate: number,
rawArgv: string[],
maximumLength: number,
) => (async () => {
intro(bgCyan(black(' aicommits ')));

Expand Down Expand Up @@ -45,6 +46,7 @@ export default async (
OPENAI_KEY,
staged.diff,
generate,
maximumLength,
);
s.stop('Changes analyzed');

Expand Down
5 changes: 3 additions & 2 deletions src/utils/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ 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 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, making sure it is exactly or less than [count] characters long:';

export const generateCommitMessage = async (
apiKey: string,
diff: string,
completions: number,
maximumLength: number,
) => {
const prompt = `${promptTemplate}\n${diff}`;
const prompt = `${promptTemplate.replace('[count]', maximumLength.toString())}\n${diff}`;

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