Compare commits

9 Commits

Author SHA1 Message Date
b9caddd286 Added instruction to put each change on its own line as a separate sentence in the commit message.
Added instruction not to put quotation marks around the commit message.
2024-08-16 17:30:49 -07:00
4e9a32f501 Added dependency on babashka.fs and babashka.cli.
Introduced functions to find git repository and read/write commit message.
Modified response processing to append to COMMIT_EDITMSG and open in editor.
2024-08-10 11:40:21 -07:00
5fb26e6220 Refactor aicommit.clj to utilize OpenAI's chat completion API for generating commit messages using groq.
Adding Cheshire core dependency for JSON parsing and generation
2024-08-10 08:12:05 -07:00
abd32275ba Added aicommit.clj script to migrate to babashka clojure. 2024-08-10 06:26:06 -07:00
7476e27e01 Refactor instructions to generate concise and informative git commit messages, removing redundant guidelines and emphasizing clarity and understandability 2024-08-09 18:31:44 -07:00
2268f9e401 Added option to use Groq API by adding --groq flag to GIT_EDITOR.
Added environment variable GROQ_API_KEY to store Groq API key.
Updated git commit example to include --groq flag.
2024-08-09 12:48:08 -07:00
e4d4abb082 Removed unnecessary variable ARGS and directly passed command line arguments to the editor. 2024-08-09 12:43:01 -07:00
32279b7fd5 Relocated echo statements. 2024-08-09 12:37:44 -07:00
a271e5624e Changed the shebang line from #!/bin/bash to #!/usr/bin/env bash.
Removed the `aicommit_groq` script and deleted the file.
Updated the `EDITOR` command to use the `ARGS` array.
Moved groq config into aicommit.
2024-08-09 12:34:29 -07:00
4 changed files with 113 additions and 80 deletions

View File

@ -19,6 +19,15 @@ To use Aicommit, you need to have the following tools installed:
```bash
export GIT_EDITOR="<path/to/>aicommit"
```
Optionally you can add the `--groq` flag to use the Groq API.
```bash
export GROQ_API_KEY=<groq-api-key>
```
```bash
export GIT_EDITOR="<path/to/>aicommit --groq"
```
3. **Use Git as normal**: Once configured, you can use Git as you normally would. When you run `git commit`, Aicommit will generate a draft commit message using RayAI, which will then be loaded into your default editor for review and editing.

View File

@ -1,16 +1,16 @@
#!/bin/bash
#!/usr/bin/env bash
set -e
ARGS=("${@-}")
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.
Give detail for every change in the diff.
EOF
)
@ -20,29 +20,55 @@ if ! diff=$(git diff --cached); then
exit 1
fi
message_cat="${system} diff: ${diff}"
message=$(
jq -n \
".message = $(jq -R -s '@json' <<<"${message_cat}")"
)
echo ""
echo "Please wait for AI reponse..."
if ! curl -s -X POST https://infer.x64.world/reset-conversation >/dev/null; then
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
fi
curl -s "https://infer.x64.world/chat" \
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
if [[ 0 -ne $? ]]; then
echo "Failed to get response."
exit 1
fi
fi
cat ".git/COMMIT_EDITMSG" >>"${TMP_FILE}"
@ -50,4 +76,4 @@ cat "${TMP_FILE}" >".git/COMMIT_EDITMSG"
rm "${TMP_FILE}"
"${EDITOR}" "$@"
"${EDITOR}" "${@-}"

54
aicommit.clj Executable file
View File

@ -0,0 +1,54 @@
#!/usr/bin/env bb
(require '[babashka.curl :as curl]
'[babashka.process :as p]
'[cheshire.core :as json]
'[babashka.fs :as fs]
'[babashka.cli :as cli])
(def url "https://api.groq.com/openai/v1/chat/completions")
(def system-msg "
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.
Put each change on it's own line as a separate sentance.
Don't put quotation marks around the message.
")
(def diff (:out (p/check (p/sh "git" "diff" "--cached"))))
(defn find-git
[dir]
(loop [d dir]
(if (fs/directory? (fs/path d ".git"))
(fs/path d ".git")
(recur (fs/parent d)))))
(def commit-message-path (fs/unixify (fs/path (find-git (fs/cwd)) "COMMIT_EDITMSG")))
(def commit-message (slurp commit-message-path))
(def message (json/generate-string
{:messages [{:role "system" :content system-msg}
{:role "user" :content diff}]
:model "llama-3.1-70b-versatile"
:temperature 1
:max_tokens 1024
:top_p 1
:stream false
:stop nil}))
(def response
(curl/post url
{:headers {"Content-Type" "application/json"
"Authorization" (str "Bearer " (System/getenv "GROQ_API_KEY"))}
:body message}))
(def content (get-in (:choices (json/parse-string (:body response) true)) [0 :message :content]))
(spit commit-message-path (str content "\n" commit-message))
(p/shell (System/getenv "EDITOR") (first *command-line-args*))

View File

@ -1,56 +0,0 @@
#!/bin/bash
set -e
set -o pipefail
ARGS=("${@-}")
TMP_FILE="/tmp/aicommit"
system=$(
cat - <<EOF
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
)
pushd "$(pwd)" >/dev/null || exit 1
if [[ -z ${GROQ_API_KEY} ]]; then
echo "API key not set."
exit 1
fi
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}")}] |
.model = \"llama-3.1-70b-versatile\" |
.temperature = 1 |
.max_tokens = 1024 |
.top_p = 1 |
.stream = false |
.stop = null"
)
# trunk-ignore(shellcheck/SC2312)
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
git commit -e -m "$(cat ${TMP_FILE})" && rm ${TMP_FILE}
popd >/dev/null || exit 1