|
| 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 | +import random |
| 16 | + |
| 17 | +from google.adk.agents import Agent |
| 18 | +from google.adk.examples.example import Example |
| 19 | +from google.adk.tools.example_tool import ExampleTool |
| 20 | +from google.genai import types |
| 21 | + |
| 22 | + |
| 23 | +# --- Roll Die Sub-Agent --- |
| 24 | +def roll_die(sides: int) -> int: |
| 25 | + """Roll a die and return the rolled result.""" |
| 26 | + return random.randint(1, sides) |
| 27 | + |
| 28 | + |
| 29 | +roll_agent = Agent( |
| 30 | + name="roll_agent", |
| 31 | + description="Handles rolling dice of different sizes.", |
| 32 | + instruction=""" |
| 33 | + You are responsible for rolling dice based on the user's request. |
| 34 | + When asked to roll a die, you must call the roll_die tool with the number of sides as an integer. |
| 35 | + """, |
| 36 | + tools=[roll_die], |
| 37 | + generate_content_config=types.GenerateContentConfig( |
| 38 | + safety_settings=[ |
| 39 | + types.SafetySetting( # avoid false alarm about rolling dice. |
| 40 | + category=types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, |
| 41 | + threshold=types.HarmBlockThreshold.OFF, |
| 42 | + ), |
| 43 | + ] |
| 44 | + ), |
| 45 | +) |
| 46 | + |
| 47 | + |
| 48 | +# --- Prime Check Sub-Agent --- |
| 49 | +def check_prime(nums: list[int]) -> str: |
| 50 | + """Check if a given list of numbers are prime.""" |
| 51 | + primes = set() |
| 52 | + for number in nums: |
| 53 | + number = int(number) |
| 54 | + if number <= 1: |
| 55 | + continue |
| 56 | + is_prime = True |
| 57 | + for i in range(2, int(number**0.5) + 1): |
| 58 | + if number % i == 0: |
| 59 | + is_prime = False |
| 60 | + break |
| 61 | + if is_prime: |
| 62 | + primes.add(number) |
| 63 | + return ( |
| 64 | + "No prime numbers found." |
| 65 | + if not primes |
| 66 | + else f"{', '.join(str(num) for num in primes)} are prime numbers." |
| 67 | + ) |
| 68 | + |
| 69 | + |
| 70 | +prime_agent = Agent( |
| 71 | + name="prime_agent", |
| 72 | + description="Handles checking if numbers are prime.", |
| 73 | + instruction=""" |
| 74 | + You are responsible for checking whether numbers are prime. |
| 75 | + When asked to check primes, you must call the check_prime tool with a list of integers. |
| 76 | + Never attempt to determine prime numbers manually. |
| 77 | + Return the prime number results to the root agent. |
| 78 | + """, |
| 79 | + tools=[check_prime], |
| 80 | + generate_content_config=types.GenerateContentConfig( |
| 81 | + safety_settings=[ |
| 82 | + types.SafetySetting( # avoid false alarm about rolling dice. |
| 83 | + category=types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, |
| 84 | + threshold=types.HarmBlockThreshold.OFF, |
| 85 | + ), |
| 86 | + ] |
| 87 | + ), |
| 88 | +) |
| 89 | + |
| 90 | + |
| 91 | +def get_current_weather(location: str): |
| 92 | + """ |
| 93 | + Returns the current weather. |
| 94 | + """ |
| 95 | + if location == "New York": |
| 96 | + return "Sunny" |
| 97 | + else: |
| 98 | + return "Raining" |
| 99 | + |
| 100 | + |
| 101 | +root_agent = Agent( |
| 102 | + # find supported models here: https://google.github.io/adk-docs/get-started/streaming/quickstart-streaming/ |
| 103 | + # model='gemini-live-2.5-flash-preview-native-audio', # for Vertex project |
| 104 | + model="gemini-live-2.5-flash-preview", # for AI studio key |
| 105 | + name="root_agent", |
| 106 | + instruction=""" |
| 107 | + You are a helpful assistant that can check time, roll dice and check if numbers are prime. |
| 108 | + You can check time on your own. |
| 109 | + You delegate rolling dice tasks to the roll_agent and prime checking tasks to the prime_agent. |
| 110 | + Follow these steps: |
| 111 | + 1. If the user asks to roll a die, delegate to the roll_agent. |
| 112 | + 2. If the user asks to check primes, delegate to the prime_agent. |
| 113 | + 3. If the user asks to roll a die and then check if the result is prime, call roll_agent first, then pass the result to prime_agent. |
| 114 | + Always clarify the results before proceeding. |
| 115 | + """, |
| 116 | + global_instruction=( |
| 117 | + "You are DicePrimeBot, ready to roll dice and check prime numbers." |
| 118 | + ), |
| 119 | + sub_agents=[roll_agent, prime_agent], |
| 120 | + tools=[get_current_weather], |
| 121 | + generate_content_config=types.GenerateContentConfig( |
| 122 | + safety_settings=[ |
| 123 | + types.SafetySetting( # avoid false alarm about rolling dice. |
| 124 | + category=types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, |
| 125 | + threshold=types.HarmBlockThreshold.OFF, |
| 126 | + ), |
| 127 | + ] |
| 128 | + ), |
| 129 | +) |
0 commit comments