diff --git a/src/service/handler.py b/src/service/handler.py index ab68f58..22f9ac0 100644 --- a/src/service/handler.py +++ b/src/service/handler.py @@ -14,6 +14,7 @@ from ..observability.trace import RequestTrace, redact, write_trace from ..providers.resilience import LlmUnavailable from ..guard.roles import RoleContext, Role, can_access from ..guard.tenant import TenantError, normalize_school +from ..understand import Intent, analyze def scope_pairs(scope) -> list: @@ -126,6 +127,17 @@ SCOPE_TEXT = ("Seçtiğin bölüm bir seferde özetlenemeyecek kadar geniş. " "Daha dar bir aralık seçer misin?") TENANT_TEXT = ("Bu istek bir okula bağlanamadı; kaynaklar açılamıyor. Lütfen " "tekrar dene ya da öğretmenine haber ver.") +GREETING_TEXT = "Merhaba! Dersle ilgili bir sorun varsa sorabilirsin." + + +def sohbet_reply(query: str) -> dict | None: + """A greeting is not a missing source. None means the caller should retrieve.""" + analysis = analyze(query) + if analysis.is_out_of_scope and analysis.intent == Intent.SOHBET: + return {"text": GREETING_TEXT, "abstained": False, "reason": "", + "citations": [], "used_source_ids": [], + "invalid_citations": [], "cost_usd": 0.0, "cache_hit": False} + return None def _unknown_school(kind: str = "chat") -> dict: @@ -215,6 +227,11 @@ class RagService: if not query: return {"text": "", "abstained": True, "reason": "empty_query", "citations": [], "used_source_ids": [], "cost_usd": 0.0, "cache_hit": False} + # Smalltalk is not a missing source. Answer it here so it never reaches + # retrieval or the insufficient_data gate. + hit = sohbet_reply(query) + if hit is not None: + return hit # KİRACILIK: okul istekten gelir ve burada doğrulanır. Geçersiz/ayrılmış # bir değer 500'e değil TİPLİ bir redde düşmelidir (bkz. guard/tenant.py); # doğrulanmış değer aşağıya (retrieval + cache anahtarı) geçer. diff --git a/src/service/multi.py b/src/service/multi.py index aa560d9..af5b860 100644 --- a/src/service/multi.py +++ b/src/service/multi.py @@ -415,6 +415,13 @@ class MultiCorpusService: return {**ortak, "items": [], "span_ids": [], "pages": []} def chat(self, req: dict) -> dict: + # Same gate as RagService.chat. Routing first would turn "selam" into + # no_corpus when the asker has no pair, which is the admin study bug. + from .handler import sohbet_reply + query = (req.get("query") or "").strip() + hit = sohbet_reply(query) if query else None + if hit is not None: + return hit svc, sebep = self._route(req) return svc.chat(req) if svc is not None else self._refused(sebep, "chat") diff --git a/tests/unit/test_multi_corpus_service.py b/tests/unit/test_multi_corpus_service.py index d223737..61a0645 100644 --- a/tests/unit/test_multi_corpus_service.py +++ b/tests/unit/test_multi_corpus_service.py @@ -70,6 +70,17 @@ def _svc(specs=("10/biyoloji", "10/kimya", "10/fizik"), gecikme=0.0, return s + +class GreetingRouteTests(unittest.TestCase): + def test_greeting_is_not_a_missing_corpus(self): + s = _svc() + out = s.chat({"query": "selam", "school": _OKUL, + "role": {"role": "admin"}, "scope": []}) + self.assertFalse(out["abstained"]) + self.assertNotEqual(out["reason"], "no_corpus") + self.assertNotIn("bulunamadı", out["text"]) + self.assertEqual(s.registry.keys(), []) + def _bekle(s, sinif, ders, timeout=5.0): """Arka plan korpus kurulumunun bitmesini bekler. diff --git a/tests/unit/test_service.py b/tests/unit/test_service.py index fe71d81..e4ad911 100644 --- a/tests/unit/test_service.py +++ b/tests/unit/test_service.py @@ -77,6 +77,32 @@ class ChatTests(unittest.TestCase): self.assertEqual(out["reason"], "empty_query") self.assertEqual(g.last_role, "UNSET") # generator ÇAĞRILMADI + def test_greeting_is_not_insufficient_data(self): + g = _GenStub(_ANS) + svc = RagService(g) + for q in ("selam", "merhaba"): + out = svc.chat({"query": q}) + self.assertNotEqual(out["reason"], "insufficient_data", q) + self.assertFalse(out["abstained"], q) + self.assertEqual(out["reason"], "", q) + self.assertEqual(out["citations"], [], q) + self.assertEqual(out["cost_usd"], 0.0, q) + self.assertFalse(out["cache_hit"], q) + self.assertTrue(out["text"].strip(), q) + self.assertNotIn("bulunamadı", out["text"]) + self.assertEqual(g.last_role, "UNSET") + + def test_non_greeting_question_still_calls_generator(self): + g = _GenStub(_ANS) + out = RagService(g).chat({ + "query": "Fotosentez nedir?", + "role": {"role": "student", "sinif": "12", "ders_list": ["biyoloji"]}, + }) + self.assertNotEqual(g.last_role, "UNSET") + self.assertEqual(g.last_role.role, Role.STUDENT) + self.assertEqual(out["text"], "Cevap [1].") + self.assertFalse(out["abstained"]) + def test_invalid_role_becomes_none(self): g = _GenStub(_ANS) RagService(g).chat({"query": "q", "role": {"role": "hacker"}})