From a7509a913c66f2e386e41c42f53019ad0b9b50f0 Mon Sep 17 00:00:00 2001 From: anaxios Date: Wed, 7 Aug 2024 03:45:24 -0700 Subject: [PATCH] Refactor aicommit_groq script to improve code readability and consistency --- aicommit_groq | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/aicommit_groq b/aicommit_groq index 9856ced..1f1f4b3 100755 --- a/aicommit_groq +++ b/aicommit_groq @@ -5,26 +5,25 @@ set -o pipefail ARGS=("${@-}") -pushd $(pwd) >/dev/null || exit 1 +pushd "$(pwd)" >/dev/null || exit 1 TMP_FILE="/tmp/aicommit" - -if [[ -z "${GROQ_API_KEY}" ]]; then - echo "API key not set." - exit 1 +if [[ -z ${GROQ_API_KEY} ]]; then + echo "API key not set." + exit 1 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." -diff=$(git diff --cached) -if [[ 0 -ne $? ]]; then - echo "Failed to get diff." - exit 1 +if ! diff=$(git diff --cached); then + echo "Failed to get diff." + exit 1 fi -message=$(jq -n \ - ".messages = [{role: \"system\", content: $(jq -R -s '@json' <<< "$system")}, {role: \"user\", content: $(jq -R -s '@json' <<< "$diff")}] | +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 | @@ -34,16 +33,16 @@ message=$(jq -n \ ) 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 + -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 - - + git commit -e -m "$(cat ${TMP_FILE})" && rm ${TMP_FILE} popd >/dev/null || exit 1