#!/bin/sh # Start an OpenAI-compatible chat server with the Bonsai model. # Usage: ./scripts/start_llama_server.sh # Then open http://localhost:8080 in your browser. set -e SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" . "$SCRIPT_DIR/common.sh" assert_valid_model DEMO_DIR="$(resolve_demo_dir)" cd "$DEMO_DIR" assert_gguf_downloaded HOST="0.0.0.0" PORT=8080 : "${BONSAI_CACHE_TYPE_K:=f16}" : "${BONSAI_CACHE_TYPE_V:=f16}" # ── Check port is free ── if curl -s --max-time 2 "http://localhost:$PORT/health" >/dev/null 2>&1; then warn "llama-server is already running on port $PORT." echo " Stop it first with: kill \$(lsof -ti TCP:$PORT)" exit 1 fi # ── Find model ── MODEL="" for _m in $GGUF_MODEL_DIR/*.gguf; do [ -f "$_m" ] && MODEL="$DEMO_DIR/$_m" && break done # ── Find binary ── BIN="$(find_llama_bin "$DEMO_DIR" llama-server || true)" if [ -z "$BIN" ]; then err "llama-server not found. Build the cloned llama.cpp fork or set BONSAI_LLAMA_BIN_DIR." exit 1 fi BIN_DIR="$(cd "$(dirname "$BIN")" && pwd)" export LD_LIBRARY_PATH="$BIN_DIR${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}" echo "" echo "=== llama.cpp server (GGUF) ===" echo " Model: $(basename "$MODEL")" echo " Binary: $BIN" echo " Context: auto-fit (-c 0)" echo "" echo " Open http://localhost:$PORT in your browser to chat." echo " API: http://localhost:$PORT/v1/chat/completions" echo " Press Ctrl+C to stop." echo "" exec "$BIN" -m "$MODEL" --host "$HOST" --port "$PORT" -ngl 99 -c "$CTX_SIZE_DEFAULT" \ --cache-type-k "$BONSAI_CACHE_TYPE_K" \ --cache-type-v "$BONSAI_CACHE_TYPE_V" \ --temp 0.5 --top-p 0.85 --top-k 20 --min-p 0 \ --reasoning-budget 0 --reasoning-format none \ --chat-template-kwargs '{"enable_thinking": false}' \ "$@"