-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathhierarchical_agents.py
More file actions
456 lines (364 loc) · 14.1 KB
/
hierarchical_agents.py
File metadata and controls
456 lines (364 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
"""
Hierarchical Agents in LangGraph
Multi-level supervisors with department routing using subgraphs
"""
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import MessagesState, add_messages
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage, BaseMessage
from typing_extensions import TypedDict, Annotated
from typing import Literal
from pydantic import BaseModel, Field
import operator
from dotenv import load_dotenv
load_dotenv()
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
# ============================================================
# Shared state schema used across all levels
# ============================================================
class TeamState(TypedDict):
messages: Annotated[list[BaseMessage], add_messages]
final_answer: str
# ============================================================
# Department 1: Research Team (subgraph)
# ============================================================
def build_research_team() -> StateGraph:
"""Build the research department subgraph."""
def web_researcher(state: TeamState) -> dict:
"""Searches the web for information."""
# Extract the query from the last human message
query = ""
for msg in reversed(state["messages"]):
if isinstance(msg, HumanMessage):
query = msg.content
break
response = llm.invoke(
[
SystemMessage(
content=(
"You are a web researcher. Find key facts and data about "
"the topic. Provide 3-4 bullet points of findings. Be specific."
)
),
HumanMessage(content=query),
]
)
return {
"messages": [
AIMessage(
content=f"[WEB RESEARCHER]: {response.content}",
name="web_researcher",
)
]
}
def paper_reviewer(state: TeamState) -> dict:
"""Reviews academic/technical sources."""
query = ""
for msg in reversed(state["messages"]):
if isinstance(msg, HumanMessage):
query = msg.content
break
response = llm.invoke(
[
SystemMessage(
content=(
"You are an academic reviewer. Provide technical depth and "
"cite relevant concepts or frameworks. 3-4 bullet points."
)
),
HumanMessage(content=query),
]
)
return {
"messages": [
AIMessage(
content=f"[PAPER REVIEWER]: {response.content}",
name="paper_reviewer",
)
]
}
def research_lead(state: TeamState) -> dict:
"""Synthesizes findings from both researchers."""
response = llm.invoke(
[
SystemMessage(
content=(
"You are the research lead. Synthesize the web researcher's "
"and paper reviewer's findings into a cohesive research brief. "
"Keep it to one short paragraph."
)
),
*state["messages"],
]
)
return {
"messages": [
AIMessage(
content=f"[RESEARCH LEAD]: {response.content}", name="research_lead"
)
],
"final_answer": response.content,
}
# Build the research subgraph
research_graph = StateGraph(TeamState)
research_graph.add_node("web_researcher", web_researcher)
research_graph.add_node("paper_reviewer", paper_reviewer)
research_graph.add_node("research_lead", research_lead)
# Fan-out: both researchers work in parallel
research_graph.add_edge(START, "web_researcher")
research_graph.add_edge(START, "paper_reviewer")
# Fan-in: both feed into the research lead
research_graph.add_edge("web_researcher", "research_lead")
research_graph.add_edge("paper_reviewer", "research_lead")
research_graph.add_edge("research_lead", END)
return research_graph
def demo_single_department():
"""Demo a single department subgraph in isolation."""
print("Single Department Demo (Research Team):\n")
research_team = build_research_team().compile()
result = research_team.invoke(
{
"messages": [
HumanMessage(content="What is retrieval-augmented generation (RAG)?")
],
"final_answer": "",
}
)
for msg in result["messages"]:
if isinstance(msg, AIMessage):
print(f"{msg.content[:200]}...\n")
print(f"Research Brief:\n{result['final_answer']}")
# ============================================================
# Department 2: Content Team (subgraph)
# ============================================================
def build_content_team() -> StateGraph:
"""Build the content department subgraph."""
def content_writer(state: TeamState) -> dict:
"""Writes content based on available context."""
response = llm.invoke(
[
SystemMessage(
content=(
"You are a skilled content writer. Using any research or context "
"in the conversation, write a clear, engaging short piece "
"(one paragraph). Match a professional but accessible tone."
)
),
*state["messages"],
]
)
return {
"messages": [
AIMessage(
content=f"[WRITER]: {response.content}", name="content_writer"
)
]
}
def content_editor(state: TeamState) -> dict:
"""Edits and polishes the writer's output."""
response = llm.invoke(
[
SystemMessage(
content=(
"You are a content editor. Take the writer's draft and "
"improve clarity, fix any issues, and tighten the language. "
"Return the polished version only."
)
),
*state["messages"],
]
)
return {
"messages": [
AIMessage(
content=f"[EDITOR]: {response.content}", name="content_editor"
)
],
"final_answer": response.content,
}
content_graph = StateGraph(TeamState)
content_graph.add_node("writer", content_writer)
content_graph.add_node("editor", content_editor)
content_graph.add_edge(START, "writer")
content_graph.add_edge("writer", "editor")
content_graph.add_edge("editor", END)
return content_graph
# ============================================================
# Department 3: Analysis Team (subgraph)
# ============================================================
def build_analysis_team() -> StateGraph:
"""Build the analysis department subgraph."""
def data_analyst(state: TeamState) -> dict:
"""Provides data-driven analysis."""
response = llm.invoke(
[
SystemMessage(
content=(
"You are a data analyst. Analyze the topic with numbers, "
"trends, and quantitative reasoning. Provide 3-4 data-driven "
"insights. Make up plausible stats for demonstration."
)
),
*state["messages"],
]
)
return {
"messages": [
AIMessage(
content=f"[DATA ANALYST]: {response.content}", name="data_analyst"
)
]
}
def strategy_advisor(state: TeamState) -> dict:
"""Provides strategic recommendations."""
response = llm.invoke(
[
SystemMessage(
content=(
"You are a strategy advisor. Based on the data analysis in the "
"conversation, provide 3 actionable strategic recommendations. "
"Be specific and practical."
)
),
*state["messages"],
]
)
return {
"messages": [
AIMessage(
content=f"[STRATEGY ADVISOR]: {response.content}",
name="strategy_advisor",
)
],
"final_answer": response.content,
}
analysis_graph = StateGraph(TeamState)
analysis_graph.add_node("data_analyst", data_analyst)
analysis_graph.add_node("strategy_advisor", strategy_advisor)
analysis_graph.add_edge(START, "data_analyst")
analysis_graph.add_edge("data_analyst", "strategy_advisor")
analysis_graph.add_edge("strategy_advisor", END)
return analysis_graph
# ============================================================
# Top-Level Supervisor (parent graph)
# ============================================================
def create_hierarchical_system():
"""
Top-level supervisor that routes to department subgraphs.
Each department is a compiled subgraph added as a single node.
"""
# Compile department subgraphs
research_team = build_research_team().compile()
content_team = build_content_team().compile()
analysis_team = build_analysis_team().compile()
# Supervisor routing schema
class DepartmentRoute(BaseModel):
department: Literal["research", "content", "analysis"] = Field(
description="Which department should handle this request"
)
reasoning: str = Field(description="Why this department was chosen")
router_llm = llm.with_structured_output(DepartmentRoute)
def ceo_supervisor(state: TeamState) -> dict:
"""Top-level supervisor routes to the right department."""
decision = router_llm.invoke(
[
SystemMessage(
content=(
"You are the CEO supervisor. Route the request to the right department:\n"
"- research: Fact-finding, investigation, technical deep-dives\n"
"- content: Writing, blog posts, marketing copy, summaries\n"
"- analysis: Data analysis, strategy, business decisions\n\n"
"Choose the BEST fit department."
)
),
*state["messages"],
]
)
return {
"messages": [
AIMessage(
content=f"[CEO]: Routing to {decision.department} — {decision.reasoning}",
name="ceo",
)
]
}
def route_to_department(state: TeamState) -> str:
"""Read the CEO's routing decision from the last message."""
last_ai = None
for msg in reversed(state["messages"]):
if isinstance(msg, AIMessage) and msg.name == "ceo":
last_ai = msg
break
if last_ai and "research" in last_ai.content.lower():
return "research_team"
elif last_ai and "content" in last_ai.content.lower():
return "content_team"
elif last_ai and "analysis" in last_ai.content.lower():
return "analysis_team"
return "research_team" # default
# Build parent graph — departments are compiled subgraphs as nodes
parent = StateGraph(TeamState)
parent.add_node("ceo", ceo_supervisor)
parent.add_node("research_team", research_team) # compiled subgraph
parent.add_node("content_team", content_team) # compiled subgraph
parent.add_node("analysis_team", analysis_team) # compiled subgraph
parent.add_edge(START, "ceo")
parent.add_conditional_edges(
"ceo",
route_to_department,
{
"research_team": "research_team",
"content_team": "content_team",
"analysis_team": "analysis_team",
},
)
parent.add_edge("research_team", END)
parent.add_edge("content_team", END)
parent.add_edge("analysis_team", END)
return parent.compile()
def demo_hierarchical_routing():
"""Demo the full hierarchical system with routing."""
system = create_hierarchical_system()
print("Hierarchical Routing Demo:\n")
queries = [
"What are the latest trends in large language models?",
"Write a short blog introduction about AI agents",
"Should my startup invest in building AI features this year?",
]
for query in queries:
print(f"Query: {query}")
print("-" * 40)
result = system.invoke(
{"messages": [HumanMessage(content=query)], "final_answer": ""}
)
# Show the CEO routing decision
for msg in result["messages"]:
if isinstance(msg, AIMessage) and msg.name == "ceo":
print(f" {msg.content}")
# Show the final answer
print(f" Final: {result['final_answer'][:200]}...")
print("=" * 50 + "\n")
def demo_hierarchical_trace():
"""Show full trace through the hierarchy."""
system = create_hierarchical_system()
print("Full Hierarchical Trace:\n")
result = system.invoke(
{
"messages": [
HumanMessage(
content="Research the impact of AI agents on software development productivity"
)
],
"final_answer": "",
}
)
for i, msg in enumerate(result["messages"]):
if isinstance(msg, AIMessage):
label = msg.name or "unknown"
print(f"[Step {i}] {label}:")
print(f" {msg.content[:150]}...")
print()
if __name__ == "__main__":
# demo_single_department()
demo_hierarchical_routing()
demo_hierarchical_trace()