|
| 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.planners import BuiltInPlanner |
| 19 | +from google.adk.planners import PlanReActPlanner |
| 20 | +from google.adk.tools.tool_context import ToolContext |
| 21 | +from google.genai import types |
| 22 | + |
| 23 | + |
| 24 | +def roll_die(sides: int, tool_context: ToolContext) -> int: |
| 25 | + """Roll a die and return the rolled result. |
| 26 | +
|
| 27 | + Args: |
| 28 | + sides: The integer number of sides the die has. |
| 29 | +
|
| 30 | + Returns: |
| 31 | + An integer of the result of rolling the die. |
| 32 | + """ |
| 33 | + result = random.randint(1, sides) |
| 34 | + if not 'rolls' in tool_context.state: |
| 35 | + tool_context.state['rolls'] = [] |
| 36 | + |
| 37 | + tool_context.state['rolls'] = tool_context.state['rolls'] + [result] |
| 38 | + return result |
| 39 | + |
| 40 | + |
| 41 | +async def check_prime(nums: list[int]) -> str: |
| 42 | + """Check if a given list of numbers are prime. |
| 43 | +
|
| 44 | + Args: |
| 45 | + nums: The list of numbers to check. |
| 46 | +
|
| 47 | + Returns: |
| 48 | + A str indicating which number is prime. |
| 49 | + """ |
| 50 | + primes = set() |
| 51 | + for number in nums: |
| 52 | + number = int(number) |
| 53 | + if number <= 1: |
| 54 | + continue |
| 55 | + is_prime = True |
| 56 | + for i in range(2, int(number**0.5) + 1): |
| 57 | + if number % i == 0: |
| 58 | + is_prime = False |
| 59 | + break |
| 60 | + if is_prime: |
| 61 | + primes.add(number) |
| 62 | + return ( |
| 63 | + 'No prime numbers found.' |
| 64 | + if not primes |
| 65 | + else f"{', '.join(str(num) for num in primes)} are prime numbers." |
| 66 | + ) |
| 67 | + |
| 68 | + |
| 69 | +async def before_agent_callback(callback_context): |
| 70 | + print('@before_agent_callback') |
| 71 | + return None |
| 72 | + |
| 73 | + |
| 74 | +async def after_agent_callback(callback_context): |
| 75 | + print('@after_agent_callback') |
| 76 | + return None |
| 77 | + |
| 78 | + |
| 79 | +async def before_model_callback(callback_context, llm_request): |
| 80 | + print('@before_model_callback') |
| 81 | + return None |
| 82 | + |
| 83 | + |
| 84 | +async def after_model_callback(callback_context, llm_response): |
| 85 | + print('@after_model_callback') |
| 86 | + return None |
| 87 | + |
| 88 | + |
| 89 | +def after_agent_cb1(callback_context): |
| 90 | + print('@after_agent_cb1') |
| 91 | + |
| 92 | + |
| 93 | +def after_agent_cb2(callback_context): |
| 94 | + print('@after_agent_cb2') |
| 95 | + return types.Content( |
| 96 | + parts=[ |
| 97 | + types.Part( |
| 98 | + text='(stopped) after_agent_cb2', |
| 99 | + ), |
| 100 | + ], |
| 101 | + ) |
| 102 | + |
| 103 | + |
| 104 | +def after_agent_cb3(callback_context): |
| 105 | + print('@after_agent_cb3') |
| 106 | + |
| 107 | + |
| 108 | +def before_agent_cb1(callback_context): |
| 109 | + print('@before_agent_cb1') |
| 110 | + |
| 111 | + |
| 112 | +def before_agent_cb2(callback_context): |
| 113 | + print('@before_agent_cb2') |
| 114 | + |
| 115 | + |
| 116 | +def before_agent_cb3(callback_context): |
| 117 | + print('@before_agent_cb3') |
| 118 | + |
| 119 | + |
| 120 | +def before_tool_cb1(tool, args, tool_context): |
| 121 | + print('@before_tool_cb1') |
| 122 | + |
| 123 | + |
| 124 | +def before_tool_cb2(tool, args, tool_context): |
| 125 | + print('@before_tool_cb2') |
| 126 | + |
| 127 | + |
| 128 | +def before_tool_cb3(tool, args, tool_context): |
| 129 | + print('@before_tool_cb3') |
| 130 | + |
| 131 | + |
| 132 | +def after_tool_cb1(tool, args, tool_context, tool_response): |
| 133 | + print('@after_tool_cb1') |
| 134 | + |
| 135 | + |
| 136 | +def after_tool_cb2(tool, args, tool_context, tool_response): |
| 137 | + print('@after_tool_cb2') |
| 138 | + return {'test': 'after_tool_cb2', 'response': tool_response} |
| 139 | + |
| 140 | + |
| 141 | +def after_tool_cb3(tool, args, tool_context, tool_response): |
| 142 | + print('@after_tool_cb3') |
| 143 | + |
| 144 | + |
| 145 | +root_agent = Agent( |
| 146 | + model='gemini-2.0-flash-exp', |
| 147 | + name='data_processing_agent', |
| 148 | + description=( |
| 149 | + 'hello world agent that can roll a dice of 8 sides and check prime' |
| 150 | + ' numbers.' |
| 151 | + ), |
| 152 | + instruction=""" |
| 153 | + You roll dice and answer questions about the outcome of the dice rolls. |
| 154 | + You can roll dice of different sizes. |
| 155 | + You can use multiple tools in parallel by calling functions in parallel(in one request and in one round). |
| 156 | + It is ok to discuss previous dice roles, and comment on the dice rolls. |
| 157 | + 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. |
| 158 | + You should never roll a die on your own. |
| 159 | + 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. |
| 160 | + You should not check prime numbers before calling the tool. |
| 161 | + When you are asked to roll a die and check prime numbers, you should always make the following two function calls: |
| 162 | + 1. You should first call the roll_die tool to get a roll. Wait for the function response before calling the check_prime tool. |
| 163 | + 2. After you get the function response from roll_die tool, you should call the check_prime tool with the roll_die result. |
| 164 | + 2.1 If user asks you to check primes based on previous rolls, make sure you include the previous rolls in the list. |
| 165 | + 3. When you respond, you must include the roll_die result from step 1. |
| 166 | + You should always perform the previous 3 steps when asking for a roll and checking prime numbers. |
| 167 | + You should not rely on the previous history on prime results. |
| 168 | + """, |
| 169 | + tools=[ |
| 170 | + roll_die, |
| 171 | + check_prime, |
| 172 | + ], |
| 173 | + # planner=BuiltInPlanner( |
| 174 | + # thinking_config=types.ThinkingConfig( |
| 175 | + # include_thoughts=True, |
| 176 | + # ), |
| 177 | + # ), |
| 178 | + generate_content_config=types.GenerateContentConfig( |
| 179 | + safety_settings=[ |
| 180 | + types.SafetySetting( # avoid false alarm about rolling dice. |
| 181 | + category=types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, |
| 182 | + threshold=types.HarmBlockThreshold.OFF, |
| 183 | + ), |
| 184 | + ] |
| 185 | + ), |
| 186 | + before_agent_callback=[ |
| 187 | + before_agent_cb1, |
| 188 | + before_agent_cb2, |
| 189 | + before_agent_cb3, |
| 190 | + ], |
| 191 | + after_agent_callback=[after_agent_cb1, after_agent_cb2, after_agent_cb3], |
| 192 | + before_model_callback=before_model_callback, |
| 193 | + after_model_callback=after_model_callback, |
| 194 | + before_tool_callback=[before_tool_cb1, before_tool_cb2, before_tool_cb3], |
| 195 | + after_tool_callback=[after_tool_cb1, after_tool_cb2, after_tool_cb3], |
| 196 | +) |
0 commit comments