Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
1222 lines (990 loc) · 44.4 KB
/
Copy pathmain.py
File metadata and controls
1222 lines (990 loc) · 44.4 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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Remote Medical Assistant - LangGraph MultiAgent System V3
Refactored with improved architecture, error handling, and system prompts
"""
import os
import base64
import requests
from typing import TypedDict, Annotated, Optional, List, Dict, Any
import json
import operator
from enum import Enum
from openai import OpenAI
from spitch import Spitch
from datetime import datetime, timedelta
from dotenv import load_dotenv
from doclist import dummy_doctors
from langgraph.graph import StateGraph, END
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
from langchain_core.tools import tool
from google.cloud import translate
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, Field
import logging
# pyright: reportReturnType=false
# pyright: reportGeneralTypeIssues=false
# pyright: reportOptionalMemberAccess=false
# ============================================================================
# LOGGING CONFIGURATION
# ============================================================================
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# ============================================================================
# ENVIRONMENT SETUP
# ============================================================================
load_dotenv()
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
GOOGLE_PROJECT_ID = os.getenv("GOOGLE_PROJECT_ID", "medconnect-479308")
RUNPOD_BASE_URL = os.getenv("RUNPOD_BASE_URL", "https://0br7mnhp94yl8x-8000.proxy.runpod.net/v1")
USERS_ENDPOINT = "https://medconnect-api-xrmi.onrender.com/api/agents"
if not GEMINI_API_KEY:
raise ValueError("GEMINI_API_KEY environment variable is required")
# ============================================================================
# ENUMS
# ============================================================================
class NodeType(str, Enum):
"""Available nodes in the system"""
CONTROLLER = "controller"
ORCHESTRATOR = "orchestrator"
SPECIALIST = "specialist"
CLERKING = "clerking"
SOAP_GENERATION = "soap_generation"
HANDOFF = "handoff"
class ExperienceLevel(str, Enum):
"""Doctor experience levels"""
JUNIOR = "junior"
MID_LEVEL = "mid-level"
SENIOR = "senior"
ANY = "any"
# ============================================================================
# TYPE DEFINITIONS
# ============================================================================
class DoctorInfo(TypedDict):
"""Doctor information structure"""
id: str
name: str
gender: str
specialty: str
rating: float
years_experience: int
consultation_fee: int
location: str
languages: List[str]
available_slots: List[str]
response_time_avg: str
experience_level: str
class AgentState(TypedDict):
"""State shared across all nodes in the graph"""
messages: Annotated[List, operator.add]
active_node: str
handoff_summary: Optional[str]
clerking_convo: str
soap_summary: Optional[str]
doctor_preferences: Dict[str, Any]
matched_doctor: Optional[DoctorInfo]
awaiting_user_input: bool
conversation_ended: bool
is_doctor_id: bool
#request_doctor_list: bool
#doctor_list: List[DoctorInfo]
selected_doctor: str
language: str
premium: bool
# ============================================================================
# API MODELS
# ============================================================================
class UserMessage(BaseModel):
"""User input structure"""
audio: str = Field("", min_length=0, description = "Base64 encode audio file")
message: str = Field("", min_length=0, description="User's message")
premium: bool = Field(default=False, description="Whether user is a premium user")
language: str = Field(default="english", description="User's preferred language")
class AgentResponse(BaseModel):
"""Agent response structure"""
audio: str = Field("", min_length=0, description="base64 encoded audion")
message: str = Field(..., description="Agent's response message")
doctorid: str = Field(default="", description="Selected doctor ID")
medical_summary: str = Field(default = "", min_length=0, description="Patient's collected medical summary")
class TranslateMessage(BaseModel):
"""User input structure"""
message: str = Field("", min_length=0, description="message to translate")
source_language: str = Field("", min_length=0, description = "source language")
target_language: str = Field(default="english", description="target language")
class TranslateResponse(BaseModel):
"""Agent response structure"""
message: str = Field(default="", description="Agent's response message")
# ============================================================================
# FASTAPI APPLICATION
# ============================================================================
app = FastAPI(
title="MedConnect Agent API",
description="Multi-agent medical assistant system with intelligent routing",
version="3.0.0"
)
# ============================================================================
# CLIENT INITIALIZATION
# ============================================================================
class ClientManager:
"""Manages external API clients"""
def __init__(self):
self.openai_client: Optional[OpenAI] = None
self.translate_client: Optional[translate.TranslationServiceClient] = None
self.parent = f"projects/{GOOGLE_PROJECT_ID}/locations/global"
self._initialize_clients()
self.voice_dict = {
"english": "comfort",
"hausa": "zainab",
"yoruba": "sade",
"igbo": "amara"
}
def _initialize_clients(self):
"""Initialize all external clients"""
# Initialize OpenAI client
try:
self.openai_client = OpenAI(
base_url=RUNPOD_BASE_URL,
api_key="not-needed"
)
logger.info("OpenAI client initialized successfully")
except Exception as e:
logger.error(f"Failed to initialize OpenAI client: {e}")
# Initialize Translation client
try:
self.translate_client = translate.TranslationServiceClient()
logger.info("Translation client initialized successfully")
except Exception as e:
logger.error(f"Failed to initialize translation client: {e}")
# Initialize spitch client
try:
self.spitch_client = Spitch()
logger.info("Spitch client initialized successfully")
except Exception as e:
logger.error(f"Failed to initialize spitch client: {e}")
def translate_text(self, text: str, source_lang: str, target_lang: str) -> str:
"""Translate text between languages"""
if not self.translate_client:
logger.warning("Translation client not available, returning original text")
return text
try:
result = self.translate_client.translate_text(
parent=self.parent,
contents=[text],
source_language_code=source_lang[:2],
target_language_code=target_lang[:2],
mime_type="text/plain"
)
return result.translations[0].translated_text
except Exception as e:
logger.error(f"Translation failed: {e}")
return text
def call_medical_llm(self, system_prompt: str, user_message: str) -> Optional[str]:
"""Call the medical LLM model"""
if not self.openai_client:
logger.error("OpenAI client not available")
return None
try:
response = self.openai_client.chat.completions.create(
model="Agaba-Embedded4/MedConnectAI_Merged",
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_message}
]
)
return response.choices[0].message.content
except Exception as e:
logger.error(f"Medical LLM call failed: {e}")
return None
def extract_text(self, content):
if isinstance(content, str):
return content
if isinstance(content, list):
# Gemini list of parts
return "".join(
part.get("text", "") for part in content if isinstance(part, dict)
)
if isinstance(content, dict):
# Gemini { "parts": [...] }
parts = content.get("parts", [])
return "".join(
part.get("text", "") for part in parts if isinstance(part, dict)
)
return str(content)
# Global client manager
client_manager = ClientManager()
spitch_client = Spitch()
# ============================================================================
# LLM INITIALIZATION
# ============================================================================
def initialize_llm(api_key: str) -> ChatGoogleGenerativeAI:
"""Initialize Gemini model"""
return ChatGoogleGenerativeAI(
model="gemini-2.5-flash-lite",
google_api_key=api_key,
temperature=0.7
)
# ============================================================================
# TOOL AND METHODS DEFINITIONS
# ============================================================================
def get_closest_slot(available_slots):
# Mapping Python weekdays: Monday=0 ... Sunday=6
weekday_map = {
"monday": 0, "tuesday": 1, "wednesday": 2, "thursday": 3,
"friday": 4, "saturday": 5, "sunday": 6
}
now = datetime.now()
closest = None
closest_dt = None
for slot in available_slots:
day_str = slot["day"].lower()
start_time_str = slot["start"]
# Parse time
hour, minute = map(int, start_time_str.split(":"))
slot_weekday = weekday_map[day_str]
# Compute the next date that matches this weekday
today_weekday = now.weekday()
days_ahead = slot_weekday - today_weekday
if days_ahead < 0:
days_ahead += 7 # next week
# Build full datetime
slot_date = now.date() + timedelta(days=days_ahead)
slot_dt = datetime(slot_date.year, slot_date.month, slot_date.day, hour, minute)
# If the slot is today but time has passed → move to next week
if slot_dt < now:
slot_dt += timedelta(days=7)
# Compare to find closest
if closest_dt is None or slot_dt < closest_dt:
closest_dt = slot_dt
closest = slot
return closest
@tool
def orchestrator_handoff(node_to_handoff: str, summary: str) -> Dict[str, str]:
"""
Handoff control from orchestrator to another node.
Args:
node_to_handoff: Target node ("specialist" or "clerking")
summary: Brief summary of the issue for the next node
Returns:
Dictionary with handoff information
"""
logger.info(f"Orchestrator handing off to {node_to_handoff}: {summary}")
return {
"active_node": node_to_handoff,
"handoff_summary": summary
}
@tool
def specialist_handoff(node_to_handoff: str, summary: str) -> Dict[str, str]:
"""
Handoff control from specialist to clerking node.
Args:
node_to_handoff: Target node (typically "clerking")
summary: Brief summary of the medical complaint
Returns:
Dictionary with handoff information
"""
logger.info(f"Specialist handing off to {node_to_handoff}: {summary}")
return {
"active_node": node_to_handoff,
"handoff_summary": summary
}
@tool
def clerking_handoff(node_to_handoff: str, summary: str) -> Dict[str, str]:
"""
Handoff control from clerking to SOAP generation.
Args:
node_to_handoff: Target node (typically "soap_generation")
summary: Brief note about clerking completion
Returns:
Dictionary with handoff information
"""
logger.info(f"Clerking handing off to {node_to_handoff}: {summary}")
return {
"active_node": node_to_handoff,
"handoff_summary": summary
}
@tool
def doctor_search(
specialty: str,
location: str = "Any",
max_price: float = 20000.0,
experience_level: str = "any",
urgent: str = "any",
gender: str = "any"
) -> tuple[List[DoctorInfo], str]:
"""
Search for doctors based on user preferences.
Args:
specialty: Medical specialty needed
location: Preferred location
max_price: Maximum consultation fee
experience_level: Preferred experience level
availability: Availability requirements
gender: Gender preference
Returns:
Tuple of (matching doctors list, message)
"""
# Get doctor list from global state
#doctor_list = global_state.get("doctor_list", [])
language = global_state.get("language", "english")
#try:
# response = requests.get(USERS_ENDPOINT)
# response_data = response.json()
# logger.info("doctor list fetched successfully")
# doctor_list = [doc for doc in response_data if doc["verified"] and doc["role"]=="doctor" ]
# logger.info(doctor_list)
# doctor_list = dummy_doctors
#except requests.exceptions.RequestException as e:
# logger.error(f"Unable to fetch doctor list: {e}")
# doctor_list = []
doctor_list = dummy_doctors
doctor_list = [doc for doc in doctor_list if doc["verified"]]
if not doctor_list:
logger.warning("No verified doctors available in database")
return [], "No doctors available at the moment."
filtered = doctor_list.copy()
criteria_met = 0
today = datetime.today().strftime('%A')
# Apply filters
filters = [
("urgency", lambda d: today.lower() in [available["day"].lower() for available in d["available_slots"]]),
("price", lambda d: d["consultation_fee"] <= max_price),
("language", lambda d: language.lower() in [l.lower() for l in d["languages"]]),
("location", lambda d: location.lower() == "any" or location.lower() in d["location"].lower()),
("gender", lambda d: gender.lower() == "any" or d["gender"].lower() == gender.lower()),
("experience", lambda d: experience_level.lower() == "any" or d["experience_level"].lower() == experience_level.lower()),
]
# Filter by specialty
specialty_filtered = [d for d in filtered if specialty.lower() in d["specialty"].lower()]
if specialty_filtered:
filtered = specialty_filtered
criteria_met += 1
else:
# Fallback to general practitioners
gp_filtered = [d for d in filtered if "general" in d["specialty"].lower()]
if gp_filtered:
filtered = gp_filtered
for filter_name, filter_func in filters[1:] if urgent else filters:
temp_filtered = [d for d in filtered if filter_func(d)]
if temp_filtered:
filtered = temp_filtered
criteria_met += 1
# Sort by rating and experience
filtered.sort(key=lambda x: (-x["rating"], -x["years_experience"], x["consultation_fee"]))
# Determine message based on criteria met
message = "" if criteria_met >= 6 else "Your perfect match wasn't found, but these doctors closely match your preferences:\n\n"
return filtered[:5], message
# ============================================================================
# NODE IMPLEMENTATIONS
# ============================================================================
def controller_node(state: AgentState) -> AgentState:
"""
Routes user input to the appropriate active node.
"""
if not state.get("active_node"):
logger.info("Initializing new conversation")
return {
"active_node": NodeType.ORCHESTRATOR.value, #NodeType.ORCHESTRATOR.value,
}
return {"awaiting_user_input": False}
def orchestrator_node(state: AgentState, llm: ChatGoogleGenerativeAI) -> AgentState:
"""
Receptionist agent - handles greetings and routes to specialists.
"""
language = state.get("language", "english")
system_prompt = f"""You are a professional medical receptionist assistant communicating in {language}.
you work for MedConnect a company that connects patients to doctor (like a medical freelance platform).
CORE RESPONSIBILITIES:
• Warmly greet patients and make them feel comfortable
• Handle general conversations and answer basic non-medical questions
• Identify when to route to medical specialists
• Maintain a professional, empathetic tone
ROUTING RULES:
Use the orchestrator_handoff tool when:
1. MEDICAL QUESTIONS → Route to "specialist"
- Patient asks about medical conditions, symptoms, treatments
- Questions like: "What causes headaches?", "Is aspirin safe?", "What is diabetes?"
2. MEDICAL COMPLAINTS → Route to "clerking"
- Patient describes personal health issues or symptoms
- Statements like: "I have a headache", "My chest hurts", "I've been coughing for 3 days"
HANDOFF FORMAT:
When routing, provide a concise 1-2 sentence summary of the patient's concern.
IMPORTANT GUIDELINES:
• You do NOT provide medical advice or diagnoses
• You are the first point of contact, not the medical expert
• Be warm, professional, and efficient
• Keep responses conversational and concise
Remember: Questions about medical topics = specialist | Personal health complaints = clerking"""
messages = [SystemMessage(content=system_prompt)]
messages.extend(state["messages"][-10:])
llm_with_tools = llm.bind_tools([orchestrator_handoff])
response = llm_with_tools.invoke(messages)
if response.tool_calls:
tool_call = response.tool_calls[0]
handoff_result = orchestrator_handoff.invoke(tool_call["args"])
if not state["premium"]:
return {
"active_node": handoff_result["active_node"],
"awaiting_user_input": False
}
else:
return {
"active_node": "specialist",
"awaiting_user_input": False
}
return {
"messages": [AIMessage(content=client_manager.extract_text(response.content))],
"awaiting_user_input": True
}
def specialist_node(state: AgentState, llm: ChatGoogleGenerativeAI) -> AgentState:
"""
Medical specialist - answers medical questions.
"""
language = state.get("language", "english")
user_message = state["messages"][-1].content
# Build conversation history
history = ""
for msg in state["messages"][:-1]:
role = "DOCTOR" if isinstance(msg, AIMessage) else "PATIENT"
history += f"{role}: {msg.content}\n"
# Translate history if needed
if language != "english":
history = client_manager.translate_text(history, language, "english")
# Try using specialized medical model
medical_response = None
if client_manager.openai_client:
system_prompt = f"""You are an expert medical doctor. Provide accurate, evidence-based answers.
CONVERSATION HISTORY:
{history}
GUIDELINES:
• Answer clearly and concisely
• Use simple language, avoid excessive jargon
• Provide actionable information
• Be empathetic and supportive"""
medical_response = client_manager.call_medical_llm(system_prompt, user_message)
if medical_response and language != "english":
medical_response = client_manager.translate_text(medical_response, "english", language)
# Fallback to Gemini if medical model unavailable
if state["premium"] or not medical_response:
if state["premium"]:
system_prompt = f"""You are an expert medical specialist communicating in {language}.
CORE RESPONSIBILITIES:
• Answer medical questions with accurate, evidence-based information
• Explain conditions, medications, and treatments clearly
• Detect when questions become personal health complaints
QUESTION vs COMPLAINT:
• Question: "What causes migraines?" "How does insulin work?" "What is hypertension?"
• Complaint: "I have a migraine" "My blood sugar is high" "My pressure is elevated"
HANDOFF RULE:
Use specialist_handoff to route to "clerking" when:
• Patient describes personal symptoms
• Patient asks what to do about their own symptoms
• Conversation shifts from general info to personal health concerns
RESPONSE GUIDELINES:
• Be thorough but concise
• Use plain language
• Always disclaim: "This is general information, not personal medical advice"
• Be empathetic and supportive
• If uncertain, recommend professional consultation
Remember: Answer questions, don't diagnose. Personal symptoms require clerking."""
else:
system_prompt = f"""You are an expert medical specialist communicating in {language}.
CORE RESPONSIBILITIES:
• Answer medical questions with accurate, evidence-based information
• Explain conditions, medications, and treatments clearly
• Make no direct prescription or diagnosis or presciptions, only give medical tips
RESPONSE GUIDELINES:
• Be thorough but concise
• Use plain language
• Always disclaim: "This is general information, not personal medical advice"
• Be empathetic and supportive
• If uncertain, recommend professional consultation
Remember: Answer questions, don't diagnose and call no tool."""
messages = [SystemMessage(content=system_prompt)]
messages.extend(state["messages"][-10:])
llm_with_tools = llm.bind_tools([specialist_handoff])
response = llm_with_tools.invoke(messages)
if response.tool_calls and state["premium"]:
tool_call = response.tool_calls[0]
handoff_result = specialist_handoff.invoke(tool_call["args"])
return {
"active_node": handoff_result["active_node"],
"awaiting_user_input": False
}
if not medical_response:
medical_response = client_manager.extract_text(response.content)
return {
"messages": [AIMessage(content=medical_response)],
"awaiting_user_input": True
}
def clerking_node(state: AgentState, llm: ChatGoogleGenerativeAI) -> AgentState:
"""
Medical history collector - systematically clerks patient.
"""
language = state.get("language", "english")
system_prompt = f"""You are a medical history collection specialist communicating in {language}.
CORE RESPONSIBILITY:
Systematically collect comprehensive medical history following standard medical clerking structure.
CLERKING FRAMEWORK (Follow in order):
1. CHIEF COMPLAINT (usually already provided)
- Confirm the main problem
2. HISTORY OF PRESENT ILLNESS
- Onset: When did it start?
- Duration: How long has it lasted?
- Severity: Rate 1-10?
- Character: Describe the sensation (sharp, dull, throbbing, etc.)
- Aggravating/Relieving factors: What makes it worse or better?
- Associated symptoms: Any other symptoms?
3. PAST MEDICAL HISTORY
- Chronic conditions (diabetes, hypertension, asthma, etc.)
- Previous hospitalizations or surgeries
4. MEDICATIONS & ALLERGIES
- Current medications, supplements
- Known drug allergies
5. SOCIAL HISTORY (brief)
- Smoking/alcohol use
- Occupation
- Recent travel
6. REVIEW OF SYSTEMS (if relevant)
- Any other symptoms in other body systems
CONVERSATION STYLE:
• Ask 1-2 focused questions at a time
• Be empathetic and reassuring
• Acknowledge patient concerns
• Use simple, clear language
• Build rapport and trust
COMPLETION CRITERIA:
When you have comprehensive information covering the above areas, use clerking_handoff to move to "soap_generation" with summary: "Clerking completed, comprehensive history obtained"
IMPORTANT:
• Be thorough but efficient
• Focus on GATHERING information, not providing advice
• Do not diagnose during clerking
• Every question should have clear clinical purpose"""
messages = [SystemMessage(content=system_prompt)]
if state.get("clerking_convo"):
messages.append(SystemMessage(content=f"Clerking progress:\n{state['clerking_convo']}"))
messages.extend(state["messages"][-5:])
llm_with_tools = llm.bind_tools([clerking_handoff])
response = llm_with_tools.invoke(messages)
# Accumulate conversation
last_user_msg = state["messages"][-1].content if state["messages"] else ""
clerking_addition = f"\nPatient: {last_user_msg}\nDoctor: {response.content}\n"
if response.tool_calls:
tool_call = response.tool_calls[0]
handoff_result = clerking_handoff.invoke(tool_call["args"])
return {
"clerking_convo": state.get("clerking_convo", "") + clerking_addition,
"active_node": handoff_result["active_node"],
"awaiting_user_input": False
}
return {
"messages": [AIMessage(content=client_manager.extract_text(response.content))],
"clerking_convo": state.get("clerking_convo", "") + clerking_addition,
"awaiting_user_input": True
}
def soap_generation_node(state: AgentState, llm: ChatGoogleGenerativeAI) -> AgentState:
"""
Generates SOAP note from clerking conversation.
"""
clerking_data = state.get("clerking_convo", "")
language = state.get("language", "english")
# Translate to English if needed
if language != "english":
clerking_data = client_manager.translate_text(clerking_data, language, "english")
logger.info(f"Generating SOAP note from clerking data (length: {len(clerking_data)})")
# Try medical model first
soap_summary = None
if client_manager.openai_client:
system_prompt = """You are a medical documentation specialist. Create a professional SOAP note.
FORMAT:
S (Subjective): Patient's reported symptoms, chief complaint, and relevant history
O (Objective): Vital signs, lab results, physical findings (use placeholders if not provided)
A (Assessment): Clinical assessment, primary diagnosis, differential diagnoses
P (Plan): Management plan including medications, consultations, patient education
GUIDELINES:
• Base content solely on the provided transcript
• Use standard medical terminology and abbreviations
• Be concise and clinically relevant
• Format as plain text: "S:", "O:", "A:", "P:" without markdown
• Include only information explicitly stated or clearly implied"""
soap_summary = client_manager.call_medical_llm(system_prompt, clerking_data)
# Fallback to Gemini
if not soap_summary:
soap_prompt = f"""Generate a professional SOAP note from this patient-doctor interaction:
{clerking_data}
Create a Medical SOAP note following these guidelines:
S (Subjective): Summarize patient's reported symptoms, chief complaint, relevant history
O (Objective): Document vital signs, findings, examination results (note if limited info available)
A (Assessment): Provide clinical assessment, primary diagnosis, differential diagnoses
P (Plan): Outline management including medications, referrals, patient education
Format as plain text without markdown. Use "S:", "O:", "A:", "P:" labels directly.
Be concise, professional, and use standard medical terminology."""
response = llm.invoke([HumanMessage(content=soap_prompt)])
soap_summary = client_manager.extract_text(response.content).strip()
logger.info("SOAP note generated successfully")
return {
"soap_summary": soap_summary,
"active_node": NodeType.HANDOFF.value,
"awaiting_user_input": False,
"request_doctor_list": True
}
def handoff_node(state: AgentState, llm: ChatGoogleGenerativeAI) -> AgentState:
"""
Doctor matching and handoff coordinator.
"""
language = state.get("language", "english")
last_msg = state["messages"][-1].content.lower()
# Translate for analysis if needed
if language != "english":
last_msg = client_manager.translate_text(last_msg, language, "english").lower()
# Check for doctor selection
search_results = state.get("doctor_preferences", {}).get("search_results", [])
if search_results:
selection_keywords = ["select", "choose", "pick", "first", "second", "third", "fourth", "fifth", "1", "2", "3", "4", "5" "dr", "doctor", "one", "two", "three", "four", "five"]
if any(word in last_msg for word in selection_keywords):
selected_doctor = None
# Try to identify selected doctor
for i, doc in enumerate(search_results):
if str(i + 1) in last_msg or doc['name'].lower() in last_msg:
selected_doctor = doc
break
if not selected_doctor:
selected_doctor = search_results[0]
confirmation_msg = f"Perfect! I'll connect you with **{selected_doctor['name']}** ({selected_doctor['specialty']}). They will receive your medical summary and contact you at: {get_closest_slot(selected_doctor['available_slots'])["start"] + ' on ' + get_closest_slot(selected_doctor['available_slots'])["day"]}.\n\n Is there anything else you'd like to know?" # pyright: ignore[reportOptionalSubscript]
if language != "english":
confirmation_msg = client_manager.translate_text(confirmation_msg, "english", language)
return {
"messages": [AIMessage(content=confirmation_msg)],
"matched_doctor": selected_doctor,
"selected_doctor": selected_doctor['id'],
"is_doctor_id": True,
"awaiting_user_input": False,
"conversation_ended": True
}
# Continue doctor matching conversation
system_prompt = f"""You are a patient-doctor matching coordinator communicating in {language}.
CORE RESPONSIBILITIES:
• Help patients find the right doctor for their medical needs
• Gather preferences systematically
• Use doctor_search tool to find matching doctors
• Present options clearly and professionally in natural language
• Don't insist on user to make entry, use default for requirements the user intentionally left empty
CONVERSATION FLOW:
1. ACKNOWLEDGE MEDICAL SITUATION
If SOAP summary exists, acknowledge understanding of their condition
2. GATHER PREFERENCES (ask naturally, not as a form):
- Specialty needed (infer from SOAP note if available)
- Preferred location (state in Nigeria)
- Budget/consultation fee range
- Experience level preference (junior/mid-level/senior)
- Gender preference (if any)
- urgent (if situation is urgent yes or no)
3. SEARCH FOR DOCTORS
Once you have key preferences, use doctor_search tool with:
• specialty: Inferred from SOAP recommendations
• location: User's preference (default "Any")
• max_price: Budget (default 50000)
• experience_level: Preference (default "any")
• urgent: Urgency (default False)
• gender: Preference (default "any")
4. PRESENT OPTIONS
After search, present doctors clearly with all relevant details
HOW TO COLLECT PREFERENCES
• show user list of required preferences
• Ask them to enter values for the ones that matters to them
• Inform user to leave blank the one that does not matter
• Use defualt values for preferences user left empty
• Don't insist on user to make entry, use default for empty requirements
IMPORTANT GUIDELINES:
• Be patient and helpful
• Respect budget constraints
• Don't push expensive options
• Provide multiple options when available
• Be transparent about qualifications
• Ask clarifying questions if preferences unclear
Remember: Your goal is finding the best match for the patient's needs and preferences."""
messages = [SystemMessage(content=system_prompt)]
if state.get("soap_summary"):
messages.append(SystemMessage(content=f"Patient's Medical Summary:\n{state['soap_summary']}"))
if state.get("doctor_preferences"):
messages.append(SystemMessage(content=f"Gathered preferences: {json.dumps(state['doctor_preferences'])}"))
messages.extend(state["messages"][-10:])
llm_with_tools = llm.bind_tools([doctor_search])
response = llm_with_tools.invoke(messages)
if response.tool_calls:
tool_call = response.tool_calls[0]
doctors, search_message = doctor_search.invoke(tool_call["args"])
if doctors:
doctors_text = f"\n\n{search_message}**Available Doctors:**\n\n"
for i, doc in enumerate(doctors, 1):
doctors_text += f"""**{i}. {doc['name']}** - {doc['specialty']}
⭐ Rating: {doc['rating']}/5.0 | 📅 {doc['years_experience']} years experience
💰 Consultation Fee: ₦{doc['consultation_fee']:,}
📍 Location: {doc['location']}
🗣️ Languages: {', '.join(doc['languages'])}
⏰ Available: {', '.join([available["day"] + " " + available["start"] for available in doc['available_slots']])}
⚡ Avg Response Time: {doc['response_time_avg']} minutes
"""
doctors_text += "\nWhich doctor would you prefer? You can choose by number or name."
full_response = client_manager.extract_text(response.content) + doctors_text
if language != "english":
full_response = client_manager.translate_text(full_response, "english", language)
return {
"messages": [AIMessage(content=full_response)],
"doctor_preferences": {
**state.get("doctor_preferences", {}),
"search_results": doctors
},
"awaiting_user_input": True
}
else:
no_match_msg = "I couldn't find doctors matching those exact criteria. Could you adjust your preferences? For example, try a different location or higher budget?"
if language != "english":
no_match_msg = client_manager.translate_text(no_match_msg, "english", language)
return {
"messages": [AIMessage(content=no_match_msg)],
"awaiting_user_input": True
}
return {
"messages": [AIMessage(content=client_manager.extract_text(response.content))],
"awaiting_user_input": True
}
# ============================================================================
# ROUTING FUNCTIONS
# ============================================================================
def route_from_controller(state: AgentState) -> str:
"""Route from controller to active node"""
active = state.get("active_node", NodeType.ORCHESTRATOR.value)
return active
def route_from_orchestrator(state: AgentState) -> str:
"""Route from orchestrator"""
if state.get("awaiting_user_input"):
return "end"
active = state.get("active_node", NodeType.ORCHESTRATOR.value)
if active in [NodeType.SPECIALIST.value, NodeType.CLERKING.value]:
return active
return "end"
def route_from_specialist(state: AgentState) -> str:
"""Route from specialist"""
if state.get("awaiting_user_input"):
return "end"
if state.get("active_node") == NodeType.CLERKING.value:
return NodeType.CLERKING.value
return "end"
def route_from_clerking(state: AgentState) -> str:
"""Route from clerking"""
if state.get("awaiting_user_input"):
return "end"
if state.get("active_node") == NodeType.SOAP_GENERATION.value:
return NodeType.SOAP_GENERATION.value
return "end"
# ============================================================================
# GRAPH CONSTRUCTION
# ============================================================================
def create_medical_assistant_graph(api_key: str) -> StateGraph:
"""Creates the complete LangGraph workflow"""
llm = initialize_llm(api_key)
workflow = StateGraph(AgentState)
# Add nodes
workflow.add_node("controller", controller_node)
workflow.add_node("orchestrator", lambda state: orchestrator_node(state, llm)) # pyright: ignore[reportArgumentType]
workflow.add_node("specialist", lambda state: specialist_node(state, llm)) # pyright: ignore[reportArgumentType]
workflow.add_node("clerking", lambda state: clerking_node(state, llm)) # pyright: ignore[reportArgumentType]
workflow.add_node("soap_generation", lambda state: soap_generation_node(state, llm))
workflow.add_node("handoff", lambda state: handoff_node(state, llm))
# Set entry point
workflow.set_entry_point("controller")
# Define edges
workflow.add_conditional_edges(
"controller",
route_from_controller,
{
NodeType.ORCHESTRATOR.value: "orchestrator",
NodeType.SPECIALIST.value: "specialist",
NodeType.CLERKING.value: "clerking",
NodeType.HANDOFF.value: "handoff"
}
)
workflow.add_conditional_edges(
"orchestrator",