Skip to content

Instantly share code, notes, and snippets.

@laszpio
Created March 10, 2025 08:52
Show Gist options
  • Save laszpio/bbdfeae654d1f242cab3f8f41ee469c6 to your computer and use it in GitHub Desktop.
Save laszpio/bbdfeae654d1f242cab3f8f41ee469c6 to your computer and use it in GitHub Desktop.
Oh My ZSH plugin to load model for llama.cpp
# ~/.oh-my-zsh/plugins/llama-start/llama-cpp-start.plugin.zsh
llama-start() {
# Define model directory
local model_dir="${HOME}/models"
echo "Debug: Using model directory $model_dir"
# Check if directory exists
if [[ ! -d "$model_dir" ]]; then
echo "Error: $model_dir directory not found. Please create it and add GGUF or bin models."
return 1
fi
# Get list of local model files
local -a models
models=("$model_dir"/*.(gguf|bin)(N))
# Check if any models were found
if [[ ${#models[@]} -eq 0 ]]; then
echo "No model files (.gguf or .bin) found in $model_dir"
return 1
fi
# ANSI escape codes
local CLEAR_SCREEN="\033[2J\033[H" # Clear screen and move to top-left
local BOLD_ON="\033[1m"
local BOLD_OFF="\033[0m"
local REVERSE_ON="\033[7m" # Reverse video (highlight)
local REVERSE_OFF="\033[27m"
# Calculate width based on longest model name + extra padding
local max_length=0
for model in "${models[@]}"; do
local name=$(basename "$model")
local len=${#name}
if (( len > max_length )); then
max_length=$len
fi
done
local width=$(( max_length + 12 )) # "X) " (3) + borders (2) + left space (1) + right padding (6)
if (( width < 50 )); then width=50; fi # Minimum width
local height=$(( ${#models[@]} + 4 )) # Models + border + title + footer
# Function to draw the GUI
draw_gui() {
local selected=$1
print -n "$CLEAR_SCREEN"
# Draw top border
print -n "┌"
printf '─%.0s' {1..$((width-2))}
print -n "┐"
echo
# Center title
local title="Model Selector"
local title_len=${#title}
local title_padding=$(( (width - 2 - title_len) / 2 ))
print -n "│"
printf ' %.0s' {1..$title_padding}
print -n "$title"
printf ' %.0s' {1..$((width - 2 - title_len - title_padding))}
print -n "│"
echo
# Draw separator
print -n "├"
printf '─%.0s' {1..$((width-2))}
print -n "┤"
echo
# Draw model list
for ((i=1; i<=${#models[@]}; i++)); do
local model_name=$(basename "${models[$i]}")
local padding=$(( width - 6 - ${#model_name} )) # Adjust for "X) " (3) + borders (2) + left space (1)
print -n "│ "
if [[ $i -eq $selected ]]; then
print -n "$REVERSE_ON$i) $model_name$REVERSE_OFF"
else
print -n "$i) $model_name"
fi
printf ' %.0s' {1..$padding}
print -n "│"
echo
done
# Draw bottom border and instructions
print -n "└"
printf '─%.0s' {1..$((width-2))}
print -n "┘"
echo
print -n "$BOLD_ON"
local instr="Use ↑↓ to navigate, Enter to select, q to quit"
local instr_padding=$(( (width - ${#instr}) / 2 ))
printf ' %.0s' {1..$instr_padding}
print -n "$instr"
print -n "$BOLD_OFF"
echo
}
# Main loop
local selected=1
local key
while true; do
draw_gui $selected
# Read keypress
read -k 1 key
case $key in
$'\e') # Arrow keys
read -k 2 -t 0.1 key2
if [[ $key2 == "[A" && $selected -gt 1 ]]; then
((selected--)) # Up
elif [[ $key2 == "[B" && $selected -lt ${#models[@]} ]]; then
((selected++)) # Down
fi
;;
$'\n') # Enter
break
;;
q) # Quit
print -n "$CLEAR_SCREEN"
echo "Exiting..."
return 1
;;
esac
done
# Clear screen and start server
print -n "$CLEAR_SCREEN"
local selected_model="${models[$selected-1]}"
echo "Starting llama-server with $selected_model..."
if ! llama-server --model "$selected_model" -c 2048 --port 8080 --host 0.0.0.0; then
echo "Error: Failed to start llama-server with $selected_model"
return 1
fi
}
compdef _gnu_generic llama-cpp-start
@laszpio
Copy link
Author

laszpio commented Mar 10, 2025

Screenshot 2025-03-10 at 08 53 25

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment