Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| import sys | |
| import os | |
| sys.path.append(os.path.join(os.path.dirname(__file__), 'src')) | |
| from chatbot import Chatbot | |
| from models import ChatbotRequest | |
| def test_interactive_transactions(): | |
| print("π§ͺ Testing Interactive Transaction Completion") | |
| print("="*70) | |
| print("Note: Make sure to set OPENAI_API_KEY environment variable") | |
| print("="*70) | |
| chatbot = Chatbot() | |
| test_scenarios = [ | |
| { | |
| "name": "Complete Purchase Transaction", | |
| "initial": "I bought 20 USB drives from TechMart at β¬5 each", | |
| "expected_complete": True, | |
| "description": "Should be complete with all required fields" | |
| }, | |
| { | |
| "name": "Incomplete Purchase - Missing Supplier", | |
| "initial": "I bought 10 laptops at β¬800 each", | |
| "clarifications": ["Electronics Plus"], | |
| "expected_questions": ["supplier"], | |
| "description": "Should ask for supplier information" | |
| }, | |
| { | |
| "name": "Incomplete Purchase - Missing Multiple Fields", | |
| "initial": "I bought some office chairs", | |
| "clarifications": ["15 chairs", "Office Supplies Co", "β¬150 per chair"], | |
| "expected_questions": ["quantity", "supplier", "unit_price"], | |
| "description": "Should ask for quantity, supplier, and price" | |
| }, | |
| { | |
| "name": "Sale with Missing Customer", | |
| "initial": "Sold 5 laptops at β¬900 each", | |
| "clarifications": ["ABC Corporation"], | |
| "expected_questions": ["customer"], | |
| "description": "Should ask for customer information" | |
| }, | |
| { | |
| "name": "Transaction with N/A Fields", | |
| "initial": "Bought 100 pens", | |
| "clarifications": ["Staples", "$2 each", "N/A"], | |
| "expected_questions": ["supplier", "unit_price"], | |
| "description": "Should handle N/A responses gracefully" | |
| } | |
| ] | |
| for i, scenario in enumerate(test_scenarios, 1): | |
| print(f"\nπ Test Scenario {i}: {scenario['name']}") | |
| print("-" * 60) | |
| print(f"Description: {scenario['description']}") | |
| print(f"Initial input: {scenario['initial']}") | |
| # Test initial transaction request | |
| session_id = f"test_session_{i}" | |
| request = ChatbotRequest(message=scenario['initial'], session_id=session_id) | |
| response = chatbot.process_message(request) | |
| print(f"\nπ€ Initial Response:") | |
| print(response.response) | |
| if response.awaiting_clarification: | |
| print(f"β Correctly identified as incomplete transaction") | |
| # Process clarifications if provided | |
| if "clarifications" in scenario: | |
| print(f"\nπ Providing clarifications...") | |
| for j, clarification in enumerate(scenario["clarifications"], 1): | |
| print(f"\n Clarification {j}: {clarification}") | |
| clarification_request = ChatbotRequest( | |
| message=clarification, | |
| session_id=session_id | |
| ) | |
| clarification_response = chatbot.process_message(clarification_request) | |
| print(f" π€ Response: {clarification_response.response[:100]}{'...' if len(clarification_response.response) > 100 else ''}") | |
| if not clarification_response.awaiting_clarification: | |
| print(f" β Transaction completed!") | |
| break | |
| else: | |
| print(f" β³ Still waiting for more information...") | |
| else: | |
| if scenario.get("expected_complete", False): | |
| print(f"β Correctly completed transaction without clarification") | |
| else: | |
| print(f"β Expected clarification but transaction was completed") | |
| print(f"\nIntent detected: {response.intent_detected}") | |
| if response.entities_extracted: | |
| entities = response.entities_extracted | |
| print(f"Entities: {entities.transaction_type} - {entities.product} ({entities.quantity}x) - β¬{entities.total_amount}") | |
| print("\n" + "="*60) | |
| print("\nπ§ͺ Testing Edge Cases") | |
| print("-" * 40) | |
| # Test cancellation | |
| print("\nπ Testing Transaction Cancellation") | |
| request = ChatbotRequest(message="I bought some items", session_id="cancel_test") | |
| response = chatbot.process_message(request) | |
| if response.awaiting_clarification: | |
| print("β Transaction requires clarification") | |
| cancel_request = ChatbotRequest(message="cancel", session_id="cancel_test") | |
| cancel_response = chatbot.process_message(cancel_request) | |
| print(f"π€ Cancel response: {cancel_response.response}") | |
| if not cancel_response.awaiting_clarification: | |
| print("β Transaction successfully cancelled") | |
| else: | |
| print("β Transaction not properly cancelled") | |
| # Test invalid session | |
| print("\nπ Testing Invalid Session Response") | |
| invalid_request = ChatbotRequest(message="More information here", session_id="nonexistent") | |
| invalid_response = chatbot.process_message(invalid_request) | |
| print(f"π€ Invalid session response: {invalid_response.response}") | |
| chatbot.close() | |
| print("\nβ Interactive transaction tests completed!") | |
| def test_clarification_quality(): | |
| print("\n㪠Testing Clarification Question Quality") | |
| print("-" * 50) | |
| chatbot = Chatbot() | |
| # Test various incomplete scenarios to see question quality | |
| incomplete_scenarios = [ | |
| "I bought something expensive", | |
| "Purchase from TechMart", | |
| "Sold items to a customer", | |
| "β¬1000 transaction yesterday", | |
| "Bought 50 units" | |
| ] | |
| for i, scenario in enumerate(incomplete_scenarios, 1): | |
| print(f"\nπ Scenario {i}: {scenario}") | |
| print("-" * 30) | |
| request = ChatbotRequest(message=scenario, session_id=f"quality_test_{i}") | |
| response = chatbot.process_message(request) | |
| if response.awaiting_clarification: | |
| print("π Clarification questions generated:") | |
| # Extract questions from response for analysis | |
| lines = response.response.split('\n') | |
| questions = [line.strip() for line in lines if line.strip() and any(char.isdigit() and line.strip().startswith(char) for char in '123456789')] | |
| for q in questions[:3]: # Show first 3 questions | |
| print(f" β’ {q}") | |
| print(f"β Generated {len(questions)} clarification questions") | |
| else: | |
| print("β No clarification requested (unexpected)") | |
| chatbot.close() | |
| print("\nβ Clarification quality tests completed!") | |
| if __name__ == "__main__": | |
| if not os.getenv('OPENAI_API_KEY'): | |
| print("β Error: OPENAI_API_KEY environment variable not set") | |
| print("Please set your OpenAI API key:") | |
| print("export OPENAI_API_KEY='your-key-here'") | |
| sys.exit(1) | |
| test_interactive_transactions() | |
| test_clarification_quality() |