aicommit/aicommit

80 lines
1.9 KiB
Bash
Executable File

#!/usr/bin/env bash
set -e
TMP_FILE="/tmp/aicommit_tmp"
system=$(
cat - <<EOF
You are an expert programmer that values clear, unambiguous communication and are specialized in generating concise and informative git commit messages.
Your task is to generate a concise, informative git commit message based on the following git diff.
Be sure that the commit message reflects the entire diff.
It is very important that the entire commit is clear and understandable.
Only reply with the commit message and nothing else.
EOF
)
if ! diff=$(git diff --cached); then
echo "Failed to get diff."
exit 1
fi
echo ""
echo "Please wait for AI reponse..."
if [[ "--groq" == "$1" ]]; then
shift
message=$(
jq -n \
".messages = [{role: \"system\", content: $(jq -R -s '@json' <<<"${system}")}, {role: \"user\", content: $(jq -R -s '@json' <<<"${diff}")}] |
.model = \"llama-3.1-70b-versatile\" |
.temperature = 1 |
.max_tokens = 1024 |
.top_p = 1 |
.stream = false |
.stop = null"
)
curl -s "https://api.groq.com/openai/v1/chat/completions" \
-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
fi
else
message_cat="${system} diff: ${diff}"
message=$(
jq -n \
".message = $(jq -R -s '@json' <<<"${message_cat}")"
)
if ! curl -s -X POST https://infer.x64.world/reset-conversation >/dev/null; then
echo "Failed to reset conversation."
exit 1
fi
curl -s "https://infer.x64.world/chat" \
-X POST \
-H "Content-Type: application/json" \
-d "${message}" | jq -r '.content | gsub("\""; "")' >"${TMP_FILE}"
if [[ 0 -ne $? ]]; then
echo "Failed to get response."
exit 1
fi
fi
cat ".git/COMMIT_EDITMSG" >>"${TMP_FILE}"
cat "${TMP_FILE}" >".git/COMMIT_EDITMSG"
rm "${TMP_FILE}"
"${EDITOR}" "${@-}"