Skip to content

Commit a58cc3d

Browse files
hangfeicopybara-github
authored andcommitted
chore: Create an example for multi agent live streaming
This example include multi agents: - Root agent. - Sub agent for Rolling Dice. - Sub agent for checking primes. Added README.md to demonstrate how to use it. PiperOrigin-RevId: 777599625
1 parent 37108be commit a58cc3d

File tree

6 files changed

+187
-0
lines changed

6 files changed

+187
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Simplistic Live (Bidi-Streaming) Multi-Agent
2+
This project provides a basic example of a live, bidirectional streaming multi-agent
3+
designed for testing and experimentation.
4+
5+
You can see full documentation [here](https://google.github.io/adk-docs/streaming/).
6+
7+
## Getting Started
8+
9+
Follow these steps to get the agent up and running:
10+
11+
1. **Start the ADK Web Server**
12+
Open your terminal, navigate to the root directory that contains the
13+
`live_bidi_streaming_agent` folder, and execute the following command:
14+
```bash
15+
adk web
16+
```
17+
18+
2. **Access the ADK Web UI**
19+
Once the server is running, open your web browser and navigate to the URL
20+
provided in the terminal (it will typically be `http://localhost:8000`).
21+
22+
3. **Select the Agent**
23+
In the top-left corner of the ADK Web UI, use the dropdown menu to select
24+
this agent.
25+
26+
4. **Start Streaming**
27+
Click on either the **Audio** or **Video** icon located near the chat input
28+
box to begin the streaming session.
29+
30+
5. **Interact with the Agent**
31+
You can now begin talking to the agent, and it will respond in real-time.
32+
33+
## Usage Notes
34+
35+
* You only need to click the **Audio** or **Video** button once to initiate the
36+
stream. The current version does not support stopping and restarting the stream
37+
by clicking the button again during a session.
38+
39+
## Sample Queries
40+
41+
- Hello, what's the weather in Seattle and New York?
42+
- Could you roll a 6-sided dice for me?
43+
- Could you check if the number you rolled is a prime number or not?
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
from . import agent

0 commit comments

Comments
 (0)