75 lines
2.1 KiB
Bash
Executable File
75 lines
2.1 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# list-models.sh — Query the LLM proxy /v1/models endpoint
|
|
# Usage:
|
|
# list-models.sh # List all model IDs (sorted)
|
|
# list-models.sh --providers # List unique provider names
|
|
# list-models.sh --json # Raw JSON response
|
|
set -euo pipefail
|
|
|
|
# Resolve proxy URL and API key from environment or defaults
|
|
PROXY_URL="${LLM_PROXY_URL:-https://llm-proxy.ext.ben.io/v1}"
|
|
PROXY_KEY="${PROXY_API_KEY:-${LLM_PROXY_API_KEY:-}}"
|
|
|
|
if [[ -z "$PROXY_KEY" ]]; then
|
|
# Try to read from openclaw config
|
|
for cfg_path in \
|
|
"${OPENCLAW_STATE_DIR:-$HOME/.openclaw/state}/openclaw.json" \
|
|
"${OPENCLAW_CONFIG_DIR:-$HOME/.openclaw/config}/openclaw.json"; do
|
|
if [[ -f "$cfg_path" ]]; then
|
|
# Extract apiKey from llm-proxy provider config (handles JSON5 comments)
|
|
key=$(grep -A5 '"llm-proxy"' "$cfg_path" | grep '"apiKey"' | head -1 | sed 's/.*"apiKey"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/' || true)
|
|
if [[ -n "$key" && "$key" != *'${'* ]]; then
|
|
PROXY_KEY="$key"
|
|
break
|
|
fi
|
|
fi
|
|
done
|
|
fi
|
|
|
|
if [[ -z "$PROXY_KEY" ]]; then
|
|
echo "ERROR: No API key found. Set PROXY_API_KEY or LLM_PROXY_API_KEY environment variable." >&2
|
|
exit 1
|
|
fi
|
|
|
|
# Strip trailing /v1 from PROXY_URL if present, then always append /v1/models
|
|
# This prevents double /v1/v1/ when LLM_PROXY_URL already includes /v1
|
|
PROXY_BASE="${PROXY_URL%/v1}"
|
|
PROXY_BASE="${PROXY_BASE%/}"
|
|
|
|
response=$(curl -s -f -H "Authorization: Bearer $PROXY_KEY" "${PROXY_BASE}/v1/models" 2>&1) || {
|
|
echo "ERROR: Failed to query ${PROXY_BASE}/v1/models" >&2
|
|
echo "$response" >&2
|
|
exit 1
|
|
}
|
|
|
|
case "${1:-}" in
|
|
--providers)
|
|
echo "$response" | python3 -c "
|
|
import sys, json
|
|
data = json.load(sys.stdin)
|
|
providers = sorted(set(m['id'].split('/')[0] for m in data.get('data', [])))
|
|
for p in providers:
|
|
print(p)
|
|
"
|
|
;;
|
|
--json)
|
|
echo "$response"
|
|
;;
|
|
--count)
|
|
echo "$response" | python3 -c "
|
|
import sys, json
|
|
data = json.load(sys.stdin)
|
|
print(len(data.get('data', [])))
|
|
"
|
|
;;
|
|
*)
|
|
echo "$response" | python3 -c "
|
|
import sys, json
|
|
data = json.load(sys.stdin)
|
|
models = sorted(m['id'] for m in data.get('data', []))
|
|
for m in models:
|
|
print(m)
|
|
"
|
|
;;
|
|
esac
|