diff --git a/src/cli.ts b/src/cli.ts index 9b02e88..79c03df 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -3,36 +3,10 @@ import { execSync } from 'child_process'; import chalk from 'chalk'; import inquirer from 'inquirer'; -import { Configuration, OpenAIApi } from 'openai'; +import { generateCommitMessage } from './utils'; const OPENAI_KEY = process.env.OPENAI_KEY ?? process.env.OPENAI_API_KEY; -const generateCommitMessage = async ( - apiKey: string, - prompt: string, -) => { - const openai = new OpenAIApi(new Configuration({ apiKey })); - try { - const completion = await openai.createCompletion({ - model: 'text-davinci-003', - prompt, - temperature: 0.7, - top_p: 1, - frequency_penalty: 0, - presence_penalty: 0, - max_tokens: 200, - stream: false, - n: 1, - }); - - return completion.data.choices[0].text!.trim().replace(/[\n\r]/g, ''); - } catch (error) { - const errorAsAny = error as any; - errorAsAny.message = `OpenAI API Error: ${errorAsAny.message} - ${errorAsAny.response.statusText}`; - throw errorAsAny; - } -}; - (async () => { console.log(chalk.white('▲ ') + chalk.green('Welcome to AICommits!')); diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 0000000..6ed4290 --- /dev/null +++ b/src/utils.ts @@ -0,0 +1,27 @@ +import { Configuration, OpenAIApi } from 'openai'; + +export const generateCommitMessage = async ( + apiKey: string, + prompt: string, +) => { + const openai = new OpenAIApi(new Configuration({ apiKey })); + try { + const completion = await openai.createCompletion({ + model: 'text-davinci-003', + prompt, + temperature: 0.7, + top_p: 1, + frequency_penalty: 0, + presence_penalty: 0, + max_tokens: 200, + stream: false, + n: 1, + }); + + return completion.data.choices[0].text!.trim().replace(/[\n\r]/g, ''); + } catch (error) { + const errorAsAny = error as any; + errorAsAny.message = `OpenAI API Error: ${errorAsAny.message} - ${errorAsAny.response.statusText}`; + throw errorAsAny; + } +};