Spaces:
Runtime error
Runtime error
Update agents/planning.py
Browse files- agents/planning.py +39 -63
agents/planning.py
CHANGED
|
@@ -5,11 +5,10 @@ This agent creates a data pipeline plan based on user intent.
|
|
| 5 |
|
| 6 |
import time
|
| 7 |
from typing import Dict, Any
|
| 8 |
-
from
|
| 9 |
-
from anthropic import Anthropic
|
| 10 |
from anthropic.types import MessageParam
|
| 11 |
|
| 12 |
-
def planning_agent(anthropic_client
|
| 13 |
"""
|
| 14 |
Agent that creates a data pipeline plan based on user intent.
|
| 15 |
|
|
@@ -22,7 +21,6 @@ def planning_agent(anthropic_client: Anthropic, state: AgentState) -> AgentState
|
|
| 22 |
"""
|
| 23 |
# Get current messages and user intent
|
| 24 |
messages = state.get("messages", [])
|
| 25 |
-
current_agent = state.get("current_agent", ["planning_agent"])
|
| 26 |
user_intent = state.get("user_intent", {})
|
| 27 |
|
| 28 |
# Add agent-specific instructions
|
|
@@ -52,66 +50,44 @@ def planning_agent(anthropic_client: Anthropic, state: AgentState) -> AgentState
|
|
| 52 |
- Data products: DP_SALES_DASHBOARD, DP_HCP_TARGETING
|
| 53 |
"""
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
prompt_messages = [
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
))
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
)
|
| 69 |
)
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
response = anthropic_client.messages.create(
|
| 74 |
-
model="claude-3-7-sonnet-20250219",
|
| 75 |
-
system=system_message,
|
| 76 |
-
messages=prompt_messages,
|
| 77 |
-
max_tokens=3000
|
| 78 |
-
)
|
| 79 |
-
|
| 80 |
-
# Extract the response
|
| 81 |
-
agent_response = response.content[0].text
|
| 82 |
-
|
| 83 |
-
# Check if plan is complete
|
| 84 |
-
plan_complete = "PLAN_COMPLETE" in agent_response
|
| 85 |
-
|
| 86 |
-
# Prepare the new state updates
|
| 87 |
-
new_state = state.copy()
|
| 88 |
-
|
| 89 |
-
# Add agent's response to messages
|
| 90 |
-
new_state["messages"].append({
|
| 91 |
-
"role": "assistant",
|
| 92 |
-
"content": agent_response.replace("PLAN_COMPLETE", "").strip()
|
| 93 |
-
})
|
| 94 |
-
|
| 95 |
-
# Add the pipeline plan if complete
|
| 96 |
-
if plan_complete:
|
| 97 |
-
new_state["pipeline_plan"] = {
|
| 98 |
-
"description": agent_response.replace("PLAN_COMPLETE", "").strip(),
|
| 99 |
-
"created_at": time.time()
|
| 100 |
-
}
|
| 101 |
-
# Use .append() to add next agent to the list
|
| 102 |
-
new_state["current_agent"].append("sql_generator_agent")
|
| 103 |
-
else:
|
| 104 |
-
# Use .append() to add current agent if plan is not complete
|
| 105 |
-
new_state["current_agent"].append("planning_agent")
|
| 106 |
-
|
| 107 |
-
return new_state
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
import time
|
| 7 |
from typing import Dict, Any
|
| 8 |
+
from langgraph.graph.message import add_messages
|
|
|
|
| 9 |
from anthropic.types import MessageParam
|
| 10 |
|
| 11 |
+
def planning_agent(anthropic_client, state: Dict[str, Any]) -> Dict[str, Any]:
|
| 12 |
"""
|
| 13 |
Agent that creates a data pipeline plan based on user intent.
|
| 14 |
|
|
|
|
| 21 |
"""
|
| 22 |
# Get current messages and user intent
|
| 23 |
messages = state.get("messages", [])
|
|
|
|
| 24 |
user_intent = state.get("user_intent", {})
|
| 25 |
|
| 26 |
# Add agent-specific instructions
|
|
|
|
| 50 |
- Data products: DP_SALES_DASHBOARD, DP_HCP_TARGETING
|
| 51 |
"""
|
| 52 |
|
| 53 |
+
# Prepare prompt for Claude
|
| 54 |
+
prompt_messages = [
|
| 55 |
+
MessageParam(role="system", content=system_message),
|
| 56 |
+
*[MessageParam(role=m["role"], content=m["content"]) for m in messages],
|
| 57 |
+
MessageParam(role="user", content=f"Based on my request, create a data pipeline plan. Here is information about available data: {context}")
|
| 58 |
+
]
|
|
|
|
| 59 |
|
| 60 |
+
# Call Claude API
|
| 61 |
+
response = anthropic_client.messages.create(
|
| 62 |
+
model="claude-3-7-sonnet-20250219",
|
| 63 |
+
messages=prompt_messages,
|
| 64 |
+
max_tokens=3000
|
|
|
|
| 65 |
)
|
| 66 |
|
| 67 |
+
# Extract the response
|
| 68 |
+
agent_response = response.content[0].text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
+
# Check if plan is complete
|
| 71 |
+
plan_complete = "PLAN_COMPLETE" in agent_response
|
| 72 |
+
|
| 73 |
+
# Update state based on plan completeness
|
| 74 |
+
new_state = state.copy()
|
| 75 |
+
if plan_complete:
|
| 76 |
+
# Parse the plan from the response
|
| 77 |
+
pipeline_plan = {
|
| 78 |
+
"description": agent_response.replace("PLAN_COMPLETE", "").strip(),
|
| 79 |
+
"created_at": time.time()
|
| 80 |
+
}
|
| 81 |
+
new_state["pipeline_plan"] = pipeline_plan
|
| 82 |
+
new_state["current_agent"] = "sql_generator_agent"
|
| 83 |
+
else:
|
| 84 |
+
# Need more information or planning, stay with planning agent
|
| 85 |
+
new_state["current_agent"] = "planning_agent"
|
| 86 |
+
|
| 87 |
+
# Add agent's response to messages
|
| 88 |
+
new_messages = add_messages(state, [
|
| 89 |
+
{"role": "assistant", "content": agent_response.replace("PLAN_COMPLETE", "").strip()}
|
| 90 |
+
])
|
| 91 |
+
new_state["messages"] = new_messages
|
| 92 |
+
|
| 93 |
+
return new_state
|