madia / graph /workflow.py
hamba-ho's picture
feat: enforce extreme conciseness and reduce max rounds to 2 to save tokens and speed up consensus
74b842d
Raw
History Blame Contribute Delete
1.72 kB
"""
LangGraph workflow β€” the main debate orchestration graph.
Flow:
START β†’ chef β†’ nutrition β†’ eco β†’ moderator β†’ should_continue?
β”œβ”€β”€ decision_reached=True β†’ END
β”œβ”€β”€ round >= 4 β†’ END
└── else β†’ chef (next round)
"""
from langgraph.graph import END, StateGraph
from graph.nodes import chef_node, eco_node, moderator_node, nutrition_node
from graph.state import DebateState
def should_continue(state: DebateState) -> str:
"""
Conditional edge: decide whether to continue the debate or end it.
Returns:
'end' if decision reached or max rounds exceeded, 'continue' otherwise.
"""
if state.get("decision_reached", False):
return "end"
if state.get("current_round", 1) >= 2:
return "end"
return "continue"
def build_graph() -> StateGraph:
"""
Build and compile the multi-agent debate graph.
Returns:
A compiled LangGraph StateGraph ready for invocation.
"""
workflow = StateGraph(DebateState)
# Add agent nodes
workflow.add_node("chef", chef_node)
workflow.add_node("nutrition", nutrition_node)
workflow.add_node("eco", eco_node)
workflow.add_node("moderator", moderator_node)
# Set the entry point
workflow.set_entry_point("chef")
# Linear flow within each round
workflow.add_edge("chef", "nutrition")
workflow.add_edge("nutrition", "eco")
workflow.add_edge("eco", "moderator")
# Conditional edge after moderator: continue or end
workflow.add_conditional_edges(
"moderator",
should_continue,
{
"continue": "chef",
"end": END,
},
)
return workflow.compile()