forked from bojieli/ai-agent-book
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_proactive_service.py
More file actions
179 lines (153 loc) · 6.77 KB
/
Copy pathtest_proactive_service.py
File metadata and controls
179 lines (153 loc) · 6.77 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
#!/usr/bin/env python3
"""Test script to demonstrate the agent's proactive service (主动服务) capabilities"""
import logging
from datetime import datetime, timedelta
from contextual_indexer import ContextualMemoryIndexer
from contextual_agent import ContextualUserMemoryAgent
from advanced_memory_manager import AdvancedMemoryCard
from config import Config
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
def test_proactive_service():
"""Test the agent's ability to provide proactive service"""
print("\n" + "="*80)
print("测试主动服务 (Testing Proactive Service)")
print("="*80)
# Initialize system
config = Config.from_env()
user_id = "proactive_test_user"
indexer = ContextualMemoryIndexer(
user_id=user_id,
index_config=config.index,
chunking_config=config.chunking,
use_contextual=False
)
# Add test memory cards with potential issues
current_date = datetime.now()
# 1. Passport expiring soon
passport_card = AdvancedMemoryCard(
category="travel",
card_key="passport_info",
backstory="User mentioned passport details when booking international travel",
date_created=current_date.strftime('%Y-%m-%d %H:%M:%S'),
person="Jessica Thompson (primary)",
relationship="primary account holder",
data={
"passport_number": "XXXXX1234",
"expiration_date": (current_date + timedelta(days=45)).strftime('%Y-%m-%d'),
"issuing_country": "USA"
}
)
indexer.memory_manager.add_card(passport_card)
# 2. Upcoming travel plan
travel_card = AdvancedMemoryCard(
category="travel",
card_key="tokyo_trip_jan_2025",
backstory="User booked a trip to Tokyo for late January",
date_created=current_date.strftime('%Y-%m-%d %H:%M:%S'),
person="Jessica Thompson (primary)",
relationship="primary account holder",
data={
"destination": "Tokyo, Japan",
"departure_date": (current_date + timedelta(days=30)).strftime('%Y-%m-%d'),
"return_date": (current_date + timedelta(days=37)).strftime('%Y-%m-%d'),
"airline": "United Airlines",
"booking_reference": "UA1234567"
}
)
indexer.memory_manager.add_card(travel_card)
# 3. Medical appointment
medical_card = AdvancedMemoryCard(
category="medical",
card_key="annual_checkup_2025",
backstory="User scheduled annual physical exam",
date_created=current_date.strftime('%Y-%m-%d %H:%M:%S'),
person="Jessica Thompson (primary)",
relationship="primary account holder",
data={
"appointment_type": "Annual Physical",
"doctor": "Dr. Sarah Chen",
"clinic": "Portland Medical Center",
"date": (current_date + timedelta(days=5)).strftime('%Y-%m-%d'),
"time": "09:00 AM",
"fasting_required": True
}
)
indexer.memory_manager.add_card(medical_card)
# 4. Insurance card
insurance_card = AdvancedMemoryCard(
category="insurance",
card_key="travel_insurance_2024",
backstory="User has annual travel insurance that needs renewal",
date_created=current_date.strftime('%Y-%m-%d %H:%M:%S'),
person="Jessica Thompson (primary)",
relationship="primary account holder",
data={
"provider": "SafeTravel Insurance",
"policy_number": "ST-2024-789456",
"expiration_date": (current_date + timedelta(days=20)).strftime('%Y-%m-%d'),
"coverage": "International travel medical and trip cancellation"
}
)
indexer.memory_manager.add_card(insurance_card)
# Initialize agent
agent = ContextualUserMemoryAgent(
indexer=indexer,
config=config
)
print("\n" + "="*80)
print("Scenario: User asks about Tokyo trip preparation")
print("Expected: Agent should proactively identify passport expiration risk")
print("="*80)
# Test questions that should trigger proactive service
test_questions = [
"我一月底的东京之行,还有什么要准备的吗?",
"What do I need for my Tokyo trip?",
"我下周有什么安排吗?",
]
for i, question in enumerate(test_questions, 1):
print(f"\n{'='*60}")
print(f"Test {i}: {question}")
print('='*60)
trajectory = agent.answer_question(
question=question,
test_id=f"proactive_test_{i}",
max_iterations=5,
stream=False
)
print("\n📝 Agent Response:")
print("-" * 40)
print(trajectory.final_answer)
print("-" * 40)
# Check if agent identified key issues
if trajectory.final_answer:
answer_lower = trajectory.final_answer.lower()
print("\n✅ Proactive Service Check:")
# Check if passport expiration was mentioned
if "passport" in answer_lower and ("expir" in answer_lower or "过期" in answer_lower):
print(" ✓ Identified passport expiration risk")
else:
print(" ✗ Missed passport expiration risk")
# Check if insurance was mentioned
if "insurance" in answer_lower or "保险" in answer_lower:
print(" ✓ Mentioned travel insurance status")
else:
print(" ✗ Missed insurance consideration")
# Check if medical appointment was mentioned (for weekly schedule question)
if i == 3 and ("appointment" in answer_lower or "physical" in answer_lower or "医生" in answer_lower):
print(" ✓ Reminded about medical appointment")
# Check for urgency markers
if any(marker in trajectory.final_answer for marker in ["⚠️", "🔴", "⏰", "需要立即", "urgent", "ASAP"]):
print(" ✓ Used urgency markers for time-sensitive items")
print(f"\nMemory Cards Used: {trajectory.memory_cards_used}")
print(f"Iterations: {len(trajectory.iterations)}")
print("\n" + "="*80)
print("主动服务测试完成 (Proactive Service Test Complete)")
print("="*80)
print("\nKey Features Demonstrated:")
print("1. Risk Detection: Identifying passport expiration before travel")
print("2. Comprehensive Assistance: Connecting travel with insurance needs")
print("3. Proactive Reminders: Highlighting upcoming appointments")
print("4. Urgency Indicators: Using markers for time-sensitive matters")
if __name__ == "__main__":
test_proactive_service()