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 max-tokens option #263

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ The maximum character length of the generated commit message.

Default: `50`

#### max-tokens
The maximum number of tokens that the OpenAI models can generate.

Default: `200`

```sh
aicommits config set max-length=100
```
Expand Down
1 change: 1 addition & 0 deletions src/commands/aicommits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export default async (
config['max-length'],
config.type,
config.timeout,
config['max-tokens'],
config.proxy,
);
} finally {
Expand Down
1 change: 1 addition & 0 deletions src/commands/prepare-commit-msg-hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export default () => (async () => {
config['max-length'],
config.type,
config.timeout,
config['max-tokens'],
config.proxy,
);
} finally {
Expand Down
10 changes: 10 additions & 0 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ const configParsers = {

return parsed;
},
'max-tokens'(maxTokens?: string) {
if (!maxTokens) {
return 200;
}

parseAssert('max-length', /^\d+$/.test(maxTokens), 'Must be an integer');

const parsed = Number(maxTokens);
return parsed;
},
} as const;

type ConfigKeys = keyof typeof configParsers;
Expand Down
3 changes: 2 additions & 1 deletion src/utils/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const httpsPost = async (
headers: Record<string, string>,
json: unknown,
timeout: number,
maxTokens: number,
proxy?: string,
) => new Promise<{
request: ClientRequest;
Expand Down Expand Up @@ -152,7 +153,7 @@ export const generateCommitMessage = async (
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
max_tokens: 200,
max_tokens: maxTokens,
stream: false,
n: completions,
},
Expand Down
2 changes: 1 addition & 1 deletion tests/specs/openai/conventional-commits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export default testSuite(({ describe }) => {
'max-length': 50,
...configOverrides,
} as ValidConfig;
const commitMessages = await generateCommitMessage(OPENAI_KEY!, 'gpt-3.5-turbo', config.locale, gitDiff, config.generate, config['max-length'], config.type, 7000);
const commitMessages = await generateCommitMessage(OPENAI_KEY!, 'gpt-3.5-turbo', config.locale, gitDiff, config.generate, config['max-length'], config.type, 7000, config['max-tokens']);

return commitMessages[0];
}
Expand Down