A bash script `ci` to help you write commit message and commit with OPEN AI/Groq API
Writing commit message could be time-consuming if you want a good summrize. ChatGPT just released their cheapest but still powerful model gpt-4o-mini, we can use it to save our time and keep the commit message accurate.
Attention
I refine the following script time by time and keep the latest version on github. Click here to see the latest version!
You need to put your API KEY in environment variable OPENAI_API_KEY before running this script.
You need to run git add
first before running this ci
script.
If you don't want to bother adding first, change git diff --cached -w -b
to git diff -w -b
and add -a
option in the commit command (the last line of the script)
#!/bin/bash
# Get git changes
changes=$(git diff --cached -w -b)
# Exit if no changes
if [ -z "$changes" ]; then
echo "No changes to commit."
exit 0
fi
# Set prompt template
prompt_template=$(cat <<-END
You are skilled at writing git commit messages and follow the conventional commits specification:
refactor: for refactoring
fix: for bug fixes
minor: for minor changes
...
You will be given the changes from a 'git diff -w -b HEAD' command.
Please summarize all meaningful changes into one commit message, no more than 100 characters.
Respond only with the commit message. Do not mention unchanged aspects. Do not put doube quote in the message. Do not escape underscore characters.
DO REVISE THE MESSAGE THREE TIMES before sending it out. And again: no more than 100 characters!!!
Here are the changes:
END
)
prompt_template+="$changes"
# Build JSON request body using jq
request_body=$(jq -n --arg model "gpt-4o-mini-2024-07-18" --arg prompt "$prompt_template" \
'{model: $model, messages: [{role:"system",content:"You are a programmer"},{role: "user", content: $prompt}], max_tokens: 32768, seed: 45, temperature: 0}')
# Use curl to request the OpenAI API to generate a commit message
response=$(curl -s --request POST \
--url https://api.openai.com/v1/chat/completions \
--header "Authorization: Bearer $OPENAI_API_KEY" \
--header "Content-Type: application/json" \
--data "$request_body")
# Extract commit message from the response
commit_message=$(echo "$response" | jq -r '.choices[0].message.content')
# Exit if the commit message is empty
if [ -z "$commit_message" ] || [ "$commit_message" == "null" ]; then
echo 'failed to generate a commit message:'
echo $response
exit 1
fi
# Commit changes
git commit -m "$commit_message"
```bash
**2024-07-24 update**
## Free alternative provided by Groq
If you don't want to pay for the ChatGPT API, you can opt for the free alternative provided by [Groq](https://groq.com). To get started, simply sign up for a Groq account using Google Login. Once you've created your account, you can generate an API key by following this link: <https://console.groq.com/keys>. Finally, make a minor modification to your script, as outlined below.
Build JSON request body using jq
#request_body=$(jq -n arg model "gpt-4o-mini-2024-07-18" arg prompt "$prompt_template" \
'{model: $model, messages: [{role:"system",content:"You are a programmer"},{role: "user", content: $prompt}], max_tokens: 50, seed: 45, temperature: 0}')
#response=$(curl -s --request POST \
--url https://api.openai.com/v1/chat/completions \
--header "Authorization: Bearer $OPENAI_API_KEY" \
--header "Content-Type: application/json" \
--data "$request_body")
request_body=$(jq -n arg model "mixtral-8x7b-32768" arg prompt "$prompt_template"
'{model: $model, messages: [{role:"system",content:"You are a programmer"},{role: "user", content: $prompt}], max_tokens: 32768, stream: false, temperature: 0, top_p: 1, stop: null}')
Use curl to request the OpenAI API to generate a commit message
response=$(curl -s request POST
url https://api.groq.com/openai/v1/chat/completions
header "Authorization: Bearer $GROQ_API_KEY"
header "Content-Type: application/json"
--data "$request_body")