|
| 1 | +"""Google ADK multi-agent-remote example: same pipeline as multi-agent but with sub-agents hosted remotely via A2A. |
| 2 | +
|
| 3 | +Demonstrates how to mix local orchestration with remote agent implementations: |
| 4 | +- Coordinator and formatter run locally (they hold the orchestration logic) |
| 5 | +- Specialist sub-agents (research, code) are RemoteA2aAgent instances hosted elsewhere |
| 6 | +- The remote services don't need to know about each other — coordination stays local |
| 7 | +
|
| 8 | +Compare with the multi-agent sample: |
| 9 | + multi-agent: Agent(tools=[search_web]) Agent(tools=[run_python]) |
| 10 | + multi-agent-remote: RemoteA2aAgent(agent_card=...) for each specialist |
| 11 | +
|
| 12 | +The key insight: RemoteA2aAgent cannot have sub_agents (it's not an LlmAgent), |
| 13 | +but a local Agent CAN have RemoteA2aAgent instances as its sub_agents. This lets |
| 14 | +you keep orchestration logic local while moving implementations to remote services. |
| 15 | +""" |
| 16 | + |
| 17 | +import os |
| 18 | + |
| 19 | +import httpx |
| 20 | +from a2a.client.client import ClientConfig as A2AClientConfig |
| 21 | +from a2a.client.client_factory import ClientFactory as A2AClientFactory |
| 22 | +from a2a.types import TransportProtocol as A2ATransport |
| 23 | +from google.adk.agents import Agent, SequentialAgent |
| 24 | +from google.adk.agents.remote_a2a_agent import RemoteA2aAgent |
| 25 | +from pydantic import BaseModel, Field |
| 26 | + |
| 27 | + |
| 28 | +class ReportInput(BaseModel): |
| 29 | + """Structured input for the report generation pipeline.""" |
| 30 | + |
| 31 | + topic: str = Field( |
| 32 | + default="Natural Language Processing fundamentals", |
| 33 | + description="The topic to research and analyze", |
| 34 | + ) |
| 35 | + depth: str = Field( |
| 36 | + default="standard", |
| 37 | + description="How deep the analysis should be: 'brief', 'standard', or 'detailed'", |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +class ReportOutput(BaseModel): |
| 42 | + """Structured output from the report generation pipeline.""" |
| 43 | + |
| 44 | + title: str = Field(description="Report title") |
| 45 | + summary: str = Field(description="Executive summary of findings") |
| 46 | + key_findings: list[str] = Field(description="Key findings as bullet points") |
| 47 | + code_snippet: str = Field(description="A relevant Python code example") |
| 48 | + |
| 49 | + |
| 50 | +# UIPATH_ACCESS_TOKEN is set automatically by `uipath auth` |
| 51 | +_access_token = os.environ.get("UIPATH_ACCESS_TOKEN", "") |
| 52 | + |
| 53 | +_http_client = httpx.AsyncClient( |
| 54 | + headers={"Authorization": f"Bearer {_access_token}"}, |
| 55 | + timeout=httpx.Timeout(300.0), |
| 56 | +) |
| 57 | + |
| 58 | +_a2a_client_factory = A2AClientFactory( |
| 59 | + config=A2AClientConfig( |
| 60 | + httpx_client=_http_client, |
| 61 | + supported_transports=[A2ATransport.jsonrpc], |
| 62 | + streaming=False, |
| 63 | + polling=False, |
| 64 | + accepted_output_modes=["text"], |
| 65 | + ), |
| 66 | +) |
| 67 | + |
| 68 | +# --- Remote Sub-agents --- |
| 69 | +# Replace the URLs with your actual deployed agent endpoints. |
| 70 | +ORG_NAME = "YourOrgName" |
| 71 | +TENANT_NAME = "YourTenantName" |
| 72 | +RESEARCH_AGENT_FOLDER_KEY = "a11f72b1-90fd-4b30-b733-f0285cbf4a19" |
| 73 | +RESEARCH_AGENT_RELEASE_ID = "1234" |
| 74 | +CODE_AGENT_FOLDER_KEY = "b22f83c2-91fe-5c41-c844-g1396dcg5b2a" |
| 75 | +CODE_AGENT_RELEASE_ID = "5678" |
| 76 | + |
| 77 | +research_agent = RemoteA2aAgent( |
| 78 | + name="research_agent", |
| 79 | + agent_card=f"https://cloud.uipath.com/{ORG_NAME}/{TENANT_NAME}/agenthub_/a2a/{RESEARCH_AGENT_FOLDER_KEY}/{RESEARCH_AGENT_RELEASE_ID}/.well-known/agent-card.json", |
| 80 | + description="Remote research specialist that searches the web and summarizes findings", |
| 81 | + a2a_client_factory=_a2a_client_factory, |
| 82 | +) |
| 83 | + |
| 84 | +code_agent = RemoteA2aAgent( |
| 85 | + name="code_agent", |
| 86 | + agent_card=f"https://cloud.uipath.com/{ORG_NAME}/{TENANT_NAME}/agenthub_/a2a/{CODE_AGENT_FOLDER_KEY}/{CODE_AGENT_RELEASE_ID}/.well-known/agent-card.json", |
| 87 | + description="Remote Python developer that writes and executes code examples", |
| 88 | + a2a_client_factory=_a2a_client_factory, |
| 89 | +) |
| 90 | + |
| 91 | + |
| 92 | +# --- Coordinator (local Agent, sub_agents are remote) --- |
| 93 | +coordinator = Agent( |
| 94 | + name="coordinator", |
| 95 | + model="gemini-2.5-flash", |
| 96 | + instruction=( |
| 97 | + "You are a report coordinator. Given a topic:\n" |
| 98 | + "1. Delegate research to research_agent to gather information\n" |
| 99 | + "2. Delegate to code_agent to write a relevant Python code example\n" |
| 100 | + "3. Compile all findings into a comprehensive text report\n" |
| 101 | + "Include the research findings and the code example in your response." |
| 102 | + ), |
| 103 | + sub_agents=[research_agent, code_agent], |
| 104 | + input_schema=ReportInput, |
| 105 | + output_key="research_results", |
| 106 | +) |
| 107 | + |
| 108 | + |
| 109 | +# --- Formatter (local Agent with output_schema) --- |
| 110 | +formatter = Agent( |
| 111 | + name="formatter", |
| 112 | + model="gemini-2.5-flash", |
| 113 | + instruction=( |
| 114 | + "You are a report formatter. Take the research results from the previous " |
| 115 | + "step and format them into a structured report with a title, summary, " |
| 116 | + "key findings, and a code snippet. Output valid JSON matching the schema." |
| 117 | + ), |
| 118 | + output_schema=ReportOutput, |
| 119 | + output_key="report", |
| 120 | +) |
| 121 | + |
| 122 | + |
| 123 | +# --- Root: SequentialAgent pipeline --- |
| 124 | +agent = SequentialAgent( |
| 125 | + name="pipeline", |
| 126 | + sub_agents=[coordinator, formatter], |
| 127 | +) |
0 commit comments