mirror of
https://github.com/google/adk-docs.git
synced 2026-09-14 16:16:59 +08:00
b947bc849f
* Phase 1: ADK Onboarding journey revision - Quickstart: split by language and reduce time - Build your agent: new section for agent building - Home page: Simplify onboarding choices - Surface "About ADK" in navigation * Updates from first review round and Java research - completely revised Java quickstart * further updates from review * more review updates --------- Co-authored-by: Kristopher Overholt <koverholt@gmail.com>
188 lines
7.4 KiB
Markdown
188 lines
7.4 KiB
Markdown
# Build a streaming agent with Python
|
|
|
|
With this quickstart, you'll learn to create a simple agent and use ADK Streaming to enable voice and video communication with it that is low-latency and bidirectional. We will install ADK, set up a basic "Google Search" agent, try running the agent with Streaming with `adk web` tool, and then explain how to build a simple asynchronous web app by yourself using ADK Streaming and [FastAPI](https://fastapi.tiangolo.com/).
|
|
|
|
**Note:** This guide assumes you have experience using a terminal in Windows, Mac, and Linux environments.
|
|
|
|
## Supported models for voice/video streaming {#supported-models}
|
|
|
|
In order to use voice/video streaming in ADK, you will need to use Gemini models that support the Live API. You can find the **model ID(s)** that supports the Gemini Live API in the documentation:
|
|
|
|
- [Google AI Studio: Gemini Live API](https://ai.google.dev/gemini-api/docs/models#live-api)
|
|
- [Vertex AI: Gemini Live API](https://cloud.google.com/vertex-ai/generative-ai/docs/live-api)
|
|
|
|
## 1. Setup Environment & Install ADK { #setup-environment-install-adk }
|
|
|
|
Create & Activate Virtual Environment (Recommended):
|
|
|
|
```bash
|
|
# Create
|
|
python -m venv .venv
|
|
# Activate (each new terminal)
|
|
# macOS/Linux: source .venv/bin/activate
|
|
# Windows CMD: .venv\Scripts\activate.bat
|
|
# Windows PowerShell: .venv\Scripts\Activate.ps1
|
|
```
|
|
|
|
Install ADK:
|
|
|
|
```bash
|
|
pip install google-adk
|
|
```
|
|
|
|
## 2. Project Structure { #project-structure }
|
|
|
|
Create the following folder structure with empty files:
|
|
|
|
```console
|
|
adk-streaming/ # Project folder
|
|
└── app/ # the web app folder
|
|
├── .env # Gemini API key
|
|
└── google_search_agent/ # Agent folder
|
|
├── __init__.py # Python package
|
|
└── agent.py # Agent definition
|
|
```
|
|
|
|
### agent.py
|
|
|
|
Copy-paste the following code block into the `agent.py` file.
|
|
|
|
For `model`, please double check the model ID as described earlier in the [Models section](#supported-models).
|
|
|
|
```py
|
|
from google.adk.agents import Agent
|
|
from google.adk.tools import google_search # Import the tool
|
|
|
|
root_agent = Agent(
|
|
# A unique name for the agent.
|
|
name="basic_search_agent",
|
|
# The Large Language Model (LLM) that agent will use.
|
|
# Please fill in the latest model id that supports live from
|
|
# https://google.github.io/adk-docs/get-started/streaming/quickstart-streaming/#supported-models
|
|
model="...", # for example: model="gemini-2.0-flash-live-001" or model="gemini-2.0-flash-live-preview-04-09"
|
|
# A short description of the agent's purpose.
|
|
description="Agent to answer questions using Google Search.",
|
|
# Instructions to set the agent's behavior.
|
|
instruction="You are an expert researcher. You always stick to the facts.",
|
|
# Add google_search tool to perform grounding with Google search.
|
|
tools=[google_search]
|
|
)
|
|
```
|
|
|
|
`agent.py` is where all your agent(s)' logic will be stored, and you must have a `root_agent` defined.
|
|
|
|
Notice how easily you integrated [grounding with Google Search](https://ai.google.dev/gemini-api/docs/grounding?lang=python#configure-search) capabilities. The `Agent` class and the `google_search` tool handle the complex interactions with the LLM and grounding with the search API, allowing you to focus on the agent's *purpose* and *behavior*.
|
|
|
|

|
|
|
|
Copy-paste the following code block to `__init__.py` file.
|
|
|
|
```py title="__init__.py"
|
|
from . import agent
|
|
```
|
|
|
|
## 3\. Set up the platform { #set-up-the-platform }
|
|
|
|
To run the agent, choose a platform from either Google AI Studio or Google Cloud Vertex AI:
|
|
|
|
=== "Gemini - Google AI Studio"
|
|
1. Get an API key from [Google AI Studio](https://aistudio.google.com/apikey).
|
|
2. Open the **`.env`** file located inside (`app/`) and copy-paste the following code.
|
|
|
|
```env title=".env"
|
|
GOOGLE_GENAI_USE_VERTEXAI=FALSE
|
|
GOOGLE_API_KEY=PASTE_YOUR_ACTUAL_API_KEY_HERE
|
|
```
|
|
|
|
3. Replace `PASTE_YOUR_ACTUAL_API_KEY_HERE` with your actual `API KEY`.
|
|
|
|
=== "Gemini - Google Cloud Vertex AI"
|
|
1. You need an existing
|
|
[Google Cloud](https://cloud.google.com/?e=48754805&hl=en) account and a
|
|
project.
|
|
* Set up a
|
|
[Google Cloud project](https://cloud.google.com/vertex-ai/generative-ai/docs/start/quickstarts/quickstart-multimodal#setup-gcp)
|
|
* Set up the
|
|
[gcloud CLI](https://cloud.google.com/vertex-ai/generative-ai/docs/start/quickstarts/quickstart-multimodal#setup-local)
|
|
* Authenticate to Google Cloud, from the terminal by running
|
|
`gcloud auth login`.
|
|
* [Enable the Vertex AI API](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com).
|
|
2. Open the **`.env`** file located inside (`app/`). Copy-paste
|
|
the following code and update the project ID and location.
|
|
|
|
```env title=".env"
|
|
GOOGLE_GENAI_USE_VERTEXAI=TRUE
|
|
GOOGLE_CLOUD_PROJECT=PASTE_YOUR_ACTUAL_PROJECT_ID
|
|
GOOGLE_CLOUD_LOCATION=us-central1
|
|
```
|
|
|
|
## 4. Try the agent with `adk web` { #try-the-agent-with-adk-web }
|
|
|
|
Now it's ready to try the agent. Run the following command to launch the **dev UI**. First, make sure to set the current directory to `app`:
|
|
|
|
```shell
|
|
cd app
|
|
```
|
|
|
|
Also, set `SSL_CERT_FILE` variable with the following command. This is required for the voice and video tests later.
|
|
|
|
=== "OS X & Linux"
|
|
```bash
|
|
export SSL_CERT_FILE=$(python -m certifi)
|
|
```
|
|
|
|
=== "Windows"
|
|
```powershell
|
|
$env:SSL_CERT_FILE = (python -m certifi)
|
|
```
|
|
|
|
|
|
|
|
Then, run the dev UI:
|
|
|
|
```shell
|
|
adk web
|
|
```
|
|
|
|
!!!info "Note for Windows users"
|
|
|
|
When hitting the `_make_subprocess_transport NotImplementedError`, consider using `adk web --no-reload` instead.
|
|
|
|
|
|
Open the URL provided (usually `http://localhost:8000` or
|
|
`http://127.0.0.1:8000`) **directly in your browser**. This connection stays
|
|
entirely on your local machine. Select `google_search_agent`.
|
|
|
|
### Try with text
|
|
|
|
Try the following prompts by typing them in the UI.
|
|
|
|
* What is the weather in New York?
|
|
* What is the time in New York?
|
|
* What is the weather in Paris?
|
|
* What is the time in Paris?
|
|
|
|
The agent will use the google_search tool to get the latest information to answer those questions.
|
|
|
|
### Try with voice and video
|
|
|
|
To try with voice, reload the web browser, click the microphone button to enable the voice input, and ask the same question in voice. You will hear the answer in voice in real-time.
|
|
|
|
To try with video, reload the web browser, click the camera button to enable the video input, and ask questions like "What do you see?". The agent will answer what they see in the video input.
|
|
|
|
(Just clicking the microphone or camera button once is enough. Your voice or video will be streamed to models and the model response will be streamed back continuously. Clicking on the microphone or camera button multiple times is not supported.)
|
|
|
|
### Stop the tool
|
|
|
|
Stop `adk web` by pressing `Ctrl-C` on the console.
|
|
|
|
### Note on ADK Streaming
|
|
|
|
The following features will be supported in the future versions of the ADK Streaming: Callback, LongRunningTool, ExampleTool, and Shell agent (e.g. SequentialAgent).
|
|
|
|
Congratulations\! You've successfully created and interacted with your first Streaming agent using ADK\!
|
|
|
|
## Next steps: build custom streaming app
|
|
|
|
In [Custom Audio Streaming app](../../streaming/custom-streaming.md) tutorial, it overviews the server and client code for a custom asynchronous web app built with ADK Streaming and [FastAPI](https://fastapi.tiangolo.com/), enabling real-time, bidirectional audio and text communication.
|