Spaces:
Runtime error
Runtime error
| import asyncio | |
| import sys | |
| import os | |
| import time | |
| # Add parent dir to path | |
| sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
| from app.services.ai_engine import get_ai_engine | |
| from app.services.intent_classifier import get_classifier | |
| from app.services.query_router import get_router | |
| # Test Configuration | |
| WEBSITE_ID = 19 # CareHome Pakistan | |
| INDUSTRY = "healthcare" | |
| TEST_QUESTIONS = [ | |
| # General Info (FAQ/Business) | |
| "What services does CareHome Pakistan provide?", | |
| "Is CareHome available 24/7?", | |
| "Which cities does CareHome operate in?", | |
| "Where is CareHome located?", | |
| "How can I contact CareHome?", | |
| # Trust/Validation (Business Specific) | |
| "Is CareHome a licensed healthcare provider?", | |
| "Are your doctors PMDC registered?", | |
| "What makes CareHome different from other home care services?", | |
| # Service Scope | |
| "Do you provide services all over Pakistan?", | |
| "Is CareHome suitable for elderly patients?", | |
| # Healthcare / Medical Intent (Testing Routing Fix) | |
| "I have a high fever and severe headache.", | |
| "My father has high blood pressure and is dizziness. What should I do?", | |
| # Edge Case / Out of Scope | |
| "Do you sell ceiling fans?" | |
| ] | |
| async def run_test(): | |
| print(f"\nπ Starting RAG Stress Test for Website ID: {WEBSITE_ID} ({INDUSTRY})") | |
| print("=" * 80) | |
| ai = get_ai_engine() | |
| classifier = get_classifier() | |
| router = get_router() | |
| results = [] | |
| for query in TEST_QUESTIONS: | |
| print(f"\nβ Query: {query}") | |
| start_time = time.time() | |
| # 1. Test Intent Classification | |
| intent_res = await classifier.classify(query, industry=INDUSTRY) | |
| print(f" π§ Intent: {intent_res.category.value} (Conf: {intent_res.confidence:.2f})") | |
| # 2. Test Router | |
| route_res = await router.route_query(query, WEBSITE_ID, industry=INDUSTRY) | |
| print(f" π Handler: {route_res['handler']}") | |
| # 3. Test Full Generation | |
| # Simulate context from a retrieval (mock for speed, or let AI engine fetch?) | |
| # We will let AI Engine do the full flow including retrieval | |
| response, confidence, _ = await ai.generate_response( | |
| query=query, | |
| context=[], # AI engine will fetch context via strategy | |
| website_id=WEBSITE_ID, | |
| industry=INDUSTRY | |
| ) | |
| duration = time.time() - start_time | |
| print(f" π€ Response: {response[:150]}..." if len(response) > 150 else f" π€ Response: {response}") | |
| print(f" β±οΈ Time: {duration:.2f}s") | |
| results.append({ | |
| "query": query, | |
| "intent": intent_res.category.value, | |
| "latency": duration, | |
| "preview": response[:50] | |
| }) | |
| print("\n" + "=" * 80) | |
| print("π Test Summary") | |
| print(f"{'Query':<50} | {'Intent':<15} | {'Time':<6}") | |
| print("-" * 80) | |
| for r in results: | |
| print(f"{r['query'][:47]:<50} | {r['intent']:<15} | {r['latency']:.2f}s") | |
| if __name__ == "__main__": | |
| asyncio.run(run_test()) | |