Spaces:
Sleeping
Sleeping
Upload agent.py
Browse files- api/agent.py +38 -3
api/agent.py
CHANGED
|
@@ -498,7 +498,42 @@ def chat(session_id: str, user_message: str, history: List[Dict]) -> str:
|
|
| 498 |
result = g.invoke(state)
|
| 499 |
|
| 500 |
# Extract final text response
|
|
|
|
| 501 |
final_msg = result["messages"][-1]
|
| 502 |
-
if hasattr(final_msg, "content")
|
| 503 |
-
|
| 504 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 498 |
result = g.invoke(state)
|
| 499 |
|
| 500 |
# Extract final text response
|
| 501 |
+
# Handle both native tool_calls (Ollama) and text-embedded JSON (HF Inference API)
|
| 502 |
final_msg = result["messages"][-1]
|
| 503 |
+
content = final_msg.content if hasattr(final_msg, "content") else str(final_msg)
|
| 504 |
+
|
| 505 |
+
# If HF returned a raw tool-call JSON block instead of prose, execute it and re-invoke
|
| 506 |
+
import json as _json, re as _re
|
| 507 |
+
tool_json_match = _re.search(
|
| 508 |
+
r'[{]"type"\s*:\s*"function".*?"arguments"\s*:\s*([{].*?[}])\s*[}]',
|
| 509 |
+
content, _re.DOTALL
|
| 510 |
+
)
|
| 511 |
+
if tool_json_match:
|
| 512 |
+
try:
|
| 513 |
+
outer = _json.loads(tool_json_match.group(0))
|
| 514 |
+
tool_name = outer.get("name", "")
|
| 515 |
+
args = outer.get("arguments", {})
|
| 516 |
+
# Execute the tool directly
|
| 517 |
+
tool_result = ""
|
| 518 |
+
if tool_name == "classify_symptoms":
|
| 519 |
+
tool_result = classify_symptoms.invoke(args)
|
| 520 |
+
elif tool_name == "search_knowledge":
|
| 521 |
+
tool_result = search_knowledge.invoke(args)
|
| 522 |
+
elif tool_name == "get_workup_recommendations":
|
| 523 |
+
tool_result = get_workup_recommendations.invoke(args)
|
| 524 |
+
|
| 525 |
+
if tool_result:
|
| 526 |
+
# Re-invoke LLM with the tool result so it generates a proper response
|
| 527 |
+
from langchain_core.messages import ToolMessage
|
| 528 |
+
followup_messages = (
|
| 529 |
+
[SystemMessage(content=SYSTEM_PROMPT)]
|
| 530 |
+
+ lc_history
|
| 531 |
+
+ [AM(content=content)]
|
| 532 |
+
+ [HM(content="Tool result for " + tool_name + ":\n" + str(tool_result) + "\n\nNow give the patient a clear clinical response based on this.")]
|
| 533 |
+
)
|
| 534 |
+
final_response = llm.invoke(followup_messages)
|
| 535 |
+
return final_response.content if hasattr(final_response, "content") else str(final_response)
|
| 536 |
+
except Exception as e:
|
| 537 |
+
logger.warning("[agent] Tool JSON fallback failed: {}", e)
|
| 538 |
+
|
| 539 |
+
return content
|