You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/get-started/quickstart-streaming.md
+2-342Lines changed: 2 additions & 342 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -166,348 +166,8 @@ Stop `adk web` by pressing `Ctrl-C` on the console.
166
166
167
167
The following features will be supported in the future versions of the ADK Streaming: Callback, LongRunningTool, ExampleTool, and Shell agent (e.g. SequentialAgent).
168
168
169
-
## 5. Building a Custom Streaming App (Optional) {#5.-build-custom-app}
170
-
171
-
In the previous section, we have checked that our basic search agent works with the ADK Streaming using `adk web` tool. In the this section, we will learn how to build your own web application capable of the streaming communication using [FastAPI](https://fastapi.tiangolo.com/).
172
-
173
-
Add `static` directory under `app`, and add `main.py` and `index.html` as empty files, as in the following structure:
174
-
175
-
```console
176
-
adk-streaming/ # Project folder
177
-
└── app/ # the web app folder
178
-
├── main.py # FastAPI web app
179
-
└── static/ # Static content folder
180
-
└── index.html # The web client page
181
-
```
182
-
183
-
By adding the directories and files above, the entire directory structure and files will look like:
184
-
185
-
```console
186
-
adk-streaming/ # Project folder
187
-
└── app/ # the web app folder
188
-
├── main.py # FastAPI web app
189
-
├── static/ # Static content folder
190
-
| └── index.html # The web client page
191
-
├── .env # Gemini API key
192
-
└── google_search_agent/ # Agent folder
193
-
├── __init__.py # Python package
194
-
└── agent.py # Agent definition
195
-
```
196
-
197
-
**main.py**
198
-
199
-
Copy-paste the following code block to the main.py file.
200
-
201
-
```py
202
-
import os
203
-
import json
204
-
import asyncio
205
-
206
-
from pathlib import Path
207
-
from dotenv import load_dotenv
208
-
209
-
from google.genai.types import (
210
-
Part,
211
-
Content,
212
-
)
213
-
214
-
from google.adk.runners import Runner
215
-
from google.adk.agents import LiveRequestQueue
216
-
from google.adk.agents.run_config import RunConfig
217
-
from google.adk.sessions.in_memory_session_service import InMemorySessionService
This code creates a real-time chat application using ADK and FastAPI. It sets up a WebSocket endpoint where clients can connect and interact with a Google Search Agent.
352
-
353
-
Key functionalities:
354
-
355
-
* Loads the Gemini API key.
356
-
* Uses ADK to manage agent sessions and run the \`google\_search\_agent\`.
357
-
*\`start\_agent\_session\` initializes an agent session with a live request queue for real-time communication.
358
-
*\`agent\_to\_client\_messaging\` asynchronously streams the agent's text responses and status updates (turn complete, interrupted) to the connected WebSocket client.
359
-
*\`client\_to\_agent\_messaging\` asynchronously receives text messages from the WebSocket client and sends them as user input to the agent.
360
-
* FastAPI serves a static frontend and handles WebSocket connections at \`/ws/{session\_id}\`.
361
-
* When a client connects, it starts an agent session and creates concurrent tasks for bidirectional communication between the client and the agent via WebSockets.
362
-
363
-
Copy-paste the following code block to the `index.html` file.
* A form (\`messageForm\`) with an input field for typing messages and a "Send" button.
476
-
* JavaScript that:
477
-
* Connects to a WebSocket server at \`wss://\[current host\]/ws/\[random session ID\]\`.
478
-
* Enables the "Send" button upon successful connection.
479
-
* Appends received messages from the WebSocket to the \`messages\` div, handling streaming responses and turn completion.
480
-
* Sends the text entered in the input field to the WebSocket server when the form is submitted.
481
-
* Attempts to reconnect if the WebSocket connection closes.
482
-
483
-
## 6\. Interact with Your Streaming app {#4.-interact-with-your-streaming-app}
484
-
485
-
1\.**Navigate to the Correct Directory:**
486
-
487
-
To run your agent effectively, you need to be in the **app folder (`adk-streaming/app`)**
488
-
489
-
2\.**Start the Fast API**: Run the following command to start CLI interface with
490
-
491
-
```console
492
-
uvicorn main:app --reload
493
-
```
494
-
495
-
3\.**Access the UI:** Once the UI server starts, the terminal will display a local URL (e.g., [http://localhost:8000](http://localhost:8501)). Click this link to open the UI in your browser.
Try asking a question `What is Gemini?`. The agent will use Google Search to respond to your queries. You would notice that the UI shows the agent's response as streaming text. You can also send messages to the agent at any time, even while the agent is still responding. This demonstrates the bidirectional communication capability of ADK Streaming.
* More responsive and engaging: No need to wait for full responses or constant refreshing. Feels like a live conversation.
507
-
* Can be extended to multimodal apps with audio, image and video streaming support.
508
-
509
169
Congratulations\! You've successfully created and interacted with your first Streaming agent using ADK\!
510
170
511
-
## Next steps
171
+
## Next steps: build custom streaming app
512
172
513
-
***Add audio/image modality:** with the Streaming, you can also have real-time communication with the agent using audio and image. We will add more samples for the multimodal support in the future. Stay tuned!
173
+
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.
0 commit comments