Spaces:
Runtime error
Runtime error
Update graph/workflow.py
Browse files- graph/workflow.py +8 -84
graph/workflow.py
CHANGED
|
@@ -1,12 +1,11 @@
|
|
| 1 |
-
# workflow.py
|
| 2 |
"""
|
| 3 |
LangGraph workflow for pharmaceutical data management agents.
|
| 4 |
"""
|
| 5 |
|
| 6 |
from langgraph.graph import StateGraph, END, START
|
| 7 |
-
from langchain.tools import StructuredTool
|
| 8 |
from langchain_core.tools import tool
|
| 9 |
-
from typing import Dict, Any
|
|
|
|
| 10 |
|
| 11 |
from agents.state import AgentState
|
| 12 |
from agents.understanding import understanding_agent
|
|
@@ -59,68 +58,13 @@ def create_agent_graph(anthropic_client, db):
|
|
| 59 |
state_provider = lambda: state_dict
|
| 60 |
|
| 61 |
# Create tools node with database-related tools
|
| 62 |
-
#
|
| 63 |
-
lc_tools = []
|
| 64 |
-
|
| 65 |
-
# List Tables Tool
|
| 66 |
-
@tool
|
| 67 |
-
def list_tables():
|
| 68 |
-
"""List available tables in the database, categorized by pipeline stage."""
|
| 69 |
-
return db.get_tables()
|
| 70 |
-
lc_tools.append(list_tables)
|
| 71 |
-
|
| 72 |
-
# Describe Table Tool
|
| 73 |
-
@tool
|
| 74 |
-
def describe_table(table_name: str):
|
| 75 |
-
"""Get the schema of a specific table."""
|
| 76 |
-
query = f"DESCRIBE {table_name}"
|
| 77 |
-
return db.execute_query(query)
|
| 78 |
-
lc_tools.append(describe_table)
|
| 79 |
-
|
| 80 |
-
# Sample Table Tool
|
| 81 |
-
@tool
|
| 82 |
-
def sample_table(table_name: str, rows: int = 5):
|
| 83 |
-
"""Get a sample of rows from a specific table."""
|
| 84 |
-
return db.get_table_sample(table_name, rows)
|
| 85 |
-
lc_tools.append(sample_table)
|
| 86 |
-
|
| 87 |
-
# Execute Query Tool
|
| 88 |
-
@tool
|
| 89 |
-
def execute_query(query: str):
|
| 90 |
-
"""Execute a SQL query on the database."""
|
| 91 |
-
return db.execute_query(query)
|
| 92 |
-
lc_tools.append(execute_query)
|
| 93 |
-
|
| 94 |
-
# Get Confidence Tool
|
| 95 |
-
@tool
|
| 96 |
-
def get_confidence(area: str = "overall"):
|
| 97 |
-
"""Calculate confidence score for the current plan or specific area."""
|
| 98 |
-
state = state_dict
|
| 99 |
-
user_intent = state.get("user_intent", {})
|
| 100 |
-
pipeline_plan = state.get("pipeline_plan", {})
|
| 101 |
-
|
| 102 |
-
# Confidence scoring logic
|
| 103 |
-
completeness = len(user_intent) / 5 if user_intent else 0
|
| 104 |
-
clarity = 0.7 if pipeline_plan and "description" in pipeline_plan else 0.3
|
| 105 |
-
feasibility = 0.85 # High by default for demo
|
| 106 |
-
|
| 107 |
-
if area == "intent":
|
| 108 |
-
return {"confidence": round(completeness * 100, 1), "area": "intent"}
|
| 109 |
-
elif area == "plan":
|
| 110 |
-
return {"confidence": round(clarity * 100, 1), "area": "plan"}
|
| 111 |
-
elif area == "feasibility":
|
| 112 |
-
return {"confidence": round(feasibility * 100, 1), "area": "feasibility"}
|
| 113 |
-
else:
|
| 114 |
-
overall = (completeness + clarity + feasibility) / 3
|
| 115 |
-
return {"confidence": round(overall * 100, 1), "area": "overall"}
|
| 116 |
-
lc_tools.append(get_confidence)
|
| 117 |
|
| 118 |
# Create Tool Agent Node
|
| 119 |
def tool_handler(state):
|
| 120 |
"""Handle tool calls from the agent workflow."""
|
| 121 |
log_agent_activity("TOOL", state)
|
| 122 |
-
#
|
| 123 |
-
return state
|
| 124 |
|
| 125 |
nodes["tools"] = tool_handler
|
| 126 |
|
|
@@ -134,42 +78,27 @@ def create_agent_graph(anthropic_client, db):
|
|
| 134 |
# Set the entry point to understanding_agent
|
| 135 |
workflow.add_edge(START, "understanding_agent")
|
| 136 |
|
| 137 |
-
# Define routing functions
|
| 138 |
def route_understanding(state):
|
| 139 |
"""Route from understanding agent."""
|
| 140 |
-
# Get current agent, ensuring it's a string
|
| 141 |
current_agent = state.get("current_agent", "understanding_agent")
|
| 142 |
-
|
| 143 |
-
if isinstance(current_agent, list):
|
| 144 |
-
current_agent = current_agent[-1] if current_agent else "understanding_agent"
|
| 145 |
-
|
| 146 |
-
# Determine routing based on current agent
|
| 147 |
if current_agent == "planning_agent":
|
| 148 |
return "planning_agent"
|
| 149 |
return "understanding_agent"
|
| 150 |
|
| 151 |
def route_planning(state):
|
| 152 |
"""Route from planning agent."""
|
| 153 |
-
# Get current agent, ensuring it's a string
|
| 154 |
current_agent = state.get("current_agent", "planning_agent")
|
| 155 |
-
|
| 156 |
-
if isinstance(current_agent, list):
|
| 157 |
-
current_agent = current_agent[-1] if current_agent else "planning_agent"
|
| 158 |
-
|
| 159 |
-
# Determine routing based on current agent
|
| 160 |
if current_agent == "sql_generator_agent":
|
| 161 |
return "sql_generator_agent"
|
| 162 |
return "planning_agent"
|
| 163 |
|
| 164 |
def route_sql_generator(state):
|
| 165 |
"""Route from SQL generator agent."""
|
| 166 |
-
# Get current agent, ensuring it's a string
|
| 167 |
current_agent = state.get("current_agent", "sql_generator_agent")
|
| 168 |
-
|
| 169 |
-
if isinstance(current_agent, list):
|
| 170 |
-
current_agent = current_agent[-1] if current_agent else "sql_generator_agent"
|
| 171 |
-
|
| 172 |
-
# Determine routing based on current agent
|
| 173 |
if current_agent == "executor_agent":
|
| 174 |
return "executor_agent"
|
| 175 |
return "sql_generator_agent"
|
|
@@ -215,11 +144,6 @@ def create_agent_graph(anthropic_client, db):
|
|
| 215 |
|
| 216 |
# Update the state dictionary reference (used by the confidence tool)
|
| 217 |
def update_state_dict(new_state):
|
| 218 |
-
# Ensure current_agent is a string, not a list
|
| 219 |
-
if "current_agent" in new_state:
|
| 220 |
-
if isinstance(new_state["current_agent"], list):
|
| 221 |
-
new_state["current_agent"] = new_state["current_agent"][-1] if new_state["current_agent"] else "understanding_agent"
|
| 222 |
-
|
| 223 |
state_dict.clear()
|
| 224 |
state_dict.update(new_state)
|
| 225 |
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
LangGraph workflow for pharmaceutical data management agents.
|
| 3 |
"""
|
| 4 |
|
| 5 |
from langgraph.graph import StateGraph, END, START
|
|
|
|
| 6 |
from langchain_core.tools import tool
|
| 7 |
+
from typing import Dict, Any
|
| 8 |
+
import operator
|
| 9 |
|
| 10 |
from agents.state import AgentState
|
| 11 |
from agents.understanding import understanding_agent
|
|
|
|
| 58 |
state_provider = lambda: state_dict
|
| 59 |
|
| 60 |
# Create tools node with database-related tools
|
| 61 |
+
# Tools implementation remains the same
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
# Create Tool Agent Node
|
| 64 |
def tool_handler(state):
|
| 65 |
"""Handle tool calls from the agent workflow."""
|
| 66 |
log_agent_activity("TOOL", state)
|
| 67 |
+
return {} # Return empty state update since we don't modify state
|
|
|
|
| 68 |
|
| 69 |
nodes["tools"] = tool_handler
|
| 70 |
|
|
|
|
| 78 |
# Set the entry point to understanding_agent
|
| 79 |
workflow.add_edge(START, "understanding_agent")
|
| 80 |
|
| 81 |
+
# Define routing functions
|
| 82 |
def route_understanding(state):
|
| 83 |
"""Route from understanding agent."""
|
|
|
|
| 84 |
current_agent = state.get("current_agent", "understanding_agent")
|
| 85 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
if current_agent == "planning_agent":
|
| 87 |
return "planning_agent"
|
| 88 |
return "understanding_agent"
|
| 89 |
|
| 90 |
def route_planning(state):
|
| 91 |
"""Route from planning agent."""
|
|
|
|
| 92 |
current_agent = state.get("current_agent", "planning_agent")
|
| 93 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
if current_agent == "sql_generator_agent":
|
| 95 |
return "sql_generator_agent"
|
| 96 |
return "planning_agent"
|
| 97 |
|
| 98 |
def route_sql_generator(state):
|
| 99 |
"""Route from SQL generator agent."""
|
|
|
|
| 100 |
current_agent = state.get("current_agent", "sql_generator_agent")
|
| 101 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
if current_agent == "executor_agent":
|
| 103 |
return "executor_agent"
|
| 104 |
return "sql_generator_agent"
|
|
|
|
| 144 |
|
| 145 |
# Update the state dictionary reference (used by the confidence tool)
|
| 146 |
def update_state_dict(new_state):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
state_dict.clear()
|
| 148 |
state_dict.update(new_state)
|
| 149 |
|