AI generated git commit messages
Tags: ai, openai, github, git
Published: 2023-03-03 12:29:10


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. 

_image was generated using [midjourney](https://www.midjourney.com/app/jobs/8da90610-e0a8-41de-90b6-1a97f82c90e8/)_

In this [9 days of OpenAI series](https://dev.to/bdougieyo/30-days-of-openai-1ek6), I am looking at AI projects and their code to help demystify how any dev can build AI-generated projects.

_Find more AI projects using [OpenSauced](https://insights.opensauced.pizza/ai/repositories/filter/recent)_

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](https://github.com/Nutlope/aicommits).

{% github Nutlope/aicommits noreadme %}

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

{% twitter 1630959061716946945 %} 

## 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](https://github.com/privatenumber/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](https://github.com/Nutlope/aicommits/blob/develop/src/cli.ts).

```js
// 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}`,
  },
  
  ...
```
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](https://github.com/Nutlope/aicommits/blob/develop/src/utils/openai.ts), I can see OpenAI being invoked and using the [/v1/completions](https://platform.openai.com/docs/api-reference/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.

```js
// src/utils/openai.ts

if (response.statusCode === 500) {
    errorMessage += '; Check the API status: https://status.openai.com';
}
``` 
Finally, taking a looking at the [createCompletion](https://github.com/Nutlope/aicommits/blob/develop/src/utils/openai.ts#LL82C3-L92C6) 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. 

```js
// 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
});
``` 

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mr98h9o5vp8qbgvq7zy9.png)

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 [9 days of OpenAI series](https://dev.to/bdougieyo/30-days-of-openai-1ek6).

Stay saucy.