AI generated git commit messages
Brian Douglas

Brian Douglas @bdougieyo

About: Brian writes code

Location:
Oakland, CA
Joined:
May 27, 2017

AI generated git commit messages

Publish Date: Mar 3 '23
176 19

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

In this 9 days of OpenAI series, 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

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 9 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 9 days of OpenAI series.

Stay saucy.

Comments 19 total

  • adriens
    adriensMar 4, 2023

    Adopted here ;-p

  • AI Ocean
    AI OceanMar 4, 2023

    Look like developers have the sam idea, I also did the same thing with you. But i named it ai-commit instead. Use conversation form to ajust the message

    • Brian Douglas
      Brian DouglasMar 4, 2023

      Please drop a link if its open source. Also to be clear this isn't my project, but a project open sourced by @nutlope

  • k1lgor
    k1lgorMar 4, 2023

    It's nice, I tested it yesterday, but please note that too many commits (requests) in a short period of time will result in an error 400 - bad request. 👏🔝🎉

    • Brian Douglas
      Brian DouglasMar 4, 2023

      Good call out. There are some obvious areas of improvements and I think this project is set up well for some one to take what is already there and make something useful for a lot of people.

  • Vsevolod
    VsevolodMar 4, 2023

    Very simple and inspiring implementation. I'll probably do a couple of experiments too =)

  • Bharat
    BharatMar 4, 2023

    Nice one

  • TED Vortex (Teodor Eugen Duțulescu)
    TED Vortex (Teodor Eugen Duțulescu)Mar 5, 2023

    the too many requests are killing my vibe tho :<

  • Marco Lackovic
    Marco LackovicMar 6, 2023

    Hey there, are you using the latest version of AI Commits? Commits with imperative mood have been merged two weeks ago (see this PR) and merged but your screenshots do not reflect that and still use the past tense.

  • David Cantrell
    David CantrellMar 6, 2023

    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.

    git commit -v really helps here. Since I started enforcing it on myself (through use of a shell function that intercepts all my attempts to run git) my commit messages have got much better.

    • Vinícius Hoyer
      Vinícius HoyerMar 6, 2023

      git add --patch|-p has helped a lot in this also, both tools we all should be using, actually

  • Simon Appelt
    Simon AppeltMar 6, 2023

    Funny Project.
    But i don't think that's a good idea to send all you code/commits to OpenAi.
    Pretty sure your boss/client will not appreciate that.
    Probably it's also against your NDA. Be cautious.

  • Tom Henry
    Tom HenryMar 7, 2023

    Cool idea

  • Saleumsack
    SaleumsackMar 7, 2023

    It's very nice, but sometimes I'm too lazy to think about the commit message after the long hour code.

  • John Colagioia (he/him)
    John Colagioia (he/him)Mar 8, 2023

    I have to walk a fine line, in this response, because...

    1. I love the technical challenge and want to encourage that sort of thing, but
    2. The premise seems horrifying and will generate ill-will from colleagues.

    First, if you don't remember what work you did, then you probably put too much into your commits. While I realize that nobody's perfect and things slip through the cracks, each commit should do a specific thing, and someone can ideally add a specific feature (and nothing else) by cherry-picking other otherwise moving a defined set of commits.

    Then, changes (in a professional environment) should all be connected to a ticket, so that people can track the work happening without analyzing the repository. Commits should call out the relevant ticket numbers and probably echo the language used in the ticket. I'm never going to search a repository for "the time that Zemzem removed a text box." I want "the work done on ticket #4586, ideally seeing the individual tasks involved in finishing that ticket."

    Finally, you're allowed to use a graphical interface, when you commit, and look at the changes that you're committing. Again, if you did a bunch of things at once, you should probably use such a tool, so that you can commit specific lines that go together, instead of full files.

    Again, that doesn't take away from the technical achievement or the entertainment value. And (like anything with generative AI) if you use the results as a springboard to what you'll really write, that'll probably serve you better. But I definitely worry about trying to "outsource" communication with your closest colleagues...

  • Eric Amodio
    Eric AmodioMar 9, 2023

    If you use VS Code the latest pre-release version of GitLens also provides the ability to use ChatGPT to generate commit messages (and probably soon to explain commits). There is even a setting so the user can control the commit message style as part of the prompt.

  • Liam Dawson
    Liam DawsonApr 6, 2023

    I really enjoyed reading your post about AI-generated git commit messages. As a developer myself, I can relate to the struggle of writing clear and informative commit messages. I think Nutlope/aicommits is a really interesting tool, and it's impressive to see how popular it has become in just two weeks.

    I found it helpful that you took the time to explain how the AI part of the code works, and it's great to see how OpenAI is being used in practical applications. I agree with you that the createCompletion function is where the magic happens, and your comments on each line were very helpful in understanding what's going on.

    I also appreciate that you shared your thoughts on the code and your experience with building CLIs. It's always great to hear from other developers and learn from their experiences.

    Overall, I thought your post was well-written and informative. Thank you for sharing your insights and for encouraging others to contribute to open source projects. I look forward to reading more of your 30 days of OpenAI series, and I'll definitely check out Nutlope/aicommits!

  • Martina Waissma
    Martina WaissmaApr 15, 2023

    I have waited for this!

Add comment