Compare commits
14 Commits
v0.1.0
...
refactor-t
Author | SHA1 | Date | |
---|---|---|---|
b9caddd286
|
|||
4e9a32f501
|
|||
5fb26e6220
|
|||
abd32275ba
|
|||
7476e27e01
|
|||
2268f9e401
|
|||
e4d4abb082
|
|||
32279b7fd5
|
|||
a271e5624e
|
|||
43b79b2914
|
|||
baed3d5d17
|
|||
e3704b4e6e
|
|||
141b53b4ba
|
|||
f863bce0fa
|
33
README.md
33
README.md
@ -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.
|
||||
|
65
aicommit
65
aicommit
@ -1,39 +1,53 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env bash
|
||||
set -e
|
||||
|
||||
ARGS=("${@-}")
|
||||
TMP_FILE="/tmp/aicommit_tmp"
|
||||
|
||||
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.
|
||||
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
|
||||
)
|
||||
|
||||
pushd "$(pwd)" >/dev/null
|
||||
|
||||
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'
|
||||
}
|
||||
|
||||
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=$(
|
||||
@ -41,26 +55,25 @@ message=$(
|
||||
".message = $(jq -R -s '@json' <<<"${message_cat}")"
|
||||
)
|
||||
|
||||
# trunk-ignore(shellcheck/SC2091)
|
||||
# trunk-ignore(shellcheck/SC2310)
|
||||
if $(hasFlag -r --reset); then
|
||||
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" \
|
||||
-X POST \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "${message}" | jq -r '.content | gsub("\""; "")' >"${TMP_FILE}"
|
||||
|
||||
# trunk-ignore(shellcheck/SC2181)
|
||||
if [[ 0 -ne $? ]]; then
|
||||
echo "Failed to get response."
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
git commit -e -m "$(cat "${TMP_FILE}")" && rm "${TMP_FILE}"
|
||||
cat ".git/COMMIT_EDITMSG" >>"${TMP_FILE}"
|
||||
cat "${TMP_FILE}" >".git/COMMIT_EDITMSG"
|
||||
|
||||
popd >/dev/null
|
||||
rm "${TMP_FILE}"
|
||||
|
||||
"${EDITOR}" "${@-}"
|
||||
|
54
aicommit.clj
Executable file
54
aicommit.clj
Executable 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*))
|
@ -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
|
Reference in New Issue
Block a user