#!/bin/bash
# M3 v17：从服务器上运行中的运幄后端配置提取可用 LLM 上游并实测
echo "=== 1. 运行中后端的 LLM 配置 ==="
grep -B2 -A4 "APIBase" /root/knowledge/etc/config.yaml 2>/dev/null | head -40

echo ""
echo "=== 2. 实测每个上游 ==="
# 提取 APIBase+APIKey 组合并测试
python3 - <<'EOF'
import re, subprocess
conf = open('/root/knowledge/etc/config.yaml').read()
combos = re.findall(r'APIBase:\s*(\S+)\s*\n\s*APIKey:\s*(\S+)', conf)
seen = set()
for base, key in combos:
    if (base, key) in seen: continue
    seen.add((base, key))
    r = subprocess.run(['curl','-sS','--max-time','15','-H',f'Authorization: Bearer {key}',
        '-H','Content-Type: application/json',
        '-d','{"model":"glm-4-flash","messages":[{"role":"user","content":"reply ok"}],"max_tokens":10}',
        f'{base}/chat/completions'], capture_output=True, text=True)
    out = (r.stdout or r.stderr)[:150].replace('\n',' ')
    print(f'{base} => {out}')
EOF
echo DONE
