-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07_proxy_client.py
More file actions
70 lines (59 loc) · 2.43 KB
/
Copy path07_proxy_client.py
File metadata and controls
70 lines (59 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
"""07 — Talk to a live Switchyard proxy (or any OpenAI-compatible gateway).
If SWITCHYARD_URL is unset / unreachable, prints the curl recipe and exits 0
so CI stays green offline.
export OPENROUTER_API_KEY=...
switchyard-server --config configs/routes.llm_classifier.toml --port 4000
export SWITCHYARD_URL=http://127.0.0.1:4000
python examples/07_proxy_client.py
"""
from __future__ import annotations
import json
import os
import sys
import httpx
def main() -> int:
base = os.getenv("SWITCHYARD_URL", "http://127.0.0.1:4000").rstrip("/")
model = os.getenv("SWITCHYARD_MODEL", "switchyard")
prompt = os.getenv("PROMPT", "Say hello in one short sentence.")
health = f"{base}/health"
chat = f"{base}/v1/chat/completions"
print(f"Target: {base} model={model}")
try:
with httpx.Client(timeout=5.0) as client:
h = client.get(health)
print(f"GET /health → {h.status_code} {h.text[:120]}")
r = client.post(
chat,
headers={"Content-Type": "application/json"},
json={
"model": model,
"messages": [{"role": "user", "content": prompt}],
},
)
print(f"POST /v1/chat/completions → {r.status_code}")
if r.status_code >= 400:
print(r.text[:500])
return 1
data = r.json()
content = (
data.get("choices", [{}])[0]
.get("message", {})
.get("content", json.dumps(data)[:300])
)
print(f"Reply: {content}")
return 0
except httpx.HTTPError as e:
print(f"[offline] could not reach Switchyard ({e.__class__.__name__}: {e})")
print("\nStart the official proxy, then re-run:")
print(" uv tool install --python 3.12 'nemo-switchyard[cli,server]'")
print(" # or: cargo install --locked switchyard-server")
print(" export OPENROUTER_API_KEY=...")
print(" switchyard-server --config configs/routes.llm_classifier.toml --host 127.0.0.1 --port 4000")
print(" export SWITCHYARD_URL=http://127.0.0.1:4000")
print(" python examples/07_proxy_client.py")
print("\nOr launch a coding agent through Switchyard:")
print(" switchyard launch claude --model switchyard")
return 0
if __name__ == "__main__":
raise SystemExit(main())