Skip to content

armgit / ODS

Public Imported from GitHub
0

ODS V3 Pre-Release: Public testing and refinement ahead of the official V3 launch. Turn your PC, Mac, or Linux box into a private AI server.

4.0 KB
Raw Blame
#!/bin/bash
set -euo pipefail
# repair-perplexica.sh — Re-seed Perplexica config via HTTP API
# Called by: ods repair perplexica
# Requires: Perplexica container running, python3 available

PERPLEXICA_URL="${1:-http://127.0.0.1:3004}"
LLM_MODEL="${2:-qwen3-30b-a3b}"
PERPLEXICA_MODEL="${PERPLEXICA_MODEL:-}"
PERPLEXICA_LLM_BASE_URL="${PERPLEXICA_LLM_BASE_URL:-${LLM_API_URL:-http://llama-server:8080}}"
PERPLEXICA_API_KEY="${PERPLEXICA_API_KEY:-${LITELLM_KEY:-${OPENAI_API_KEY:-no-key}}}"
_perplexica_switchboard_mode="$(printf '%s' "${ODS_MODEL_SWITCHBOARD:-enabled}" | tr '[:upper:]' '[:lower:]')"
if [[ "$_perplexica_switchboard_mode" == "enabled" ]]; then
    : "${PERPLEXICA_MODEL:=ods/current}"
    PERPLEXICA_LLM_BASE_URL="http://litellm:4000/v1"
    PERPLEXICA_API_KEY="${LITELLM_KEY:-${OPENAI_API_KEY:-no-key}}"
fi
case "$PERPLEXICA_LLM_BASE_URL" in
    */v1|*/api/v1) ;;
    *) PERPLEXICA_LLM_BASE_URL="${PERPLEXICA_LLM_BASE_URL%/}/v1" ;;
esac

if [[ -z "$PERPLEXICA_MODEL" ]]; then
    if [[ -n "${EXTERNAL_LLM_URL:-}" && -n "${EXTERNAL_LLM_MODEL:-}" ]]; then
        PERPLEXICA_MODEL="$EXTERNAL_LLM_MODEL"
    elif [[ -n "${GGUF_FILE:-}" ]]; then
        # llama-server serves the GGUF file name (--alias) on every runtime.
        PERPLEXICA_MODEL="$GGUF_FILE"
    else
        PERPLEXICA_MODEL="$LLM_MODEL"
    fi
fi

SCRIPT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
PYTHON_CMD="python3"
if [[ -f "$SCRIPT_DIR/../lib/python-cmd.sh" ]]; then
    . "$SCRIPT_DIR/../lib/python-cmd.sh"
    PYTHON_CMD="$(ods_detect_python_cmd)"
elif command -v python >/dev/null 2>&1; then
    PYTHON_CMD="python"
fi

# Wait for Perplexica to be ready (up to 60s)
for i in $(seq 1 12); do
    if curl -sf --max-time 5 "${PERPLEXICA_URL}/api/config" >/dev/null 2>&1; then
        break
    fi
    [[ $i -lt 12 ]] && sleep 5
done

# Seed config via API — export vars so Python reads from env (no shell interpolation)
export PERPLEXICA_URL PERPLEXICA_MODEL PERPLEXICA_LLM_BASE_URL PERPLEXICA_API_KEY

curl -sf --max-time 10 "${PERPLEXICA_URL}/api/config" | \
"$PYTHON_CMD" -c '
import sys, os, json, urllib.request

config = json.load(sys.stdin)["values"]
providers = config.get("modelProviders", [])
openai_index = next((i for i, p in enumerate(providers) if p["type"] == "openai"), None)
openai_prov = providers[openai_index] if openai_index is not None else None
transformers_prov = next((p for p in providers if p["type"] == "transformers"), None)

if not openai_prov:
    print("error: no openai provider found")
    sys.exit(1)

url = os.environ["PERPLEXICA_URL"] + "/api/config"
model = os.environ["PERPLEXICA_MODEL"]
base_url = os.environ["PERPLEXICA_LLM_BASE_URL"]
api_key = os.environ["PERPLEXICA_API_KEY"] or "no-key"

def post(key, value):
    data = json.dumps({"key": key, "value": value}).encode()
    req = urllib.request.Request(url, data=data, headers={"Content-Type": "application/json"})
    urllib.request.urlopen(req, timeout=10)

def post_setup_complete():
    setup_url = os.environ["PERPLEXICA_URL"] + "/api/config/setup-complete"
    req = urllib.request.Request(setup_url, data=b"{}", headers={"Content-Type": "application/json"})
    try:
        urllib.request.urlopen(req, timeout=10)
    except Exception:
        post("setupComplete", True)

# Set connection details
openai_prov["config"] = {
    **(openai_prov.get("config") or {}),
    "apiKey": api_key,
    "baseURL": base_url,
}
openai_prov["chatModels"] = [{"key": model, "name": model}]
# GET includes Vane-built-in models. Write only route fields for this provider.
post(f"modelProviders.{openai_index}.chatModels", openai_prov["chatModels"])
post(f"modelProviders.{openai_index}.config", openai_prov["config"])

# Set default providers and models
post("preferences", {
    "defaultChatProvider": openai_prov["id"],
    "defaultChatModel": model,
    "defaultEmbeddingProvider": transformers_prov["id"] if transformers_prov else openai_prov["id"],
    "defaultEmbeddingModel": "Xenova/all-MiniLM-L6-v2"
})

# Mark setup complete to bypass the wizard
post_setup_complete()
print("ok")
'