feat: config command (#71)

This commit is contained in:
Hiroki Osame
2023-02-21 22:04:40 +09:00
parent 05f70e344e
commit 0d3f35c135
7 changed files with 275 additions and 190 deletions

41
src/utils/openai.ts Normal file
View File

@@ -0,0 +1,41 @@
import { Configuration, OpenAIApi } from 'openai';
const sanitizeMessage = (message: string) => message.trim().replace(/[\n\r]/g, '').replace(/(\w)\.$/, '$1');
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:';
export const generateCommitMessage = async (
apiKey: string,
diff: string,
completions: number,
) => {
const prompt = `${promptTemplate}\n${diff}`;
// Accounting for GPT-3's input req of 4k tokens (approx 8k chars)
if (prompt.length > 8000) {
throw new Error('The diff is too large for the OpenAI API');
}
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: completions,
});
return completion.data.choices
.filter(choice => choice.text)
.map(choice => sanitizeMessage(choice.text!));
} catch (error) {
const errorAsAny = error as any;
errorAsAny.message = `OpenAI API Error: ${errorAsAny.message} - ${errorAsAny.response.statusText}`;
throw errorAsAny;
}
};