91 lines
3.2 KiB
Bash
Executable File
91 lines
3.2 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Explicit model selection. Stops current gateway requests; keeps both model files.
|
|
set -Eeuo pipefail
|
|
umask 077
|
|
config_dir="${XDG_CONFIG_HOME:-$HOME/.config}/hermes-stack-maintenance"
|
|
usage() {
|
|
printf 'Aufruf: %s [status|routine|fallback|large] (Modellwechsel beendet laufende Anfragen)\n' "$0"
|
|
}
|
|
case "${1:-}" in
|
|
''|status)
|
|
# shellcheck disable=SC1091
|
|
source "$config_dir/llama.env"
|
|
printf 'Aktives Modell: %s\nKonfiguration: %s/llama.env\n' "$LLAMA_MODEL" "$config_dir"
|
|
exit 0 ;;
|
|
-h|--help) usage; exit 0 ;;
|
|
routine|fallback|large) selected="$1" ;;
|
|
*) usage >&2; exit 2 ;;
|
|
esac
|
|
[[ $# -eq 1 ]] || exit 2
|
|
exec 9>"${XDG_RUNTIME_DIR:-/tmp}/hermes-stack-hermes-update.lock"
|
|
flock -n 9 || { printf 'Maintenance läuft.\n' >&2; exit 1; }
|
|
runner="$(dirname -- "$(readlink -f -- "${BASH_SOURCE[0]}")")/hermes-stack-llama-run"
|
|
candidate="$config_dir/llama-$selected.env"
|
|
HERMES_LLAMA_CONFIG="$candidate" "$runner" --check
|
|
backup="$(mktemp "$config_dir/llama.env.before-model-switch.XXXXXX")"
|
|
cp -p -- "$config_dir/llama.env" "$backup"
|
|
hermes_config="${HERMES_HOME:-$HOME/.hermes}/config.yaml"
|
|
hermes_backup=""
|
|
if [[ -f "$hermes_config" ]]; then
|
|
hermes_backup="$(mktemp "$config_dir/hermes.yaml.before-model-switch.XXXXXX")"
|
|
cp -p -- "$hermes_config" "$hermes_backup"
|
|
fi
|
|
rollback() {
|
|
local rc=$?
|
|
trap - EXIT
|
|
if ((rc != 0)); then
|
|
install -m 0600 "$backup" "$config_dir/llama.env" || true
|
|
if [[ -n "$hermes_backup" ]]; then
|
|
install -m 0600 "$hermes_backup" "$hermes_config" || true
|
|
fi
|
|
systemctl --user restart llama-qwen.service || true
|
|
systemctl --user start hermes-gateway.service || true
|
|
printf 'Modellwechsel fehlgeschlagen; vorherige Konfiguration zurückkopiert. Dienststatus prüfen.\n' >&2
|
|
fi
|
|
exit "$rc"
|
|
}
|
|
trap rollback EXIT
|
|
systemctl --user stop hermes-gateway.service
|
|
install -m 0600 "$candidate" "$config_dir/llama.env"
|
|
# Keep Hermes' actual context and compression trigger paired with the model.
|
|
# Legacy profiles without metadata use the original 64K window.
|
|
LLAMA_CONTEXT_TOKENS=65536
|
|
# shellcheck disable=SC1090
|
|
source "$candidate"
|
|
if [[ -n "$hermes_backup" ]]; then
|
|
python3 - "$hermes_config" "$LLAMA_CONTEXT_TOKENS" <<'PY'
|
|
import os
|
|
from pathlib import Path
|
|
import sys
|
|
import tempfile
|
|
import yaml
|
|
path = Path(sys.argv[1])
|
|
context = int(sys.argv[2])
|
|
if context < 64000:
|
|
raise ValueError("Hermes requires at least 64000 context tokens")
|
|
config = yaml.safe_load(path.read_text())
|
|
config.setdefault("model", {})["context_length"] = context
|
|
config.setdefault("compression", {})["threshold_tokens"] = min(24000, context * 3 // 8)
|
|
with tempfile.NamedTemporaryFile(mode="w", dir=path.parent, delete=False) as out:
|
|
yaml.safe_dump(config, out, allow_unicode=True, sort_keys=False, width=1000)
|
|
temporary = Path(out.name)
|
|
os.chmod(temporary, 0o600)
|
|
temporary.replace(path)
|
|
PY
|
|
fi
|
|
ready=0
|
|
if systemctl --user restart llama-qwen.service; then
|
|
for ((i=0; i<90; i++)); do
|
|
if curl -fsS --max-time 2 http://127.0.0.1:8080/health 2>/dev/null | jq -e '.status == "ok"' >/dev/null 2>&1; then
|
|
ready=1; break
|
|
fi
|
|
sleep 2
|
|
done
|
|
fi
|
|
if ((ready == 0)); then
|
|
exit 1
|
|
fi
|
|
systemctl --user start hermes-gateway.service
|
|
trap - EXIT
|
|
printf 'Modellprofil %s aktiv. Vorherige Konfiguration: %s\n' "$selected" "$backup"
|