Consolidated local ollama model logic

This commit is contained in:
2026-04-07 15:44:17 +02:00
parent 9b2573ebb8
commit e737c51924
10 changed files with 1943 additions and 350 deletions
+149
View File
@@ -0,0 +1,149 @@
#!/usr/bin/env bash
set -euo pipefail
ROOT="${ROOT_OVERRIDE:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)}"
ENV_FILE="${ENV_FILE:-${ROOT}/.env}"
MODFILE="${MODFILE:-${ROOT}/Modelfile}"
SQLAI_BIN="${SQLAI_BIN:-${ROOT}/bin/sqlai}"
LOG_FILE=""
_ts_prefix(){ date -Is; }
ts(){ _ts_prefix; }
log(){ echo "[$(ts)] $*"; }
ok(){ log "OK $*"; }
warn(){ log "WARN $*"; }
fail(){
local msg="$1"
local code="${2:-1}"
log "FAIL $msg"
exit "$code"
}
need_cmd(){
command -v "$1" >/dev/null 2>&1 || fail "missing command: $1" 10
}
init_log(){
local name="$1"
local log_dir="${ROOT}/logs"
mkdir -p "$log_dir"
LOG_FILE="${log_dir}/${name}-$(date -Iseconds).log"
export LOG_FILE
exec > >(tee -a "$LOG_FILE") 2>&1
}
load_env(){
if [[ ! -f "$ENV_FILE" && -f "${ROOT}/.env.example" ]]; then
cp -n "${ROOT}/.env.example" "$ENV_FILE" || true
fi
if [[ -f "$ENV_FILE" ]]; then
set -a
# shellcheck disable=SC1090
source "$ENV_FILE"
set +a
fi
: "${OLLAMA_URL:=http://127.0.0.1:11434}"
: "${EXPERT_MODEL:=jr-sql-expert}"
: "${BASE_MODEL:=}"
: "${EXTRA_MODELS:=}"
export OLLAMA_URL EXPERT_MODEL BASE_MODEL EXTRA_MODELS
# The Ollama CLI honors OLLAMA_HOST.
export OLLAMA_HOST="$OLLAMA_URL"
}
is_json(){
python -c 'import json,sys; json.load(sys.stdin)' >/dev/null 2>&1
}
wait_for_ollama_api(){
local url="$1"
local max_wait="${2:-120}"
local i
for ((i=1; i<=max_wait; i++)); do
if curl -fsS "${url}/api/tags" >/dev/null 2>&1; then
return 0
fi
sleep 1
done
return 1
}
fetch_tags(){
curl -fsS "${OLLAMA_URL}/api/tags"
}
model_in_tags(){
local model="$1"
local tags="$2"
TAGS_JSON="$tags" python - "$model" <<'PY'
import json
import os
import sys
m = sys.argv[1]
obj = json.loads(os.environ['TAGS_JSON'])
names = {it.get('name') for it in obj.get('models', []) if it.get('name')}
ok = (
m in names or
f"{m}:latest" in names or
(m.endswith(':latest') and m[:-7] in names)
)
sys.exit(0 if ok else 1)
PY
}
render_modelfile(){
local out_file="$1"
[[ -f "$MODFILE" ]] || fail "Modelfile not found at $MODFILE" 20
[[ -n "$BASE_MODEL" ]] || fail "BASE_MODEL is empty" 21
python - "$MODFILE" "$out_file" "$BASE_MODEL" <<'PY'
from pathlib import Path
import sys
src = Path(sys.argv[1]).read_text(encoding='utf-8')
out = src.replace('${BASE_MODEL}', sys.argv[3])
Path(sys.argv[2]).write_text(out, encoding='utf-8')
PY
}
check_for_legacy_docker_ollama(){
if ! command -v docker >/dev/null 2>&1; then
return 0
fi
if docker ps --format '{{.Names}}' | grep -qx 'ollama'; then
fail "legacy docker container 'ollama' is still running; run cleanup-docker-ollama.sh first" 30
fi
if docker ps -a --format '{{.Names}}' | grep -qx 'ollama'; then
warn "legacy docker container 'ollama' still exists in stopped state; cleanup recommended"
fi
if docker images --format '{{.Repository}}:{{.Tag}}' | grep -qx 'ollama/ollama:latest'; then
warn "legacy docker image ollama/ollama:latest still exists; cleanup recommended"
fi
}
warmup_sqlai(){
if [[ -x "$SQLAI_BIN" ]]; then
log "warmup via ${SQLAI_BIN}"
"$SQLAI_BIN" ask --text "Warmup: reply with exactly 'OK'." --no-metrics || warn "warmup failed"
else
warn "${SQLAI_BIN} not executable; skipping warmup"
fi
}
print_header(){
echo "================================================================================"
log "$1 ROOT=$ROOT"
}
print_footer(){
log "$1"
echo "================================================================================"
}