A2A 客户端获取 Agent Card 并创建连接
A2A 协议 Python 教程的交互章节说明测试客户端如何从服务端获取 Agent Card,再通过 create_client 建立客户端,并展示非流式请求返回的 task 结构。
## 6. 与服务器进行交互[¶](<https://a2a-protocol.org/latest/tutorials/python/6-interact-with-server/#6-interacting-with-the-server>) 随着Helloworld A2A服务器的运行,让我们向它发送一些请求. ## Helloworld 测试客户端[¶](<https://a2a-protocol.org/latest/tutorials/python/6-interact-with-server/#the-helloworld-test-client>) 那个... `test_client.py` 脚本演示如何: 1. 从服务器中获取代理卡 。 2. 使用 `create_client`. 3. 两者都发送 `Send Message` 财务报告和财务报告 `Send Streaming Message` 请求。 打开 a **新终端窗口**,激活虚拟环境,并导航到 `a2a-samples` 目录。 激活虚拟环境( 一定要在创建该环境的同一目录中这样做 ) : Mac/LinuxWindows ``` source .venv/bin/activate ``` ``` .venv\Scripts\activate ``` 运行测试客户端 : ``` # from the a2a-samples directory python samples/python/agents/helloworld/test_client.py ``` ## 理解客户端代码[¶](<https://a2a-protocol.org/latest/tutorials/python/6-interact-with-server/#understanding-the-client-code>) 让我们看看关键部分 `test_client.py`: 1. **获取代理卡**: ``` import httpx # noqa: PLC0415 from a2a.client import A2ACardResolver # noqa: PLC0415 # Initializes the A2ACardResolver instance with an HTTP client, base URL, # and uses the default path for the agent card. async with httpx.AsyncClient() as httpx_client: resolver = A2ACardResolver( httpx_client=httpx_client, base_url='http://127.0.0.1:9999', # Provide agent_card_path, if your agent uses a different path # agent_card_path='' # noqa: ERA001 ) public_agent_card = await resolver.get_agent_card() ``` 那个... `A2ACardResolver` 课是方便的,什么时候 `get_agent_card()` 叫做,它获取 `AgentCard` 从服务器的 `/.well-known/agent-card.json` 端点(基于所提供的基础URL),然后用于初始化客户端. 2. **初始化客户端发送非 streaming 信件( S)**: ``` from a2a.client import ClientConfig, create_client # noqa: PLC0415 from a2a.helpers import new_text_message # noqa: PLC0415 from a2a.types import Role, SendMessageRequest # noqa: PLC0415 print('\nInitializing a non-streaming client.') config = ClientConfig(streaming=False) client = await create_client(agent=public_agent_card, client_config=config) # Creates a new text message to be sent to the A2A Server. # Ex: text_query = 'Why is the sky blue?' # noqa: ERA001 message = new_text_message(text_query, role=Role.ROLE_USER) request = SendMessageRequest(message=message) print('Response:') async for chunk in client.send_message(request): print(chunk) ``` - 那个... `create_client` 函数创建一个 `Client` 根据《公约》第16条第1款(a)项 `AgentCard` 备注a `ClientConfig`. - 我们构建一个 `Message` 使用 `new_text_message` 助手( 通过) `role=Role.ROLE_USER`),然后将它包裹在一个 `SendMessageRequest`. - 客户的 `send_message` 方法返回一个产生单个最终结果的同步执行器 `Task` 或 时 间 `Message` 代理商的答复。在此示例中,它是一个 `Task`. 3. **初始化客户端发送流媒体信件( S)**: ``` print('\nInitializing a streaming client.') client_config = ClientConfig(streaming=True) # Streaming client = await create_client(agent=public_agent_card, client_config=client_config) print('Response:') async for chunk in client.send_message(request): print(chunk) ``` - 创建单独的流媒体客户端 `create_client` 与 `streaming=True` 编号 `ClientConfig`. - 我们又打来电话 `send_message`,它现在流出事件: 循环的每次迭代都会在网络上到达时显示一个离散块。 - 调用 `await streaming_client.close()` 在循环后释放基础的HTTP连接. ## 预期产出[¶](<https://a2a-protocol.org/latest/tutorials/python/6-interact-with-server/#expected-output>) 你跑的时候 `test_client.py`,您可以看到输出 : - 公共代理卡,以格式化摘要显示. - 非流化反应:单一 `task` 以原型的文本格式,包含完成状态,生成的文物有"Hello, World!",以及代理商在历史上的中间状态消息. - 流体反应:四个块——初始 `task`, a `status_update` 用于WORKING, 一种 `artifact_update` 以及最终结果 `status_update` 用于COMPLETED. - 以格式摘要显示的扩展代理卡(另加 `super_hello_world` 技术). 那个... `id` 字段在输出时会随每次运行而变化。 ``` AgentCard --- General --- Name : Hello World Agent Description : Just a hello world agent Version : 0.0.1 --- Interfaces --- [0] http://127.0.0.1:9999 (JSONRPC) --- Capabilities --- Streaming : True Push notifications : False Extended agent card : True --- I/O Modes --- Input : text/plain Output : text/plain --- Skills --- ---------------------------------------------------- ID : hello_world Name : Returns hello world Description : just returns hello world Tags : hello world Example : hi Example : hello world --- Non-Streaming Call --- Non-streaming Client initialized. Response: // Non-streaming response task { id: "xxxxxxxx" context_id: "yyyyyyyy" status { state: TASK_STATE_COMPLETED } artifacts { artifact_id: "zzzzzzzz" name: "result" parts { text: "Hello, World!" } } history { message_id: "vvvvvvvv" context_id: "yyyyyyyy" task_id: "xxxxxxxx" role: ROLE_USER parts { text: "Say hello." } } history { message_id: "wwwwwwww" role: ROLE_AGENT parts { text: "Processing request..." } } } // Streaming response task { id: "xxxxxxxx-s" context_id: "yyyyyyyy-s" status { state: TASK_STATE_SUBMITTED } history { message_id: "vvvvvvvv" context_id: "yyyyyyyy-s" task_id: "xxxxxxxx-s" role: ROLE_USER parts { text: "Say hello." } } } Response chunk: status_update { task_id: "xxxxxxxx-s" context_id: "yyyyyyyy-s" status { state: TASK_STATE_WORKING message { message_id: "zzzzzzzz-s" role: ROLE_AGENT parts { text: "Processing request..." } } } } Response chunk: artifact_update { task_id: "xxxxxxxx-s" context_id: "yyyyyyyy-s" artifact { artifact_id: "wwwwwwww-s" name: "result" parts { text: "Hello, World!" } } } Response chunk: status_update { task_id: "xxxxxxxx-s" context_id: "yyyyyyyy-s" status { state: TASK_STATE_COMPLETED } } AgentCard --- General --- Name : Hello World Agent - Extended Edition Description : The full-featured hello world agent for authenticated users. Version : 0.0.2 --- Interfaces --- [0] http://127.0.0.1:9999 (JSONRPC) --- Capabilities --- Streaming : True Push notifications : False Extended agent card : True --- I/O Modes --- Input : text/plain Output : text/plain --- Skills --- ---------------------------------------------------- ID : hello_world Name : Returns hello world Description : just returns hello world Tags : hello world Example : hi Example : hello world ---------------------------------------------------- ID : super_hello_world Name : Returns a SUPER Hello World Description : A more enthusiastic greeting, only for authenticated users. Tags : hello world, super, extended Example : super hi Example : give me a super hello ``` *(实际身份证如: `xxxxxxxx`, `yyyyyyyy`, `zzzzzzzz`, `wwwwwwww`,以及 `vvvvvvvv` 在每个运行中将会是不同的UUID. )* 这证实了您的服务器正在正确处理与更新的SDK结构的基本A2A交互. 您可以在终端窗口中按 Ctrl+C 关闭服务器 。 `__main__.py` 正在运行。
查看原始来源