Spaces:
Runtime error
Runtime error
Update agents/executor.py
Browse files- agents/executor.py +59 -48
agents/executor.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
"""
|
| 2 |
Executor agent for pharmaceutical data management.
|
| 3 |
This agent executes SQL queries and reports on the results.
|
|
@@ -25,7 +26,7 @@ def executor_agent(anthropic_client, db, state: Dict[str, Any]) -> Dict[str, Any
|
|
| 25 |
messages = state.get("messages", [])
|
| 26 |
sql_queries = state.get("sql_queries", [])
|
| 27 |
|
| 28 |
-
# Add agent-specific instructions
|
| 29 |
system_message = """
|
| 30 |
You are an AI assistant specializing in executing and validating pharmaceutical data pipelines.
|
| 31 |
Your job is to execute the SQL queries and report on the results.
|
|
@@ -47,65 +48,75 @@ def executor_agent(anthropic_client, db, state: Dict[str, Any]) -> Dict[str, Any
|
|
| 47 |
# Prepare context for Claude
|
| 48 |
execution_context = json.dumps(execution_results, indent=2)
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
|
| 52 |
-
anthropic_messages = []
|
| 53 |
for msg in messages:
|
| 54 |
-
if msg
|
| 55 |
-
|
| 56 |
role=msg["role"],
|
| 57 |
content=msg["content"]
|
| 58 |
))
|
| 59 |
|
| 60 |
# Add final user message with context
|
| 61 |
-
|
| 62 |
MessageParam(
|
| 63 |
role="user",
|
| 64 |
content=f"Here are the execution results of the SQL queries. Please analyze and report on them.\n\n{execution_context}"
|
| 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 |
def _execute_queries(db, sql_queries: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
| 111 |
"""
|
|
|
|
| 1 |
+
# executor_agent.py - Fixed version
|
| 2 |
"""
|
| 3 |
Executor agent for pharmaceutical data management.
|
| 4 |
This agent executes SQL queries and reports on the results.
|
|
|
|
| 26 |
messages = state.get("messages", [])
|
| 27 |
sql_queries = state.get("sql_queries", [])
|
| 28 |
|
| 29 |
+
# Add agent-specific instructions
|
| 30 |
system_message = """
|
| 31 |
You are an AI assistant specializing in executing and validating pharmaceutical data pipelines.
|
| 32 |
Your job is to execute the SQL queries and report on the results.
|
|
|
|
| 48 |
# Prepare context for Claude
|
| 49 |
execution_context = json.dumps(execution_results, indent=2)
|
| 50 |
|
| 51 |
+
# Format messages for the Anthropic API
|
| 52 |
+
formatted_messages = []
|
|
|
|
| 53 |
for msg in messages:
|
| 54 |
+
if isinstance(msg, dict) and "role" in msg and "content" in msg:
|
| 55 |
+
formatted_messages.append(MessageParam(
|
| 56 |
role=msg["role"],
|
| 57 |
content=msg["content"]
|
| 58 |
))
|
| 59 |
|
| 60 |
# Add final user message with context
|
| 61 |
+
formatted_messages.append(
|
| 62 |
MessageParam(
|
| 63 |
role="user",
|
| 64 |
content=f"Here are the execution results of the SQL queries. Please analyze and report on them.\n\n{execution_context}"
|
| 65 |
)
|
| 66 |
)
|
| 67 |
|
| 68 |
+
try:
|
| 69 |
+
# Call Claude API
|
| 70 |
+
response = anthropic_client.messages.create(
|
| 71 |
+
model="claude-3-7-sonnet-20250219",
|
| 72 |
+
system=system_message,
|
| 73 |
+
messages=formatted_messages,
|
| 74 |
+
max_tokens=2000
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
# Extract the response
|
| 78 |
+
agent_response = response.content[0].text
|
| 79 |
+
|
| 80 |
+
# Create a new state instead of modifying the original
|
| 81 |
+
new_state = state.copy()
|
| 82 |
+
|
| 83 |
+
# Add agent's response to messages
|
| 84 |
+
new_message = {"role": "assistant", "content": agent_response.replace("EXECUTION_COMPLETE", "").strip()}
|
| 85 |
+
new_state["messages"] = messages + [new_message]
|
| 86 |
+
|
| 87 |
+
# Update execution results
|
| 88 |
+
new_state["execution_results"] = {
|
| 89 |
+
"queries_executed": len(execution_results),
|
| 90 |
+
"success_rate": success_rate,
|
| 91 |
+
"details": execution_results,
|
| 92 |
+
"summary": agent_response.replace("EXECUTION_COMPLETE", "").strip(),
|
| 93 |
+
"completed_at": time.time()
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
# Calculate confidence scores
|
| 97 |
+
confidence_scores = {
|
| 98 |
+
"intent_understanding": 0.95, # High by default for demo
|
| 99 |
+
"plan_quality": 0.85 if success_rate > 0.8 else 0.6,
|
| 100 |
+
"execution_success": success_rate,
|
| 101 |
+
"overall": (0.95 + (0.85 if success_rate > 0.8 else 0.6) + success_rate) / 3
|
| 102 |
+
}
|
| 103 |
+
new_state["confidence_scores"] = confidence_scores
|
| 104 |
+
|
| 105 |
+
# Update status
|
| 106 |
+
new_state["status"] = "complete"
|
| 107 |
+
new_state["current_agent"] = "complete"
|
| 108 |
+
|
| 109 |
+
return new_state
|
| 110 |
+
|
| 111 |
+
except Exception as e:
|
| 112 |
+
# Handle any errors
|
| 113 |
+
print(f"Error in executor_agent: {str(e)}")
|
| 114 |
+
# Return a valid state in case of error
|
| 115 |
+
error_state = state.copy()
|
| 116 |
+
error_state["messages"] = messages + [{"role": "assistant", "content": f"I encountered an error: {str(e)}"}]
|
| 117 |
+
error_state["current_agent"] = "executor_agent"
|
| 118 |
+
error_state["status"] = "error"
|
| 119 |
+
return error_state
|
| 120 |
|
| 121 |
def _execute_queries(db, sql_queries: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
| 122 |
"""
|