Skip to content

Instantly share code, notes, and snippets.

@tobischw
Created June 4, 2022 19:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobischw/937def6638edd8f1be218dcb272e17e1 to your computer and use it in GitHub Desktop.
Save tobischw/937def6638edd8f1be218dcb272e17e1 to your computer and use it in GitHub Desktop.
Automatically generate commit messages based on diffs using GPT-3
// 1. npm init
// 2. npm install -save openai simple-git
// 3. Grab an API key from OpenAI: https://beta.openai.com/account/api-keys
// 4. Replace FOLDER_TO_CHECK with the root path of your git-enabled project
//
// Eventually the goal of this is to have a command line utility that lets users
// generates the commit message and commit immediately.
const { Configuration, OpenAIApi } = require("openai");
const simpleGit = require('simple-git');
const configuration = new Configuration({
apiKey: 'API_KEY',
});
const openai = new OpenAIApi(configuration);
const git = simpleGit({ baseDir: 'FOLDER_TO_CHECK' });
(async () => {
const diff = await git.diff();
if(diff) {
const completion = await openai.createCompletion("text-davinci-002", {
prompt: `Generate a commit message based on this diff:\n ${diff}`,
});
console.log(`--> ${completion.data.choices[0].text}`);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment