Compare commits

24 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
43b79b2914 Delete test file: remove This is a test. line from test.txt 2024-08-08 02:26:00 -07:00
baed3d5d17 Update README.md with new instructions on using aicommit
Replace outdated information about `aicommit_groq`
Clarify prerequisites for using Aicommit
Add setup and usage section to README.md
Document bypassing AI-generated commit messages using the `-m` flag
2024-08-08 02:22:20 -07:00
e3704b4e6e update README to reflect new feature and remove deprecated usage of aicommit_groq 2024-08-08 02:15:21 -07:00
141b53b4ba Added AI-driven commit message drafting with aicommit, injecting draft messages before editor review. Fixed README to reflect usage and requirements. 2024-08-08 01:38:37 -07:00
f863bce0fa Now stands as middleware between the git commit command and the default
editor.
2024-08-08 01:30:10 -07:00
455ae71cd3 Removed unnecessary line break and improved error message. 2024-08-07 07:07:58 -07:00
6fa213550d Add pipefail option to set command to prevent pipes from hiding errors, improve script reliability 2024-08-07 04:40:22 -07:00
e0ee195fca Content: Replaced custom spinner with curl command and removed unnecessary code for setting up spinner. 2024-08-07 04:33:31 -07:00
7de04c06c9 Title: Update GROQ API request to match official documentation
Content: Fixed JSON formatting and removed unnecessary comment.
URL: https://console.groq.com/docs/
2024-08-07 04:32:23 -07:00
ab5738d14e Title: Update GROQ API request to match official documentation
Content: Updated curl command and removed unnecessary code for setting up spinner.
2024-08-07 04:21:13 -07:00
624861e320 Refactor aicommit_groq script: extract system message to a heredoc, remove redundant variable assignment, and improve code organization 2024-08-07 04:12:18 -07:00
cf655b7202 Title: Fix typo in system message
Content: Removed unnecessary newline and changed -in back to in in the system message definition.
2024-08-07 04:09:28 -07:00
41dc5665b4 Title: Improve system message definition
Content: Replaced string concatenation with a here document to improve readability and maintainability. Fixed trailing whitespace by adding a newline at the end of the last line.
2024-08-07 04:08:00 -07:00
74909d05f4 Title: Update system message
Content: Changed system message to include detail about every change in the diff for better clarity and precision
2024-08-07 03:58:46 -07:00
c72a7835da Title: Improve script robustness
Content: Switch from set -eu to set -e for more robust error handling
2024-08-07 03:54:16 -07:00
4 changed files with 139 additions and 126 deletions

View File

@ -1,15 +1,34 @@
## use AI to draft commit messages
# **Use AI to Draft Commit Messages with Aicommit**
clone this repo and then link the scripts into your path.
Aicommit integrates with RayAI to generate a draft commit message, which is then loaded into your default editor for review and editing.
requires: jq
## **Prerequisites**
### use
Instead of
To use Aicommit, you need to have the following tools installed:
- `curl`
- `jq`
**Note**: Aicommit-groq is currently broken and should not be used.
## **Setup and Usage**
1. **Clone the repository**: Clone the Aicommit repository to your local machine.
2. **Configure your Git editor**: Add the following line to your `~/.bashrc` file, replacing `<path/to/>` with the actual path to the Aicommit executable:
```bash
git commit -m "your message here"`
export GIT_EDITOR="<path/to/>aicommit"
```
Optionally you can add the `--groq` flag to use the Groq API.
```bash
aicommit
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.
**Bypassing AI-generated messages**: If you prefer to write your own commit message, you can use the `-m` flag with `git commit`, like this: `git commit -m "your message here"`. This will bypass the AI-generated message and allow you to write your own message directly.

117
aicommit
View File

@ -1,78 +1,79 @@
#!/bin/bash
set -eu
#!/usr/bin/env bash
set -e
ARGS=("${@-}")
TMP_FILE="/tmp/aicommit_tmp"
TMP_FILE="/tmp/aicommit"
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=$(
cat - <<EOF
pushd "$(pwd)" >/dev/null
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.
hasFlag() {
local flags=("$@")
for var in "${ARGS[@]}"; do
for flag in "${flags[@]}"; do
if [[ ${flag} == "${var}" ]]; then
echo 'true'
return
fi
done
done
echo 'false'
}
spin() {
echo "${@}"
while true; do
spinner=('-' '\' '|' '/')
for i in "${spinner[@]}"; do
echo -ne "\r${i}"
sleep .05
done
done
echo ""
}
EOF
)
if ! diff=$(git diff --cached); then
echo "Failed to get diff."
exit 1
fi
message_cat="${system} diff: ${diff}"
echo ""
echo "Please wait for AI reponse..."
message=$(
jq -n \
".message = $(jq -R -s '@json' <<<"${message_cat}")"
)
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}")"
)
# trunk-ignore(shellcheck/SC2091)
# trunk-ignore(shellcheck/SC2310)
if $(hasFlag -r --reset); then
spin "resetting AI history." &
SPINNER="$!"
if ! curl -s -X POST https://infer.x64.world/reset-conversation >/dev/null; then
echo "Failed to reset conversation."
kill ${SPINNER}
exit 1
fi
kill "${SPINNER}"
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
spin "AI is thinking." &
SPINNER="$!"
curl -s "https://infer.x64.world/chat" \
-X POST \
-H "Content-Type: application/json" \
-d "${message}" | jq -r '.content | gsub("\""; "")' >"${TMP_FILE}"
cat ".git/COMMIT_EDITMSG" >>"${TMP_FILE}"
cat "${TMP_FILE}" >".git/COMMIT_EDITMSG"
# trunk-ignore(shellcheck/SC2181)
if [[ 0 -ne $? ]]; then
kill "${SPINNER}"
echo "Failed to get response."
exit 1
fi
kill "${SPINNER}"
rm "${TMP_FILE}"
git commit -e -m "$(cat "${TMP_FILE}")" && rm "${TMP_FILE}"
popd >/dev/null
"${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,61 +0,0 @@
#!/bin/bash
set -e
ARGS=("${@-}")
TMP_FILE="/tmp/aicommit"
pushd "$(pwd)" >/dev/null || exit 1
spin() {
echo "${@}"
while true; do
spinner=('-' '\' '|' '/')
for i in "${spinner[@]}"; do
echo -ne "\r${i}"
sleep .05
done
done
echo ""
}
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."
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"
)
spin "AI is thinking..." &
SPINNER="$!"
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."
kill "${SPINNER}"
exit 1
fi
kill "${SPINNER}"
git commit -e -m "$(cat ${TMP_FILE})" && rm ${TMP_FILE}
popd >/dev/null || exit 1