File size: 8,366 Bytes
3998131 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
"""
Test script for context-aware chat functionality
This demonstrates the new chat endpoint with conversation context
"""
import requests
import json
from typing import Optional
# Configuration
BASE_URL = "http://localhost:8000" # Adjust to your API URL
API_TOKEN = None # Will be set after login
def login(email: str, password: str) -> Optional[str]:
"""Login and get access token"""
url = f"{BASE_URL}/auth/login"
payload = {"email": email, "password": password}
try:
response = requests.post(url, json=payload)
response.raise_for_status()
data = response.json()
token = data.get("session", {}).get("access_token")
print(f"β Login successful")
return token
except Exception as e:
print(f"β Login failed: {e}")
return None
def create_conversation(token: str, title: str = "Test Conversation") -> Optional[str]:
"""Create a new conversation"""
url = f"{BASE_URL}/chat-history/conversations"
headers = {"Authorization": f"Bearer {token}"}
payload = {"title": title}
try:
response = requests.post(url, json=payload, headers=headers)
response.raise_for_status()
data = response.json()
conv_id = data.get("id")
print(f"β Conversation created: {conv_id}")
return conv_id
except Exception as e:
print(f"β Failed to create conversation: {e}")
return None
def send_chat_message(
token: str,
query: str,
conversation_id: Optional[str] = None
) -> dict:
"""Send a chat message with context awareness"""
url = f"{BASE_URL}/law-explanation/chat"
headers = {"Authorization": f"Bearer {token}"}
payload = {
"query": query,
"conversation_id": conversation_id
}
try:
response = requests.post(url, json=payload, headers=headers)
response.raise_for_status()
data = response.json()
return data
except Exception as e:
print(f"β Chat request failed: {e}")
if hasattr(e, 'response'):
print(f"Response: {e.response.text}")
return {}
def get_conversation_history(token: str, conversation_id: str) -> list:
"""Get conversation messages"""
url = f"{BASE_URL}/chat-history/conversations/{conversation_id}/messages"
headers = {"Authorization": f"Bearer {token}"}
try:
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json()
except Exception as e:
print(f"β Failed to get messages: {e}")
return []
def print_response(response: dict, test_name: str):
"""Pretty print the response"""
print(f"\n{'='*60}")
print(f"Test: {test_name}")
print(f"{'='*60}")
if not response:
print("No response received")
return
print(f"Query: {response.get('query', 'N/A')}")
print(f"Context Used: {response.get('context_used', False)}")
print(f"Is Non-Legal: {response.get('is_non_legal', False)}")
if response.get('original_query'):
print(f"Original Query: {response['original_query']}")
if response.get('summarized_query'):
print(f"Summarized Query: {response['summarized_query']}")
print(f"\nSummary: {response.get('summary', 'N/A')}")
print(f"\nExplanation: {response.get('explanation', 'N/A')[:200]}...")
print(f"\nSources: {len(response.get('sources', []))} documents")
print(f"{'='*60}\n")
def run_test_scenario(token: str):
"""Run comprehensive test scenarios"""
print("\n" + "="*60)
print("CONTEXT-AWARE CHAT TEST SCENARIOS")
print("="*60 + "\n")
# Create a new conversation for testing
conv_id = create_conversation(token, "Context Awareness Test")
if not conv_id:
print("Cannot proceed without conversation ID")
return
# Test 1: Initial legal query
print("\n--- Test 1: Initial Legal Query (No Context) ---")
response1 = send_chat_message(
token,
"I had a fight with my brother over property",
conv_id
)
print_response(response1, "Initial Property Dispute Query")
# Test 2: Dependent follow-up query
print("\n--- Test 2: Dependent Follow-up Query (With Context) ---")
response2 = send_chat_message(
token,
"He is making fake allegations",
conv_id
)
print_response(response2, "Follow-up About Brother's Allegations")
# Test 3: Another dependent query
print("\n--- Test 3: Another Dependent Query ---")
response3 = send_chat_message(
token,
"What evidence do I need to counter this?",
conv_id
)
print_response(response3, "Evidence Question (Continuation)")
# Test 4: Independent new topic
print("\n--- Test 4: Independent New Topic ---")
response4 = send_chat_message(
token,
"How do I apply for citizenship in Nepal?",
conv_id
)
print_response(response4, "New Independent Query - Citizenship")
# Test 5: Non-legal query (greeting)
print("\n--- Test 5: Non-Legal Query (Greeting) ---")
response5 = send_chat_message(
token,
"Thank you so much for your help!",
conv_id
)
print_response(response5, "Gratitude Message")
# Test 6: Non-legal query (small talk)
print("\n--- Test 6: Non-Legal Query (Greeting) ---")
response6 = send_chat_message(
token,
"Hi, how are you?",
conv_id
)
print_response(response6, "Casual Greeting")
# Test 7: Back to legal query
print("\n--- Test 7: Back to Legal Topic ---")
response7 = send_chat_message(
token,
"What are the divorce laws in Nepal?",
conv_id
)
print_response(response7, "New Legal Topic - Divorce")
# Show conversation history summary
print("\n" + "="*60)
print("CONVERSATION HISTORY SUMMARY")
print("="*60)
messages = get_conversation_history(token, conv_id)
print(f"Total messages in conversation: {len(messages)}")
for i, msg in enumerate(messages, 1):
role = msg.get('role', 'unknown')
content = msg.get('content', '')
print(f"\n{i}. [{role.upper()}]: {content[:100]}...")
def run_edge_case_tests(token: str):
"""Test edge cases"""
print("\n" + "="*60)
print("EDGE CASE TESTS")
print("="*60 + "\n")
# Test without conversation_id
print("\n--- Test: No Conversation ID (Standalone Query) ---")
response = send_chat_message(
token,
"What are tenant rights in Nepal?",
conversation_id=None
)
print_response(response, "Standalone Query Without Conversation")
# Test with empty conversation
conv_id = create_conversation(token, "Empty Conversation Test")
print("\n--- Test: First Message in New Conversation ---")
response = send_chat_message(
token,
"Tell me about labor laws",
conversation_id=conv_id
)
print_response(response, "First Message in Fresh Conversation")
if __name__ == "__main__":
print("""
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Context-Aware Chat Testing Suite β
β Tests conversation context awareness, independence β
β detection, and non-legal query filtering β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
""")
# Get credentials
print("Please provide your test credentials:")
email = input("Email: ").strip()
password = input("Password: ").strip()
# Login
token = login(email, password)
if not token:
print("\nCannot proceed without authentication token")
exit(1)
# Run tests
try:
run_test_scenario(token)
print("\n" + "="*60)
run_edge_case_tests(token)
print("\n" + "="*60)
print("ALL TESTS COMPLETED")
print("="*60 + "\n")
except KeyboardInterrupt:
print("\n\nTests interrupted by user")
except Exception as e:
print(f"\n\nTest suite failed: {e}")
import traceback
traceback.print_exc()
|