Spaces:
Sleeping
Sleeping
priyansh-saxena1 commited on
Commit ·
0b46033
1
Parent(s): 03af64f
fix: emergency issue
Browse files- app/graph.py +6 -0
- app/llm.py +1 -1
- app/main.py +12 -0
app/graph.py
CHANGED
|
@@ -112,9 +112,15 @@ def agent_node(state: IntakeState) -> dict:
|
|
| 112 |
current_json = state.get("clinical_state") or CombinedOutput().model_dump_json()
|
| 113 |
transcript = format_transcript(msgs)
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
llm = get_llm()
|
| 116 |
result: CombinedOutput = llm.combined_call(transcript, current_json)
|
| 117 |
|
|
|
|
|
|
|
| 118 |
if result.emergency:
|
| 119 |
return {
|
| 120 |
"messages": [{"role": "assistant", "content": (
|
|
|
|
| 112 |
current_json = state.get("clinical_state") or CombinedOutput().model_dump_json()
|
| 113 |
transcript = format_transcript(msgs)
|
| 114 |
|
| 115 |
+
import time
|
| 116 |
+
t_agent = time.time()
|
| 117 |
+
print(f"[{time.time():.3f}] [Graph Node] Requesting LLM inference...")
|
| 118 |
+
|
| 119 |
llm = get_llm()
|
| 120 |
result: CombinedOutput = llm.combined_call(transcript, current_json)
|
| 121 |
|
| 122 |
+
print(f"[{time.time():.3f}] [Graph Node] LLM returned. Preparing node dictionaries...")
|
| 123 |
+
|
| 124 |
if result.emergency:
|
| 125 |
return {
|
| 126 |
"messages": [{"role": "assistant", "content": (
|
app/llm.py
CHANGED
|
@@ -14,7 +14,7 @@ CRITICAL RULES:
|
|
| 14 |
- Do NOT diagnose or give medical advice.
|
| 15 |
- Do NOT ask more than one question.
|
| 16 |
- If all fields are complete, set reply to "Thank you — I have everything I need."
|
| 17 |
-
-
|
| 18 |
|
| 19 |
OUTPUT FORMAT (strictly follow this, no extra text):
|
| 20 |
{
|
|
|
|
| 14 |
- Do NOT diagnose or give medical advice.
|
| 15 |
- Do NOT ask more than one question.
|
| 16 |
- If all fields are complete, set reply to "Thank you — I have everything I need."
|
| 17 |
+
- EMERGENCY RULE: Set emergency=true ONLY IF the patient explicitly describes an acute life-threatening event right now (e.g., EXACT words "crushing chest pain", "I can't breathe right now", "I want to kill myself"). Do NOT set emergency=true for generic "chest pain", "headache", or past symptoms. Default is always false.
|
| 18 |
|
| 19 |
OUTPUT FORMAT (strictly follow this, no extra text):
|
| 20 |
{
|
app/main.py
CHANGED
|
@@ -82,26 +82,38 @@ async def health():
|
|
| 82 |
|
| 83 |
@app.post("/chat", response_model=ChatResponse)
|
| 84 |
async def chat(request: ChatRequest):
|
|
|
|
|
|
|
|
|
|
| 85 |
config = {"configurable": {"thread_id": request.session_id}}
|
| 86 |
|
| 87 |
# Get current checkpoint state
|
| 88 |
snapshot = graph.get_state(config)
|
|
|
|
| 89 |
|
| 90 |
# Check if graph is interrupted and waiting for input
|
|
|
|
| 91 |
if snapshot.next:
|
|
|
|
| 92 |
# First update state with the user message
|
| 93 |
graph.update_state(config, {"messages": [{"role": "user", "content": request.message}]})
|
| 94 |
# Then resume execution
|
| 95 |
result = graph.invoke(None, config=config)
|
| 96 |
else:
|
|
|
|
| 97 |
# New conversation - start fresh
|
| 98 |
input_state = {"messages": [{"role": "user", "content": request.message}]}
|
| 99 |
result = graph.invoke(input_state, config=config)
|
|
|
|
| 100 |
|
|
|
|
| 101 |
current_node = get_current_node(request.session_id)
|
| 102 |
reply = get_last_reply(request.session_id)
|
| 103 |
brief_dict = get_brief(request.session_id)
|
| 104 |
|
|
|
|
|
|
|
|
|
|
| 105 |
return ChatResponse(reply=reply, state=current_node, brief=brief_dict)
|
| 106 |
|
| 107 |
|
|
|
|
| 82 |
|
| 83 |
@app.post("/chat", response_model=ChatResponse)
|
| 84 |
async def chat(request: ChatRequest):
|
| 85 |
+
import time
|
| 86 |
+
t0 = time.time()
|
| 87 |
+
print(f"\n[{t0:.3f}] [API] -> POST /chat received for {request.session_id}")
|
| 88 |
config = {"configurable": {"thread_id": request.session_id}}
|
| 89 |
|
| 90 |
# Get current checkpoint state
|
| 91 |
snapshot = graph.get_state(config)
|
| 92 |
+
print(f"[{time.time():.3f}] [API] Read existing state snapshot.")
|
| 93 |
|
| 94 |
# Check if graph is interrupted and waiting for input
|
| 95 |
+
t_start_graph = time.time()
|
| 96 |
if snapshot.next:
|
| 97 |
+
print(f"[{time.time():.3f}] [API] Resuming graph from interrupt...")
|
| 98 |
# First update state with the user message
|
| 99 |
graph.update_state(config, {"messages": [{"role": "user", "content": request.message}]})
|
| 100 |
# Then resume execution
|
| 101 |
result = graph.invoke(None, config=config)
|
| 102 |
else:
|
| 103 |
+
print(f"[{time.time():.3f}] [API] Starting new graph invoke...")
|
| 104 |
# New conversation - start fresh
|
| 105 |
input_state = {"messages": [{"role": "user", "content": request.message}]}
|
| 106 |
result = graph.invoke(input_state, config=config)
|
| 107 |
+
print(f"[{time.time():.3f}] [API] <- Graph invoke returned in {time.time() - t_start_graph:.2f}s")
|
| 108 |
|
| 109 |
+
t_final = time.time()
|
| 110 |
current_node = get_current_node(request.session_id)
|
| 111 |
reply = get_last_reply(request.session_id)
|
| 112 |
brief_dict = get_brief(request.session_id)
|
| 113 |
|
| 114 |
+
total_t = time.time() - t0
|
| 115 |
+
print(f"[{time.time():.3f}] [API] Chat completed in {total_t:.2f}s total. Reply length: {len(reply)}")
|
| 116 |
+
|
| 117 |
return ChatResponse(reply=reply, state=current_node, brief=brief_dict)
|
| 118 |
|
| 119 |
|