|
| 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 logging |
| 16 | +import random |
| 17 | + |
| 18 | +from google.adk.agents.llm_agent import Agent |
| 19 | +from google.adk.models.gemma_llm import Gemma3Ollama |
| 20 | + |
| 21 | +litellm_logger = logging.getLogger("LiteLLM") |
| 22 | +litellm_logger.setLevel(logging.WARNING) |
| 23 | + |
| 24 | + |
| 25 | +def roll_die(sides: int) -> int: |
| 26 | + """Roll a die and return the rolled result. |
| 27 | +
|
| 28 | + Args: |
| 29 | + sides: The integer number of sides the die has. |
| 30 | +
|
| 31 | + Returns: |
| 32 | + An integer of the result of rolling the die. |
| 33 | + """ |
| 34 | + return random.randint(1, sides) |
| 35 | + |
| 36 | + |
| 37 | +async def check_prime(nums: list[int]) -> str: |
| 38 | + """Check if a given list of numbers are prime. |
| 39 | +
|
| 40 | + Args: |
| 41 | + nums: The list of numbers to check. |
| 42 | +
|
| 43 | + Returns: |
| 44 | + A str indicating which number is prime. |
| 45 | + """ |
| 46 | + primes = set() |
| 47 | + for number in nums: |
| 48 | + number = int(number) |
| 49 | + if number <= 1: |
| 50 | + continue |
| 51 | + is_prime = True |
| 52 | + for i in range(2, int(number**0.5) + 1): |
| 53 | + if number % i == 0: |
| 54 | + is_prime = False |
| 55 | + break |
| 56 | + if is_prime: |
| 57 | + primes.add(number) |
| 58 | + return ( |
| 59 | + "No prime numbers found." |
| 60 | + if not primes |
| 61 | + else f"{', '.join(str(num) for num in primes)} are prime numbers." |
| 62 | + ) |
| 63 | + |
| 64 | + |
| 65 | +root_agent = Agent( |
| 66 | + model=Gemma3Ollama(model="ollama/gemma3:12b"), |
| 67 | + name="data_processing_agent", |
| 68 | + description=( |
| 69 | + "hello world agent that can roll a dice of 8 sides and check prime" |
| 70 | + " numbers." |
| 71 | + ), |
| 72 | + instruction=""" |
| 73 | + You roll dice and answer questions about the outcome of the dice rolls. |
| 74 | + You can roll dice of different sizes. |
| 75 | + You can use multiple tools in parallel by calling functions in parallel(in one request and in one round). |
| 76 | + It is ok to discuss previous dice roles, and comment on the dice rolls. |
| 77 | + 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. |
| 78 | + You should never roll a die on your own. |
| 79 | + 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. |
| 80 | + You should not check prime numbers before calling the tool. |
| 81 | + When you are asked to roll a die and check prime numbers, you should always make the following two function calls: |
| 82 | + 1. You should first call the roll_die tool to get a roll. Wait for the function response before calling the check_prime tool. |
| 83 | + 2. After you get the function response from roll_die tool, you should call the check_prime tool with the roll_die result. |
| 84 | + 2.1 If user asks you to check primes based on previous rolls, make sure you include the previous rolls in the list. |
| 85 | + 3. When you respond, you must include the roll_die result from step 1. |
| 86 | + You should always perform the previous 3 steps when asking for a roll and checking prime numbers. |
| 87 | + You should not rely on the previous history on prime results. |
| 88 | + """, |
| 89 | + tools=[ |
| 90 | + roll_die, |
| 91 | + check_prime, |
| 92 | + ], |
| 93 | +) |
0 commit comments