Datasets:
Download scripts/llama-server.sh from tesraghavan/agent-trace: direct link, hf CLI and curl.
- Browser
- Download file 5.9 kB
-
https://huggingface.co/datasets/tesraghavan/agent-trace/resolve/main/scripts/llama-server.sh
- Command line
-
hf download hf://datasets/tesraghavan/agent-trace/scripts/llama-server.sh
-
curl -L -o llama-server.sh https://huggingface.co/datasets/tesraghavan/agent-trace/resolve/main/scripts/llama-server.sh
5.9 kB
| # Start/stop/check the local llama-server for trace collection. | |
| # | |
| # Usage: | |
| # ./scripts/llama-server.sh start # start in background | |
| # ./scripts/llama-server.sh stop # stop | |
| # ./scripts/llama-server.sh status # check if running | |
| # ./scripts/llama-server.sh logs # tail the log file | |
| set -euo pipefail | |
| PORT="${LLAMA_PORT:-8080}" | |
| MODEL="${LLAMA_MODEL:-Qwen/Qwen3-0.6B-GGUF}" | |
| CTX="${LLAMA_CTX:-16384}" | |
| LOGFILE="${LLAMA_LOGFILE:-.llama-server.log}" | |
| CHAT_TEMPLATE_FILE="${LLAMA_CHAT_TEMPLATE_FILE:-}" | |
| JINJA="${LLAMA_JINJA:-1}" | |
| FLASH_ATTN="${LLAMA_FLASH_ATTN:-1}" | |
| N_GPU_LAYERS="${LLAMA_N_GPU_LAYERS:-}" | |
| cmd="${1:-status}" | |
| pid_of_server() { | |
| lsof -ti:"$PORT" 2>/dev/null || true | |
| } | |
| is_truthy() { | |
| case "${1:-}" in | |
| 1|true|TRUE|yes|YES|on|ON) return 0 ;; | |
| *) return 1 ;; | |
| esac | |
| } | |
| print_server_metadata() { | |
| python3 - "$PORT" "$CHAT_TEMPLATE_FILE" "$JINJA" "$FLASH_ATTN" "$N_GPU_LAYERS" <<'PY' | |
| import hashlib | |
| import json | |
| import sys | |
| from pathlib import Path | |
| from urllib.error import HTTPError, URLError | |
| from urllib.request import urlopen | |
| port, chat_template_file, jinja, flash_attn, n_gpu_layers = sys.argv[1:] | |
| origin = f"http://127.0.0.1:{port}" | |
| def fetch(url): | |
| try: | |
| with urlopen(url, timeout=3) as response: | |
| return json.loads(response.read().decode("utf-8")) | |
| except (HTTPError, URLError, TimeoutError, ValueError): | |
| return {} | |
| models = fetch(f"{origin}/v1/models") | |
| props = fetch(f"{origin}/props") | |
| health = fetch(f"{origin}/health") | |
| print(f"health: {health.get('status', 'unknown')}") | |
| model_path = props.get("model_path") | |
| data = models.get("data") | |
| if not model_path and isinstance(data, list) and data: | |
| first = data[0] | |
| if isinstance(first, dict): | |
| model_path = first.get("id") | |
| model_artifact = Path(model_path).name if model_path else "unknown" | |
| print(f"model_artifact: {model_artifact}") | |
| quant = None | |
| for token in ("Q8_0", "Q6_K", "Q5_K", "Q4_K", "Q4_0", "Q4_1", "F16", "BF16", "FP16", "FP32"): | |
| if token.lower() in model_artifact.lower(): | |
| quant = token | |
| break | |
| if quant: | |
| print(f"quantization: {quant}") | |
| generation = props.get("default_generation_settings", {}) | |
| params = generation.get("params", {}) if isinstance(generation, dict) else {} | |
| n_ctx = generation.get("n_ctx") | |
| if n_ctx is not None: | |
| print(f"context_length: {n_ctx}") | |
| build_info = props.get("build_info") | |
| if build_info: | |
| print(f"build_info: {str(build_info).splitlines()[0]}") | |
| chat_format = params.get("chat_format") | |
| if chat_format: | |
| print(f"chat_format: {chat_format}") | |
| reasoning_format = params.get("reasoning_format") | |
| if reasoning_format: | |
| print(f"reasoning_format: {reasoning_format}") | |
| reasoning_in_content = params.get("reasoning_in_content") | |
| if reasoning_in_content is not None: | |
| print(f"reasoning_in_content: {reasoning_in_content}") | |
| model_meta = None | |
| if isinstance(data, list) and data: | |
| first = data[0] | |
| if isinstance(first, dict): | |
| model_meta = first.get("meta", {}) | |
| if isinstance(model_meta, dict): | |
| n_ctx_train = model_meta.get("n_ctx_train") | |
| n_params = model_meta.get("n_params") | |
| if n_ctx_train is not None: | |
| print(f"n_ctx_train: {n_ctx_train}") | |
| if n_params is not None: | |
| print(f"n_params: {n_params}") | |
| if chat_template_file: | |
| print(f"chat_template_file: {Path(chat_template_file).name}") | |
| else: | |
| raw_template = props.get("chat_template") | |
| if raw_template: | |
| digest = hashlib.sha256(raw_template.encode("utf-8")).hexdigest()[:12] | |
| print(f"chat_template_hash: sha256:{digest}") | |
| print(f"jinja_env: {jinja}") | |
| print(f"flash_attn_env: {flash_attn}") | |
| if n_gpu_layers: | |
| print(f"n_gpu_layers_env: {n_gpu_layers}") | |
| PY | |
| } | |
| case "$cmd" in | |
| start) | |
| existing=$(pid_of_server) | |
| if [ -n "$existing" ]; then | |
| echo "llama-server already running on port $PORT (pid $existing)" | |
| exit 0 | |
| fi | |
| server_args=( | |
| -hf "$MODEL" | |
| -c "$CTX" | |
| --port "$PORT" | |
| ) | |
| if is_truthy "$JINJA"; then | |
| server_args+=(--jinja) | |
| fi | |
| if [ -n "$CHAT_TEMPLATE_FILE" ]; then | |
| server_args+=(--chat-template-file "$CHAT_TEMPLATE_FILE") | |
| fi | |
| if is_truthy "$FLASH_ATTN"; then | |
| server_args+=(--flash-attn) | |
| fi | |
| if [ -n "$N_GPU_LAYERS" ]; then | |
| server_args+=(--n-gpu-layers "$N_GPU_LAYERS") | |
| fi | |
| echo "Starting llama-server (model=$MODEL ctx=$CTX port=$PORT)..." | |
| echo " jinja=$JINJA flash_attn=$FLASH_ATTN n_gpu_layers=${N_GPU_LAYERS:-unset}" | |
| if [ -n "$CHAT_TEMPLATE_FILE" ]; then | |
| echo " chat_template_file=$CHAT_TEMPLATE_FILE" | |
| fi | |
| nohup llama-server "${server_args[@]}" > "$LOGFILE" 2>&1 & | |
| # Wait for server to be ready | |
| echo -n "Waiting for server" | |
| for i in $(seq 1 30); do | |
| if curl -sf "http://127.0.0.1:$PORT/health" > /dev/null 2>&1; then | |
| echo " ready (pid $!)" | |
| exit 0 | |
| fi | |
| echo -n "." | |
| sleep 1 | |
| done | |
| echo " timeout! Check $LOGFILE" | |
| exit 1 | |
| ;; | |
| stop) | |
| existing=$(pid_of_server) | |
| if [ -z "$existing" ]; then | |
| echo "No llama-server on port $PORT" | |
| exit 0 | |
| fi | |
| echo "Stopping llama-server (pid $existing)..." | |
| kill "$existing" | |
| echo "Stopped." | |
| ;; | |
| status) | |
| existing=$(pid_of_server) | |
| if [ -n "$existing" ]; then | |
| echo "llama-server running on port $PORT (pid $existing)" | |
| print_server_metadata || echo "(metadata unavailable)" | |
| else | |
| echo "llama-server not running on port $PORT" | |
| exit 1 | |
| fi | |
| ;; | |
| logs) | |
| tail -f "$LOGFILE" | |
| ;; | |
| *) | |
| echo "Usage: $0 {start|stop|status|logs}" | |
| exit 1 | |
| ;; | |
| esac | |