forge
Explore
Log in

Don't you dare go Hollow

hezarfen_rule_based_chatbot

Clone
★ 0

0 watching0 forksPython1.2 MiBa039b60Ahmet Kaan Gümüş2 months ago64 commits

test_prepared_questions.py67 lines, 2.6 KiBHistoryRawDownload
1"""Aşama 19 e2e: hazırladığımız sorularla chatbotu deneme (öz-tutarlılık).
2
3example_questions'ı uçtan uca motordan geçirir. Bot kendi geliştirme sorularını
4yüksek doğrulukla çözmeli. Güvenlik katmanının kısa devresi de kabul edilir
5(ör. gizlilik soruları güvenlikçe/ niyetçe doğru ele alınır).
6"""
7
8import unittest
9
10from src.catalog import INTENTS
11from src.engine import Engine
12
13
14def _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 # güvenlik katmanı bilinçli müdahale etti
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
39class 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 # Temiz how-to örnekleri güvenlik tarafından yanlışlıkla BLOCK edilmemeli.
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
59if __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}")