madDegen commited on
Commit
6ff51b1
·
verified ·
1 Parent(s): 7d89115

consolidate: HQ orchestrator FastAPI app

Browse files
Files changed (1) hide show
  1. hq/orchestrator.py +64 -0
hq/orchestrator.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Agent Q3 [HQ] — Orchestrator
3
+ FastAPI app exposing /v1/chat, /v1/reason, /v1/code, /v1/tandem, /health, /metrics
4
+ """
5
+ from fastapi import FastAPI, Request
6
+ from fastapi.responses import JSONResponse
7
+ from compute_router import ComputeRouter
8
+ from tandem_core import TandemCore
9
+ from metrics import track_request
10
+ import uvicorn, os, time
11
+
12
+ app = FastAPI(title="Agent Q3 [HQ]", version="1.0.0")
13
+ router = ComputeRouter()
14
+ tandem = TandemCore()
15
+
16
+ def classify(messages: list) -> str:
17
+ """Auto-classify: reasoner for planning/research/audit, coder for code/debug/file ops."""
18
+ content = " ".join(m.get("content", "") for m in messages).lower()
19
+ code_keywords = ["fix", "debug", "implement", "write code", "function", "class", "solidity", "script"]
20
+ return "coder" if any(k in content for k in code_keywords) else "reasoner"
21
+
22
+ @app.post("/v1/chat")
23
+ async def chat(req: Request):
24
+ body = await req.json()
25
+ messages = body.get("messages", [])
26
+ target = classify(messages)
27
+ start = time.time()
28
+ result = await router.route(messages, target=target)
29
+ track_request(target, time.time() - start)
30
+ return JSONResponse(result)
31
+
32
+ @app.post("/v1/reason")
33
+ async def reason(req: Request):
34
+ body = await req.json()
35
+ start = time.time()
36
+ result = await router.route(body.get("messages", []), target="reasoner")
37
+ track_request("reasoner", time.time() - start)
38
+ return JSONResponse(result)
39
+
40
+ @app.post("/v1/code")
41
+ async def code(req: Request):
42
+ body = await req.json()
43
+ start = time.time()
44
+ result = await router.route(body.get("messages", []), target="coder")
45
+ track_request("coder", time.time() - start)
46
+ return JSONResponse(result)
47
+
48
+ @app.post("/v1/tandem")
49
+ async def tandem_route(req: Request):
50
+ body = await req.json()
51
+ result = await tandem.run(body.get("messages", []))
52
+ return JSONResponse(result)
53
+
54
+ @app.get("/health")
55
+ async def health():
56
+ return {"status": "ok", "backends": router.health()}
57
+
58
+ @app.get("/metrics")
59
+ async def metrics():
60
+ from metrics import get_metrics
61
+ return JSONResponse(get_metrics())
62
+
63
+ if __name__ == "__main__":
64
+ uvicorn.run(app, host="0.0.0.0", port=int(os.getenv("PORT", 8000)))