Skip to content

Commit d87feb8

Browse files
wuliang229copybara-github
authored andcommitted
docs(config): add examples for config agents
PiperOrigin-RevId: 793871778
1 parent 88114d7 commit d87feb8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+761
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Basic Confg-based Agent
2+
3+
This sample only covers:
4+
5+
* name
6+
* description
7+
* model
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# yaml-language-server: $schema=https://raw.githubusercontent.com/google/adk-python/refs/heads/main/src/google/adk/agents/config_schemas/AgentConfig.json
2+
name: assistant_agent
3+
model: gemini-2.5-flash
4+
description: A helper agent that can answer users' questions.
5+
instruction: |
6+
You are an agent to help answer users' various questions.
7+
8+
1. If the user's intention is not clear, ask clarifying questions to better understand their needs.
9+
2. Once the intention is clear, provide accurate and helpful answers to the user's questions.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
from google.genai import types
2+
3+
4+
async def before_agent_callback(callback_context):
5+
print('@before_agent_callback')
6+
return None
7+
8+
9+
async def after_agent_callback(callback_context):
10+
print('@after_agent_callback')
11+
return None
12+
13+
14+
async def before_model_callback(callback_context, llm_request):
15+
print('@before_model_callback')
16+
return None
17+
18+
19+
async def after_model_callback(callback_context, llm_response):
20+
print('@after_model_callback')
21+
return None
22+
23+
24+
def after_agent_callback1(callback_context):
25+
print('@after_agent_callback1')
26+
27+
28+
def after_agent_callback2(callback_context):
29+
print('@after_agent_callback2')
30+
# ModelContent (or Content with role set to 'model') must be returned.
31+
# Otherwise, the event will be excluded from the context in the next turn.
32+
return types.ModelContent(
33+
parts=[
34+
types.Part(
35+
text='(stopped) after_agent_callback2',
36+
),
37+
],
38+
)
39+
40+
41+
def after_agent_callback3(callback_context):
42+
print('@after_agent_callback3')
43+
44+
45+
def before_agent_callback1(callback_context):
46+
print('@before_agent_callback1')
47+
48+
49+
def before_agent_callback2(callback_context):
50+
print('@before_agent_callback2')
51+
52+
53+
def before_agent_callback3(callback_context):
54+
print('@before_agent_callback3')
55+
56+
57+
def before_tool_callback1(tool, args, tool_context):
58+
print('@before_tool_callback1')
59+
60+
61+
def before_tool_callback2(tool, args, tool_context):
62+
print('@before_tool_callback2')
63+
64+
65+
def before_tool_callback3(tool, args, tool_context):
66+
print('@before_tool_callback3')
67+
68+
69+
def after_tool_callback1(tool, args, tool_context, tool_response):
70+
print('@after_tool_callback1')
71+
72+
73+
def after_tool_callback2(tool, args, tool_context, tool_response):
74+
print('@after_tool_callback2')
75+
return {'test': 'after_tool_callback2', 'response': tool_response}
76+
77+
78+
def after_tool_callback3(tool, args, tool_context, tool_response):
79+
print('@after_tool_callback3')
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# yaml-language-server: $schema=https://raw.githubusercontent.com/google/adk-python/refs/heads/main/src/google/adk/agents/config_schemas/AgentConfig.json
2+
name: hello_world_agent
3+
model: gemini-2.0-flash
4+
description: hello world agent that can roll a dice and check prime numbers.
5+
instruction: |
6+
You roll dice and answer questions about the outcome of the dice rolls.
7+
You can roll dice of different sizes.
8+
You can use multiple tools in parallel by calling functions in parallel(in one request and in one round).
9+
It is ok to discuss previous dice roles, and comment on the dice rolls.
10+
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.
11+
You should never roll a die on your own.
12+
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.
13+
You should not check prime numbers before calling the tool.
14+
When you are asked to roll a die and check prime numbers, you should always make the following two function calls:
15+
1. You should first call the roll_die tool to get a roll. Wait for the function response before calling the check_prime tool.
16+
2. After you get the function response from roll_die tool, you should call the check_prime tool with the roll_die result.
17+
2.1 If user asks you to check primes based on previous rolls, make sure you include the previous rolls in the list.
18+
3. When you respond, you must include the roll_die result from step 1.
19+
You should always perform the previous 3 steps when asking for a roll and checking prime numbers.
20+
You should not rely on the previous history on prime results.
21+
tools:
22+
- name: callbacks.tools.roll_die
23+
- name: callbacks.tools.check_prime
24+
before_agent_callbacks:
25+
- name: callbacks.callbacks.before_agent_callback1
26+
- name: callbacks.callbacks.before_agent_callback2
27+
- name: callbacks.callbacks.before_agent_callback3
28+
after_agent_callbacks:
29+
- name: callbacks.callbacks.after_agent_callback1
30+
- name: callbacks.callbacks.after_agent_callback2
31+
- name: callbacks.callbacks.after_agent_callback3
32+
before_model_callbacks:
33+
- name: callbacks.callbacks.before_model_callback
34+
after_model_callbacks:
35+
- name: callbacks.callbacks.after_model_callback
36+
before_tool_callbacks:
37+
- name: callbacks.callbacks.before_tool_callback1
38+
- name: callbacks.callbacks.before_tool_callback2
39+
- name: callbacks.callbacks.before_tool_callback3
40+
after_tool_callbacks:
41+
- name: callbacks.callbacks.after_tool_callback1
42+
- name: callbacks.callbacks.after_tool_callback2
43+
- name: callbacks.callbacks.after_tool_callback3
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import random
2+
3+
from google.adk.tools.tool_context import ToolContext
4+
5+
6+
def roll_die(sides: int, tool_context: ToolContext) -> int:
7+
"""Roll a die and return the rolled result.
8+
9+
Args:
10+
sides: The integer number of sides the die has.
11+
12+
Returns:
13+
An integer of the result of rolling the die.
14+
"""
15+
result = random.randint(1, sides)
16+
if not 'rolls' in tool_context.state:
17+
tool_context.state['rolls'] = []
18+
19+
tool_context.state['rolls'] = tool_context.state['rolls'] + [result]
20+
return result
21+
22+
23+
def check_prime(nums: list[int]) -> str:
24+
"""Check if a given list of numbers are prime.
25+
26+
Args:
27+
nums: The list of numbers to check.
28+
29+
Returns:
30+
A str indicating which number is prime.
31+
"""
32+
primes = set()
33+
for number in nums:
34+
number = int(number)
35+
if number <= 1:
36+
continue
37+
is_prime = True
38+
for i in range(2, int(number**0.5) + 1):
39+
if number % i == 0:
40+
is_prime = False
41+
break
42+
if is_prime:
43+
primes.add(number)
44+
return (
45+
'No prime numbers found.'
46+
if not primes
47+
else f"{', '.join(str(num) for num in primes)} are prime numbers."
48+
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# yaml-language-server: $schema=https://raw.githubusercontent.com/google/adk-python/refs/heads/main/src/google/adk/agents/config_schemas/AgentConfig.json
2+
name: search_agent
3+
model: gemini-2.0-flash
4+
description: 'an agent whose job it is to perform Google search queries and answer questions about the results.'
5+
instruction: You are an agent whose job is to perform Google search queries and answer questions about the results.
6+
tools:
7+
- name: google_search
8+
generate_content_config:
9+
temperature: 0.1
10+
max_output_tokens: 2000

contributing/samples/core_custom_agent/__init__.py

Whitespace-only changes.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from __future__ import annotations
2+
3+
from keyword import kwlist
4+
from typing import Any
5+
from typing import AsyncGenerator
6+
from typing import ClassVar
7+
from typing import Dict
8+
from typing import Type
9+
10+
from google.adk.agents import BaseAgent
11+
from google.adk.agents.base_agent_config import BaseAgentConfig
12+
from google.adk.agents.invocation_context import InvocationContext
13+
from google.adk.events.event import Event
14+
from google.genai import types
15+
from pydantic import ConfigDict
16+
from typing_extensions import override
17+
18+
19+
class MyCustomAgentConfig(BaseAgentConfig):
20+
model_config = ConfigDict(
21+
extra="forbid",
22+
)
23+
agent_class: str = "core_cutom_agent.my_agents.MyCustomAgent"
24+
my_field: str = ""
25+
26+
27+
class MyCustomAgent(BaseAgent):
28+
my_field: str = ""
29+
30+
config_type: ClassVar[type[BaseAgentConfig]] = MyCustomAgentConfig
31+
32+
@override
33+
@classmethod
34+
def _parse_config(
35+
cls: Type[MyCustomAgent],
36+
config: MyCustomAgentConfig,
37+
config_abs_path: str,
38+
kwargs: Dict[str, Any],
39+
) -> Dict[str, Any]:
40+
if config.my_field:
41+
kwargs["my_field"] = config.my_field
42+
return kwargs
43+
44+
async def _run_async_impl(
45+
self, ctx: InvocationContext
46+
) -> AsyncGenerator[Event, None]:
47+
yield Event(
48+
invocation_id=ctx.invocation_id,
49+
author=self.name,
50+
content=types.ModelContent(
51+
parts=[
52+
types.Part(
53+
text=f"I feel good! value in my_field: `{self.my_field}`"
54+
)
55+
]
56+
),
57+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# yaml-language-server: $schema=https://raw.githubusercontent.com/google/adk-python/refs/heads/main/src/google/adk/agents/config_schemas/AgentConfig.json
2+
name: working_agent
3+
agent_class: core_custom_agent.my_agents.MyCustomAgent
4+
description: Handles all the work.
5+
my_field: my_field_value

contributing/samples/ma_llm/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Config-based Agent Sample - LLM multi-agent
2+
3+
http://google3/third_party/py/google/adk/open_source_workspace/contributing/samples/hello_world_ma/

0 commit comments

Comments
 (0)