Skip to content

Commit e88aa80

Browse files
committed
Add support for temperature and context window size configuration
1 parent a0e8609 commit e88aa80

File tree

3 files changed

+120
-87
lines changed

3 files changed

+120
-87
lines changed

src/giv.sh

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ cmd_message() {
550550
pr=$(portable_mktemp "commit_message_prompt_XXXXXX.md")
551551
build_prompt "${TEMPLATE_DIR}/message_prompt.md" "${hist}" >"${pr}"
552552
print_debug "Generated prompt file ${pr}"
553-
res=$(generate_response "${pr}" "${model_mode}")
553+
res=$(generate_response "${pr}" "${model_mode}" "0.9" "32768")
554554
if [ $? -ne 0 ]; then
555555
printf 'Error: Failed to generate AI response.\n' >&2
556556
exit 1
@@ -614,7 +614,8 @@ cmd_summary() {
614614
tmp_prompt_file=$(portable_mktemp "final_summary_prompt_XXXXXX.md")
615615
build_prompt "${prompt_file_name}" "${summaries_file}" >"${tmp_prompt_file}"
616616
print_debug "$(cat "${tmp_prompt_file}" || true)"
617-
generate_from_prompt "${tmp_prompt_file}" "${sum_output_file}" "${sum_model_mode}"
617+
generate_from_prompt "${tmp_prompt_file}" "${sum_output_file}" \
618+
"${sum_model_mode}" "0.7"
618619
}
619620
cmd_release_notes() {
620621
summaries_file=$(portable_mktemp "release_notes_summaries_XXXXXX.md")
@@ -632,7 +633,8 @@ cmd_release_notes() {
632633
[ "${debug}" = "true" ] && printf 'Debug: Generated prompt file %s\n' "${tmp_prompt_file}"
633634

634635
generate_from_prompt "${tmp_prompt_file}" \
635-
"${output_file:-${release_notes_file}}" "${model_mode}"
636+
"${output_file:-${release_notes_file}}" \
637+
"${model_mode}" "0.6" "65536"
636638
}
637639

638640
cmd_announcement() {
@@ -654,7 +656,8 @@ cmd_announcement() {
654656
print_debug "Generated prompt file: ${tmp_prompt_file}"
655657
print_info "$(cat "${tmp_prompt_file}" || true)"
656658
generate_from_prompt "${tmp_prompt_file}" \
657-
"${output_file:-${announce_file}}" "${model_mode}"
659+
"${output_file:-${announce_file}}" \
660+
"${model_mode}" "0.6" "65536"
658661
}
659662

660663
# -------------------------------------------------------------------
@@ -703,7 +706,8 @@ cmd_changelog() {
703706
rm -f "$summaries_file" "$tmp_prompt_file"
704707
exit 1
705708
}
706-
if ! generate_from_prompt "$tmp_prompt_file" "$response_file" "$model_mode"; then
709+
if ! generate_from_prompt "$tmp_prompt_file" "$response_file" \
710+
"$model_mode" "0.7"; then
707711
printf 'Error: generate_from_prompt failed\n' >&2
708712
rm -f "$summaries_file" "$tmp_prompt_file" "$response_file"
709713
exit 1
@@ -735,16 +739,18 @@ cmd_changelog() {
735739
printf 'Error: manage_section failed\n' >&2
736740
exit 1
737741
}
742+
cat "$updated" >"$tmp_out"
743+
append_link "$tmp_out" "Managed by giv" "https://github.com/giv-cli/giv"
738744

739745
# 8) Dry‐run?
740746
if [ "$dry_run" = "true" ]; then
741747
print_debug "Dry run: updated changelog content:"
742-
cat "$updated"
748+
cat "$tmp_out"
743749
return 0
744750
fi
745751

746752
# 9) Write back to real changelog
747-
if cat "$updated" >"$output_file"; then
753+
if cat "$tmp_out" >"$output_file"; then
748754
printf 'Changelog written to %s\n' "$output_file"
749755
else
750756
printf 'Error: Failed to write %s\n' "$output_file" >&2

src/helpers.sh

Lines changed: 106 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ replace_tokens() {
124124
' "$@"
125125
}
126126

127-
128127
# build_prompt [--project-title X] [--version V] [--example E] [--rules R] \
129128
# <template_file> <diff_file>
130129
#
@@ -373,37 +372,59 @@ generate_remote() {
373372
#print_debug "Parsed response:$result"
374373
echo "${result}"
375374
}
376-
377375
run_local() {
376+
# Backup original values of OLLAMA_TEMPERATURE and OLLAMA_NUM_CTX
377+
orig_ollama_temperature="${OLLAMA_TEMPERATURE:-}"
378+
orig_ollama_num_ctx="${OLLAMA_NUM_CTX:-}"
379+
380+
export OLLAMA_TEMPERATURE="${3:-0.9}"
381+
export OLLAMA_NUM_CTX="${4:-32768}"
382+
378383
if [ "$debug" = "true" ]; then
379384
# shellcheck disable=SC2154
380385
ollama run "${model}" --verbose <"$1"
381386
else
382387
ollama run "${model}" <"$1"
383388
fi
389+
390+
# Reset to original values after the command completes
391+
export OLLAMA_TEMPERATURE="${orig_ollama_temperature}"
392+
export OLLAMA_NUM_CTX="${orig_ollama_num_ctx}"
384393
}
385394

386395
# The `generate_response` function generates a response based on the specified mode.
387396
#
388397
# Parameters:
389398
# $1 - Path to the input file.
390399
# $2 (optional) - Mode for generating the response. Possible values are 'remote', 'none', or any other value for local generation.
400+
# $3 (optional) - Temperature setting for the model, if applicable.
401+
# $4 (optional) - Context window size for the model, if applicable.
391402
#
392403
# Description:
393404
# The function determines the mode of operation based on the second argument ($2), falling back to the `model_mode` environment variable, and finally defaulting to 'auto'.
394405
# If debugging is enabled (via the `debug` environment variable), it prints a debug message indicating the chosen mode.
395406
#
396407
# Depending on the mode:
397-
# - 'remote': Calls the `generate_remote` function with the input file path as an argument.
408+
# - 'remote': Calls the `generate_remote` function with the input file path as an argument, along with temperature and context window size if provided.
398409
# - 'none': Outputs the content of the input file directly using `cat`.
399-
# - Any other value: Calls the `run_local` function with the input file path as an argument.
410+
# - Any other value: Calls the `run_local` function with the input file path as an argument, along with temperature and context window size if provided.
400411
generate_response() {
401412
gen_mode="${2:-$model_mode:-auto}"
402-
print_debug "Generating response using $gen_mode mode"
413+
temp="${3:-0.9}" # Default to a neutral temperature of 1.0
414+
ctx_window="${4:-32768}" # Default context window size
415+
416+
print_debug "Generating response using $gen_mode mode with temperature=$temp and context window size=$ctx_window"
417+
403418
case ${gen_mode} in
404-
remote) generate_remote "$1" ;;
405-
none) cat "$1" ;;
406-
*) run_local "$1" ;;
419+
remote)
420+
generate_remote "$1" "$temp" "$ctx_window"
421+
;;
422+
none)
423+
cat "$1"
424+
;;
425+
*)
426+
run_local "$1" "$temp" "$ctx_window"
427+
;;
407428
esac
408429
}
409430

@@ -418,13 +439,15 @@ generate_from_prompt() {
418439
prompt_file="$1"
419440
response_output_file="$2"
420441
gen_mode="${3:-${GIV_MODEL_MODE:-${model_mode:-auto}}}"
442+
temperature="${4:-0.9}" # Default value for temperature
443+
context_window="${5:-32768}" # Default value for context window
421444

422445
print_debug "Prompt file: $prompt_file"
423446
print_debug "Output file: $response_output_file"
424447
print_debug "Model mode: $gen_mode"
425448

426449
# 1) Invoke the AI
427-
if ! res=$(generate_response "$prompt_file" "$gen_mode"); then
450+
if ! res=$(generate_response "$prompt_file" "$gen_mode" "$temperature" "$context_window"); then
428451
printf 'Error: generate_response failed (mode=%s)\n' "$gen_mode" >&2
429452
exit 1
430453
fi
@@ -784,7 +807,7 @@ summarize_commit() {
784807
summary_template=$(build_prompt --version "${sc_version}" "${TEMPLATE_DIR}/summary_prompt.md" "${hist}")
785808
print_debug "Using summary prompt: ${summary_template}"
786809
printf '%s\n' "${summary_template}" >"${pr}"
787-
res=$(generate_response "${pr}" "${gen_mode}")
810+
res=$(generate_response "${pr}" "${gen_mode}" "0.9" "32768")
788811
echo "${res}" >"${res_file}"
789812

790813
printf '%s\n' "${res}"
@@ -841,93 +864,96 @@ extract_section() {
841864
sed -n "${start},${end}p" "$file"
842865
}
843866

844-
845867
is_glow_installed() {
846-
command -v glow >/dev/null 2>&1
868+
command -v glow >/dev/null 2>&1
847869
}
848870

849871
install_pkg() {
850-
echo "Checking package managers..."
851-
if command -v brew >/dev/null 2>&1; then
852-
brew install glow && return 0
853-
elif command -v port >/dev/null 2>&1; then
854-
sudo port install glow && return 0
855-
elif command -v pacman >/dev/null 2>&1; then
856-
sudo pacman -S --noconfirm glow && return 0
857-
elif command -v xbps-install >/dev/null 2>&1; then
858-
sudo xbps-install -Sy glow && return 0
859-
elif command -v nix-shell >/dev/null 2>&1; then
860-
nix-shell -p glow --run glow && return 0
861-
elif command -v pkg >/dev/null 2>&1 && uname -s | grep -qi freebsd; then
862-
sudo pkg install -y glow && return 0
863-
elif command -v eopkg >/dev/null 2>&1; then
864-
sudo eopkg install glow && return 0
865-
elif command -v snap >/dev/null 2>&1; then
866-
sudo snap install glow && return 0
867-
elif command -v choco >/dev/null 2>&1; then
868-
choco install glow -y && return 0
869-
elif command -v scoop >/dev/null 2>&1; then
870-
scoop install glow && return 0
871-
elif command -v winget >/dev/null 2>&1; then
872-
winget install --id=charmbracelet.glow -e && return 0
873-
fi
874-
return 1
872+
echo "Checking package managers..."
873+
if command -v brew >/dev/null 2>&1; then
874+
brew install glow && return 0
875+
elif command -v port >/dev/null 2>&1; then
876+
sudo port install glow && return 0
877+
elif command -v pacman >/dev/null 2>&1; then
878+
sudo pacman -S --noconfirm glow && return 0
879+
elif command -v xbps-install >/dev/null 2>&1; then
880+
sudo xbps-install -Sy glow && return 0
881+
elif command -v nix-shell >/dev/null 2>&1; then
882+
nix-shell -p glow --run glow && return 0
883+
elif command -v pkg >/dev/null 2>&1 && uname -s | grep -qi freebsd; then
884+
sudo pkg install -y glow && return 0
885+
elif command -v eopkg >/dev/null 2>&1; then
886+
sudo eopkg install glow && return 0
887+
elif command -v snap >/dev/null 2>&1; then
888+
sudo snap install glow && return 0
889+
elif command -v choco >/dev/null 2>&1; then
890+
choco install glow -y && return 0
891+
elif command -v scoop >/dev/null 2>&1; then
892+
scoop install glow && return 0
893+
elif command -v winget >/dev/null 2>&1; then
894+
winget install --id=charmbracelet.glow -e && return 0
895+
fi
896+
return 1
875897
}
876898

877899
install_from_github() {
878-
echo "Installing glow binary from GitHub releases…"
879-
os=$(uname -s | tr '[:upper:]' '[:lower:]')
880-
arch=$(uname -m)
881-
case "$arch" in
882-
x86_64|amd64) arch="x86_64" ;;
883-
arm64|aarch64) arch="arm64" ;;
884-
*) echo "Unsupported arch: $arch"; exit 1 ;;
885-
esac
900+
echo "Installing glow binary from GitHub releases…"
901+
os=$(uname -s | tr '[:upper:]' '[:lower:]')
902+
arch=$(uname -m)
903+
case "$arch" in
904+
x86_64 | amd64) arch="x86_64" ;;
905+
arm64 | aarch64) arch="arm64" ;;
906+
*)
907+
echo "Unsupported arch: $arch"
908+
exit 1
909+
;;
910+
esac
886911

887-
tag=$(curl -fsSL https://api.github.com/repos/charmbracelet/glow/releases/latest \
888-
| grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
889-
file="glow_${tag#v}_${os}_${arch}.tar.gz"
912+
tag=$(curl -fsSL https://api.github.com/repos/charmbracelet/glow/releases/latest |
913+
grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
914+
file="glow_${tag#v}_${os}_${arch}.tar.gz"
890915

891-
tmpdir=$(mktemp -d)
892-
curl -fsSL "https://github.com/charmbracelet/glow/releases/download/$tag/$file" -o "$tmpdir/glow.tar.gz"
893-
curl -fsSL "https://github.com/charmbracelet/glow/releases/download/$tag/checksums.txt" -o "$tmpdir/checksums.txt"
916+
tmpdir=$(mktemp -d)
917+
curl -fsSL "https://github.com/charmbracelet/glow/releases/download/$tag/$file" -o "$tmpdir/glow.tar.gz"
918+
curl -fsSL "https://github.com/charmbracelet/glow/releases/download/$tag/checksums.txt" -o "$tmpdir/checksums.txt"
894919

895-
cd "$tmpdir"
896-
sha256sum -c checksums.txt --ignore-missing --quiet || {
897-
echo "Checksum verification failed"; exit 1;
898-
}
920+
cd "$tmpdir"
921+
sha256sum -c checksums.txt --ignore-missing --quiet || {
922+
echo "Checksum verification failed"
923+
exit 1
924+
}
899925

900-
tar -xzf glow.tar.gz
901-
chmod +x glow
926+
tar -xzf glow.tar.gz
927+
chmod +x glow
902928

903-
bindir="/usr/local/bin"
904-
if [ -w "$bindir" ]; then
905-
mv glow "$bindir"
906-
else
907-
sudo mv glow "$bindir"
908-
fi
929+
bindir="/usr/local/bin"
930+
if [ -w "$bindir" ]; then
931+
mv glow "$bindir"
932+
else
933+
sudo mv glow "$bindir"
934+
fi
909935

910-
cd -
911-
rm -rf "$tmpdir"
936+
cd -
937+
rm -rf "$tmpdir"
912938

913-
echo "glow installed to $bindir"
939+
echo "glow installed to $bindir"
914940
}
915941

916942
ensure_glow() {
917-
if is_installed; then
918-
echo "✔ glow already installed: $(command -v glow)"
919-
return
920-
fi
943+
if is_installed; then
944+
echo "✔ glow already installed: $(command -v glow)"
945+
return
946+
fi
921947

922-
echo "✗ glow not found. Installing…"
923-
if install_pkg; then
924-
echo "Installed via package manager."
925-
else
926-
install_from_github
927-
fi
948+
echo "✗ glow not found. Installing…"
949+
if install_pkg; then
950+
echo "Installed via package manager."
951+
else
952+
install_from_github
953+
fi
928954

929-
if ! is_installed; then
930-
echo "Installation failed. See https://github.com/charmbracelet/glow#installation"
931-
exit 1
932-
fi
955+
if ! is_installed; then
956+
echo "Installation failed. See https://github.com/charmbracelet/glow#installation"
957+
exit 1
958+
fi
933959
}

templates/message_prompt.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ Write a git commit message for the provided git history following the Keep a Cha
4646
[EXAMPLE]
4747

4848
[RULES]
49+
Do not include ``` code fencing in your response.

0 commit comments

Comments
 (0)