Automating Git commit messages with OpenAI's GPT-3
Have you ever struggled to come up with a good commit message for your changes? Or have you found yourself writing the same type of commit message over and over again? With OpenAI's GPT-3 language model, you can now automate the process of generating commit messages for your changes.
This blog post will guide you through a simple Python script that uses OpenAI's GPT-3 to generate commit messages based on the changes made to your code.
The code for this blog post can be found on GitHub at gpt-commit-message-helper.
The script is written in Python and uses the OpenAI API to generate commit messages. Before using the script, you will need to set up an OpenAI API key and install the openai Python library.
Overview of the script
The script works by generating a summary of the changes made to each file, and then using those summaries as prompts for the OpenAI API. The API generates a commit message based on the prompts, which is then returned to the script.
Here's a high-level overview of the script:
Get the list of files that have been changed (using Git).
For each file, generate a summary of the changes made.
Use the summaries as prompts for the OpenAI API to generate a commit message.
Print the suggested commit message and prompt the user to approve it or enter a custom one.
Commit the changes using the selected commit message.
Let's dive into the code.
Importing the necessary libraries and setting up the OpenAI API key
The first few lines of the script import the necessary libraries and set up the OpenAI API key:
import openai import os import sys import subprocess from requests.exceptions import RequestException
openai.api_key = os.environ.get("OPENAI_API_KEY")
The openai library is used to interact with the OpenAI API. The os, sys, and subprocess libraries are used to interact with the operating system and Git. The RequestException class is imported from the requests library to handle errors when making API requests.
The OpenAI API key is loaded from an environment variable, which should be set up before running the script.
Setting up default values for the OpenAI API parameters
Next, the script sets up default values for the OpenAI API parameters:
openai_engine = os.getenv("OPENAI_ENGINE", "text-davinci-002") openai_max_tokens = int(os.getenv("OPENAI_MAX_TOKENS", "60")) openai_temperature = float(os.getenv("OPENAI_TEMPERATURE", "0.7")) openai_stop = os.getenv("OPENAI_STOP", None)
These parameters control how the OpenAI API generates the commit message. The openai_engine parameter selects which OpenAI model to use. The openai_max_tokens parameter controls how many tokens (words or punctuation) are generated in the message. The openai_temperature parameter controls the "creativity" of the message. The openai_stop parameter can be used to add custom stop words that will cause the API to stop generating text.
Generating a summary of the changes made to a file
The generate_summary function takes a file path as input and generates a summary of the changes made to the file:
def generate_summary(file_path): # Get git diff of the file output = subprocess.run(['git', 'diff', '--cached', file_path], capture_output=True, text=True) # Extract added/modified/deleted lines from git diff added_lines =