Upload backend/scripts/test_rag_pipeline.py with huggingface_hub
Browse files
backend/scripts/test_rag_pipeline.py
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
"""
|
| 3 |
+
Script để test RAG pipeline với data mới.
|
| 4 |
+
"""
|
| 5 |
+
import os
|
| 6 |
+
import sys
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
|
| 9 |
+
ROOT_DIR = Path(__file__).resolve().parents[2]
|
| 10 |
+
BACKEND_DIR = ROOT_DIR / "backend"
|
| 11 |
+
HUE_PORTAL_DIR = BACKEND_DIR / "hue_portal"
|
| 12 |
+
|
| 13 |
+
for path in (HUE_PORTAL_DIR, BACKEND_DIR, ROOT_DIR):
|
| 14 |
+
if str(path) not in sys.path:
|
| 15 |
+
sys.path.insert(0, str(path))
|
| 16 |
+
|
| 17 |
+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hue_portal.hue_portal.settings")
|
| 18 |
+
|
| 19 |
+
import django
|
| 20 |
+
django.setup()
|
| 21 |
+
|
| 22 |
+
from hue_portal.core.rag import rag_pipeline
|
| 23 |
+
from hue_portal.chatbot.chatbot import Chatbot
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def test_rag_procedure():
|
| 27 |
+
"""Test RAG với queries về procedure."""
|
| 28 |
+
print("="*60)
|
| 29 |
+
print("Test RAG Pipeline - Procedure")
|
| 30 |
+
print("="*60)
|
| 31 |
+
|
| 32 |
+
test_queries = [
|
| 33 |
+
"Làm thủ tục cư trú cần gì?",
|
| 34 |
+
"Thủ tục đăng ký thường trú",
|
| 35 |
+
"Làm thủ tục PCCC như thế nào?",
|
| 36 |
+
"Thủ tục ANTT cần giấy tờ gì?",
|
| 37 |
+
]
|
| 38 |
+
|
| 39 |
+
for query in test_queries:
|
| 40 |
+
print(f"\n📝 Query: {query}")
|
| 41 |
+
try:
|
| 42 |
+
result = rag_pipeline(query, 'search_procedure', top_k=3)
|
| 43 |
+
print(f" ✅ Results: {result['count']}")
|
| 44 |
+
print(f" ✅ Confidence: {result['confidence']:.4f}")
|
| 45 |
+
if result['count'] > 0:
|
| 46 |
+
print(f" ✅ Answer preview: {result['answer'][:150]}...")
|
| 47 |
+
print(f" ✅ Documents:")
|
| 48 |
+
for i, doc in enumerate(result['documents'][:3], 1):
|
| 49 |
+
print(f" {i}. {doc.title} - {doc.domain}")
|
| 50 |
+
else:
|
| 51 |
+
print(" ⚠️ No results found")
|
| 52 |
+
except Exception as e:
|
| 53 |
+
print(f" ❌ Error: {e}")
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def test_rag_advisory():
|
| 57 |
+
"""Test RAG với queries về advisory."""
|
| 58 |
+
print("\n" + "="*60)
|
| 59 |
+
print("Test RAG Pipeline - Advisory")
|
| 60 |
+
print("="*60)
|
| 61 |
+
|
| 62 |
+
test_queries = [
|
| 63 |
+
"Cảnh báo lừa đảo giả danh công an",
|
| 64 |
+
"Lừa đảo mạo danh cán bộ",
|
| 65 |
+
"Cảnh giác lừa đảo online",
|
| 66 |
+
]
|
| 67 |
+
|
| 68 |
+
for query in test_queries:
|
| 69 |
+
print(f"\n📝 Query: {query}")
|
| 70 |
+
try:
|
| 71 |
+
result = rag_pipeline(query, 'search_advisory', top_k=3)
|
| 72 |
+
print(f" ✅ Results: {result['count']}")
|
| 73 |
+
print(f" ✅ Confidence: {result['confidence']:.4f}")
|
| 74 |
+
if result['count'] > 0:
|
| 75 |
+
print(f" ✅ Answer preview: {result['answer'][:150]}...")
|
| 76 |
+
print(f" ✅ Documents:")
|
| 77 |
+
for i, doc in enumerate(result['documents'][:3], 1):
|
| 78 |
+
print(f" {i}. {doc.title}")
|
| 79 |
+
else:
|
| 80 |
+
print(" ⚠️ No results found")
|
| 81 |
+
except Exception as e:
|
| 82 |
+
print(f" ❌ Error: {e}")
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def test_chatbot_integration():
|
| 86 |
+
"""Test chatbot integration."""
|
| 87 |
+
print("\n" + "="*60)
|
| 88 |
+
print("Test Chatbot Integration")
|
| 89 |
+
print("="*60)
|
| 90 |
+
|
| 91 |
+
chatbot = Chatbot()
|
| 92 |
+
|
| 93 |
+
test_queries = [
|
| 94 |
+
"Làm thủ tục cư trú cần gì?",
|
| 95 |
+
"Cảnh báo lừa đảo giả danh công an",
|
| 96 |
+
"Thủ tục PCCC như thế nào?",
|
| 97 |
+
]
|
| 98 |
+
|
| 99 |
+
for query in test_queries:
|
| 100 |
+
print(f"\n📝 Query: {query}")
|
| 101 |
+
try:
|
| 102 |
+
response = chatbot.generate_response(query)
|
| 103 |
+
print(f" ✅ Intent: {response.get('intent', 'N/A')}")
|
| 104 |
+
print(f" ✅ Confidence: {response.get('confidence', 0):.4f}")
|
| 105 |
+
print(f" ✅ Results: {response.get('count', 0)}")
|
| 106 |
+
if response.get('results'):
|
| 107 |
+
first_result = response['results'][0].get('data', {})
|
| 108 |
+
print(f" ✅ First result: {first_result.get('title', 'N/A')}")
|
| 109 |
+
print(f" ✅ Message preview: {response.get('message', '')[:150]}...")
|
| 110 |
+
except Exception as e:
|
| 111 |
+
print(f" ❌ Error: {e}")
|
| 112 |
+
import traceback
|
| 113 |
+
traceback.print_exc()
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def main():
|
| 117 |
+
print("="*60)
|
| 118 |
+
print("RAG Pipeline & Chatbot Integration Test")
|
| 119 |
+
print("="*60)
|
| 120 |
+
|
| 121 |
+
# Test RAG pipeline
|
| 122 |
+
test_rag_procedure()
|
| 123 |
+
test_rag_advisory()
|
| 124 |
+
|
| 125 |
+
# Test chatbot integration
|
| 126 |
+
test_chatbot_integration()
|
| 127 |
+
|
| 128 |
+
print("\n" + "="*60)
|
| 129 |
+
print("Test Complete")
|
| 130 |
+
print("="*60)
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
if __name__ == "__main__":
|
| 134 |
+
main()
|
| 135 |
+
|