forked from odsc2015/agentic-hackathon-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
366 lines (287 loc) · 12 KB
/
cli.py
File metadata and controls
366 lines (287 loc) · 12 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/usr/bin/env python3
"""
Command Line Interface for Intelligent Healthcare Navigator
Provides text-based interaction with the healthcare navigation system
"""
import asyncio
import argparse
import sys
import os
from typing import Dict, Any
import json
# Add src to path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
from src.agent import HealthcareNavigatorAgent
from src.config import Config
from src.utils import setup_logging
logger = setup_logging()
class HealthcareCLI:
"""Command line interface for healthcare navigation"""
def __init__(self):
"""Initialize CLI"""
self.agent = None
self.session_id = "cli_session"
async def initialize(self):
"""Initialize the healthcare agent"""
try:
self.agent = HealthcareNavigatorAgent(self.session_id)
print("🏥 Healthcare Navigator CLI initialized successfully!")
print("Type 'help' for available commands or 'quit' to exit.\n")
except Exception as e:
print(f"❌ Failed to initialize Healthcare Navigator: {e}")
sys.exit(1)
def print_help(self):
"""Print help information"""
help_text = """
🏥 Healthcare Navigator CLI Commands:
Basic Commands:
help - Show this help message
quit, exit - Exit the application
clear - Clear conversation history
status - Show system status
Query Commands:
ask <question> - Ask a medical question
drug <drug_name> - Get drug information
symptom <symptoms> - Analyze symptoms
term <medical_term> - Look up medical term
Document Commands:
upload <file_path> - Upload and analyze a medical document
Preference Commands:
set <key> <value> - Set user preference
get <key> - Get user preference
Examples:
ask "What is diabetes?"
drug "aspirin"
symptom "headache and fever"
term "hypertension"
upload "medical_report.pdf"
set age 35
"""
print(help_text)
async def process_command(self, command: str) -> bool:
"""Process user command. Returns False if should quit."""
command = command.strip()
if not command:
return True
# Parse command
parts = command.split(' ', 1)
cmd = parts[0].lower()
args = parts[1] if len(parts) > 1 else ""
try:
if cmd in ['quit', 'exit']:
return False
elif cmd == 'help':
self.print_help()
elif cmd == 'clear':
success = self.agent.clear_conversation_history()
if success:
print("✅ Conversation history cleared.")
else:
print("❌ Failed to clear conversation history.")
elif cmd == 'status':
await self.show_status()
elif cmd == 'ask':
if not args:
print("❌ Please provide a question. Example: ask 'What is diabetes?'")
else:
await self.process_query(args)
elif cmd == 'drug':
if not args:
print("❌ Please provide a drug name. Example: drug aspirin")
else:
await self.process_query(f"Tell me about the drug {args}")
elif cmd == 'symptom':
if not args:
print("❌ Please provide symptoms. Example: symptom 'headache and fever'")
else:
await self.process_query(f"I have these symptoms: {args}")
elif cmd == 'term':
if not args:
print("❌ Please provide a medical term. Example: term hypertension")
else:
await self.process_query(f"What is {args}?")
elif cmd == 'upload':
if not args:
print("❌ Please provide a file path. Example: upload medical_report.pdf")
else:
await self.upload_document(args)
elif cmd == 'set':
await self.set_preference(args)
elif cmd == 'get':
await self.get_preference(args)
else:
print(f"❌ Unknown command: {cmd}. Type 'help' for available commands.")
except Exception as e:
print(f"❌ Error processing command: {e}")
logger.error(f"CLI command error: {e}")
return True
async def process_query(self, query: str):
"""Process a medical query"""
print(f"\n🤔 Processing: {query}")
print("⏳ Please wait...")
try:
response = await self.agent.process_query(query)
print(f"\n📋 Response:")
print("=" * 60)
print(response.response.response_text)
if response.response.sources:
print(f"\n📚 Sources: {', '.join(response.response.sources)}")
if response.response.confidence_score:
print(f"🎯 Confidence: {response.response.confidence_score:.1%}")
if response.response.disclaimers:
print(f"\n⚠️ Important Disclaimers:")
for disclaimer in response.response.disclaimers:
print(f"• {disclaimer}")
print(f"\n⏱️ Processing time: {response.processing_time:.2f}s")
print("=" * 60)
except Exception as e:
print(f"❌ Query failed: {e}")
async def upload_document(self, file_path: str):
"""Upload and process a document"""
try:
if not os.path.exists(file_path):
print(f"❌ File not found: {file_path}")
return
print(f"\n📄 Uploading document: {file_path}")
print("⏳ Processing...")
with open(file_path, 'rb') as f:
file_data = f.read()
file_size = len(file_data)
filename = os.path.basename(file_path)
response = await self.agent.handle_document_upload(file_data, filename, file_size)
print(f"\n📋 Document Analysis:")
print("=" * 60)
print(response.response.response_text)
if response.response.disclaimers:
print(f"\n⚠️ Important Notes:")
for disclaimer in response.response.disclaimers:
print(f"• {disclaimer}")
print(f"\n⏱️ Processing time: {response.processing_time:.2f}s")
print("=" * 60)
except Exception as e:
print(f"❌ Document upload failed: {e}")
async def show_status(self):
"""Show system status"""
try:
status = await self.agent.get_system_status()
print(f"\n🔍 System Status:")
print("=" * 40)
print(f"Session ID: {status['session_id']}")
print(f"System Healthy: {'✅' if status['system_healthy'] else '❌'}")
print(f"\n📊 Components:")
for component, healthy in status['component_status'].items():
status_icon = '✅' if healthy else '❌'
print(f" {status_icon} {component}")
if 'session_stats' in status:
stats = status['session_stats']
print(f"\n💬 Session Stats:")
print(f" Interactions: {stats.get('total_interactions', 0)}")
print(f" Last activity: {stats.get('last_activity', 'Never')}")
print("=" * 40)
except Exception as e:
print(f"❌ Status check failed: {e}")
async def set_preference(self, args: str):
"""Set user preference"""
try:
parts = args.split(' ', 1)
if len(parts) != 2:
print("❌ Usage: set <key> <value>")
return
key, value = parts
success = self.agent.set_user_preference(key, value)
if success:
print(f"✅ Preference set: {key} = {value}")
else:
print(f"❌ Failed to set preference: {key}")
except Exception as e:
print(f"❌ Error setting preference: {e}")
async def get_preference(self, key: str):
"""Get user preference"""
try:
if not key:
print("❌ Usage: get <key>")
return
value = self.agent.get_user_preference(key)
if value is not None:
print(f"📋 {key} = {value}")
else:
print(f"❌ Preference not found: {key}")
except Exception as e:
print(f"❌ Error getting preference: {e}")
async def run_interactive(self):
"""Run interactive CLI mode"""
await self.initialize()
print("💬 Interactive mode started. Type your commands below:")
try:
while True:
try:
command = input("\n🏥 > ").strip()
if not await self.process_command(command):
break
except KeyboardInterrupt:
print("\n\n👋 Goodbye!")
break
except EOFError:
print("\n\n👋 Goodbye!")
break
except Exception as e:
print(f"❌ Unexpected error: {e}")
logger.error(f"CLI runtime error: {e}")
async def main():
"""Main CLI entry point"""
parser = argparse.ArgumentParser(
description="Intelligent Healthcare Navigator CLI",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python cli.py # Interactive mode
python cli.py --query "What is diabetes?"
python cli.py --drug "aspirin"
python cli.py --upload "report.pdf"
"""
)
parser.add_argument('--query', '-q', help='Ask a medical question')
parser.add_argument('--drug', '-d', help='Get drug information')
parser.add_argument('--symptom', '-s', help='Analyze symptoms')
parser.add_argument('--term', '-t', help='Look up medical term')
parser.add_argument('--upload', '-u', help='Upload and analyze document')
parser.add_argument('--status', action='store_true', help='Show system status')
parser.add_argument('--session', default='cli_session', help='Session ID')
args = parser.parse_args()
cli = HealthcareCLI()
cli.session_id = args.session
# Handle single commands
if args.status:
await cli.initialize()
await cli.show_status()
return
if args.query:
await cli.initialize()
await cli.process_query(args.query)
return
if args.drug:
await cli.initialize()
await cli.process_query(f"Tell me about the drug {args.drug}")
return
if args.symptom:
await cli.initialize()
await cli.process_query(f"I have these symptoms: {args.symptom}")
return
if args.term:
await cli.initialize()
await cli.process_query(f"What is {args.term}?")
return
if args.upload:
await cli.initialize()
await cli.upload_document(args.upload)
return
# Default to interactive mode
await cli.run_interactive()
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
print("\n👋 Goodbye!")
except Exception as e:
print(f"❌ Application error: {e}")
sys.exit(1)