chore: import upstream snapshot with attribution
Continuous Integration / Pre-commit Linter (push) Waiting to run
Continuous Integration / Mypy Check (Python 3.10) (push) Waiting to run
Continuous Integration / Mypy Check (Python 3.11) (push) Waiting to run
Continuous Integration / Mypy Check (Python 3.12) (push) Waiting to run
Continuous Integration / Mypy Check (Python 3.13) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.10) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.11) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.12) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.13) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.14) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.10) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.11) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.12) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.13) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.14) (push) Waiting to run
Copybara PR Handler / close-imported-pr (push) Waiting to run
Continuous Integration / Pre-commit Linter (push) Waiting to run
Continuous Integration / Mypy Check (Python 3.10) (push) Waiting to run
Continuous Integration / Mypy Check (Python 3.11) (push) Waiting to run
Continuous Integration / Mypy Check (Python 3.12) (push) Waiting to run
Continuous Integration / Mypy Check (Python 3.13) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.10) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.11) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.12) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.13) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.14) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.10) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.11) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.12) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.13) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.14) (push) Waiting to run
Copybara PR Handler / close-imported-pr (push) Waiting to run
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
# JSON Passing Agent
|
||||
|
||||
This sample demonstrates how to pass structured JSON data between agents. The example uses a pizza ordering scenario where one agent takes the order and passes it to another agent for confirmation.
|
||||
|
||||
## How to run
|
||||
|
||||
1. Run the agent:
|
||||
|
||||
```bash
|
||||
adk run .
|
||||
```
|
||||
|
||||
2. Talk to the agent:
|
||||
|
||||
```
|
||||
I want to order a pizza
|
||||
```
|
||||
|
||||
## Example conversation
|
||||
|
||||
```
|
||||
[user]: I'd like a large pizza with pepperoni and mushrooms on a thin crust.
|
||||
[order_intake_agent]: (tool call to get available sizes, crusts, toppings)
|
||||
[order_intake_agent]: (returns a PizzaOrder JSON)
|
||||
[order_confirmation_agent]: (tool call to calculate_price)
|
||||
[order_confirmation_agent]: You ordered a large thin crust pizza with pepperoni and mushrooms. The total price is $15.00.
|
||||
```
|
||||
@@ -0,0 +1,15 @@
|
||||
# Copyright 2026 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from . import agent
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
# Copyright 2026 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from google.adk import Agent
|
||||
from google.adk.agents import sequential_agent
|
||||
from google.adk.tools import tool_context
|
||||
from pydantic import BaseModel
|
||||
|
||||
SequentialAgent = sequential_agent.SequentialAgent
|
||||
ToolContext = tool_context.ToolContext
|
||||
|
||||
|
||||
# 1. Define the data structure for the pizza order.
|
||||
class PizzaOrder(BaseModel):
|
||||
"""A data class to hold the details of a pizza order."""
|
||||
|
||||
size: str
|
||||
crust: str
|
||||
toppings: list[str]
|
||||
|
||||
|
||||
# 2. Define tools for the order intake agent.
|
||||
def get_available_sizes() -> list[str]:
|
||||
"""Returns the available pizza sizes."""
|
||||
return ['small', 'medium', 'large']
|
||||
|
||||
|
||||
def get_available_crusts() -> list[str]:
|
||||
"""Returns the available pizza crusts."""
|
||||
return ['thin', 'thick', 'stuffed']
|
||||
|
||||
|
||||
def get_available_toppings() -> list[str]:
|
||||
"""Returns the available pizza toppings."""
|
||||
return ['pepperoni', 'mushrooms', 'onions', 'sausage', 'bacon', 'pineapple']
|
||||
|
||||
|
||||
# 3. Define the order intake agent.
|
||||
# This agent's job is to interact with the user to fill out a PizzaOrder object.
|
||||
# It uses the output_schema to structure its response as a JSON object that
|
||||
# conforms to the PizzaOrder model.
|
||||
order_intake_agent = Agent(
|
||||
name='order_intake_agent',
|
||||
instruction=(
|
||||
"You are a pizza order intake agent. Your goal is to get the user's"
|
||||
' pizza order. Use the available tools to find out what sizes, crusts,'
|
||||
' and toppings are available. Once you have all the information,'
|
||||
' provide it in the requested format. Your output MUST be a JSON object'
|
||||
' that conforms to the PizzaOrder schema and nothing else.'
|
||||
),
|
||||
output_key='pizza_order',
|
||||
output_schema=PizzaOrder,
|
||||
tools=[get_available_sizes, get_available_crusts, get_available_toppings],
|
||||
)
|
||||
|
||||
|
||||
# 4. Define a tool for the order confirmation agent.
|
||||
def calculate_price(tool_context: ToolContext) -> str:
|
||||
"""Calculates the price of a pizza order and returns a descriptive string."""
|
||||
order_dict = tool_context.state.get('pizza_order')
|
||||
if not order_dict:
|
||||
return "I can't find an order to calculate the price for."
|
||||
|
||||
order = PizzaOrder.model_validate(order_dict)
|
||||
|
||||
price = 0.0
|
||||
if order.size == 'small':
|
||||
price += 8.0
|
||||
elif order.size == 'medium':
|
||||
price += 10.0
|
||||
elif order.size == 'large':
|
||||
price += 12.0
|
||||
|
||||
if order.crust == 'stuffed':
|
||||
price += 2.0
|
||||
|
||||
price += len(order.toppings) * 1.5
|
||||
return f'The total price for your order is ${price:.2f}.'
|
||||
|
||||
|
||||
# 5. Define the order confirmation agent.
|
||||
# This agent reads the PizzaOrder object from the session state (placed there by
|
||||
# the order_intake_agent) and confirms the order with the user.
|
||||
order_confirmation_agent = Agent(
|
||||
name='order_confirmation_agent',
|
||||
instruction=(
|
||||
'Confirm the pizza order with the user. The order is in the state'
|
||||
' variable `pizza_order`. First, use the `calculate_price` tool to get'
|
||||
' the price. Then, summarize the order details from {pizza_order} and'
|
||||
' include the price in your summary. For example: "You ordered a large'
|
||||
' thin crust pizza with pepperoni and mushrooms. The total price is'
|
||||
' $15.00."'
|
||||
),
|
||||
tools=[calculate_price],
|
||||
)
|
||||
|
||||
# 6. Define the root agent as a sequential agent.
|
||||
# This agent directs the conversation by running its sub-agents in order.
|
||||
root_agent = SequentialAgent(
|
||||
name='pizza_ordering_agent',
|
||||
sub_agents=[
|
||||
order_intake_agent,
|
||||
order_confirmation_agent,
|
||||
],
|
||||
description=(
|
||||
'This agent is used to order pizza. It will ask the user for their'
|
||||
' pizza order and then confirm the order with the user.'
|
||||
),
|
||||
)
|
||||
@@ -0,0 +1,68 @@
|
||||
# Copyright 2026 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import asyncio
|
||||
import time
|
||||
|
||||
import agent
|
||||
from dotenv import load_dotenv
|
||||
from google.adk.cli.utils import logs
|
||||
from google.adk.runners import InMemoryRunner
|
||||
from google.adk.sessions.session import Session
|
||||
from google.genai import types
|
||||
|
||||
load_dotenv(override=True)
|
||||
logs.log_to_tmp_folder()
|
||||
|
||||
|
||||
async def main():
|
||||
"""Runs the pizza ordering agent."""
|
||||
app_name = 'pizza_app'
|
||||
user_id = 'user1'
|
||||
runner = InMemoryRunner(
|
||||
agent=agent.root_agent,
|
||||
app_name=app_name,
|
||||
)
|
||||
session = await runner.session_service.create_session(
|
||||
app_name=app_name, user_id=user_id
|
||||
)
|
||||
|
||||
async def run_prompt(session: Session, new_message: str):
|
||||
content = types.Content(
|
||||
role='user', parts=[types.Part.from_text(text=new_message)]
|
||||
)
|
||||
print(f'** User says: {new_message}')
|
||||
async for event in runner.run_async(
|
||||
user_id=user_id,
|
||||
session_id=session.id,
|
||||
new_message=content,
|
||||
):
|
||||
if event.content and event.content.parts and event.content.parts[0].text:
|
||||
print(f'** {event.author}: {event.content.parts[0].text}')
|
||||
|
||||
start_time = time.time()
|
||||
print('Start time:', time.ctime(start_time))
|
||||
print('------------------------------------')
|
||||
await run_prompt(
|
||||
session,
|
||||
"I'd like a large pizza with pepperoni and mushrooms on a thin crust.",
|
||||
)
|
||||
print('------------------------------------')
|
||||
end_time = time.time()
|
||||
print('End time:', time.ctime(end_time))
|
||||
print(f'Total time: {end_time - start_time:.2f} seconds')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user