跳转到主要内容
Chinese, Simplified

category

大型语言模型(LLM)提高了人机交互的标准,用户的期望是他们可以通过自然语言与应用程序进行通信。除了简单的语言理解,现实世界的应用程序还需要管理复杂的工作流程,连接到外部数据,并协调多种人工智能功能。想象一下,在安排医生预约时,人工智能代理会检查你的日历,访问你的医疗服务提供者的系统,验证保险,并一次性确认一切——不再需要应用程序切换或等待时间。在这些现实世界的场景中,代理可以改变游戏规则,提供更多定制的生成人工智能应用程序。

LLM代理充当应用程序控制流的决策系统。然而,这些系统在扩展和开发过程中面临着几个操作挑战。主要问题包括工具选择效率低下,可以访问众多工具的代理在最佳工具选择和排序方面遇到困难,上下文管理限制阻碍了单个代理有效管理日益复杂的上下文信息,以及复杂应用程序需要规划、研究和分析等不同专业领域的专业化要求。解决方案在于实现一个多代理架构,该架构涉及将主系统分解为独立运行的较小的专用代理。实现选项从基本的提示LLM组合到复杂的ReAct(推理和代理)代理,允许更有效的任务分配和对不同应用程序组件的专门处理。这种模块化方法增强了系统的可管理性,并允许更好地扩展基于LLM的应用程序,同时通过专用组件保持功能效率。

本文演示了如何将开源多代理框架LangGraph与Amazon Bedrock集成。它解释了如何使用LangGraph和Amazon Bedrock构建强大的交互式多代理应用程序,这些应用程序使用基于图的编排。

AWS为Amazon Bedrock代理引入了多代理协作功能,使开发人员能够构建、部署和管理多个在复杂任务中协同工作的AI代理。此功能允许创建处理流程不同方面的专用代理,由主管代理协调,主管代理分解请求、委派任务并整合输出。这种方法提高了任务成功率、准确性和生产率,特别是对于复杂的多步骤任务。

多智能体系统的挑战

在单代理系统中,规划涉及LLM代理将任务分解为一系列小任务,而多代理系统必须具有涉及跨多个代理的任务分配的工作流管理。与单代理环境不同,多代理系统需要一个协调机制,其中每个代理必须与其他代理保持一致,同时为总体目标做出贡献。这在管理代理间依赖关系、资源分配和同步方面带来了独特的挑战,需要强大的框架来保持系统范围的一致性,同时优化性能。

人工智能系统中的内存管理在单代理和多代理架构之间有所不同。单代理系统使用三层结构:短期会话记忆、长期历史存储和外部数据源,如检索增强生成(RAG)。多代理系统需要更高级的框架来管理上下文数据、跟踪交互以及跨代理同步历史记录。这些系统必须处理实时交互、上下文同步和高效的数据检索,因此需要仔细设计内存层次结构、访问模式和代理间共享。

代理框架对于多代理系统至关重要,因为它们为协调自主代理、管理通信和资源以及编排工作流提供了基础设施。代理框架减轻了从头开始构建这些复杂组件的需要。

LangGraph是LangChain的一部分,通过基于图的架构来编排代理工作流,该架构处理复杂的流程并维护跨代理交互的上下文。它使用监督控制模式和存储系统进行协调。

LangGraph Studio通过图形可视化、执行监控和运行时调试功能增强了开发。LangGraph与Amazon Bedrock的集成使您能够无缝利用多个代理的优势,营造一个协作环境,提高基于LLM的系统的效率和有效性。

了解LangGraph和LangGraph Studio

LangGraph实现了用于多代理编排的状态机和有向图。该框架提供了对代理应用程序的流和状态的细粒度控制。LangGraph将代理工作流建模为图形。您可以使用三个关键组件定义代理的行为:

  • 状态–表示应用程序当前快照的共享数据结构。
  • 节点——对代理逻辑进行编码的Python函数。
  • Edges-Python函数,根据当前状态确定下一个要执行的节点。它们可以是条件分支或固定转换。

LangGraph实现了一个中央持久层,支持大多数代理架构共有的功能,包括:

  • 内存–LangGraph保存应用程序状态的任意方面,支持用户交互内部和之间的对话和其他更新的内存。
  • 人在循环中——由于状态是检查点,执行可以中断和恢复,允许通过人工输入在关键阶段进行决策、验证和纠正。

LangGraph Studio是一个专门为AI代理开发设计的集成开发环境(IDE)。它为开发人员提供了强大的可视化、实时交互和调试功能工具。LangGraph Studio的主要功能是:

  • 可视化代理图——IDE的可视化工具允许开发人员将代理流表示为直观的图形轮,使理解和修改复杂的系统架构变得简单明了。
  • 实时调试——实时与代理交互并在执行过程中修改响应的能力创造了更动态的开发体验。
  • 有状态架构——在基于图的架构中支持有状态和自适应代理,可以实现更复杂的行为和交互。

以下屏幕截图显示了在LangGraph Studio中查看的典型LangGraph代理工作流的节点、边和状态。

Figure 1: LangGraph Studio UI

In the preceding example, the state begins with __start__ and ends with __end__. The nodes for invoking the model and tools are defined by you and the edges tell you which paths can be followed by the workflow.

LangGraph Studio is available as a desktop application for MacOS users. Alternatively, you can run a local in-memory development server that can be used to connect a local LangGraph application with a web version of the studio.

解决方案概述

此示例演示了监督代理模式,其中监督代理协调多个专用代理。每个代理都有自己的草稿栏,而主管则根据代理的能力协调通信和委派任务。这种分布式方法通过允许代理专注于特定任务来提高效率,同时实现并行处理和系统可扩展性。

让我们用以下用户查询来浏览一个示例:“为我推荐一个旅行目的地并搜索航班和酒店。我想在2025年3月15日旅行5天。”工作流程包括以下步骤:

  • Supervisor Agent接收初始查询并将其分解为连续的任务:
    • 需要目的地推荐。
    • 2025年3月15日需要进行航班搜索。
    • 需要预订酒店5天。
    • Destination Agent通过访问用户存储的配置文件开始工作。它搜索其历史数据库,分析类似用户配置文件中的模式,以推荐目的地。然后,它将目的地传递回主管代理。
  • 主管代理将所选目的地转发给飞行代理,飞行代理搜索给定日期的可用航班。
  • 主管代理激活酒店代理,酒店代理搜索目的地城市的酒店。
  • 主管代理将建议汇编成一份全面的旅行计划,向用户提供完整的行程,包括目的地理由、航班选择和酒店建议。

下图显示了这些代理如何相互连接以及每个代理涉及哪些工具的多代理工作流。

Figure 2: Multi-agent workflow

Prerequisites

You will need the following prerequisites before you can proceed with this solution. For this post, we use the us-west-2 AWS Region. For details on available Regions, see Amazon Bedrock endpoints and quotas.

Core components

Each agent is structured with two primary components:

  • graph.py – This script defines the agent’s workflow and decision-making logic. It implements the LangGraph state machine for managing agent behavior and configures the communication flow between different components. For example:
    • The Flight Agent’s graph manages the flow between chat and tool operations.
    • The Hotel Agent’s graph handles conditional routing between search, booking, and modification operations.
    • The Supervisor Agent’s graph orchestrates the overall multi-agent workflow.
  • tools.py – This script contains the concrete implementations of agent capabilities. It implements the business logic for each operation and handles data access and manipulation. It provides specific functionalities like:
    • Flight tools: search_flights, book_flights, change_flight_booking, cancel_flight_booking.
    • Hotel tools: suggest_hotels, book_hotels, change_hotel_booking, cancel_hotel_booking.

This separation between graph (workflow) and tools (implementation) allows for a clean architecture where the decision-making process is separate from the actual execution of tasks. The agents communicate through a state-based graph system implemented using LangGraph, where the Supervisor Agent directs the flow of information and tasks between the specialized agents.

To set up Amazon Bedrock with LangGraph, refer to the following GitHub repo. The high-level steps are as follows:

  1. Install the required packages:
pip install boto3 langchain-aws

These packages are essential for AWS Bedrock integration:

  • boto: AWS SDK for Python, handles AWS service communication
  • langchain-aws: Provides LangChain integrations for AWS services
  1. Import the modules:
from langchain_aws import ChatBedrockConverse 
from langchain_aws import ChatBedrock
  1. Create an LLM object:
bedrock_client = boto3.client("bedrock-runtime", region_name="<region_name>")
llm = ChatBedrockConverse(
        model="anthropic.claude-3-haiku-20240307-v1:0",
        temperature=0,
        max_tokens=None,
        client=bedrock_client,
        # other params...
    )

LangGraph Studio configuration

This project uses a langgraph.json configuration file to define the application structure and dependencies. This file is essential for LangGraph Studio to understand how to run and visualize your agent graphs.

{
"dependencies": [
"boto3>=1.35.87",
"langchain-aws>=0.2.10",
"."
],
"graphs": {
"supervisor": "./src/supervisor_agent/graph.py:graph",
"flight": "./src/flight_agent/graph.py:graph",
"hotel": "./src/hotel_agent/graph.py:graph"
},
"env": "./.env"
}

LangGraph Studio使用此文件构建和可视化代理工作流,允许您实时监视和调试多代理交互。

测试和调试

现在,您已经准备好测试多智能体旅行助手了。您可以使用langgraph-dev命令启动图形。它将以开发模式启动LangGraph API服务器,并具有热重新加载和调试功能。如下图所示,该界面提供了一种简单的方法,可以通过左上角的下拉菜单选择要测试的图形。底部的管理配置按钮允许您在开始之前设置特定的测试参数。此开发环境提供了彻底测试和调试具有实时反馈和监控功能的多代理系统所需的一切。
 

Testing multi-agent travel assistantFigure 3: LangGraph studio with Destination Agent recommendation

LangGraph Studio offers flexible configuration management through its intuitive interface. As shown in the following screenshot, you can create and manage multiple configuration versions (v1, v2, v3) for your graph execution. For example, in this scenario, we want to use user_id to fetch historic use information. This versioning system makes it simple to track and switch between different test configurations while debugging your multi-agent system.

Create and manage multiple configuration versions (v1, v2, v3) for your graph executionFigure 4: Runnable configuration details

In the preceding example, we set up the user_id that tools can use to retrieve history or other details.

Let’s test the Planner Agent. This agent has the compare_and_recommend_destination tool, which can check past travel data and recommend travel destinations based on the user profile. We use user_id in the configuration so that can it be used by the tool.

LangGraph has concept of checkpoint memory that is managed using a thread. The following screenshot shows that you can quickly manage threads in LangGraph Studio.

Manage threads in LangGraph StudioFigure 5: View graph state in the thread

In this example, destination_agent is using a tool; you can also check the tool’s output. Similarly, you can test flight_agent and hotel_agent to verify each agent.

When all the agents are working well, you’re ready to test the full workflow. You can evaluate the state a verify input and output of each agent.

The following screenshot shows the full view of the Supervisor Agent with its sub-agents.

Figure 6: Supervisor Agent with complete workflow

注意事项

多代理架构必须考虑代理协调、状态管理、通信、输出整合和护栏,维护处理上下文、错误处理和编排。基于图的架构相较于线性管道具有显著优势,能够实现具有非线性通信模式和更清晰系统可视化的复杂工作流程。这些结构允许动态路径和自适应通信,非常适合同时进行代理交互的大规模部署。它们在并行处理和资源分配方面表现出色,但需要复杂的设置,可能需要更高的计算资源。实施这些系统需要仔细规划系统拓扑、强大的监控和设计良好的故障交互回退机制。

在组织中实施多代理架构时,与公司既定的生成式人工智能运营和治理框架保持一致至关重要。在部署之前,请验证是否符合组织的AI安全协议、数据处理策略和模型部署指南。尽管这种架构模式提供了显著的好处,但它的实现应该量身定制,以适应您组织的特定人工智能治理结构和风险管理框架。

清理

删除专门为此职位创建的任何IAM角色和策略。删除此帖子代码的本地副本。如果您不再需要访问Amazon Bedrock FM,您可以从中删除访问权限。有关说明,请参阅添加或删除对Amazon Bedrock基础模型的访问权限

结论

LangGraph与Amazon Bedrock的集成为复杂的人工智能应用程序提供了一个强大的框架,从而显著推进了多代理系统的开发。这种组合使用LangGraph的编排功能和Amazon Bedrock中的FM来创建可扩展、高效的系统。它通过状态管理、代理协调和工作流编排来解决多代理架构中的挑战,提供内存管理、错误处理和人在环功能等功能。LangGraph Studio的可视化和调试工具能够高效地设计和维护复杂的代理交互。这种集成为下一代多代理系统提供了强大的基础,提供了有效的工作流处理、上下文维护、可靠的结果和最佳的资源利用率。

For the example code and demonstration discussed in this post, refer to the accompanying GitHub repository. You can also refer to the following GitHub repo for Amazon Bedrock multi-agent collaboration code samples.

本文地址
最后修改
星期二, 九月 23, 2025 - 15:45
Tags
 
Article