2024-08-06 22:53:06 -04:00
#!/bin/bash
2024-08-07 06:38:37 -04:00
2024-08-07 06:53:20 -04:00
set -e
2024-08-07 06:38:37 -04:00
ARGS=("${@-}")
2024-08-07 06:48:26 -04:00
TMP_FILE="/tmp/aicommit"
2024-08-07 06:38:37 -04:00
2024-08-07 07:12:18 -04:00
system=$(
2024-08-07 07:21:13 -04:00
cat - <<EOF
2024-08-07 07:12:18 -04:00
You are an expert programmer that values clear, unambiguous communication and are specialized in generating concise and informative git commit messages.
Only reply with the commit message and nothing else.
Give detail for every change in the diff.
EOF
)
2024-08-07 06:45:24 -04:00
pushd "$(pwd)" >/dev/null || exit 1
2024-08-06 22:53:06 -04:00
2024-08-07 06:45:24 -04:00
if [[ -z ${GROQ_API_KEY} ]]; then
echo "API key not set."
exit 1
2024-08-06 22:53:52 -04:00
fi
2024-08-07 06:45:24 -04:00
if ! diff=$(git diff --cached); then
echo "Failed to get diff."
exit 1
2024-08-06 22:53:06 -04:00
fi
2024-08-07 06:45:24 -04:00
message=$(
jq -n \
2024-08-07 07:32:23 -04:00
".messages = [{role: \"system\", content: $(jq -R -s '@json' <<<"${system}")}, {role: \"user\", content: $(jq -R -s '@json' <<<"${diff}")}] |
2024-08-06 22:53:06 -04:00
.model = \"llama-3.1-70b-versatile\" |
.temperature = 1 |
.max_tokens = 1024 |
.top_p = 1 |
.stream = false |
.stop = null"
)
2024-08-07 07:21:13 -04:00
2024-08-07 07:32:23 -04:00
# trunk-ignore(shellcheck/SC2312)
2024-08-06 22:53:06 -04:00
curl -s "https://api.groq.com/openai/v1/chat/completions" \
2024-08-07 06:45:24 -04:00
-X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${GROQ_API_KEY}" \
-d "${message}" | jq -r '.choices[0].message.content | gsub("\""; "")' >"${TMP_FILE}"
if [[ 0 -ne $? ]]; then
echo "Failed to get response."
exit 1
2024-08-06 22:53:06 -04:00
fi
2024-08-07 06:45:24 -04:00
2024-08-06 22:53:06 -04:00
git commit -e -m "$(cat ${TMP_FILE})" && rm ${TMP_FILE}
2024-08-07 06:38:37 -04:00
popd >/dev/null || exit 1