Answer a greeting instead of abstaining for missing sources
Browse files at this commita8a0bdf6494aabc3d0b6750c4cc559004ee0b8d8Parent: a4b3874- fix
- answer a greeting instead of abstaining for missing sources
4 changed files +61 -0
- src/service/handler.py +17 -0
- src/service/multi.py +7 -0
- tests/unit/test_multi_corpus_service.py +11 -0
- tests/unit/test_service.py +26 -0
src/service/handler.pymodified+17 -0
| @@ -14,6 +14,7 @@ from ..observability.trace import RequestTrace, redact, write_trace | |||
| 14 | from ..providers.resilience import LlmUnavailable | 14 | from ..providers.resilience import LlmUnavailable |
| 15 | from ..guard.roles import RoleContext, Role, can_access | 15 | from ..guard.roles import RoleContext, Role, can_access |
| 16 | from ..guard.tenant import TenantError, normalize_school | 16 | from ..guard.tenant import TenantError, normalize_school |
| 17 | +from ..understand import Intent, analyze | ||
| 17 | 18 | ||
| 18 | 19 | ||
| 19 | def scope_pairs(scope) -> list: | 20 | def scope_pairs(scope) -> list: |
| @@ -126,6 +127,17 @@ SCOPE_TEXT = ("Seçtiğin bölüm bir seferde özetlenemeyecek kadar geniş. " | |||
| 126 | "Daha dar bir aralık seçer misin?") | 127 | "Daha dar bir aralık seçer misin?") |
| 127 | TENANT_TEXT = ("Bu istek bir okula bağlanamadı; kaynaklar açılamıyor. Lütfen " | 128 | TENANT_TEXT = ("Bu istek bir okula bağlanamadı; kaynaklar açılamıyor. Lütfen " |
| 128 | "tekrar dene ya da öğretmenine haber ver.") | 129 | "tekrar dene ya da öğretmenine haber ver.") |
| 130 | +GREETING_TEXT = "Merhaba! Dersle ilgili bir sorun varsa sorabilirsin." | ||
| 131 | + | ||
| 132 | + | ||
| 133 | +def sohbet_reply(query: str) -> dict | None: | ||
| 134 | + """A greeting is not a missing source. None means the caller should retrieve.""" | ||
| 135 | + analysis = analyze(query) | ||
| 136 | + if analysis.is_out_of_scope and analysis.intent == Intent.SOHBET: | ||
| 137 | + return {"text": GREETING_TEXT, "abstained": False, "reason": "", | ||
| 138 | + "citations": [], "used_source_ids": [], | ||
| 139 | + "invalid_citations": [], "cost_usd": 0.0, "cache_hit": False} | ||
| 140 | + return None | ||
| 129 | 141 | ||
| 130 | 142 | ||
| 131 | def _unknown_school(kind: str = "chat") -> dict: | 143 | def _unknown_school(kind: str = "chat") -> dict: |
| @@ -215,6 +227,11 @@ class RagService: | |||
| 215 | if not query: | 227 | if not query: |
| 216 | return {"text": "", "abstained": True, "reason": "empty_query", | 228 | return {"text": "", "abstained": True, "reason": "empty_query", |
| 217 | "citations": [], "used_source_ids": [], "cost_usd": 0.0, "cache_hit": False} | 229 | "citations": [], "used_source_ids": [], "cost_usd": 0.0, "cache_hit": False} |
| 230 | + # Smalltalk is not a missing source. Answer it here so it never reaches | ||
| 231 | + # retrieval or the insufficient_data gate. | ||
| 232 | + hit = sohbet_reply(query) | ||
| 233 | + if hit is not None: | ||
| 234 | + return hit | ||
| 218 | # KİRACILIK: okul istekten gelir ve burada doğrulanır. Geçersiz/ayrılmış | 235 | # KİRACILIK: okul istekten gelir ve burada doğrulanır. Geçersiz/ayrılmış |
| 219 | # bir değer 500'e değil TİPLİ bir redde düşmelidir (bkz. guard/tenant.py); | 236 | # bir değer 500'e değil TİPLİ bir redde düşmelidir (bkz. guard/tenant.py); |
| 220 | # doğrulanmış değer aşağıya (retrieval + cache anahtarı) geçer. | 237 | # doğrulanmış değer aşağıya (retrieval + cache anahtarı) geçer. |
src/service/multi.pymodified+7 -0
| @@ -415,6 +415,13 @@ class MultiCorpusService: | |||
| 415 | return {**ortak, "items": [], "span_ids": [], "pages": []} | 415 | return {**ortak, "items": [], "span_ids": [], "pages": []} |
| 416 | 416 | ||
| 417 | def chat(self, req: dict) -> dict: | 417 | def chat(self, req: dict) -> dict: |
| 418 | + # Same gate as RagService.chat. Routing first would turn "selam" into | ||
| 419 | + # no_corpus when the asker has no pair, which is the admin study bug. | ||
| 420 | + from .handler import sohbet_reply | ||
| 421 | + query = (req.get("query") or "").strip() | ||
| 422 | + hit = sohbet_reply(query) if query else None | ||
| 423 | + if hit is not None: | ||
| 424 | + return hit | ||
| 418 | svc, sebep = self._route(req) | 425 | svc, sebep = self._route(req) |
| 419 | return svc.chat(req) if svc is not None else self._refused(sebep, "chat") | 426 | return svc.chat(req) if svc is not None else self._refused(sebep, "chat") |
| 420 | 427 | ||
tests/unit/test_multi_corpus_service.pymodified+11 -0
| @@ -70,6 +70,17 @@ def _svc(specs=("10/biyoloji", "10/kimya", "10/fizik"), gecikme=0.0, | |||
| 70 | return s | 70 | return s |
| 71 | 71 | ||
| 72 | 72 | ||
| 73 | + | ||
| 74 | +class GreetingRouteTests(unittest.TestCase): | ||
| 75 | + def test_greeting_is_not_a_missing_corpus(self): | ||
| 76 | + s = _svc() | ||
| 77 | + out = s.chat({"query": "selam", "school": _OKUL, | ||
| 78 | + "role": {"role": "admin"}, "scope": []}) | ||
| 79 | + self.assertFalse(out["abstained"]) | ||
| 80 | + self.assertNotEqual(out["reason"], "no_corpus") | ||
| 81 | + self.assertNotIn("bulunamadı", out["text"]) | ||
| 82 | + self.assertEqual(s.registry.keys(), []) | ||
| 83 | + | ||
| 73 | def _bekle(s, sinif, ders, timeout=5.0): | 84 | def _bekle(s, sinif, ders, timeout=5.0): |
| 74 | """Arka plan korpus kurulumunun bitmesini bekler. | 85 | """Arka plan korpus kurulumunun bitmesini bekler. |
| 75 | 86 | ||
tests/unit/test_service.pymodified+26 -0
| @@ -77,6 +77,32 @@ class ChatTests(unittest.TestCase): | |||
| 77 | self.assertEqual(out["reason"], "empty_query") | 77 | self.assertEqual(out["reason"], "empty_query") |
| 78 | self.assertEqual(g.last_role, "UNSET") # generator ÇAĞRILMADI | 78 | self.assertEqual(g.last_role, "UNSET") # generator ÇAĞRILMADI |
| 79 | 79 | ||
| 80 | + def test_greeting_is_not_insufficient_data(self): | ||
| 81 | + g = _GenStub(_ANS) | ||
| 82 | + svc = RagService(g) | ||
| 83 | + for q in ("selam", "merhaba"): | ||
| 84 | + out = svc.chat({"query": q}) | ||
| 85 | + self.assertNotEqual(out["reason"], "insufficient_data", q) | ||
| 86 | + self.assertFalse(out["abstained"], q) | ||
| 87 | + self.assertEqual(out["reason"], "", q) | ||
| 88 | + self.assertEqual(out["citations"], [], q) | ||
| 89 | + self.assertEqual(out["cost_usd"], 0.0, q) | ||
| 90 | + self.assertFalse(out["cache_hit"], q) | ||
| 91 | + self.assertTrue(out["text"].strip(), q) | ||
| 92 | + self.assertNotIn("bulunamadı", out["text"]) | ||
| 93 | + self.assertEqual(g.last_role, "UNSET") | ||
| 94 | + | ||
| 95 | + def test_non_greeting_question_still_calls_generator(self): | ||
| 96 | + g = _GenStub(_ANS) | ||
| 97 | + out = RagService(g).chat({ | ||
| 98 | + "query": "Fotosentez nedir?", | ||
| 99 | + "role": {"role": "student", "sinif": "12", "ders_list": ["biyoloji"]}, | ||
| 100 | + }) | ||
| 101 | + self.assertNotEqual(g.last_role, "UNSET") | ||
| 102 | + self.assertEqual(g.last_role.role, Role.STUDENT) | ||
| 103 | + self.assertEqual(out["text"], "Cevap [1].") | ||
| 104 | + self.assertFalse(out["abstained"]) | ||
| 105 | + | ||
| 80 | def test_invalid_role_becomes_none(self): | 106 | def test_invalid_role_becomes_none(self): |
| 81 | g = _GenStub(_ANS) | 107 | g = _GenStub(_ANS) |
| 82 | RagService(g).chat({"query": "q", "role": {"role": "hacker"}}) | 108 | RagService(g).chat({"query": "q", "role": {"role": "hacker"}}) |