-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmulti_agent.py
More file actions
70 lines (50 loc) · 1.88 KB
/
Copy pathmulti_agent.py
File metadata and controls
70 lines (50 loc) · 1.88 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
# ruff: noqa: N801, N805
"""Multi-agent graph — compose multiple node subclasses.
Each node metaclass produces a self-contained ReAct subgraph with its own
tools and skill access. Compose them in a parent graph for multi-agent workflows.
"""
from langchain_core.messages import HumanMessage
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
from langgraph.graph import END, START, StateGraph
from langchain_skillkit import AgentState, node
@tool
def web_search(query: str) -> str:
"""Search the web for information."""
return f"Results for: {query}"
@tool
def sql_query(query: str) -> str:
"""Run a SQL query against the database."""
return f"SQL results for: {query}"
@tool
def calculate(expression: str) -> str:
"""Evaluate a mathematical expression."""
return str(eval(expression))
class researcher(node):
llm = ChatOpenAI(model="gpt-4o")
tools = [web_search]
skills = "skills/"
async def handler(state, *, llm):
response = await llm.ainvoke(state["messages"])
return {"messages": [response], "sender": "researcher"}
class analyst(node):
llm = ChatOpenAI(model="gpt-4o")
tools = [sql_query, calculate]
skills = "skills/"
async def handler(state, *, llm):
response = await llm.ainvoke(state["messages"])
return {"messages": [response], "sender": "analyst"}
# Compose in a parent graph
workflow = StateGraph(AgentState)
workflow.add_node("researcher", researcher.compile())
workflow.add_node("analyst", analyst.compile())
workflow.add_edge(START, "researcher")
workflow.add_edge("researcher", "analyst")
workflow.add_edge("analyst", END)
graph = workflow.compile()
if __name__ == "__main__":
result = graph.invoke(
{"messages": [HumanMessage("Analyze the European SaaS market")]}
)
for msg in result["messages"]:
print(f"[{msg.type}] {msg.content[:100]}")