diff --git a/README.md b/README.md index 106eceed..945518fc 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/src/commands/aicommits.ts b/src/commands/aicommits.ts index 2c6cb2ba..21040a8f 100644 --- a/src/commands/aicommits.ts +++ b/src/commands/aicommits.ts @@ -63,6 +63,7 @@ export default async ( config['max-length'], config.type, config.timeout, + config['max-tokens'], config.proxy, ); } finally { diff --git a/src/commands/prepare-commit-msg-hook.ts b/src/commands/prepare-commit-msg-hook.ts index b8ec3e25..bdc565be 100644 --- a/src/commands/prepare-commit-msg-hook.ts +++ b/src/commands/prepare-commit-msg-hook.ts @@ -48,6 +48,7 @@ export default () => (async () => { config['max-length'], config.type, config.timeout, + config['max-tokens'], config.proxy, ); } finally { diff --git a/src/utils/config.ts b/src/utils/config.ts index 47429e5d..a17bb4aa 100644 --- a/src/utils/config.ts +++ b/src/utils/config.ts @@ -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; diff --git a/src/utils/openai.ts b/src/utils/openai.ts index 9dd31514..cb37cb7f 100644 --- a/src/utils/openai.ts +++ b/src/utils/openai.ts @@ -16,6 +16,7 @@ const httpsPost = async ( headers: Record, json: unknown, timeout: number, + maxTokens: number, proxy?: string, ) => new Promise<{ request: ClientRequest; @@ -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, }, diff --git a/tests/specs/openai/conventional-commits.ts b/tests/specs/openai/conventional-commits.ts index b89255eb..a2ad2b95 100644 --- a/tests/specs/openai/conventional-commits.ts +++ b/tests/specs/openai/conventional-commits.ts @@ -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]; }