#!/bin/bash # llama.cpp's server for Fintan on a Mac (1 Dec 2026). Ollama 0.40.19 refuses add-ons ("LoRA adapters are no longer # supported"), so on a Mac the base model runs in llama.cpp's server with every learnt update loaded beside it at # scale 0; the front door turns one on per request. # bash engine.sh start | stop | restart cd "$(dirname "$0")" PYTHON=${PYTHON:-python3} PORT=${FINTAN_ENGINE_PORT:-8081} PIDF=engine.pid BASE_GGUF=$($PYTHON -c "import json; m=json.load(open('manifest.json')); print((m.get('gguf') or {}).get(m['base'], ''))") stop() { if [ -f "$PIDF" ]; then kill "$(cat "$PIDF")" 2>/dev/null for i in $(seq 1 30); do kill -0 "$(cat "$PIDF")" 2>/dev/null || break; sleep 1; done rm -f "$PIDF" fi } start() { [ -f "$BASE_GGUF" ] || { echo "missing the base model file '$BASE_GGUF' (manifest.json: gguf)"; exit 1; } # (2 Dec 2026) another program on the port: say so plainly (llama.cpp's own message is buried in engine.log) if curl -s -o /dev/null --max-time 2 "http://127.0.0.1:$PORT/"; then echo "port $PORT is already taken by another program. Start again on another port, for example:" echo " FINTAN_ENGINE_PORT=8082 bash serve_mac.sh" exit 1 fi # every update gets its add-on file once (door/gguf_lora.py), then all are loaded ARGS=() N=0 while IFS= read -r f; do [ -n "$f" ] && { ARGS+=(--lora "$f"); N=$((N + 1)); } done < <($PYTHON door/gguf_lora.py manifest.json "$BASE_GGUF" | grep '^LORA ' | cut -c6-) nohup llama-server -m "$BASE_GGUF" "${ARGS[@]}" --lora-init-without-apply --host 127.0.0.1 --port "$PORT" \ -ngl 99 -c "${FINTAN_CTX:-16384}" --parallel "${FINTAN_PARALLEL:-4}" --jinja > engine.log 2>&1 & echo $! > "$PIDF" for i in $(seq 1 150); do if curl -s "http://127.0.0.1:$PORT/health" | grep -q '"ok"'; then # every add-on off unless a request turns one on (the front door always says which), so the learner's own # writing and any other caller get the plain model $PYTHON -c "import json, urllib.request as u; a = json.loads(u.urlopen('http://127.0.0.1:$PORT/lora-adapters').read()); u.urlopen(u.Request('http://127.0.0.1:$PORT/lora-adapters', data=json.dumps([{'id': x['id'], 'scale': 0.0} for x in a]).encode(), headers={'Content-Type': 'application/json'})).read()" 2> /dev/null echo "engine up on port $PORT with $N update(s)" return 0 fi kill -0 "$(cat "$PIDF")" 2>/dev/null || break sleep 2 done echo "llama.cpp's server did not come up (engine.log):" tail -n 20 engine.log exit 1 } case "$1" in start) start ;; stop) stop ;; restart) stop; start ;; *) echo "bash engine.sh start | stop | restart"; exit 1 ;; esac