LangGraph 示例中的流式与多轮交互
该教程以货币转换 Agent 为例,说明如何用 LangChain 与 LangGraph 接入 Gemini 模型,并通过 A2A 服务端配置流式能力与多轮任务状态管理。
## 7. 流转和多回合交互( LangGraph 示例)[¶](<https://a2a-protocol.org/latest/tutorials/python/7-streaming-and-multiturn/#7-streaming-multi-turn-interactions-langgraph-example>) Hello World 实例演示了 A2A 的基本力学。 对于更先进的功能,如强力流线、任务状态管理以及由 LLM 驱动的多回合对话,我们将转向位于其中的 LangGraph 实例。 [`a2a-samples/samples/python/agents/langgraph/`](<https://github.com/a2aproject/a2a-samples/tree/main/samples/python/agents/langgraph>). 这个例子有一个"紧急代理",通过LangChain和LangGraph使用双子座模型来回答货币转换问题. ## 设置 LangGraph 示例[¶](<https://a2a-protocol.org/latest/tutorials/python/7-streaming-and-multiturn/#setting-up-the-langgraph-example>) 1. 创建一个 [双子体 API 键](<https://ai.google.dev/gemini-api/docs/api-key>) 如果你还没有一个。 2. **环境变量 :** 创建一个 `.env` 文档中 `a2a-samples/samples/python/agents/langgraph/` 目录 : ``` echo "GOOGLE_API_KEY=YOUR_API_KEY_HERE" > .env ``` 替换 `YOUR_API_KEY_HERE` 带着你真正的双子座 API键。 3. **安装依赖( 如果尚未覆盖) :** 那个... `langgraph` 实例有它自己的 `pyproject.toml` 包括依附关系 `langchain-google-genai` 财务报告和财务报告 `langgraph`。当您安装了 SDK 从 `a2a-samples` 使用根 `pip install -e .[dev]`,这也应该安装工作空间示例的依赖性,包括: `langgraph-example`。如果遇到导入错误,请确保从根目录中安装您的主要SDK成功。 ## 运行 LangGraph 服务器[¶](<https://a2a-protocol.org/latest/tutorials/python/7-streaming-and-multiturn/#running-the-langgraph-server>) 导航到 `a2a-samples/samples/python/agents/langgraph/app` 在您的终端中设置目录并确保您的虚拟环境( 从 SDK root) 被激活 。 启动 LangGraph 代理服务器 : ``` python __main__.py ``` 这将启动服务器, 通常在 `http://localhost:10000`. ## 与 LangGraph 代理交互[¶](<https://a2a-protocol.org/latest/tutorials/python/7-streaming-and-multiturn/#interacting-with-the-langgraph-agent>) 打开 a **新终端窗口**,激活虚拟环境,并导航到 `a2a-samples/samples/python/agents/langgraph/app`. 运行其测试客户端 : ``` python test_client.py ``` 您可以在终端窗口输入 Ctrl+C 关闭服务器 。 `__main__.py` 正在运行。 ## 显示关键特性[¶](<https://a2a-protocol.org/latest/tutorials/python/7-streaming-and-multiturn/#key-features-demonstrated>) 那个... `langgraph` 例如, 显示几个重要的 A2A 概念: 1. **LLM 整合**: - `agent.py` 定义 `CurrencyAgent`它使用 `ChatGoogleGenerativeAI` 和 LangGraph 's 的密码 `create_react_agent` 用于处理用户查询。 - 这说明一个真实的LLM能够如何为代理机的逻辑提供动力. 2. **国家管理工作队**: - `samples/python/agents/langgraph/app/__main__.py` 初始化 a `DefaultRequestHandler` 带有 `InMemoryTaskStore`. ``` httpx_client = httpx.AsyncClient() push_config_store = InMemoryPushNotificationConfigStore() push_sender = BasePushNotificationSender(httpx_client=httpx_client, config_store=push_config_store) request_handler = DefaultRequestHandler( agent_executor=CurrencyAgentExecutor(), task_store=InMemoryTaskStore(), push_config_store=push_config_store, push_sender= push_sender ) server = A2AStarletteApplication( agent_card=agent_card, http_handler=request_handler ) uvicorn.run(server.build(), host=host, port=port) ``` - 那个... `CurrencyAgentExecutor` (单位: `samples/langgraph/agent_executor.py`),当 `execute` 方法由 `DefaultRequestHandler`,与 `RequestContext` 包含当前任务(如果有的话)。 - 为: `Send Message`,则 `DefaultRequestHandler` 使用该 `TaskStore` 用于持续并获取交互任务状态。 `Send Message` {\fn黑体\fs22\bord1\shad0\3aHBE\4aH00\fscx67\fscy66\2cHFFFFFF\3cH808080}会是满的 `Task` 对象,如果代理执行流程涉及多个步骤或导致持续任务。 - 那个... `test_client.py`'s `run_single_turn_test` 显示获得一个 `Task` 对象返回并使用它查询 `get_task`. 3. **与流经 `TaskStatusUpdateEvent` 财务报告和财务报告 `TaskArtifactUpdateEvent`**: - 那个... `execute` 输入方法 `CurrencyAgentExecutor` 负责处理非流式和流式请求,由秘书处协调。 `DefaultRequestHandler`. - 当LangGraph代理处理请求时(这可能涉及调用工具,例如: `get_exchange_rate`,则 `CurrencyAgentExecutor` 将不同类型事件排列到 `EventQueue`: - `TaskStatusUpdateEvent`: 对于中间更新(例如"寻找汇率...","处理汇率..."). - `TaskArtifactUpdateEvent`:当最后的答案准备好后,它被刻画成一件文物. `lastChunk` 旗帜是 `True`. - 决赛 `TaskStatusUpdateEvent` 与 `state=TaskState.TASK_STATE_COMPLETED` 发送以表示任务的结束,关闭流。 - 那个... `test_client.py`'s `run_streaming_test` 函数将打印从服务器收到的这些单个事件块。 4. **多回合对话( O)`TaskState.TASK_STATE_INPUT_REQUIRED`)**: - 那个... `CurrencyAgent` 如果一个查询含糊不清,可以要求澄清(例如,用户询问"100 USD"是多少?). - 当这种情况发生时, `CurrencyAgentExecutor` 将排队a `TaskStatusUpdateEvent` 地点 `status.state` 为: `TaskState.TASK_STATE_INPUT_REQUIRED` 财务报告和财务报告 `status.message` 包含代理的问题(例如“您要转换到哪种货币? ”)。该流在此事件之后关闭。 - 那个... `test_client.py`'s `run_multi_turn_test` 函数显示此值: - 它发出一个初步的模糊的询问. - 代理商(通过 `DefaultRequestHandler` 处理已排列的事件) `Task` 其地位为 `TASK_STATE_INPUT_REQUIRED`. - 客户端随后发出第二个信息,包括: `taskId` 财务报告和财务报告 `contextId` 从第一个转弯 `Task` 答复,以提供缺失的信息(“在GBP”中)。 ## 探索守则[¶](<https://a2a-protocol.org/latest/tutorials/python/7-streaming-and-multiturn/#exploring-the-code>) 查阅这些文件需要一些时间: - `__main__.py`: 使用服务器设置 `DefaultRequestHandler` 和 Starlette 应用程序。请注意 `AgentCard` 定义包括: `capabilities.streaming=True`. - `agent.py`编号: `CurrencyAgent` 与LangGraph,LLM模型,以及工具定义. - `agent_executor.py`编号: `CurrencyAgentExecutor` 执行《公约》的情况 `execute` (并) `cancel`方法。它使用 `RequestContext` 理解当前任务和 `EventQueue` 发送各种事件( Q)`TaskStatusUpdateEvent`, `TaskArtifactUpdateEvent`新建 `Task` 如果不存在任务,则通过第一个事件隐含的对象)。 - `test_client.py`: 演示各种互动模式,包括检索任务ID,并将其用于多回合对话. 这个例子更丰富地说明了A2A如何促进代理人之间的复杂,状态和同步互动.
查看原始来源