用路由工厂搭建并启动 A2A 服务端

介绍 A2A Python SDK 通过路由工厂和辅助函数创建服务端路由,并以 Starlette 配合 Uvicorn 启动 Helloworld 服务端的基本流程。

## 5. 启动服务器[¶](<https://a2a-protocol.org/latest/tutorials/python/5-start-server/#5-starting-the-server>) 现在有了代理卡和代理执行员,可以设置并启动A2A服务器. 要设置一个A2A服务器,Python SDK提供了路由工厂和帮助器功能(L.`create_agent_card_routes`, `create_jsonrpc_routes`, `create_rest_routes`。使用路由厂为 A2A 服务器服务创建路由。这些路由可以本地附着在流行框架上,如 [星罗棋布](<https://www.starlette.dev/>) 财务报告和财务报告 [FastAPI](<https://fastapi.tiangolo.com/>),可以更好地控制认证、记录和其他特性。 在这个教程中,我们将使用 Starlette 与 [乌云](<https://uvicorn.dev/>). ## HelloWorld 服务器设置[¶](<https://a2a-protocol.org/latest/tutorials/python/5-start-server/#server-setup-in-helloworld>) 让我们看看 `__main__.py` 再次查看服务器是如何初始化并启动的 。 ``` import uvicorn from a2a.server.request_handlers import DefaultRequestHandler from a2a.server.routes import ( create_agent_card_routes, create_jsonrpc_routes, ) from a2a.server.tasks import InMemoryTaskStore from a2a.types import ( AgentCapabilities, AgentCard, AgentInterface, AgentSkill, ) from agent_executor import ( HelloWorldAgentExecutor, # type: ignore[import-untyped] ) from starlette.applications import Starlette if __name__ == '__main__': # Defines the abilities or functions that agent can perform. skill = AgentSkill( id='echo_bot', name='Echo Bot', description='An example agent that acknowledges client request and responds with a "Hello World" message.', input_modes=['text/plain'], output_modes=['text/plain'], tags=['a2a', 'echo-example'], examples=['hi', 'how are you'], ) # Defines an optional additional skill for the agent that is not visible in the public card. extended_skill = AgentSkill( id='echo_bot_super_mode', name='Echo Bot (Super Mode)', description='An extended version of Echo Bot that responds with extra enthusiasm!', tags=['a2a', 'echo-example', 'extended'], examples=['super hi', 'give me a super hello'], ) # Define a public-facing agent card that allows clients to discover your agent's capabilities. public_agent_card = AgentCard( # Basic identity information of A2A server name='Hello World Agent', # Identity description='Just a hello world agent', version='0.0.1', # Default Media Types for the agent's interactions default_input_modes=['text/plain'], # Supported media types default_output_modes=['text/plain'], # Supported A2A features (like streaming or extended config) capabilities=AgentCapabilities(streaming=True, extended_agent_card=True), # Ordered list of endpoints and protocols where the service can be reached supported_interfaces=[ AgentInterface( protocol_binding='JSONRPC', url='http://127.0.0.1:9999', protocol_version='1.0', ) ], # The list of AgentSkill objects that this agent offers skills=[skill], # Optional attributes (omitted here for simplicity): # icon_url -> A URL to an icon representing the agent ) # Defines the authenticated extended agent card with # extended skills that are visible only to authenticated users extended_agent_card = AgentCard( name='Hello World Agent - Extended Edition', description='The full-featured hello world agent for authenticated users.', version='0.0.2', default_input_modes=['text/plain'], default_output_modes=['text/plain'], capabilities=AgentCapabilities(streaming=True, extended_agent_card=True), supported_interfaces=[ AgentInterface( protocol_binding='JSONRPC', url='http://127.0.0.1:9999', protocol_version='1.0', ) ], skills=[ skill, extended_skill, ], # Both skills for the extended card ) # The RequestHandler processes incoming requests and manages tasks request_handler = DefaultRequestHandler( # Agent executor handles the execution of the client requests agent_executor=HelloWorldAgentExecutor(), # The task_store is used to store and manage tasks task_store=InMemoryTaskStore(), # Public agent card for unauthenticated users agent_card=public_agent_card, # Extended agent card for authenticated users extended_agent_card=extended_agent_card, ) # Creating the routes for the A2A server # These routes handle the incoming requests from the clients # and the outgoing responses to the clients routes = [] # Create routes for the agent card routes.extend(create_agent_card_routes(public_agent_card)) # Create routes for the JSONRPC protocol # Alternatively, you can choose GRPC or HTTP_JSON as protocol bindings # based on your requirements routes.extend(create_jsonrpc_routes(request_handler, '/')) # Create a web app with the defined routes # Here we are using Starlette, a lightweight ASGI web framework to serve the agent # Alternatively, you can choose FastAPI or other ASGI frameworks app = Starlette(routes=routes) # Run the app # Uvicorn is a production-ready ASGI HTTP server uvicorn.run(app, host='127.0.0.1', port=9999) ``` 我们来分解一下: 1. **`DefaultRequestHandler`**: - SDK 提供 `DefaultRequestHandler`。这个处理器带走了您的 `AgentExecutor` B. 执行情况(续)`HelloWorldAgentExecutor`), a `TaskStore` (`InMemoryTaskStore`),以及公众和扩展的 `AgentCard` 对象。 - 它会引导进入的 A2A RPC 在您的执行器上调用适当的方法( 如 `execute` 或 时 间 `cancel`). - 那个... `TaskStore` 用于: `DefaultRequestHandler` 用于管理任务寿命周期,特别是用于状态交互、流线和重新订阅。即使您的代理执行器是简单的,处理器也需要一个任务存储。 - `agent_card` 被传递到处理器,以便在处理收到的请求时验证代理商所声明的能力。例如,它在处理这些请求类型之前检查是否支持流式或按下通知。 - `extended_agent_card` 被传递,以便操作员 服务它通过 `GetExtendedAgentCard` RPC方法认证客户端. 2. **`create_agent_card_routes` 财务报告和财务报告 `create_jsonrpc_routes`**: - `create_agent_card_routes(public_agent_card)` 返回显示代理卡的Starlette路径 `/.well-known/agent-card.json` 公众发现的终点。 - `create_jsonrpc_routes(request_handler, '/')` 返回处理所有来袭的星际路由A2AJSON - 亚森 - JSON -RPC方法调用,通过授权给 `request_handler`. - 这些路线列表合并后被传递到一个标准 `Starlette` 应用。 3. **`uvicorn.run(app, ...)`**: - A. 已建成的建筑 `Starlette` 应用程序正在使用 `uvicorn.run()`,使您的代理在 HTTP 上访问。 - `host='127.0.0.1'` 使得服务器只能从您的本地机器访问 。 - `port=9999` 指定要监听的端口。这与定义的结束点匹配 `AgentCard`'s `supported_interfaces`. ## 运行 Helloworld 服务器[¶](<https://a2a-protocol.org/latest/tutorials/python/5-start-server/#running-the-helloworld-server>) 导航到 `a2a-samples` 终端中的目录( 如果您还没有在那里) , 并确保您的虚拟环境被激活 。 要运行 Helloworld 服务器 : ``` # from the a2a-samples directory python samples/python/agents/helloworld/__main__.py ``` 您应该看到类似此的输出, 显示服务器正在运行 : ``` INFO: Started server process [xxxxx] INFO: Waiting for application startup. INFO: Application startup complete. INFO: Uvicorn running on http://127.0.0.1:9999 (Press CTRL+C to quit) ``` 你们的A2A Helloworld代理现在现场直播,并收听请求!下一步我们将与它互动.

查看原始来源