| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | import unittest |
| 9 | |
| 10 | from src.catalog import INTENTS |
| 11 | from src.engine import Engine |
| 12 | |
| 13 | |
| 14 | def _run_examples() -> dict: |
| 15 | engine = Engine() |
| 16 | total = correct = safety_short = 0 |
| 17 | wrong: list[tuple[str, str, str]] = [] |
| 18 | for item in INTENTS: |
| 19 | role = item["min_role"] |
| 20 | authenticated = role != "ziyaretci" |
| 21 | for q in item["example_questions"]: |
| 22 | total += 1 |
| 23 | resp = engine.handle({"query": q, "session": {"role": role, "authenticated": authenticated}}) |
| 24 | if resp["intent"] == item["intent"]: |
| 25 | correct += 1 |
| 26 | elif resp["response_id"].startswith("safety_"): |
| 27 | safety_short += 1 |
| 28 | else: |
| 29 | wrong.append((q, item["intent"], resp["intent"] or resp["response_id"])) |
| 30 | return { |
| 31 | "total": total, |
| 32 | "correct": correct, |
| 33 | "safety_short": safety_short, |
| 34 | "wrong": wrong, |
| 35 | "self_consistency": (correct + safety_short) / total if total else 0.0, |
| 36 | } |
| 37 | |
| 38 | |
| 39 | class PreparedQuestionsTests(unittest.TestCase): |
| 40 | @classmethod |
| 41 | def setUpClass(cls) -> None: |
| 42 | cls.result = _run_examples() |
| 43 | |
| 44 | def test_self_consistency_high(self) -> None: |
| 45 | self.assertGreaterEqual( |
| 46 | self.result["self_consistency"], 0.93, |
| 47 | f"öz-tutarlılık={self.result['self_consistency']:.3f}, yanlış={self.result['wrong'][:10]}", |
| 48 | ) |
| 49 | |
| 50 | def test_no_clean_example_is_blocked_wrongly(self) -> None: |
| 51 | |
| 52 | engine = Engine() |
| 53 | for q in ["Yeni not nasıl yazılır?", "Bir öğrencinin notunu nasıl görürüm?", |
| 54 | "Karnemi nereden görürüm?", "sınav nasıl oluşturulur"]: |
| 55 | resp = engine.handle({"query": q, "session": {"role": "ogretmen", "authenticated": True}}) |
| 56 | self.assertNotEqual(resp["safety"]["decision"], "BLOCK", q) |
| 57 | |
| 58 | |
| 59 | if __name__ == "__main__": |
| 60 | r = _run_examples() |
| 61 | print(f"example_questions: {r['total']}") |
| 62 | print(f" intent doğru : {r['correct']}") |
| 63 | print(f" güvenlik kısa devre : {r['safety_short']}") |
| 64 | print(f" yanlış : {len(r['wrong'])}") |
| 65 | print(f" ÖZ-TUTARLILIK : {r['self_consistency']:.1%}") |
| 66 | for q, exp, got in r["wrong"]: |
| 67 | print(f" x {q!r}: beklenen={exp} bulunan={got}") |