Download tests/test_04_dms.py from recommender-system/llmproccrs: direct link, hf CLI and curl.
- Browser
- Download file 3.11 kB
-
https://huggingface.co/recommender-system/llmproccrs/resolve/main/tests/test_04_dms.py
- Command line
-
hf download hf://recommender-system/llmproccrs/tests/test_04_dms.py
-
curl -L -o test_04_dms.py https://huggingface.co/recommender-system/llmproccrs/resolve/main/tests/test_04_dms.py
3.11 kB
| import sys | |
| import os | |
| import json | |
| sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) | |
| from modules.dialog_manager.intent import IntentDetector | |
| from modules.dialog_manager.topic import TopicAnalyzer | |
| from modules.dialog_manager.generator import ResponseGenerator | |
| def test_dms(): | |
| print("\n" + "="*50) | |
| print("🤖 TESTING MODULE: DIALOG MANAGER (DMS)") | |
| print("="*50) | |
| # --- 1. TEST INTENT DETECTOR --- | |
| print("\n[Test 1] Intent Detector") | |
| intent_det = IntentDetector() | |
| cases = [ | |
| ("Halo selamat pagi", "basa_basi"), | |
| ("Saya mau cari laptop", "cari_produk"), | |
| ("Terima kasih ya", "basa_basi"), | |
| ("Ada printer epson?", "cari_produk") | |
| ] | |
| for text, expected in cases: | |
| res = intent_det.detect({"user_text": text}) | |
| status = "✅" if res == expected else f"❌ (Got: {res})" | |
| print(f"Input: '{text}' -> Intent: {res} {status}") | |
| # --- 2. TEST TOPIC ANALYZER --- | |
| print("\n[Test 2] Topic Analyzer") | |
| topic_an = TopicAnalyzer() | |
| # Case: Context Laptop, User tanya harga (SAME) | |
| state_same = { | |
| "collected_constraints": {"product": "laptop"}, | |
| "user_text": "yang harganya 5 juta ada?" | |
| } | |
| res_same = topic_an.analyze(state_same) | |
| print(f"Context: Laptop | Input: 'harganya 5jt?' -> {res_same['topic_status']} (Exp: same)") | |
| # Case: Context Laptop, User tanya Semen (CHANGED) | |
| state_change = { | |
| "collected_constraints": {"product": "laptop"}, | |
| "user_text": "kalau semen gresik 50kg berapa?" | |
| } | |
| res_change = topic_an.analyze(state_change) | |
| print(f"Context: Laptop | Input: 'semen gresik?' -> {res_change['topic_status']} (Exp: changed)") | |
| if res_change['collected_constraints'] == {}: | |
| print(" ✅ Memory Reset Successful") | |
| else: | |
| print(" ❌ Memory Reset Failed") | |
| # --- 3. TEST GENERATOR --- | |
| print("\n[Test 3] Response Generator") | |
| gen = ResponseGenerator() | |
| # Case: Navigation by Asking (> 10 items) | |
| print("--- Case A: Many Candidates (>10) ---") | |
| mock_state_ask = { | |
| "candidate_count": 20, | |
| "candidates": [{"title": "Laptop A", "supplier_detail": {"kota": "Jakarta"}} for _ in range(20)], | |
| "collected_constraints": {"product": "laptop"}, | |
| "histories": [] | |
| } | |
| res_ask = gen.generate(mock_state_ask) | |
| print(f"Bot: {res_ask['final_response'][:100]}...") # Print sebagian | |
| # Case: Recommendation (<= 10 items) | |
| print("\n--- Case B: Few Candidates (<=10) ---") | |
| mock_state_rec = { | |
| "candidate_count": 2, | |
| "candidates": [ | |
| {"title": "Laptop ASUS ROG", "price": 15000000, "supplier_detail": {"nama_toko": "Toko A", "kota": "Bandung"}}, | |
| {"title": "Laptop Lenovo Legion", "price": 14500000, "supplier_detail": {"nama_toko": "Toko B", "kota": "Jakarta"}} | |
| ], | |
| "collected_constraints": {"product": "laptop gaming"}, | |
| "histories": [] | |
| } | |
| res_rec = gen.generate(mock_state_rec) | |
| print(f"Bot: {res_rec['final_response']}") | |
| if __name__ == "__main__": | |
| test_dms() |