cryogenic22 commited on
Commit
e31ceda
·
verified ·
1 Parent(s): 6568623

Update agents/executor.py

Browse files
Files changed (1) hide show
  1. 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 as system parameter
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
- # Convert state messages to the format expected by Anthropic API
51
- # Excluding any messages with role "system"
52
- anthropic_messages = []
53
  for msg in messages:
54
- if msg["role"] != "system": # Skip system messages
55
- anthropic_messages.append(MessageParam(
56
  role=msg["role"],
57
  content=msg["content"]
58
  ))
59
 
60
  # Add final user message with context
61
- anthropic_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
- # Call Claude API with system message as separate parameter
69
- response = anthropic_client.messages.create(
70
- model="claude-3-7-sonnet-20250219",
71
- system=system_message,
72
- messages=anthropic_messages,
73
- max_tokens=2000
74
- )
75
-
76
- # Extract the response
77
- agent_response = response.content[0].text
78
-
79
- # Update state with execution results
80
- new_state = state.copy()
81
- new_state["execution_results"] = {
82
- "queries_executed": len(execution_results),
83
- "success_rate": success_rate,
84
- "details": execution_results,
85
- "summary": agent_response.replace("EXECUTION_COMPLETE", "").strip(),
86
- "completed_at": time.time()
87
- }
88
-
89
- # Calculate confidence scores
90
- confidence_scores = {
91
- "intent_understanding": 0.95, # High by default for demo
92
- "plan_quality": 0.85 if success_rate > 0.8 else 0.6,
93
- "execution_success": success_rate,
94
- "overall": (0.95 + (0.85 if success_rate > 0.8 else 0.6) + success_rate) / 3
95
- }
96
- new_state["confidence_scores"] = confidence_scores
97
-
98
- # Update status
99
- new_state["status"] = "complete"
100
- new_state["current_agent"] = "complete"
101
-
102
- # Add agent's response to messages
103
- new_messages = add_messages(state, [
104
- {"role": "assistant", "content": agent_response.replace("EXECUTION_COMPLETE", "").strip()}
105
- ])
106
- new_state["messages"] = new_messages
107
-
108
- return new_state
 
 
 
 
 
 
 
 
 
 
 
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
  """