-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai_router.py
More file actions
150 lines (132 loc) · 4.91 KB
/
ai_router.py
File metadata and controls
150 lines (132 loc) · 4.91 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
from __future__ import annotations
from typing import Any
import httpx
from .config import OLLARMA_BASE_URL, OLLARMA_TIMEOUT_SECONDS
GENERIC_HINTS = (
"i don't have access",
"i do not have access",
"i can't access",
"i cannot access",
"[blocked by guardrail",
)
def _extract_response_text(payload: dict[str, Any]) -> str:
for key in ("final_response", "response", "message", "content"):
value = payload.get(key)
if isinstance(value, str) and value.strip():
return value.strip()
return ""
def _generic_response(text: str) -> bool:
lowered = text.lower()
return any(hint in lowered for hint in GENERIC_HINTS)
def _extract_reason_code(payload: dict[str, Any], status_code: int) -> str:
reason_code = payload.get("reason_code")
if isinstance(reason_code, str) and reason_code.strip():
return reason_code.strip()
error = payload.get("error")
if isinstance(error, str) and ":" in error:
candidate = error.split(":", 1)[0].strip()
if candidate and candidate.upper() == candidate:
return candidate
return f"OLLARMA_HTTP_{status_code}"
def query_local_ai(message: str, *, project: str | None = None) -> dict[str, Any]:
endpoint = "/route" if project else "/chat"
payload: dict[str, Any] = {"project": project, "prompt": message} if project else {"message": message}
attempted_lane = "ollarma.route" if project else "ollarma.chat"
try:
with httpx.Client(base_url=OLLARMA_BASE_URL, timeout=OLLARMA_TIMEOUT_SECONDS) as client:
response = client.post(endpoint, json=payload)
body = response.json()
except httpx.TimeoutException:
return {
"attempted_lane": attempted_lane,
"lane_used": None,
"fallback_recommended": True,
"reason_code": "OLLARMA_TIMEOUT",
"response_text": "",
"raw": None,
}
except httpx.HTTPError as exc:
return {
"attempted_lane": attempted_lane,
"lane_used": None,
"fallback_recommended": True,
"reason_code": "OLLARMA_UNAVAILABLE",
"response_text": "",
"raw": {"error": str(exc)},
}
text = _extract_response_text(body)
if response.status_code >= 400:
return {
"attempted_lane": attempted_lane,
"lane_used": body.get("lane") or attempted_lane,
"fallback_recommended": True,
"reason_code": _extract_reason_code(body, response.status_code),
"response_text": text,
"raw": body,
}
route_reason = body.get("reason_code") if project else None
route_blocked = bool(route_reason) or body.get("kb_status") == "blocked" or body.get("lane") == "frontier_or_human"
if route_blocked:
return {
"attempted_lane": attempted_lane,
"lane_used": body.get("lane") or attempted_lane,
"fallback_recommended": True,
"reason_code": route_reason or "OLLARMA_ROUTE_BLOCKED",
"response_text": text,
"raw": body,
}
if _generic_response(text):
return {
"attempted_lane": attempted_lane,
"lane_used": attempted_lane,
"fallback_recommended": True,
"reason_code": "LOW_CONFIDENCE_GENERIC",
"response_text": text,
"raw": body,
}
return {
"attempted_lane": attempted_lane,
"lane_used": body.get("lane") or attempted_lane,
"fallback_recommended": False,
"reason_code": None,
"response_text": text,
"raw": body,
}
def submit_deterministic_workflow(project: str, manifest_ref: str, step_id: str) -> dict[str, Any]:
try:
with httpx.Client(base_url=OLLARMA_BASE_URL, timeout=OLLARMA_TIMEOUT_SECONDS) as client:
response = client.post(
"/workflow",
json={
"project": project,
"manifest_ref": manifest_ref,
"step_id": step_id,
},
)
body = response.json()
except httpx.TimeoutException:
return {
"attempted_lane": "ollarma.workflow",
"lane_used": None,
"accepted": False,
"status_code": 504,
"reason_code": "OLLARMA_TIMEOUT",
"raw": None,
}
except httpx.HTTPError as exc:
return {
"attempted_lane": "ollarma.workflow",
"lane_used": None,
"accepted": False,
"status_code": 503,
"reason_code": "OLLARMA_UNAVAILABLE",
"raw": {"error": str(exc)},
}
return {
"attempted_lane": "ollarma.workflow",
"lane_used": "ollarma.workflow",
"accepted": response.status_code in {200, 202},
"status_code": response.status_code,
"reason_code": body.get("reason_code"),
"raw": body,
}