掘金 人工智能 04月02日 20:02
🤖 LangGraph 多智能体群集
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

LangGraph 多智能体群集是一个基于 Python 的库,专为构建多智能体系统而设计。该系统允许智能体根据专业知识动态地将控制权转移给其他智能体,并记住最后活跃的智能体,从而在后续交互中保持对话的连贯性。该库提供了内置工具,用于智能体之间的通信,并支持自定义交接工具和智能体实现,以满足各种应用场景的需求。通过使用 InMemorySaver 等内存管理机制,可以有效地维持对话状态。LangGraph 多智能体群集为开发协作智能体系统提供了一个强大而灵活的框架。

🤖 **核心概念:** LangGraph 多智能体群集的核心在于多智能体协作,允许不同智能体相互协作,并将上下文传递给彼此,从而实现更复杂的任务处理。

🛠️ **安装与快速开始:** 通过 pip 命令安装 langgraph-swarm 和 langchain-openai 后,用户可以设置 OpenAI API 密钥。随后,可以创建智能体,并使用 create_react_agent 函数定义智能体的行为和工具,例如加法专家 Alice 和海盗风格的 Bob。

🔄 **智能体交接:** 库中内置了交接工具,智能体之间可以动态地将控制权交给彼此。例如,Bob 可以将数学问题交给 Alice 解决。这种交接机制通过 Command 对象实现,允许智能体转移到其他智能体并更新状态。

💾 **内存管理:** 系统提供了短期内存和长期内存的管理方案。InMemorySaver 用于维持对话状态,而 InMemoryStore 用于存储历史数据。这确保了对话的连贯性和上下文的完整性。

⚙️ **自定义功能:** 用户可以自定义交接工具和智能体实现。通过 create_custom_handoff_tool 函数,可以创建具有特定名称和描述的交接工具。同时,用户可以自定义智能体的状态和行为,以满足特定应用场景的需求。

LangGraph 多智能体群集是一种 Python 库,用于创建基于 LangGraph 的多智能体系统。这种系统允许智能体根据其专业化动态地将控制权交给彼此,并记住最后活跃的智能体,以便在后续交互中继续对话。

基础概念

    多智能体协作:不同智能体可以合作并将上下文传递给彼此。可定制的交接工具:内置工具用于智能体之间的通信。

安装和快速开始

    安装库
    pip install langgraph-swarm langchain-openai
    设置 OpenAI API
    export OPENAI_API_KEY=
    创建智能体
    from langchain_openai import ChatOpenAIfrom langgraph.checkpoint.memory import InMemorySaverfrom langgraph.prebuilt import create_react_agentfrom langgraph_swarm import create_handoff_tool, create_swarmmodel = ChatOpenAI(model="gpt-4o")def add(a: int, b: int) -> int:    return a + balice = create_react_agent(    model,    [add, create_handoff_tool(agent_name="Bob")],    prompt="You are Alice, an addition expert.",    name="Alice",)bob = create_react_agent(    model,    [create_handoff_tool(agent_name="Alice", description="Transfer to Alice, she can help with math")],    prompt="You are Bob, you speak like a pirate.",    name="Bob",)checkpointer = InMemorySaver()workflow = create_swarm([alice, bob], default_active_agent="Alice")app = workflow.compile(checkpointer=checkpointer)config = {"configurable": {"thread_id": "1"}}turn_1 = app.invoke({"messages": [{"role": "user", "content": "i'd like to speak to Bob"}]}, config)print(turn_1)turn_2 = app.invoke({"messages": [{"role": "user", "content": "what's 5 + 7?"}]}, config)print(turn_2)

内存管理

    短期内存:使用 InMemorySaver 来维持对话状态。长期内存:使用 InMemoryStore 来存储历史数据。

自定义

    自定义交接工具

    from langchain_core.tools import tool, BaseToolfrom langchain_core.messages import ToolMessagefrom langgraph.types import Commanddef create_custom_handoff_tool(*, agent_name: str, tool_name: str, tool_description: str) -> BaseTool:    @tool(name=tool_name, description=tool_description)    def handoff_to_agent(task_description: str, state: dict, tool_call_id: str):        tool_message = ToolMessage(            content=f"Successfully transferred to {agent_name}",            name=tool_name,            tool_call_id=tool_call_id,        )        return Command(            goto=agent_name,            graph=Command.PARENT,            update={                "messages": [state["messages"][-1], tool_message],                "active_agent": agent_name,                "task_description": task_description,            },        )    return handoff_to_agent

    自定义智能体实现

    from langchain_core.messages import AnyMessagefrom langgraph.graph import StateGraphclass AliceState:    alice_messages: list[AnyMessage]alice = (    StateGraph(AliceState)    .add_node("model", ...)    .add_node("tools", ...)    .add_edge(...)    .compile())def call_alice(state):    response = alice.invoke({"alice_messages": state["messages"]})    return {"messages": response["alice_messages"]}workflow = (    StateGraph()    .add_node("Alice", call_alice, destinations=("Bob",))    .add_node("Bob", call_bob, destinations=("Alice",)))workflow = add_active_agent_router(workflow, route_to=["Alice", "Bob"], default_active_agent="Alice")app = workflow.compile()

总结

LangGraph 多智能体群集提供了一个灵活的框架,用于创建多智能体系统。通过自定义交接工具和智能体实现,可以满足不同应用场景的需求。同时,内存管理对于维持对话状态至关重要。

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

LangGraph 多智能体 Python 智能体协作
相关文章