Refactor aicommit_groq script to improve code readability and consistency

This commit is contained in:
ilguappo 2024-08-07 03:45:24 -07:00
parent 241b951a00
commit a7509a913c

View File

@ -5,26 +5,25 @@ set -o pipefail
ARGS=("${@-}") ARGS=("${@-}")
pushd $(pwd) >/dev/null || exit 1 pushd "$(pwd)" >/dev/null || exit 1
TMP_FILE="/tmp/aicommit" TMP_FILE="/tmp/aicommit"
if [[ -z ${GROQ_API_KEY} ]]; then
if [[ -z "${GROQ_API_KEY}" ]]; then echo "API key not set."
echo "API key not set." exit 1
exit 1
fi fi
system="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." system="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."
diff=$(git diff --cached) if ! diff=$(git diff --cached); then
if [[ 0 -ne $? ]]; then echo "Failed to get diff."
echo "Failed to get diff." exit 1
exit 1
fi fi
message=$(jq -n \ message=$(
".messages = [{role: \"system\", content: $(jq -R -s '@json' <<< "$system")}, {role: \"user\", content: $(jq -R -s '@json' <<< "$diff")}] | 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\" | .model = \"llama-3.1-70b-versatile\" |
.temperature = 1 | .temperature = 1 |
.max_tokens = 1024 | .max_tokens = 1024 |
@ -34,15 +33,15 @@ message=$(jq -n \
) )
curl -s "https://api.groq.com/openai/v1/chat/completions" \ curl -s "https://api.groq.com/openai/v1/chat/completions" \
-X POST \ -X POST \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-H "Authorization: Bearer ${GROQ_API_KEY}" \ -H "Authorization: Bearer ${GROQ_API_KEY}" \
-d "$message" | jq -r '.choices[0].message.content | gsub("\""; "")' > ${TMP_FILE} -d "${message}" | jq -r '.choices[0].message.content | gsub("\""; "")' >"${TMP_FILE}"
if [[ 0 -ne $? ]]; then
echo "Failed to get response."
exit 1
fi
if [[ 0 -ne $? ]]; then
echo "Failed to get response."
exit 1
fi
git commit -e -m "$(cat ${TMP_FILE})" && rm ${TMP_FILE} git commit -e -m "$(cat ${TMP_FILE})" && rm ${TMP_FILE}