-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_customer_interaction.py
More file actions
341 lines (271 loc) · 12.3 KB
/
test_customer_interaction.py
File metadata and controls
341 lines (271 loc) · 12.3 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
"""
Customer Interaction Testing Script
This script allows you to test the Contact Center AI system as a customer
interacting with the support agents.
"""
import asyncio
import httpx
import json
from datetime import datetime
from typing import Optional, Dict, Any
class CustomerTestClient:
"""Test client for simulating customer interactions"""
def __init__(self, base_url: str = "http://localhost:8000"):
self.base_url = base_url
self.token: Optional[str] = None
self.conversation_id: Optional[str] = None
async def authenticate(self, username: str = "johndoe", password: str = "secret") -> bool:
"""Authenticate and get access token"""
print(f"🔐 Authenticating as {username}...")
async with httpx.AsyncClient() as client:
try:
response = await client.post(
f"{self.base_url}/token",
data={"username": username, "password": password},
headers={"Content-Type": "application/x-www-form-urlencoded"}
)
if response.status_code == 200:
token_data = response.json()
self.token = token_data["access_token"]
print("✅ Authentication successful!")
return True
else:
print(f"❌ Authentication failed: {response.status_code} - {response.text}")
return False
except Exception as e:
print(f"❌ Authentication error: {str(e)}")
return False
async def start_conversation(self,
customer_id: str = "CUST_001",
channel: str = "web",
initial_message: str = "Hello, I need help with my account",
priority: str = "medium") -> bool:
"""Start a new conversation with the support system"""
if not self.token:
print("❌ Please authenticate first!")
return False
print(f"💬 Starting conversation: '{initial_message}'")
async with httpx.AsyncClient() as client:
try:
headers = {"Authorization": f"Bearer {self.token}"}
payload = {
"customer_id": customer_id,
"channel": channel,
"initial_message": initial_message,
"priority": priority
}
response = await client.post(
f"{self.base_url}/api/v1/conversations",
json=payload,
headers=headers
)
if response.status_code in [200, 201]:
conversation_data = response.json()
self.conversation_id = conversation_data.get("conversation_id")
agent_type = conversation_data.get("agent_type", "unknown")
agent_response = conversation_data.get("response", "No response")
print(f"✅ Conversation started!")
print(f"📋 Conversation ID: {self.conversation_id}")
print(f"🤖 Assigned Agent: {agent_type}")
print(f"💭 Agent Response: {agent_response}")
print("-" * 50)
return True
else:
print(f"❌ Failed to start conversation: {response.status_code}")
print(f"Response: {response.text}")
return False
except Exception as e:
print(f"❌ Error starting conversation: {str(e)}")
return False
async def send_message(self, message: str) -> bool:
"""Send a message to the current conversation"""
if not self.token or not self.conversation_id:
print("❌ No active conversation. Please start a conversation first!")
return False
print(f"📤 You: {message}")
async with httpx.AsyncClient() as client:
try:
headers = {"Authorization": f"Bearer {self.token}"}
payload = {"content": message}
response = await client.post(
f"{self.base_url}/api/v1/conversations/{self.conversation_id}/messages",
json=payload,
headers=headers
)
if response.status_code == 200:
message_data = response.json()
agent_type = message_data.get("agent_type", "Agent")
content = message_data.get("content", "No response")
confidence = message_data.get("confidence_score", 0.0)
print(f"📥 {agent_type}: {content}")
print(f"🎯 Confidence: {confidence:.2f}")
print("-" * 50)
return True
else:
print(f"❌ Failed to send message: {response.status_code}")
print(f"Response: {response.text}")
return False
except Exception as e:
print(f"❌ Error sending message: {str(e)}")
return False
async def get_conversation_status(self) -> Dict[str, Any]:
"""Get the current conversation status"""
if not self.token or not self.conversation_id:
print("❌ No active conversation!")
return {}
async with httpx.AsyncClient() as client:
try:
headers = {"Authorization": f"Bearer {self.token}"}
response = await client.get(
f"{self.base_url}/api/v1/conversations/{self.conversation_id}/state",
headers=headers
)
if response.status_code == 200:
return response.json()
else:
print(f"❌ Failed to get status: {response.status_code}")
return {}
except Exception as e:
print(f"❌ Error getting status: {str(e)}")
return {}
async def check_health(self) -> bool:
"""Check if the service is healthy"""
print("🏥 Checking service health...")
async with httpx.AsyncClient() as client:
try:
response = await client.get(f"{self.base_url}/health")
if response.status_code == 200:
health_data = response.json()
print(f"✅ Service is healthy: {health_data.get('status')}")
return True
else:
print(f"❌ Service health check failed: {response.status_code}")
return False
except Exception as e:
print(f"❌ Health check error: {str(e)}")
return False
async def interactive_test_session():
"""Run an interactive test session"""
print("=" * 60)
print("🤖 CONTACT CENTER AI - Customer Test Interface")
print("=" * 60)
client = CustomerTestClient()
# Check service health
if not await client.check_health():
print("❌ Service is not available. Make sure the application is running.")
return
# Authenticate
if not await client.authenticate():
print("❌ Authentication failed. Cannot proceed.")
return
print("\n📋 Test Scenarios Available:")
print("1. General Support Query")
print("2. Technical Issue")
print("3. Billing Question")
print("4. Sales Inquiry")
print("5. Custom Message")
while True:
try:
choice = input("\nSelect a scenario (1-5) or 'q' to quit: ").strip()
if choice.lower() == 'q':
break
scenarios = {
"1": "I'm having trouble accessing my account dashboard",
"2": "My internet connection keeps dropping every few minutes",
"3": "I was charged twice for the same service this month",
"4": "I'm interested in upgrading to your premium plan",
"5": None # Custom message
}
if choice in scenarios:
if choice == "5":
message = input("Enter your custom message: ").strip()
else:
message = scenarios[choice]
if message:
# Start conversation if this is the first message
if not client.conversation_id:
success = await client.start_conversation(
initial_message=message,
customer_id=f"TEST_CUSTOMER_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
)
if not success:
continue
else:
# Send as follow-up message
await client.send_message(message)
# Allow follow-up messages
while True:
follow_up = input("\nEnter follow-up message (or press Enter to return to menu): ").strip()
if not follow_up:
break
await client.send_message(follow_up)
else:
print("❌ Invalid choice. Please select 1-5 or 'q'.")
except KeyboardInterrupt:
print("\n\n👋 Goodbye!")
break
except Exception as e:
print(f"❌ Error: {str(e)}")
async def automated_test_scenarios():
"""Run automated test scenarios"""
print("=" * 60)
print("🤖 AUTOMATED TEST SCENARIOS")
print("=" * 60)
client = CustomerTestClient()
# Health check
if not await client.check_health():
return False
# Authentication test
print("\n1. Testing Authentication...")
if not await client.authenticate():
return False
# Test scenarios
test_scenarios = [
{
"name": "Basic FAQ Query",
"initial_message": "What are your business hours?",
"follow_ups": ["Do you have weekend support?"]
},
{
"name": "Technical Support",
"initial_message": "My internet connection is very slow",
"follow_ups": [
"It's been happening for the past 3 days",
"I've already restarted my router"
]
},
{
"name": "Billing Inquiry",
"initial_message": "I need help understanding my bill",
"follow_ups": ["There's a charge I don't recognize"]
}
]
for i, scenario in enumerate(test_scenarios, 1):
print(f"\n{i+1}. Testing: {scenario['name']}")
# Start new conversation for each scenario
client.conversation_id = None
success = await client.start_conversation(
initial_message=scenario["initial_message"],
customer_id=f"AUTO_TEST_{i}"
)
if success:
# Send follow-up messages
for follow_up in scenario["follow_ups"]:
await asyncio.sleep(2) # Brief pause between messages
await client.send_message(follow_up)
print("\n✅ All automated tests completed!")
return True
async def main():
"""Main function to choose test mode"""
print("Contact Center AI - Customer Testing Tool")
print("1. Interactive Test Session")
print("2. Automated Test Scenarios")
choice = input("Select mode (1 or 2): ").strip()
if choice == "1":
await interactive_test_session()
elif choice == "2":
await automated_test_scenarios()
else:
print("Invalid choice. Exiting.")
if __name__ == "__main__":
asyncio.run(main())