基于 LangGraph 与 A2A 协议的货币转换代理实现

该示例用 LangGraph 的 ReAct 模式构建货币转换代理,并通过 A2A 协议对外提供标准化交互,支持多轮对话、流式状态更新与 webhook 推送通知,同时说明了会话级记忆和仅文本输入输出等边界。

# LangGraph货币代理 :A2A议定书 这个样本显示一个货币兑换代理商 [LangGraph](https://langchain-ai.github.io/langgraph/) 并透过 A2A 协议曝光。它展示了对话互动,支持多回合对话和流化响应。 ## 如何运作 这个代理使用LangGraph与LLM(例如Google Gemini.)通过ReAct代理模式提供货币交换信息. A2A协议可以实现与代理的标准化互动,允许客户端发送请求并接受实时更新. ```mermaid sequenceDiagram participant Client as A2A Client participant Server as A2A Server participant Agent as LangGraph Agent participant API as Frankfurter API Client->>Server: Send task with currency query Server->>Agent: Forward query to currency agent alt Complete Information Agent->>API: Call get_exchange_rate tool API->>Agent: Return exchange rate data Agent->>Server: Process data & return result Server->>Client: Respond with currency information else Incomplete Information Agent->>Server: Request additional input Server->>Client: Set state to "input-required" Client->>Server: Send additional information Server->>Agent: Forward additional info Agent->>API: Call get_exchange_rate tool API->>Agent: Return exchange rate data Agent->>Server: Process data & return result Server->>Client: Respond with currency information end alt With Streaming Note over Client,Server: Real-time status updates Server->>Client: "Looking up exchange rates..." Server->>Client: "Processing exchange rates..." Server->>Client: Final result end ``` ## 关键特性 - **多回合对话**: 特工可在需要时要求补充资料 - **实时流线**: 处理时提供状态更新 - **推动通知**: 支持基于 Webhook 的通知 - **对话内存**: 保持互动的背景 - **货币兑换工具**: 与 Frankfurter API 结合实时费率 ## 先决条件 - Python 3.12 或更高音量 - [UV](https://docs.astral.sh/uv/) - 访问 LLM 和 API 密钥 ## 设置运行( R) 1. 导航到样本目录 : ```bash cd samples/python/agents/langgraph ``` 2. 用您的 API 密钥创建环境文件 : ```bash If you're using a Google Gemini model (gemini-pro, etc.): echo "GOOGLE_API_KEY=your_api_key_here" > .env echo "GOOGLE_MODEL=gemini-3.8-flash" >> .env (optional, overrides the default Gemini model) If you're using OpenAI or any compatible API (e.g., local LLM via Ollama, LM Studio, etc.): echo "API_KEY=your_api_key_here" > .env (not neccessary if have no api key) echo "TOOL_LLM_URL=your_llm_url" > .env echo "TOOL_LLM_NAME=your_llm_name" > .env ``` 3. 运行代理 : ```bash # Basic run on default port 10000 uv run app # On custom host/port uv run app --host 0.0.0.0 --port 8080 ``` 4. 在单独的终端中,运行测试客户端: ```bash uv run app/test_client.py ``` ## 构建容器图像 代理也可以使用容器文件构建. 1. 导航到 `samples/python/agents/langgraph` 目录 : ```bash cd samples/python/agents/langgraph ``` 2. 构建容器文件 ```bash podman build . -t langgraph-a2a-server ``` > [提普 提普 提普 提普 提普 提普] > 波德曼是替补 `docker` 也可以用于这些命令。 3. 运行您的容器 ```bash podman run -p 10000:10000 -e GOOGLE_API_KEY=your_api_key_here langgraph-a2a-server ``` 4. 运行 A2A 客户端(从上面一节走步骤 5) > [重要吗?] > * **访问 URL :** 您必须通过 A2A 客户端访问 URL `0.0.0.0:10000`。使用 `localhost` 将不工作。 > * **主机名覆盖 :** 如果部署到容器外对主机名定义不同的环境,请使用 `HOST_OVERRIDE` 在代理卡上设置预期主机名的环境变量。这确保了与客户端应用程序的适当通信。 ## 技术实施 - **LangGraph ReAct 代理服务器**: 使用 ReAct 模式进行推理和工具使用 - **流动支持**: 处理过程中提供增量更新 - **检查点内存**: 在转弯之间保持对讲状态 - **推动通知系统**: 基于 Webhook 的更新, 并带有 JWK 认证 - **A2A 协议集成**:完全遵守A2A规格. ## 限制 - 只支持基于文本的输入/输出(无多式联运支持) - 使用 Frankfurter API 的货币选项有限 - 内存基于会话, 服务器重启之间不坚持 ## 实例 **同步请求** 请: ``` POST http://localhost:10000 Content-Type: application/json { "id": "12113c25-b752-473f-977e-c9ad33cf4f56", "jsonrpc": "2.0", "method": "message/send", "params": { "message": { "kind": "message", "messageId": "120ec73f93024993becf954d03a672bc", "parts": [ { "kind": "text", "text": "how much is 10 USD in INR?" } ], "role": "user" } } } ``` 答复: ``` { "id": "12113c25-b752-473f-977e-c9ad33cf4f56", "jsonrpc": "2.0", "result": { "artifacts": [ { "artifactId": "08373241-a745-4abe-a78b-9ca60882bcc6", "name": "conversion_result", "parts": [ { "kind": "text", "text": "10 USD is 856.2 INR." } ] } ], "contextId": "e329f200-eaf4-4ae9-a8ef-a33cf9485367", "history": [ { "contextId": "e329f200-eaf4-4ae9-a8ef-a33cf9485367", "kind": "message", "messageId": "120ec73f93024993becf954d03a672bc", "parts": [ { "kind": "text", "text": "how much is 10 USD in INR?" } ], "role": "user", "taskId": "58124b63-dd3b-46b8-bf1d-1cc1aefd1c8f" }, { "contextId": "e329f200-eaf4-4ae9-a8ef-a33cf9485367", "kind": "message", "messageId": "d8b4d7de-709f-40f7-ae0c-fd6ee398a2bf", "parts": [ { "kind": "text", "text": "Looking up the exchange rates..." } ], "role": "agent", "taskId": "58124b63-dd3b-46b8-bf1d-1cc1aefd1c8f" }, { "contextId": "e329f200-eaf4-4ae9-a8ef-a33cf9485367", "kind": "message", "messageId": "ee0cb3b6-c3d6-4316-8d58-315c437a2a77", "parts": [ { "kind": "text", "text": "Processing the exchange rates.." } ], "role": "agent", "taskId": "58124b63-dd3b-46b8-bf1d-1cc1aefd1c8f" } ], "id": "58124b63-dd3b-46b8-bf1d-1cc1aefd1c8f", "kind": "task", "status": { "state": "completed" } } } ``` **多回合实例** 请求 - Seq 1 : ``` POST http://localhost:10000 Content-Type: application/json { "id": "27be771b-708f-43b8-8366-968966d07ec0", "jsonrpc": "2.0", "method": "message/send", "params": { "message": { "kind": "message", "messageId": "296eafc9233142bd98279e4055165f12", "parts": [ { "kind": "text", "text": "How much is the exchange rate for 1 USD?" } ], "role": "user" } } } ``` 回应-Seq 2: ``` { "id": "27be771b-708f-43b8-8366-968966d07ec0", "jsonrpc": "2.0", "result": { "contextId": "a7cc0bef-17b5-41fc-9379-40b99f46a101", "history": [ { "contextId": "a7cc0bef-17b5-41fc-9379-40b99f46a101", "kind": "message", "messageId": "296eafc9233142bd98279e4055165f12", "parts": [ { "kind": "text", "text": "How much is the exchange rate for 1 USD?" } ], "role": "user", "taskId": "9d94c2d4-06e4-40e1-876b-22f5a2666e61" } ], "id": "9d94c2d4-06e4-40e1-876b-22f5a2666e61", "kind": "task", "status": { "message": { "contextId": "a7cc0bef-17b5-41fc-9379-40b99f46a101", "kind": "message", "messageId": "f0f5f3ff-335c-4e77-9b4a-01ff3908e7be", "parts": [ { "kind": "text", "text": "Please specify which currency you would like to convert to." } ], "role": "agent", "taskId": "9d94c2d4-06e4-40e1-876b-22f5a2666e61" }, "state": "input-required" } } } ``` 请求 - Seq 3 : ``` POST http://localhost:10000 Content-Type: application/json { "id": "b88d818d-1192-42be-b4eb-3ee6b96a7e35", "jsonrpc": "2.0", "method": "message/send", "params": { "message": { "contextId": "a7cc0bef-17b5-41fc-9379-40b99f46a101", "kind": "message", "messageId": "70371e1f231f4597b65ccdf534930ca9", "parts": [ { "kind": "text", "text": "CAD" } ], "role": "user", "taskId": "9d94c2d4-06e4-40e1-876b-22f5a2666e61" } } } ``` 回应-Seq 4: ``` { "id": "b88d818d-1192-42be-b4eb-3ee6b96a7e35", "jsonrpc": "2.0", "result": { "artifacts": [ { "artifactId": "08373241-a745-4abe-a78b-9ca60882bcc6", "name": "conversion_result", "parts": [ { "kind": "text", "text": "The exchange rate for 1 USD to CAD is 1.3739." } ] } ], "contextId": "a7cc0bef-17b5-41fc-9379-40b99f46a101", "history": [ { "contextId": "a7cc0bef-17b5-41fc-9379-40b99f46a101", "kind": "message", "messageId": "296eafc9233142bd98279e4055165f12", "parts": [ { "kind": "text", "text": "How much is the exchange rate for 1 USD?" } ], "role": "user", "taskId": "9d94c2d4-06e4-40e1-876b-22f5a2666e61" }, { "contextId": "a7cc0bef-17b5-41fc-9379-40b99f46a101", "kind": "message", "messageId": "f0f5f3ff-335c-4e77-9b4a-01ff3908e7be", "parts": [ { "kind": "text", "text": "Please specify which currency you would like to convert to." } ], "role": "agent", "taskId": "9d94c2d4-06e4-40e1-876b-22f5a2666e61" }, { "contextId": "a7cc0bef-17b5-41fc-9379-40b99f46a101", "kind": "message", "messageId": "70371e1f231f4597b65ccdf534930ca9", "parts": [ { "kind": "text", "text": "CAD" } ], "role": "user", "taskId": "9d94c2d4-06e4-40e1-876b-22f5a2666e61" }, { "contextId": "a7cc0bef-17b5-41fc-9379-40b99f46a101", "kind": "message", "messageId": "0eb4f200-a8cd-4d34-94f8-4d223eb1b2c0", "parts": [ { "kind": "text", "text": "Looking up the exchange rates..." } ], "role": "agent", "taskId": "9d94c2d4-06e4-40e1-876b-22f5a2666e61" }, { "contextId": "a7cc0bef-17b5-41fc-9379-40b99f46a101", "kind": "message", "messageId": "41c7c03a-a772-4dc8-a868-e8c7b7defc91", "parts": [ { "kind": "text", "text": "Processing the exchange rates.." } ], "role": "agent", "taskId": "9d94c2d4-06e4-40e1-876b-22f5a2666e61" } ], "id": "9d94c2d4-06e4-40e1-876b-22f5a2666e61", "kind": "task", "status": { "state": "completed" } } } ``` **串流实例** 请: ``` { "id": "6d12d159-ec67-46e6-8d43-18480ce7f6ca", "jsonrpc": "2.0", "method": "message/stream", "params": { "message": { "kind": "message", "messageId": "2f9538ef0984471aa0d5179ce3c67a28", "parts": [ { "kind": "text", "text": "how much is 10 USD in INR?" } ], "role": "user" } } } ``` 答复: ``` data: {"id":"6d12d159-ec67-46e6-8d43-18480ce7f6ca","jsonrpc":"2.0","result":{"contextId":"cd09e369-340a-4563-bca4-e5f2e0b9ff81","history":[{"contextId":"cd09e369-340a-4563-bca4-e5f2e0b9ff81","kind":"message","messageId":"2f9538ef0984471aa0d5179ce3c67a28","parts":[{"kind":"text","text":"how much is 10 USD in INR?"}],"role":"user","taskId":"423a2569-f272-4d75-a4d1-cdc6682188e5"}],"id":"423a2569-f272-4d75-a4d1-cdc6682188e5","kind":"task","status":{"state":"submitted"}}} data: {"id":"6d12d159-ec67-46e6-8d43-18480ce7f6ca","jsonrpc":"2.0","result":{"contextId":"cd09e369-340a-4563-bca4-e5f2e0b9ff81","final":false,"kind":"status-update","status":{"message":{"contextId":"cd09e369-340a-4563-bca4-e5f2e0b9ff81","kind":"message","messageId":"1854a825-c64f-4f30-96f2-c8aa558b83f9","parts":[{"kind":"text","text":"Looking up the exchange rates..."}],"role":"agent","taskId":"423a2569-f272-4d75-a4d1-cdc6682188e5"},"state":"working"},"taskId":"423a2569-f272-4d75-a4d1-cdc6682188e5"}} data: {"id":"6d12d159-ec67-46e6-8d43-18480ce7f6ca","jsonrpc":"2.0","result":{"contextId":"cd09e369-340a-4563-bca4-e5f2e0b9ff81","final":false,"kind":"status-update","status":{"message":{"contextId":"cd09e369-340a-4563-bca4-e5f2e0b9ff81","kind":"message","messageId":"e72127a6-4830-4320-bf23-235ac79b9a13","parts":[{"kind":"text","text":"Processing the exchange rates.."}],"role":"agent","taskId":"423a2569-f272-4d75-a4d1-cdc6682188e5"},"state":"working"},"taskId":"423a2569-f272-4d75-a4d1-cdc6682188e5"}} data: {"id":"6d12d159-ec67-46e6-8d43-18480ce7f6ca","jsonrpc":"2.0","result":{"artifact":{"artifactId":"08373241-a745-4abe-a78b-9ca60882bcc6","name":"conversion_result","parts":[{"kind":"text","text":"10 USD is 856.2 INR."}]},"contextId":"cd09e369-340a-4563-bca4-e5f2e0b9ff81","kind":"artifact-update","taskId":"423a2569-f272-4d75-a4d1-cdc6682188e5"}} data: {"id":"6d12d159-ec67-46e6-8d43-18480ce7f6ca","jsonrpc":"2.0","result":{"contextId":"cd09e369-340a-4563-bca4-e5f2e0b9ff81","final":true,"kind":"status-update","status":{"state":"completed"},"taskId":"423a2569-f272-4d75-a4d1-cdc6682188e5"}} ``` ## 学习更多 - [A2A 协议文档](https://a2a-protocol.org/) - [LangGraph 文档](https://langchain-ai.github.io/langgraph/) - [法兰克福 API](https://frankfurter.dev/docs/) - [谷歌双子座 API](https://ai.google.dev/gemini-api) ## 免责声明 重要的: 提供的样本代码是用于演示目的, 并说明了代理对代理( A2A) 协议的力学。 在构建生产应用程序时, 将任何在您直接控制下运行的代理视为一个潜在的不信任实体至关重要 。 从外部代理机构收到的所有数据——包括但不限于其AgentCard、消息、文物和任务状态——应作为不可信的输入处理。 例如,恶意代理机构可以提供包含其领域(例如描述、名称、技能.描述)的精心制作的数据的AgentCard。如果使用此数据时没有进行消毒以构建大语言模型( LLM ) 的提示, 可能会使您的应用面临迅速的注射攻击。 如果在使用之前无法正确验证和消毒此数据, 可能会给您的应用带来安全弱点 。 开发者负责实施适当的安全措施,如输入验证和对证书的安全处理,以保护他们的系统和用户.

查看原始来源