mirror of
https://github.com/google/adk-docs.git
synced 2026-09-14 16:16:59 +08:00
159e536aed
* doc: fixing typos in various files * Update code-exec-agent-engine.md --------- Co-authored-by: Joe Fernandez <joefernandez@users.noreply.github.com>
226 lines
6.5 KiB
Python
226 lines
6.5 KiB
Python
# Copyright 2025 Google LLC
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
import os
|
|
import json
|
|
import base64
|
|
import warnings
|
|
|
|
from pathlib import Path
|
|
from dotenv import load_dotenv
|
|
|
|
from google.genai.types import (
|
|
Part,
|
|
Content,
|
|
Blob,
|
|
)
|
|
|
|
from google.adk.runners import InMemoryRunner
|
|
from google.adk.agents import LiveRequestQueue
|
|
from google.adk.agents.run_config import RunConfig
|
|
from google.genai import types
|
|
|
|
from fastapi import FastAPI, Request
|
|
from fastapi.staticfiles import StaticFiles
|
|
from fastapi.responses import FileResponse, StreamingResponse
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
from google_search_agent.agent import root_agent
|
|
|
|
warnings.filterwarnings("ignore", category=UserWarning, module="pydantic")
|
|
|
|
#
|
|
# ADK Streaming
|
|
#
|
|
|
|
# Load Gemini API Key
|
|
load_dotenv()
|
|
|
|
APP_NAME = "ADK Streaming example"
|
|
|
|
|
|
async def start_agent_session(user_id, is_audio=False):
|
|
"""Starts an agent session"""
|
|
|
|
# Create a Runner
|
|
runner = InMemoryRunner(
|
|
app_name=APP_NAME,
|
|
agent=root_agent,
|
|
)
|
|
|
|
# Create a Session
|
|
session = await runner.session_service.create_session(
|
|
app_name=APP_NAME,
|
|
user_id=user_id, # Replace with actual user ID
|
|
)
|
|
|
|
# Set response modality
|
|
modality = "AUDIO" if is_audio else "TEXT"
|
|
run_config = RunConfig(
|
|
response_modalities=[modality],
|
|
session_resumption=types.SessionResumptionConfig()
|
|
)
|
|
|
|
# Create a LiveRequestQueue for this session
|
|
live_request_queue = LiveRequestQueue()
|
|
|
|
# Start agent session
|
|
live_events = runner.run_live(
|
|
session=session,
|
|
live_request_queue=live_request_queue,
|
|
run_config=run_config,
|
|
)
|
|
return live_events, live_request_queue
|
|
|
|
|
|
async def agent_to_client_sse(live_events):
|
|
"""Agent to client communication via SSE"""
|
|
async for event in live_events:
|
|
# If the turn complete or interrupted, send it
|
|
if event.turn_complete or event.interrupted:
|
|
message = {
|
|
"turn_complete": event.turn_complete,
|
|
"interrupted": event.interrupted,
|
|
}
|
|
yield f"data: {json.dumps(message)}\n\n"
|
|
print(f"[AGENT TO CLIENT]: {message}")
|
|
continue
|
|
|
|
# Read the Content and its first Part
|
|
part: Part = (
|
|
event.content and event.content.parts and event.content.parts[0]
|
|
)
|
|
if not part:
|
|
continue
|
|
|
|
# If it's audio, send Base64 encoded audio data
|
|
is_audio = part.inline_data and part.inline_data.mime_type.startswith("audio/pcm")
|
|
if is_audio:
|
|
audio_data = part.inline_data and part.inline_data.data
|
|
if audio_data:
|
|
message = {
|
|
"mime_type": "audio/pcm",
|
|
"data": base64.b64encode(audio_data).decode("ascii")
|
|
}
|
|
yield f"data: {json.dumps(message)}\n\n"
|
|
print(f"[AGENT TO CLIENT]: audio/pcm: {len(audio_data)} bytes.")
|
|
continue
|
|
|
|
# If it's text and a partial text, send it
|
|
if part.text and event.partial:
|
|
message = {
|
|
"mime_type": "text/plain",
|
|
"data": part.text
|
|
}
|
|
yield f"data: {json.dumps(message)}\n\n"
|
|
print(f"[AGENT TO CLIENT]: text/plain: {message}")
|
|
|
|
|
|
#
|
|
# FastAPI web app
|
|
#
|
|
|
|
app = FastAPI()
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
STATIC_DIR = Path("static")
|
|
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
|
|
|
|
# Store active sessions
|
|
active_sessions = {}
|
|
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
"""Serves the index.html"""
|
|
return FileResponse(os.path.join(STATIC_DIR, "index.html"))
|
|
|
|
|
|
@app.get("/events/{user_id}")
|
|
async def sse_endpoint(user_id: int, is_audio: str = "false"):
|
|
"""SSE endpoint for agent to client communication"""
|
|
|
|
# Start agent session
|
|
user_id_str = str(user_id)
|
|
live_events, live_request_queue = await start_agent_session(user_id_str, is_audio == "true")
|
|
|
|
# Store the request queue for this user
|
|
active_sessions[user_id_str] = live_request_queue
|
|
|
|
print(f"Client #{user_id} connected via SSE, audio mode: {is_audio}")
|
|
|
|
def cleanup():
|
|
live_request_queue.close()
|
|
if user_id_str in active_sessions:
|
|
del active_sessions[user_id_str]
|
|
print(f"Client #{user_id} disconnected from SSE")
|
|
|
|
async def event_generator():
|
|
try:
|
|
async for data in agent_to_client_sse(live_events):
|
|
yield data
|
|
except Exception as e:
|
|
print(f"Error in SSE stream: {e}")
|
|
finally:
|
|
cleanup()
|
|
|
|
return StreamingResponse(
|
|
event_generator(),
|
|
media_type="text/event-stream",
|
|
headers={
|
|
"Cache-Control": "no-cache",
|
|
"Connection": "keep-alive",
|
|
"Access-Control-Allow-Origin": "*",
|
|
"Access-Control-Allow-Headers": "Cache-Control"
|
|
}
|
|
)
|
|
|
|
|
|
@app.post("/send/{user_id}")
|
|
async def send_message_endpoint(user_id: int, request: Request):
|
|
"""HTTP endpoint for client to agent communication"""
|
|
|
|
user_id_str = str(user_id)
|
|
|
|
# Get the live request queue for this user
|
|
live_request_queue = active_sessions.get(user_id_str)
|
|
if not live_request_queue:
|
|
return {"error": "Session not found"}
|
|
|
|
# Parse the message
|
|
message = await request.json()
|
|
mime_type = message["mime_type"]
|
|
data = message["data"]
|
|
|
|
# Send the message to the agent
|
|
if mime_type == "text/plain":
|
|
content = Content(role="user", parts=[Part.from_text(text=data)])
|
|
live_request_queue.send_content(content=content)
|
|
print(f"[CLIENT TO AGENT]: {data}")
|
|
elif mime_type == "audio/pcm":
|
|
decoded_data = base64.b64decode(data)
|
|
live_request_queue.send_realtime(Blob(data=decoded_data, mime_type=mime_type))
|
|
print(f"[CLIENT TO AGENT]: audio/pcm: {len(decoded_data)} bytes")
|
|
else:
|
|
return {"error": f"Mime type not supported: {mime_type}"}
|
|
|
|
return {"status": "sent"}
|