File size: 9,631 Bytes
f1cd1c6 86911ce e22139a 86911ce e22139a 86911ce e22139a a1dbeb2 e22139a 5fb0df4 e22139a 5fb0df4 e22139a 5fb0df4 e22139a 5fb0df4 e22139a 5fb0df4 e22139a 5fb0df4 e22139a 5fb0df4 e22139a 5fb0df4 e22139a 5fb0df4 e22139a 5fb0df4 e22139a 5fb0df4 e22139a 5fb0df4 e22139a 5fb0df4 e22139a 5fb0df4 e22139a 5fb0df4 e22139a 5fb0df4 e22139a 5fb0df4 e22139a 5fb0df4 e22139a a1dbeb2 5fb0df4 e22139a 5fb0df4 e22139a 5fb0df4 e22139a 5fb0df4 e7e593a 5fb0df4 |
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 |
import os
import re
import json
import logging
import traceback
from functools import lru_cache
from typing import List, Dict, Any, Optional, TypedDict
import requests
from langchain_groq import ChatGroq
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_core.messages import HumanMessage, SystemMessage, AIMessage, ToolMessage
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_core.tools import tool
from langgraph.prebuilt import ToolExecutor
from langgraph.graph import StateGraph, END
# ββ Logging Configuration ββββββββββββββββββββββββββββββββββββββββββββββ
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
# ββ Environment Variables ββββββββββββββββββββββββββββββββββββββββββββββ
UMLS_API_KEY = os.getenv("UMLS_API_KEY")
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")
if not all([UMLS_API_KEY, GROQ_API_KEY, TAVILY_API_KEY]):
logger.error("Missing required API keys")
raise RuntimeError("Missing API keys")
# ββ Agent Configuration ββββββββββββββββββββββββββββββββββββββββββββββ
class ClinicalPrompts:
SYSTEM_PROMPT = """
You are SynapseAI, an expert AI clinical assistant engaged in an interactive consultation...
[SYSTEM PROMPT CONTENT HERE]
"""
MAX_ITERATIONS = 4
AGENT_MODEL_NAME = "llama3-70b-8192"
AGENT_TEMPERATURE = 0.1
# ββ State Definition βββββββββββββββββββββββββββββββββββββββββββββββββ
class AgentState(TypedDict):
messages: List[Any]
patient_data: Optional[Dict[str, Any]]
summary: Optional[str]
interaction_warnings: Optional[List[str]]
done: bool
iterations: int
def propagate_state(new: Dict[str, Any], old: Dict[str, Any]) -> Dict[str, Any]:
"""Merge new state changes with existing state"""
return {**old, **new}
# ββ Core Agent Node ββββββββββββββββββββββββββββββββββββββββββββββββββ
def agent_node(state: AgentState) -> Dict[str, Any]:
"""Main agent node with iteration tracking"""
state = dict(state) # Create mutable copy
# Check termination conditions
if state.get("done", False):
return state
# Update iteration count
iterations = state.get("iterations", 0) + 1
state["iterations"] = iterations
# Enforce iteration limit
if iterations >= MAX_ITERATIONS:
return {
"messages": [AIMessage(content="Consultation concluded. Maximum iterations reached.")],
"done": True,
**state
}
# Prepare message history
messages = state.get("messages", [])
if not messages or not isinstance(messages[0], SystemMessage):
messages = [SystemMessage(content=ClinicalPrompts.SYSTEM_PROMPT)] + messages
try:
# Generate response
llm_response = ChatGroq(
temperature=AGENT_TEMPERATURE,
model=AGENT_MODEL_NAME
).invoke(messages)
return propagate_state({
"messages": [llm_response],
"done": "consultation complete" in llm_response.content.lower()
}, state)
except Exception as e:
logger.error(f"Agent error: {str(e)}")
return propagate_state({
"messages": [AIMessage(content=f"System Error: {str(e)}")],
"done": True
}, state)
# ββ Tool Handling Nodes ββββββββββββββββββββββββββββββββββββββββββββββ
tool_executor = ToolExecutor([
TavilySearchResults(max_results=3),
# Include other tools here...
])
def tool_node(state: AgentState) -> Dict[str, Any]:
"""Execute tool calls from last agent message"""
state = dict(state)
messages = state["messages"]
last_message = messages[-1]
if not isinstance(last_message, AIMessage) or not last_message.tool_calls:
return state
tool_calls = last_message.tool_calls
outputs = []
for tool_call in tool_calls:
try:
output = tool_executor.invoke(tool_call)
outputs.append(
ToolMessage(
content=json.dumps(output),
tool_call_id=tool_call["id"],
name=tool_call["name"]
)
)
except Exception as e:
logger.error(f"Tool error: {str(e)}")
outputs.append(
ToolMessage(
content=json.dumps({"error": str(e)}),
tool_call_id=tool_call["id"],
name=tool_call["name"]
)
)
return propagate_state({
"messages": outputs,
"interaction_warnings": detect_interaction_warnings(outputs)
}, state)
def detect_interaction_warnings(tool_messages: List[ToolMessage]) -> List[str]:
"""Parse tool outputs for interaction warnings"""
warnings = []
for msg in tool_messages:
try:
content = json.loads(msg.content)
if content.get("status") == "warning":
warnings.extend(content.get("warnings", []))
except json.JSONDecodeError:
continue
return warnings
# ββ Safety Reflection Node βββββββββββββββββββββββββββββββββββββββββββ
def reflection_node(state: AgentState) -> Dict[str, Any]:
"""Analyze potential safety issues"""
warnings = state.get("interaction_warnings", [])
if not warnings:
return state
prompt = f"""Analyze these clinical warnings:
{chr(10).join(warnings)}
Provide concise safety recommendations:"""
try:
reflection = ChatGroq(
temperature=0.0, # Strict safety mode
model=AGENT_MODEL_NAME
).invoke([HumanMessage(content=prompt)])
return propagate_state({
"messages": [reflection],
"summary": f"Safety Review:\n{reflection.content}"
}, state)
except Exception as e:
logger.error(f"Reflection error: {str(e)}")
return propagate_state({
"messages": [AIMessage(content=f"Safety review unavailable: {str(e)}")],
"summary": "Failed safety review"
}, state)
# ββ State Routing Logic ββββββββββββββββββββββββββββββββββββββββββββββ
def route_state(state: AgentState) -> str:
"""Determine next node in workflow"""
if state.get("done", False):
return "end"
messages = state.get("messages", [])
# Prioritize safety reflection
if state.get("interaction_warnings"):
return "reflection"
# Check for tool calls
if messages and isinstance(messages[-1], AIMessage):
if messages[-1].tool_calls:
return "tools"
return "agent"
# ββ Workflow Construction ββββββββββββββββββββββββββββββββββββββββββββ
class ClinicalAgent:
def __init__(self):
self.workflow = StateGraph(AgentState)
# Define nodes
self.workflow.add_node("agent", agent_node)
self.workflow.add_node("tools", tool_node)
self.workflow.add_node("reflection", reflection_node)
# Configure edges
self.workflow.set_entry_point("agent")
self.workflow.add_conditional_edges(
"agent",
lambda state: "tools" if state.get("messages")[-1].tool_calls else "end",
{"tools": "tools", "end": END}
)
self.workflow.add_conditional_edges(
"tools",
lambda state: "reflection" if state.get("interaction_warnings") else "agent",
{"reflection": "reflection", "agent": "agent"}
)
self.workflow.add_edge("reflection", "agent")
self.app = self.workflow.compile()
def consult(self, initial_state: Dict) -> Dict:
"""Execute full consultation workflow"""
try:
return self.app.invoke(
initial_state,
{"recursion_limit": MAX_ITERATIONS + 2}
)
except Exception as e:
logger.error(f"Consultation failed: {str(e)}")
return {
"error": str(e),
"trace": traceback.format_exc(),
"done": True
}
# ββ Example Usage ββββββββββββββββββββββββββββββββββββββββββββββββββββ
if __name__ == "__main__":
agent = ClinicalAgent()
initial_state = {
"messages": [HumanMessage(content="Patient presents with chest pain")],
"patient_data": {
"age": 45,
"vitals": {"bp": "150/95", "hr": 110}
},
"done": False,
"iterations": 0
}
result = agent.consult(initial_state)
print("Final State:", json.dumps(result, indent=2)) |