Spaces:
Sleeping
Sleeping
Deploy v1 — single-Docker FastAPI + Next.js + RAG + voice + faithfulness
Browse files- backend/orchestrator.py +25 -20
backend/orchestrator.py
CHANGED
|
@@ -78,19 +78,23 @@ class BrainPick:
|
|
| 78 |
|
| 79 |
|
| 80 |
def pick_brain(intent: str, language: str) -> BrainPick:
|
| 81 |
-
"""Route to the right brain per Doc decisions.md D-016.
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
- Indic queries -> Sarvam-M (
|
| 85 |
-
|
| 86 |
-
- Comparison/recommendation ->
|
| 87 |
-
-
|
|
|
|
|
|
|
|
|
|
| 88 |
"""
|
| 89 |
if language == "indic":
|
| 90 |
return BrainPick(SarvamLLM(), "indic-query")
|
| 91 |
if intent in ("comparison", "recommendation"):
|
| 92 |
return BrainPick(OpenRouterLLM(), f"complex-{intent}")
|
| 93 |
-
|
|
|
|
| 94 |
|
| 95 |
|
| 96 |
# ---------- main entrypoint ----------
|
|
@@ -204,29 +208,30 @@ async def handle_turn(
|
|
| 204 |
)
|
| 205 |
|
| 206 |
# 5a. CROSS-CHECK RETRY — if faithfulness blocked AND the failure isn't
|
| 207 |
-
# Gate 1 (no evidence at all), try a DIFFERENT-FAMILY brain.
|
| 208 |
-
#
|
| 209 |
-
#
|
| 210 |
-
#
|
| 211 |
-
#
|
| 212 |
blocked = False
|
| 213 |
-
retry_used = False
|
| 214 |
if not verdict.passed:
|
| 215 |
gate1_failure = any("gate1_retrieval" in r for r in verdict.reasons)
|
| 216 |
-
|
| 217 |
-
|
|
|
|
| 218 |
try:
|
| 219 |
-
|
| 220 |
-
|
|
|
|
|
|
|
| 221 |
second = await secondary.chat(messages=messages, temperature=0.1, max_tokens=1500)
|
| 222 |
second_reply = strip_think_tags(second.text)
|
| 223 |
second_verdict = await check_faithfulness(
|
| 224 |
reply=second_reply, chunks=chunks, user_text=user_text, run_llm_judge=True,
|
| 225 |
)
|
| 226 |
if second_verdict.passed:
|
| 227 |
-
# Use the second-brain reply
|
| 228 |
reply = second_reply
|
| 229 |
-
pick = BrainPick(secondary, f"crosscheck-rescued-{
|
| 230 |
verdict = second_verdict
|
| 231 |
else:
|
| 232 |
blocked = True
|
|
|
|
| 78 |
|
| 79 |
|
| 80 |
def pick_brain(intent: str, language: str) -> BrainPick:
|
| 81 |
+
"""Route to the right brain per Doc decisions.md D-016 (revised 2026-05-13).
|
| 82 |
+
|
| 83 |
+
Rebalanced from eval signal:
|
| 84 |
+
- Indic queries -> Sarvam-M (Indic + cultural context + BFSI vocab — its
|
| 85 |
+
genuine strength; the Sarvam-first narrative belongs here)
|
| 86 |
+
- Comparison/recommendation -> DeepSeek-V3 (SOTA open-source reasoning)
|
| 87 |
+
- Simple English QA -> DeepSeek-V3 (eval showed Sarvam-M at 37.5%
|
| 88 |
+
factual on this slice vs Llama at 100%; DeepSeek is stronger still
|
| 89 |
+
with citation-discipline as bonus)
|
| 90 |
+
- Llama-3.3-70B reserved as grader AND cross-check rescue brain
|
| 91 |
"""
|
| 92 |
if language == "indic":
|
| 93 |
return BrainPick(SarvamLLM(), "indic-query")
|
| 94 |
if intent in ("comparison", "recommendation"):
|
| 95 |
return BrainPick(OpenRouterLLM(), f"complex-{intent}")
|
| 96 |
+
# English simple QA → DeepSeek-V3 (was Sarvam-M; rebalanced from eval data)
|
| 97 |
+
return BrainPick(OpenRouterLLM(), "simple-qa")
|
| 98 |
|
| 99 |
|
| 100 |
# ---------- main entrypoint ----------
|
|
|
|
| 208 |
)
|
| 209 |
|
| 210 |
# 5a. CROSS-CHECK RETRY — if faithfulness blocked AND the failure isn't
|
| 211 |
+
# Gate 1 (no evidence at all), try a DIFFERENT-FAMILY brain. Picks the
|
| 212 |
+
# opposite family of whatever the primary was:
|
| 213 |
+
# primary Sarvam-M → cross-check DeepSeek-V3
|
| 214 |
+
# primary DeepSeek-V3 → cross-check Sarvam-M
|
| 215 |
+
# Capped at ONE retry — no loops.
|
| 216 |
blocked = False
|
|
|
|
| 217 |
if not verdict.passed:
|
| 218 |
gate1_failure = any("gate1_retrieval" in r for r in verdict.reasons)
|
| 219 |
+
if not gate1_failure:
|
| 220 |
+
# Pick the OTHER family for the rescue pass
|
| 221 |
+
primary_name = pick.provider.name
|
| 222 |
try:
|
| 223 |
+
if primary_name == "sarvam-m":
|
| 224 |
+
secondary = OpenRouterLLM()
|
| 225 |
+
else:
|
| 226 |
+
secondary = SarvamLLM()
|
| 227 |
second = await secondary.chat(messages=messages, temperature=0.1, max_tokens=1500)
|
| 228 |
second_reply = strip_think_tags(second.text)
|
| 229 |
second_verdict = await check_faithfulness(
|
| 230 |
reply=second_reply, chunks=chunks, user_text=user_text, run_llm_judge=True,
|
| 231 |
)
|
| 232 |
if second_verdict.passed:
|
|
|
|
| 233 |
reply = second_reply
|
| 234 |
+
pick = BrainPick(secondary, f"crosscheck-rescued-{primary_name}")
|
| 235 |
verdict = second_verdict
|
| 236 |
else:
|
| 237 |
blocked = True
|