-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathllm_toolkit_demo.py
More file actions
275 lines (210 loc) · 8.19 KB
/
Copy pathllm_toolkit_demo.py
File metadata and controls
275 lines (210 loc) · 8.19 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
"""Empathy LLM Toolkit - Demonstration
Shows progression from Level 1 to Level 4 empathy with an LLM.
Copyright 2025 Deep Study AI, LLC
Licensed under Fair Source 0.9
"""
import asyncio
import os
import sys
from datetime import datetime
# Add parent directory to path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
from empathy_llm_toolkit import EmpathyLLM, PatternType, UserPattern
async def demo_level_progression():
"""Demonstrate automatic progression through empathy levels.
Shows how LLM behavior changes as trust builds and patterns emerge.
"""
print("=" * 80)
print("Empathy LLM Toolkit - Level Progression Demo")
print("=" * 80)
# Initialize (requires API key in environment or pass directly)
llm = EmpathyLLM(
provider="anthropic",
target_level=4, # Target: Anticipatory
api_key=os.getenv("ANTHROPIC_API_KEY"),
model="claude-3-5-sonnet-20241022",
)
user_id = "demo_developer"
# INTERACTION 1: Level 1 (Reactive)
print("\n" + "─" * 80)
print("INTERACTION 1: Level 1 (Reactive)")
print("─" * 80)
response = await llm.interact(user_id=user_id, user_input="What is a REST API?")
print(f"Level Used: {response['level_used']} - {response['level_description']}")
print(f"Proactive: {response['proactive']}")
print(f"\nResponse:\n{response['content'][:500]}...")
# Mark as successful
llm.update_trust(user_id, "success")
# INTERACTION 2: Level 2 (Guided)
print("\n" + "─" * 80)
print("INTERACTION 2: Level 2 (Guided)")
print("─" * 80)
response = await llm.interact(
user_id=user_id,
user_input="Help me build a user authentication system",
)
print(f"Level Used: {response['level_used']} - {response['level_description']}")
print(f"Proactive: {response['proactive']}")
print(f"\nResponse:\n{response['content'][:500]}...")
llm.update_trust(user_id, "success")
# Add pattern manually (normally detected automatically)
print("\n" + "─" * 80)
print("ADDING PATTERN: User always requests tests after writing code")
print("─" * 80)
llm.add_pattern(
user_id=user_id,
pattern=UserPattern(
pattern_type=PatternType.SEQUENTIAL,
trigger="wrote code",
action="requests tests",
confidence=0.85,
occurrences=5,
last_seen=datetime.now(),
),
)
# Build trust to enable Level 3
for _ in range(3):
llm.update_trust(user_id, "success")
# INTERACTION 3: Level 3 (Proactive)
print("\n" + "─" * 80)
print("INTERACTION 3: Level 3 (Proactive)")
print("─" * 80)
response = await llm.interact(user_id=user_id, user_input="I just wrote the login function")
print(f"Level Used: {response['level_used']} - {response['level_description']}")
print(f"Proactive: {response['proactive']}")
if response["metadata"].get("pattern"):
print(f"Pattern Detected: {response['metadata']['pattern']}")
print(f"\nResponse:\n{response['content'][:500]}...")
llm.update_trust(user_id, "success")
# Continue building trust for Level 4
for _ in range(5):
# Simulate more interactions
await llm.interact(user_id, f"Test question {_}")
llm.update_trust(user_id, "success")
# INTERACTION 4: Level 4 (Anticipatory)
print("\n" + "─" * 80)
print("INTERACTION 4: Level 4 (Anticipatory)")
print("─" * 80)
response = await llm.interact(
user_id=user_id,
user_input="I'm adding my 15th API endpoint to this service",
)
print(f"Level Used: {response['level_used']} - {response['level_description']}")
print(f"Proactive: {response['proactive']}")
print(f"Trajectory Analyzed: {response['metadata'].get('trajectory_analyzed', False)}")
print(f"\nResponse:\n{response['content'][:800]}...")
# STATISTICS
print("\n" + "=" * 80)
print("COLLABORATION STATISTICS")
print("=" * 80)
stats = llm.get_statistics(user_id)
print(
f"""
User: {stats["user_id"]}
Session Duration: {stats["session_duration"]:.0f}s
Total Interactions: {stats["total_interactions"]}
Trust Level: {stats["trust_level"]:.0%}
Success Rate: {stats["success_rate"]:.0%}
Patterns Detected: {stats["patterns_detected"]}
Current Level: {stats["current_level"]}
Average Level: {stats["average_level"]:.1f}
""",
)
async def demo_forced_levels():
"""Demonstrate forcing specific levels for comparison.
Shows how the same question gets different treatment at each level.
"""
print("\n" + "=" * 80)
print("Empathy LLM Toolkit - Forced Level Comparison Demo")
print("=" * 80)
llm = EmpathyLLM(provider="anthropic", target_level=4, api_key=os.getenv("ANTHROPIC_API_KEY"))
user_input = "How should I structure my Python project?"
for level in [1, 2, 3, 4]:
print("\n" + "─" * 80)
print(f"LEVEL {level}: {EmpathyLevel.get_description(level)}")
print("─" * 80)
response = await llm.interact(
user_id="comparison_user",
user_input=user_input,
force_level=level,
)
print(f"\nResponse:\n{response['content'][:400]}...")
print(f"\nTokens Used: {response['metadata']['tokens_used']}")
async def demo_healthcare_use_case():
"""Demonstrate healthcare application with Level 4 anticipatory.
Shows clinical documentation automation progressing to compliance monitoring.
"""
print("\n" + "=" * 80)
print("Healthcare Use Case - Clinical Documentation")
print("=" * 80)
llm = EmpathyLLM(provider="anthropic", target_level=4, api_key=os.getenv("ANTHROPIC_API_KEY"))
clinician_id = "clinician_dr_smith"
# Build up context and trust
print("\nBuilding collaboration context...")
# Simulate several successful documentation sessions
for i in range(10):
await llm.interact(
clinician_id,
f"Document patient visit {i}",
context={"patient_id": f"patient_{i}", "chief_complaint": "routine checkup"},
)
llm.update_trust(clinician_id, "success")
# Add pattern: clinician always documents vitals, allergies, meds
llm.add_pattern(
clinician_id,
UserPattern(
pattern_type=PatternType.SEQUENTIAL,
trigger="patient visit",
action="documents vitals, allergies, medications",
confidence=0.90,
occurrences=10,
last_seen=datetime.now(),
context={"domain": "clinical_documentation"},
),
)
# Level 3: Proactive pre-population
print("\n" + "─" * 80)
print("LEVEL 3: Proactive Pre-population")
print("─" * 80)
response = await llm.interact(clinician_id, "Seeing patient John Doe for follow-up")
print(f"Response:\n{response['content'][:500]}...")
# Continue building trust for Level 4
for _ in range(5):
llm.update_trust(clinician_id, "success")
# Level 4: Anticipatory compliance monitoring
print("\n" + "─" * 80)
print("LEVEL 4: Anticipatory Compliance Monitoring")
print("─" * 80)
response = await llm.interact(
clinician_id,
"How am I doing on documentation quality?",
context={
"total_notes": 50,
"last_audit": "2024-01-15",
"next_audit_expected": "approximately 90 days",
},
)
print(f"Response:\n{response['content'][:800]}...")
async def main():
"""Run all demos"""
print("\nEmpathy LLM Toolkit Demonstrations\n")
# Check for API key
if not os.getenv("ANTHROPIC_API_KEY"):
print("WARNING: ANTHROPIC_API_KEY not set. Set it to run demos.")
print("Export ANTHROPIC_API_KEY=your-key-here")
return
try:
# Demo 1: Level progression
await demo_level_progression()
# Demo 2: Forced level comparison
# await demo_forced_levels()
# Demo 3: Healthcare use case
# await demo_healthcare_use_case()
except Exception as e:
print(f"\nError: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
# Import EmpathyLevel for demo
from empathy_llm_toolkit.levels import EmpathyLevel
asyncio.run(main())