refactor: setup utils file (#50)

This commit is contained in:
hiroki osame
2023-02-15 20:20:10 -05:00
committed by GitHub
parent 1c7cf853c9
commit 707aa2ee22
2 changed files with 28 additions and 27 deletions

View File

@@ -3,36 +3,10 @@
import { execSync } from 'child_process'; import { execSync } from 'child_process';
import chalk from 'chalk'; import chalk from 'chalk';
import inquirer from 'inquirer'; 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 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 () => { (async () => {
console.log(chalk.white('▲ ') + chalk.green('Welcome to AICommits!')); console.log(chalk.white('▲ ') + chalk.green('Welcome to AICommits!'));

27
src/utils.ts Normal file
View File

@@ -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;
}
};