10000 chore: Added live-streaming sample agent · cloudbuilderspa/adk-python@8201f9a · GitHub
[go: up one dir, main page]

Skip to content

Commit 8201f9a

Browse files
hangfeicopybara-github
authored andcommitted
chore: Added live-streaming sample agent
Also added a readme. PiperOrigin-RevId: 772120698
1 parent 1cfc555 commit 8201f9a

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-0
lines changed
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
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Simplistic Live (Bidi-Streaming) Agent
2+
This project provides a basic example of a live, bidirectional streaming 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.

0 commit comments

Comments
 (0)
0