-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Description
I have two remote agents, VegetableSeller and FruitSeller. And one Orchestrator agent that is working with both the remote agents. And I am using A2A protocol. Here is code for both.
full code [https://github.com/Vardhman811/a2a-vj]
https://github.com/Vardhman811/a2a-vj
fruit seller
`from google.adk.a2a.utils.agent_to_a2a import to_a2a
from google.adk.agents import Agent
🥝 Inventory of fruits
inventory = {
"apple": {"price": 20, "stock": 10},
"banana": {"price": 10, "stock": 10},
"orange": {"price": 15, "stock": 10}
}
✅ Tool: Show available fruits
def show_fruits() -> str:
"""List available fruits with their prices."""
return ",".join([f"{fruit.capitalize()}: ₹{data['price']}" for fruit, data in inventory.items()])
✅ Tool: Buy a fruit
def buy_fruit(fruit: str, quantity: int) -> str:
"""Buy a fruit from the inventory."""
if fruit not in inventory:
return f"Sorry, {fruit} is not available."
if inventory[fruit]["stock"] < quantity:
return f"Sorry, we only have {inventory[fruit]['stock']} {fruit} in stock."
inventory[fruit]["stock"] -= quantity
return f"You have bought {quantity} {fruit} for ₹{inventory[fruit]['price'] * quantity}."
✅ Root ADK Agent
root_agent = Agent(
name="fruit_seller_agent",
description="A fruit seller who sells fruits to customers",
model="gemini-2.0-flash",
instruction="""You can ask me what fruits are available or buy fruits like apples, bananas, and oranges. You can also ask me to show the inventory. Output should be in markdown format with bullets if needed.
- If the user asks for a fruit that is not available, say that it is not available.
- If the user asks for a fruit that is available, say that it is available and the price.
- If the user asks for the inventory, show the inventory.
- If the user asks to buy a fruit, say that you will buy the fruit and the price.
- If the user asks to buy a fruit with a quantity, say that you will buy the fruit and the price.
- If the user asks to buy a fruit with a quantity that is not available, say that you will buy the fruit and the price.
- Use LLM to respond to the user's question that are not in the tools.
""",
tools=[show_fruits, buy_fruit],
)
Expose the agent via A2A
from google.adk.a2a.utils.agent_to_a2a import to_a2a
Make your agent A2A-compatible and expose it
a2a_app = to_a2a(root_agent, port=8002)
`
Vegetable is similar code
orchestrator_agent
`from google.adk.agents import Agent
from google.adk.agents.remote_a2a_agent import RemoteA2aAgent
from a2a.utils.constants import AGENT_CARD_WELL_KNOWN_PATH
import os
Load environment variables (Optional, if you are using environment variables)
from dotenv import load_dotenv
load_dotenv()
Remote agents (Fruit Seller and Vegetable Seller)
fruit_agent = RemoteA2aAgent(
name="fruit_agent",
description="Agent that handles fruit sales.",
agent_card=f"http://127.0.0.1:8001/{AGENT_CARD_WELL_KNOWN_PATH}",
)
vegetable_agent = RemoteA2aAgent(
name="vegetable_agent",
description="Agent that handles vegetable sales.",
agent_card=f"http://127.0.0.1:8002/{AGENT_CARD_WELL_KNOWN_PATH}",
)
Orchestrator Agent that delegates tasks
root_agent = Agent(
model="gemini-2.0-flash",
name="orchestrator_agent",
description="Orchestrates between Fruit and Vegetable Seller Agents.",
instruction="""
You are an orchestrator that can handle fruit and vegetable sales.
- If the user asks for fruits, delegate to the fruit_agent.
- If the user asks for vegetables, delegate to the vegetable_agent.
- If the user asks to buy a fruit, delegate to the fruit_agent.
- If the user asks to buy a vegetable, delegate to the vegetable_agent.
- If the user asks for both, first handle fruits, then vegetables.
""",
global_instruction="You are a sales orchestrator that communicates with fruit and vegetable agents.",
sub_agents=[fruit_agent, vegetable_agent], # Includes both remote agents
)
`
When I run both the host agent and the orchestrator agent and I run any query in orchestrator agent like give me the list of root, it does not give any, it just gives the errors regarding missing keys or projects.I have separate .env file for each agent.
Missing key inputs argument! To use the Google AI API, provide (api_key) arguments. To use the Google Cloud API, provide (vertexai, project & location) arguments.