Update git-commit-ai and add new scripts

- update DWM config
- add tiktoken-cli for token counting
- introduce a shell script for generating AI-assisted commit messages
This commit is contained in:
Sam 2024-06-20 13:07:20 +01:00
parent 37bd716e05
commit ed4f367713
2 changed files with 124 additions and 0 deletions

View File

@ -25,6 +25,7 @@
./scripts/clipboard-save.nix
./scripts/clipboard-recall.nix
./scripts/get-focused-monitor.nix
./scripts/git-commit-ai.nix
];
home.packages = [

View File

@ -0,0 +1,123 @@
{ pkgs, ... }:
{
home.packages = [
(pkgs.writers.writePython3Bin
"tiktoken-cli"
{ libraries = [ pkgs.python3Packages.tiktoken ]; }
''
import os
import sys
import tiktoken
content = sys.stdin.read()
default = "gpt-3.5-turbo-0301"
model = os.getenv('TIKTOKEN_MODEL', default)
try:
encoding = tiktoken.encoding_for_model(model)
except KeyError:
encoding = tiktoken.get_encoding(default)
print(len(encoding.encode(content)))
'')
(pkgs.writeShellScriptBin "git-commit-ai" ''
# This bash script performs the following tasks:
# 1. Takes a git diff of modifications made to a repo.
# 2. Passes this diff to an LLM (Large Language Model) that generates a commit
# message.
#
# Usage: ./git-commit-ai --git-diff-commands --model
#
#
endpoint="10.0.10.100:11434"
diff=$(git diff "$@" )
[ -z "$diff" ] && echo "No diff provided. Did you stage the changes?" && exit 1
tokens=$(echo "$diff" | tiktoken-cli)
echo -e "\n\nEstimated tokens: $tokens\n\n"
choose_model() {
models_response=$(curl http://$endpoint/api/tags)
model=$(echo "$models_response" | jq -r '..|.model? // empty' | fzf | sed 's/:latest$//')
}
choose_model
system_message="""
You are a git commit generator. You will be provided with a git diff file.
Your role is to create a concise commit message that outlines all of the
most relevant and impactful changes in the diff. You only output a commit
message, do not write anything else not related to the commit. Use
imperative mood for you writing style.
Your message should include a short summary in the first line followed by
more detailed descriptions of the changes on subsequent lines.
The first line summary should be no more than 70 characters, not end with a
period, and a blank line should separate the summary from the body of the
message.
There should only be 1 summary line, and 1 body with the description.
The body should include no more than 3 bullet points with more detailed
descriptions of the files and functions that have been modified.
Do not output any code.
"""
query=$(jq -n --arg model "$model" --arg system_message "$system_message" --arg prompt "$diff" '{model: $model, system: $system_message, prompt: $prompt, stream: false}')
echo "$query"
git config commit.template ".git/gitmessage"
redo=""
while true
do
[ -z "$redo" ] && echo "Using model: $model" && commit_message=$(curl -s http://$endpoint/api/generate -H "Content-Type: application/json" -d "$query" | jq -r '.response')
echo ""
echo "Message:"
echo "---------------------------------------------"
echo ""
echo "$commit_message"
echo ""
echo "---------------------------------------------"
echo "What would you like to do?"
echo ""
echo "Copy to clipboard (c)"
echo "Git commit in editor (g)"
echo "Open in editor (e)"
echo "Show diff (d)"
echo "Choose different model (m)"
echo "Exit and do nothing (q)"
read -r -p "Action: (c|g|e|q) " input
case "$input" in
"c") echo "$commit_message" | ${pkgs.xclip}/bin/xclip -i -selection CLIPBOARD &&
${pkgs.libnotify}/bin/notify-send "AI Buddy" "Copied to clipboard"
exit 0
;;
"g") echo "$commit_message" > ".git/gitmessage" &&
git commit -e
cat /dev/null > ".git/gitmessage" # clear the template
exit 0
;;
"d") echo -e "Current commit message:\n\n$commit_message\n\nDiff file:\n\n$diff" | less
redo=true
shift
;;
"e") echo "$commit_message" | nvim -
exit 0
;;
"m") choose_model && shift
;;
"q") exit 0
;;
*) exit 0
esac
done
'')
];
}