-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
252 lines (208 loc) · 7.61 KB
/
Copy pathapp.py
File metadata and controls
252 lines (208 loc) · 7.61 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/env python3
"""Switchyard Lab — interactive multi-model routing playground.
Offline-first: routes scenarios with educational algorithms and shows
tokenomics. Optional live chat when OPENROUTER_API_KEY / NVIDIA_API_KEY set.
"""
from __future__ import annotations
import json
import os
import sys
from pathlib import Path
from typing import Any
import httpx
import uvicorn
from dotenv import load_dotenv
from fastapi import FastAPI, HTTPException
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel, Field
ROOT = Path(__file__).resolve().parent
sys.path.insert(0, str(ROOT))
from switchyard_ops.metrics import summarize_run # noqa: E402
from switchyard_ops.models import Step, StepType # noqa: E402
from switchyard_ops.routers import ( # noqa: E402
EscalationRouter,
LlmClassifierRouter,
RandomRouter,
SimpleSwitchyard,
StageRouter,
)
from switchyard_ops.loaders import load_scenarios, scenario_steps # noqa: E402
load_dotenv(ROOT / ".env")
HOST = os.getenv("HOST", "127.0.0.1")
PORT = int(os.getenv("PORT", "7871"))
OFFLINE = os.getenv("OFFLINE", "").strip().lower() in {"1", "true", "yes"}
STATIC_DIR = ROOT / "static"
HEADER_SRC = ROOT / "assets" / "header.jpg"
HEADER_DST = STATIC_DIR / "header.jpg"
app = FastAPI(title="Switchyard Lab", version="0.1.0")
def _ensure_header() -> None:
STATIC_DIR.mkdir(parents=True, exist_ok=True)
if HEADER_SRC.exists() and (
not HEADER_DST.exists() or HEADER_SRC.stat().st_mtime > HEADER_DST.stat().st_mtime
):
HEADER_DST.write_bytes(HEADER_SRC.read_bytes())
_ensure_header()
app.mount("/static", StaticFiles(directory=str(STATIC_DIR)), name="static")
STRATEGIES = {
"policy": "Plan/execute policy (SimpleSwitchyard)",
"stage": "Stage router (tool/error signals)",
"classifier": "LLM classifier (content score)",
"escalation": "Escalation (weak-first + judge)",
"random": "Random 30/70 strong/weak",
}
def _make_router(name: str):
if name == "policy":
return SimpleSwitchyard()
if name == "stage":
return StageRouter()
if name == "classifier":
return LlmClassifierRouter(base_threshold=0.5)
if name == "escalation":
return EscalationRouter()
if name == "random":
return RandomRouter(weights={"strong": 0.3, "weak": 0.7})
raise HTTPException(400, f"unknown strategy {name}")
@app.get("/")
def index() -> FileResponse:
return FileResponse(STATIC_DIR / "index.html")
@app.get("/api/meta")
def meta() -> dict[str, Any]:
return {
"name": "Switchyard Lab",
"version": (ROOT / "VERSION").read_text(encoding="utf-8").strip(),
"strategies": STRATEGIES,
"offline": OFFLINE or not _api_key(),
"upstream": "https://github.com/NVIDIA-NeMo/Switchyard",
}
@app.get("/api/scenarios")
def scenarios() -> list[dict[str, Any]]:
return [
{"id": s["id"], "title": s["title"], "description": s["description"], "steps": len(s["steps"])}
for s in load_scenarios()
]
class SimulateBody(BaseModel):
scenario_id: str = "refund-investigation"
strategy: str = "stage"
@app.post("/api/simulate")
def simulate(body: SimulateBody) -> dict[str, Any]:
scenarios_list = load_scenarios()
sc = next((s for s in scenarios_list if s["id"] == body.scenario_id), None)
if not sc:
raise HTTPException(404, "scenario not found")
steps = scenario_steps(sc)
router = _make_router(body.strategy)
decisions = [router.route(s) for s in steps]
report = summarize_run(steps, decisions)
return {
"scenario": {"id": sc["id"], "title": sc["title"], "description": sc["description"]},
"strategy": body.strategy,
"steps": [
{
"type": s.type.value,
"content": s.content,
"signals": s.signals,
"risk": s.risk,
"failures": s.failures,
"tokens_in": s.tokens_in,
"tokens_out": s.tokens_out,
"target": d.target,
"model_id": d.model_id,
"reason": d.reason,
"score": d.score,
}
for s, d in zip(steps, decisions)
],
"report": report,
}
class CompareBody(BaseModel):
scenario_id: str = "coding-pr"
@app.post("/api/compare")
def compare(body: CompareBody) -> dict[str, Any]:
scenarios_list = load_scenarios()
sc = next((s for s in scenarios_list if s["id"] == body.scenario_id), None)
if not sc:
raise HTTPException(404, "scenario not found")
steps = scenario_steps(sc)
rows = []
for name in STRATEGIES:
router = _make_router(name)
decisions = [router.route(s) for s in steps]
r = summarize_run(steps, decisions)
rows.append(
{
"strategy": name,
"label": STRATEGIES[name],
"routed_cost_usd": r["routed_cost_usd"],
"frontier_only_usd": r["frontier_only_usd"],
"savings_pct": r["savings_pct"],
"tier_counts": r["tier_counts"],
}
)
return {"scenario_id": body.scenario_id, "rows": rows}
def _api_key() -> str:
return (
os.getenv("OPENROUTER_API_KEY", "").strip()
or os.getenv("NVIDIA_API_KEY", "").strip()
or os.getenv("OPENAI_API_KEY", "").strip()
)
def _base_url() -> str:
raw = (
os.getenv("OPENAI_BASE_URL", "").strip()
or os.getenv("NVIDIA_BASE_URL", "").strip()
or "https://openrouter.ai/api/v1"
)
base = raw.rstrip("/")
if base and not base.endswith("/v1"):
base = f"{base}/v1"
return base
class ChatBody(BaseModel):
message: str = Field(..., min_length=1)
model: str = "openai/gpt-4o-mini"
@app.post("/api/chat")
def chat(body: ChatBody) -> dict[str, Any]:
if OFFLINE or not _api_key():
# Educational offline reply
router = LlmClassifierRouter()
step = Step(StepType.EXECUTE, body.message, complexity="high" if len(body.message) > 80 else "low")
d = router.route(step)
return {
"offline": True,
"model": d.model_id,
"target": d.target,
"reason": d.reason,
"content": (
f"[offline simulation] Classifier would route this prompt to **{d.target}** "
f"({d.model_id}). Reason: {d.reason}. Set OPENROUTER_API_KEY for a live completion."
),
}
try:
with httpx.Client(timeout=60.0) as client:
r = client.post(
f"{_base_url()}/chat/completions",
headers={
"Authorization": f"Bearer {_api_key()}",
"Content-Type": "application/json",
},
json={
"model": body.model,
"messages": [
{
"role": "system",
"content": "You are a concise assistant explaining multi-model LLM routing.",
},
{"role": "user", "content": body.message},
],
},
)
r.raise_for_status()
data = r.json()
content = data["choices"][0]["message"]["content"]
return {"offline": False, "model": body.model, "content": content}
except Exception as e:
raise HTTPException(502, f"upstream error: {e}") from e
def main() -> None:
print(f"Switchyard Lab → http://{HOST}:{PORT}")
uvicorn.run(app, host=HOST, port=PORT, log_level="info")
if __name__ == "__main__":
main()