From a5eb750611980ec46652d66f84fbca0adf6a8425 Mon Sep 17 00:00:00 2001 From: Hassan El Mghari Date: Sun, 12 Feb 2023 00:14:28 -0500 Subject: [PATCH] Refactor diff command to use quiet function and add OpenAI API call for writing detailed commit message --- aicommit.mjs | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/aicommit.mjs b/aicommit.mjs index 9b99551..9123de6 100644 --- a/aicommit.mjs +++ b/aicommit.mjs @@ -2,8 +2,30 @@ let { OPENAI_API_KEY } = await fs.readJson("./.env.json"); -let diff = await $`git diff --cached`; +let diff = await quiet($`git diff --cached`); +let prompt = `Write one detailed commit message based on the following commit: ${diff}`; +echo(prompt); -// echo("Current branch is", branch); -// console.log(branch); -echo(diff); +const payload = { + model: "text-davinci-003", + prompt, + temperature: 0.7, + top_p: 1, + frequency_penalty: 0, + presence_penalty: 0, + max_tokens: 200, + stream: false, + n: 1, +}; + +const response = await fetch("https://api.openai.com/v1/completions", { + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${OPENAI_API_KEY ?? ""}`, + }, + method: "POST", + body: JSON.stringify(payload), +}); + +const json = await response.json(); +echo(json.choices[0].text);