|
| 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 import Agent |
| 18 | +from google.adk.tools.tool_context import ToolContext |
| 19 | +from google.genai import types |
| 20 | + |
| 21 | + |
| 22 | +def roll_die(sides: int, tool_context: ToolContext) -> int: |
| 23 | + """Roll a die and return the rolled result. |
| 24 | +
|
| 25 | + Args: |
| 26 | + sides: The integer number of sides the die has. |
| 27 | +
|
| 28 | + Returns: |
| 29 | + An integer of the result of rolling the die. |
| 30 | + """ |
| 31 | + result = random.randint(1, sides) |
| 32 | + if not 'rolls' in tool_context.state: |
| 33 | + tool_context.state['rolls'] = [] |
| 34 | + |
| 35 | + tool_context.state['rolls'] = tool_context.state['rolls'] + [result] |
| 36 | + return result |
| 37 | + |
| 38 | + |
| 39 | +async def check_prime(nums: list[int]) -> str: |
| 40 | + """Check if a given list of numbers are prime. |
| 41 | +
|
| 42 | + Args: |
| 43 | + nums: The list of numbers to check. |
| 44 | +
|
| 45 | + Returns: |
| 46 | + A str indicating which number is prime. |
| 47 | + """ |
| 48 | + primes = set() |
| 49 | + for number in nums: |
| 50 | + number = int(number) |
| 51 | + if number <= 1: |
| 52 | + continue |
| 53 | + is_prime = True |
| 54 | + for i in range(2, int(number**0.5) + 1): |
| 55 | + if number % i == 0: |
| 56 | + is_prime = False |
| 57 | + break |
| 58 | + if is_prime: |
| 59 | + primes.add(number) |
| 60 | + return ( |
| 61 | + 'No prime numbers found.' |
| 62 | + if not primes |
| 63 | + else f"{', '.join(str(num) for num in primes)} are prime numbers." |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +root_agent = Agent( |
| 68 | + model='gemini-2.0-flash-live-preview-04-09', # for Vertex project |
| 69 | + # model='gemini-2.0-flash-live-001', # for AI studio key |
| 70 | + name='hello_world_agent', |
| 71 | + description=( |
| 72 | + 'hello world agent that can roll a dice of 8 sides and check prime' |
| 73 | + ' numbers.' |
| 74 | + ), |
| 75 | + instruction=""" |
| 76 | + You roll dice and answer questions about the outcome of the dice rolls. |
| 77 | + You can roll dice of different sizes. |
| 78 | + You can use multiple tools in parallel by calling functions in parallel(in one request and in one round). |
| 79 | + It is ok to discuss previous dice roles, and comment on the dice rolls. |
| 80 | + When you are asked to roll a die, you must call the roll_die tool with the number of sides. Be sure to pass in an integer. Do not pass in a string. |
| 81 | + You should never roll a die on your own. |
| 82 | + When checking prime numbers, call the check_prime tool with a list of integers. Be sure to pass in a list of integers. You should never pass in a string. |
| 83 | + You should not check prime numbers before calling the tool. |
| 84 | + When you are asked to roll a die and check prime numbers, you should always make the following two function calls: |
| 85 | + 1. You should first call the roll_die tool to get a roll. Wait for the function response before calling the check_prime tool. |
| 86 | + 2. After you get the function response from roll_die tool, you should call the check_prime tool with the roll_die result. |
| 87 | + 2.1 If user asks you to check primes based on previous rolls, make sure you include the previous rolls in the list. |
| 88 | + 3. When you respond, you must include the roll_die result from step 1. |
| 89 | + You should always perform the previous 3 steps when asking for a roll and checking prime numbers. |
| 90 | + You should not rely on the previous history on prime results. |
| 91 | + """, |
| 92 | + tools=[ |
| 93 | + roll_die, |
| 94 | + check_prime, |
| 95 | + ], |
| 96 | + generate_content_config=types.GenerateContentConfig( |
| 97 | + safety_settings=[ |
| 98 | + types.SafetySetting( # avoid false alarm about rolling dice. |
| 99 | + category=types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, |
| 100 | + threshold=types.HarmBlockThreshold.OFF, |
| 101 | + ), |
| 102 | + ] |
| 103 | + ), |
| 104 | +) |
0 commit comments