|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from google.adk import Agent |
| 16 | +from google.adk.agents import sequential_agent |
| 17 | +from google.adk.tools import tool_context |
| 18 | +from pydantic import BaseModel |
| 19 | + |
| 20 | +SequentialAgent = sequential_agent.SequentialAgent |
| 21 | +ToolContext = tool_context.ToolContext |
| 22 | + |
| 23 | + |
| 24 | +# 1. Define the data structure for the pizza order. |
| 25 | +class PizzaOrder(BaseModel): |
| 26 | + """A data class to hold the details of a pizza order.""" |
| 27 | + |
| 28 | + size: str |
| 29 | + crust: str |
| 30 | + toppings: list[str] |
| 31 | + |
| 32 | + |
| 33 | +# 2. Define tools for the order intake agent. |
| 34 | +def get_available_sizes() -> list[str]: |
| 35 | + """Returns the available pizza sizes.""" |
| 36 | + return ['small', 'medium', 'large'] |
| 37 | + |
| 38 | + |
| 39 | +def get_available_crusts() -> list[str]: |
| 40 | + """Returns the available pizza crusts.""" |
| 41 | + return ['thin', 'thick', 'stuffed'] |
| 42 | + |
| 43 | + |
| 44 | +def get_available_toppings() -> list[str]: |
| 45 | + """Returns the available pizza toppings.""" |
| 46 | + return ['pepperoni', 'mushrooms', 'onions', 'sausage', 'bacon', 'pineapple'] |
| 47 | + |
| 48 | + |
| 49 | +# 3. Define the order intake agent. |
| 50 | +# This agent's job is to interact with the user to fill out a PizzaOrder object. |
| 51 | +# It uses the output_schema to structure its response as a JSON object that |
| 52 | +# conforms to the PizzaOrder model. |
| 53 | +order_intake_agent = Agent( |
| 54 | + name='order_intake_agent', |
| 55 | + model='gemini-2.5-flash', |
| 56 | + instruction=( |
| 57 | + "You are a pizza order intake agent. Your goal is to get the user's" |
| 58 | + ' pizza order. Use the available tools to find out what sizes, crusts,' |
| 59 | + ' and toppings are available. Once you have all the information,' |
| 60 | + ' provide it in the requested format. Your output MUST be a JSON object' |
| 61 | + ' that conforms to the PizzaOrder schema and nothing else.' |
| 62 | + ), |
| 63 | + output_key='pizza_order', |
| 64 | + output_schema=PizzaOrder, |
| 65 | + tools=[get_available_sizes, get_available_crusts, get_available_toppings], |
| 66 | +) |
| 67 | + |
| 68 | + |
| 69 | +# 4. Define a tool for the order confirmation agent. |
| 70 | +def calculate_price(tool_context: ToolContext) -> str: |
| 71 | + """Calculates the price of a pizza order and returns a descriptive string.""" |
| 72 | + order_dict = tool_context.state.get('pizza_order') |
| 73 | + if not order_dict: |
| 74 | + return "I can't find an order to calculate the price for." |
| 75 | + |
| 76 | + order = PizzaOrder.model_validate(order_dict) |
| 77 | + |
| 78 | + price = 0.0 |
| 79 | + if order.size == 'small': |
| 80 | + price += 8.0 |
| 81 | + elif order.size == 'medium': |
| 82 | + price += 10.0 |
| 83 | + elif order.size == 'large': |
| 84 | + price += 12.0 |
| 85 | + |
| 86 | + if order.crust == 'stuffed': |
| 87 | + price += 2.0 |
| 88 | + |
| 89 | + price += len(order.toppings) * 1.5 |
| 90 | + return f'The total price for your order is ${price:.2f}.' |
| 91 | + |
| 92 | + |
| 93 | +# 5. Define the order confirmation agent. |
| 94 | +# This agent reads the PizzaOrder object from the session state (placed there by |
| 95 | +# the order_intake_agent) and confirms the order with the user. |
| 96 | +order_confirmation_agent = Agent( |
| 97 | + name='order_confirmation_agent', |
| 98 | + model='gemini-2.5-flash', |
| 99 | + instruction=( |
| 100 | + 'Confirm the pizza order with the user. The order is in the state' |
| 101 | + ' variable `pizza_order`. First, use the `calculate_price` tool to get' |
| 102 | + ' the price. Then, summarize the order details from {pizza_order} and' |
| 103 | + ' include the price in your summary. For example: "You ordered a large' |
| 104 | + ' thin crust pizza with pepperoni and mushrooms. The total price is' |
| 105 | + ' $15.00."' |
| 106 | + ), |
| 107 | + tools=[calculate_price], |
| 108 | +) |
| 109 | + |
| 110 | +# 6. Define the root agent as a sequential agent. |
| 111 | +# This agent directs the conversation by running its sub-agents in order. |
| 112 | +root_agent = SequentialAgent( |
| 113 | + name='pizza_ordering_agent', |
| 114 | + sub_agents=[ |
| 115 | + order_intake_agent, |
| 116 | + order_confirmation_agent, |
| 117 | + ], |
| 118 | + description=( |
| 119 | + 'This agent is used to order pizza. It will ask the user for their' |
| 120 | + ' pizza order and then confirm the order with the user.' |
| 121 | + ), |
| 122 | +) |
0 commit comments