LangGraph实战案例之 多智能体架构
AI
from langchain.agents import create_agent
from langgraph_supervisor import create_supervisor
from app.bailian.common import llm
def add(a:float,b:float) -> float:
"""add two floats"""
return a+b
def mul(a:float,b:float) -> float:
"""multiply two floats"""
return a*b
def web_search(query:str) -> str:
"""search web page"""
return (
"2026年巨头上市公司的员工数量调查:"
"百度 有 10000员工"
"苹果 有100000员工"
"google 有15000 员工"
"华为有 30000 员工"
)
math_agent = create_agent(
model=llm,
tools=[add,mul],
name="math_agent",
system_prompt="你是一个数学方面的专家,一次执行只使用一个工具"
)
web_agent = create_agent(
model=llm,
tools=[web_search],
name="web_agent",
system_prompt="你是一个世界级的调研专家,能够使用web_search工具,不要使用任何数学工具"
)
workflow = create_supervisor(
agents=[math_agent, web_agent],
model=llm,
prompt=(
"你是一个团队管理专家,管理math_agent和web_agent"
"实时问题就去用web_agent"
"数学问题就去用math_agent"
))
app = workflow.compile()
for chunk in app.stream({"messages":[{"role":"user","content":"2026年巨头上市公司所有员工加一起多少人"}]}):
print(chunk)![[衡天云]爆款云服务器 低至12元/月](/hty.png)