customeragent-api / server /tests /verify_nlp.py
anasraza526's picture
Clean deploy to Hugging Face
ac90985
import asyncio
import sys
import os
# Add server directory to path
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from app.services.ai_engine import AIEngine
from app.core.database import SessionLocal
from app.models.website import Website
async def verify_nlp():
print("πŸš€ Initializing AI Engine...")
engine = AIEngine()
# Get a real website ID
db = SessionLocal()
website = db.query(Website).first()
website_id = website.id if website else 1
db.close()
print(f"Using Website ID: {website_id}")
test_queries = [
# English Tests
"how many products do you have?",
"products",
"what is your mission",
"tell me about your team",
# Urdu Tests
"aapke paas kitne products hain",
"mission kya hai",
"team ke bare mein batao"
]
print("\nπŸ§ͺ STARTING VERIFICATION TESTS")
print("=" * 50)
for query in test_queries:
print(f"\n❓ Query: '{query}'")
# 1. Test Intent Classification directly
intent = engine._classify_intent(query)
print(f" Intent: {intent}")
# 2. Test Full Response Generation
# Simulate context retrieval (empty list as we want to trigger fallback/dynamic logic)
# In real flow, _generate_response calls dynamic generator if needed
# We'll call the internal dynamic logic directly to verify it
# Determine language
lang_code, _ = engine.language_detector.detect(query)
print(f" Language: {lang_code}")
# Get Knowledge Graph
if website_id not in engine.knowledge_graphs:
# Force rebuild to verify fix
print(" ♻️ Forcing Knowledge Graph Rebuild...")
engine.knowledge_graphs[website_id] = engine.kg_builder.get_knowledge_graph(website_id, force_rebuild=True)
kg = engine.knowledge_graphs[website_id]
# Debug: Print full product list once
if "products" in query:
print(f" πŸ“¦ Found {len(kg.get('products', []))} products: {kg.get('products', [])}")
# Generate Response
from app.services.dynamic_response_generator import DynamicResponseGenerator
generator = DynamicResponseGenerator(kg)
response = generator.generate(query, intent, lang_code.value)
print(f" πŸ€– Response: {response[:150]}...")
# Assertions
if "products" in query or "product" in query:
if "how many" in query or "kitne" in query:
# Should have a number
has_number = any(char.isdigit() for char in response)
print(f" βœ… Count Check: {'PASS' if has_number else 'FAIL'}")
else:
# Should list items
is_list = "1." in response or "β€’" in response
print(f" βœ… List Check: {'PASS' if is_list else 'FAIL'}")
print("\n=" * 50)
print("🏁 Verification Complete")
if __name__ == "__main__":
asyncio.run(verify_nlp())