devblog

Intermediate
  • Async/Await
  • API
  • JSON
  • axios
  • React
  • #2PlaysAMonth
avatar
AmrinFeb 19, 2023
A dev to client. Read dev to blog in a new frontend
AI generated git commit messages

AI generated git commit messages

#ai
#openai
#github
#git

Over my decorated 10-year career in development, I have seen some of the worst commit messages. I don't blame the developer on this either, because it is hard to remember what you just did at the time of the git commit.

In this 30 days of OpenAI series, I am looking at AI projects and their code to help demystify how any dev can build AI-generated projects.

The last thing I want to do is traverse a git diff and figure it out after a productive day of coding. I love the Nutlope/aicommits.

GitHub logo Nutlope / aicommits

A CLI that writes your git commit messages for you with AI

What is Nutlope/aicommits?

Aicommits is a CLI that writes your git commit messages for you with AI. During my 30 days of OpenAI, you will see projects from @nutlope frequently. He has been shipping cool projects and sharing them on Twitter.

How does it work?

This post is meant to focus on the AI part of the code, but as soon as I looked at the GitHub, I was surprised to see the use of TABS, jk. I was impressed by this CLI tool that caught my eye, cleye (cleverly named). I built a few CLIs back in my day, and this cleye is the chosen tool for building the aicommits interactions on the command line. I will take a deeper look at cleye in the future and perhaps make something with it.

If you'd like to see the CLI implementation, it is a quick read in the src/cli.ts.

// src/cli.ts

const request = https.request({
  port: 443,
  hostname: 'api.openai.com',
  path: '/v1/completions',
  method: 'POST',
  headers: {
      'Content-Type': 'application/json',
      'Content-Length': postContent.length,
      Authorization: `Bearer ${apiKey}`,
  },

  ...
Enter fullscreen mode Exit fullscreen mode

I have not built anything with OpenAI as of yet, but this is a clean example of how someone could approach it.

Now looking at the code src/utils/openai.ts, I can see OpenAI being invoked and using the /v1/completions path. Per the README, this is not the ChatGPT completions but regular GTP-3 text completions.

After the REST call, there is some clean-up of the response and this clever error checker. This is required because of the frequent OpenAI downtime and server loads.

// src/utils/openai.ts

if (response.statusCode === 500) {
    errorMessage += '; Check the API status: https://status.openai.com';
}
Enter fullscreen mode Exit fullscreen mode

Finally, taking a looking at the createCompletion function, this is the actual place the magic is made. I found this more understandable than reading the OpenAI documentation, which I found a little overwhelming. I wish it had the ability to search (why does it not have search?).

I left comments below on what each line is doing.

// src/utils/openai.ts

const completion = await createCompletion(apiKey, {
    model, // text-davinci-003 - made for longer output, and consistent instruction
    prompt, // promptTemplate provided by nutlope
    temperature: 0.7, // higher the number, the more random the output
    top_p: 1, // like temperature but different results
    frequency_penalty: 0, // decreasing the model's likelihood of repeating the same line
    presence_penalty: 0, // increasing the model's likelihood of talking about new topics
    max_tokens: 200, // how much will this cost you? 
    stream: false, // partial message sending is off
    n: completions, // How many chat completion choices to generate for each input message
});
Enter fullscreen mode Exit fullscreen mode

There is a lot more I'd love to dig into, but I will leave the rest for you to take look. I do recommend installing aicommits locally to try it out. Just be sure to sign up for OpenAI to add your token.

demo of aicommits on the command line

If I need to correct something, or if you have some insight into the code, please comment. I enjoyed walking through the code and learning how this works. Thanks to Nutlope for sharing this with us, and be sure to contribute back upstream. Open Source FTW!

Also, if you have a project leveraging OpenAI, leave a link in the comments. I'd love to take a look and include it in my 30 days of OpenAI series.

Stay saucy.

image was generated using midjourney