diff --git a/examples/aiortc/voice-agent/README.rst b/examples/aiortc/voice-agent/README.rst new file mode 100644 index 000000000..899adc861 --- /dev/null +++ b/examples/aiortc/voice-agent/README.rst @@ -0,0 +1,34 @@ +# Video transform + +A Pipecat example demonstrating the simplest way to create a voice agent with SmallWebRTCTransport. + +## Quick Start + +### First, start the bot server: + +1. Create and activate a virtual environment: + ```bash + python3 -m venv venv + source venv/bin/activate # On Windows: venv\Scripts\activate + ``` +2. Install requirements: + ```bash + pip install -r requirements.txt + ``` +3. Copy env.example to .env and configure: + - Add your API keys +4. Start the server: + ```bash + python server.py + ``` + +### Next, connect using the client app: + +Visit http://localhost:7860 in your browser. + +## Requirements + +- Python 3.10+ +- Node.js 16+ (for JavaScript) +- Google API key +- Modern web browser with WebRTC support \ No newline at end of file diff --git a/examples/aiortc/voice-agent/aiortc_bot.py b/examples/aiortc/voice-agent/aiortc_bot.py new file mode 100644 index 000000000..07106022c --- /dev/null +++ b/examples/aiortc/voice-agent/aiortc_bot.py @@ -0,0 +1,102 @@ +# +# Copyright (c) 2025, Daily +# +# SPDX-License-Identifier: BSD 2-Clause License +# +import os +import sys + +from dotenv import load_dotenv +from loguru import logger + +from pipecat.audio.vad.silero import SileroVADAnalyzer +from pipecat.pipeline.pipeline import Pipeline +from pipecat.pipeline.runner import PipelineRunner +from pipecat.pipeline.task import PipelineParams, PipelineTask +from pipecat.processors.aggregators.openai_llm_context import OpenAILLMContext +from pipecat.services.gemini_multimodal_live import GeminiMultimodalLiveLLMService +from pipecat.transports.base_transport import TransportParams +from pipecat.transports.network.small_webrtc import SmallWebRTCTransport + +load_dotenv(override=True) + +logger.remove(0) +logger.add(sys.stderr, level="DEBUG") + + +SYSTEM_INSTRUCTION = f""" +"You are Gemini Chatbot, a friendly, helpful robot. + +Your goal is to demonstrate your capabilities in a succinct way. + +Your output will be converted to audio so don't include special characters in your answers. + +Respond to what the user said in a creative and helpful way. Keep your responses brief. One or two sentences at most. +""" + + +async def run_bot(webrtc_connection): + pipecat_transport = SmallWebRTCTransport( + webrtc_connection=webrtc_connection, + params=TransportParams( + audio_in_enabled=True, + audio_out_enabled=True, + vad_enabled=True, + vad_analyzer=SileroVADAnalyzer(), + vad_audio_passthrough=True, + ), + ) + + llm = GeminiMultimodalLiveLLMService( + api_key=os.getenv("GOOGLE_API_KEY"), + voice_id="Puck", # Aoede, Charon, Fenrir, Kore, Puck + transcribe_user_audio=True, + transcribe_model_audio=True, + system_instruction=SYSTEM_INSTRUCTION, + ) + + context = OpenAILLMContext( + [ + { + "role": "user", + "content": "Start by greeting the user warmly and introducing yourself.", + } + ], + ) + context_aggregator = llm.create_context_aggregator(context) + + pipeline = Pipeline( + [ + pipecat_transport.input(), + context_aggregator.user(), + llm, # LLM + pipecat_transport.output(), + context_aggregator.assistant(), + ] + ) + + task = PipelineTask( + pipeline, + params=PipelineParams( + allow_interruptions=True, + ), + ) + + @pipecat_transport.event_handler("on_client_connected") + async def on_client_connected(transport, client): + logger.info("Pipecat Client connected") + # Kick off the conversation. + await task.queue_frames([context_aggregator.user().get_context_frame()]) + + @pipecat_transport.event_handler("on_client_disconnected") + async def on_client_disconnected(transport, client): + logger.info("Pipecat Client disconnected") + + @pipecat_transport.event_handler("on_client_closed") + async def on_client_closed(transport, client): + logger.info("Pipecat Client closed") + await task.cancel() + + runner = PipelineRunner(handle_sigint=False) + + await runner.run(task) diff --git a/examples/aiortc/voice-agent/env.example b/examples/aiortc/voice-agent/env.example new file mode 100644 index 000000000..b8d79805b --- /dev/null +++ b/examples/aiortc/voice-agent/env.example @@ -0,0 +1 @@ +GOOGLE_API_KEY= \ No newline at end of file diff --git a/examples/aiortc/voice-agent/index.html b/examples/aiortc/voice-agent/index.html new file mode 100644 index 000000000..4fce3a993 --- /dev/null +++ b/examples/aiortc/voice-agent/index.html @@ -0,0 +1,99 @@ + + + + + + WebRTC Voice Agent + + + +

WebRTC Voice Agent

+

Disconnected

+ + + + + + diff --git a/examples/aiortc/voice-agent/requirements.txt b/examples/aiortc/voice-agent/requirements.txt new file mode 100644 index 000000000..d8ffd53ef --- /dev/null +++ b/examples/aiortc/voice-agent/requirements.txt @@ -0,0 +1,5 @@ +python-dotenv +fastapi[all] +uvicorn +aiortc +pipecat-ai[google,silero] \ No newline at end of file diff --git a/examples/aiortc/voice-agent/server.py b/examples/aiortc/voice-agent/server.py new file mode 100644 index 000000000..2dc4867a3 --- /dev/null +++ b/examples/aiortc/voice-agent/server.py @@ -0,0 +1,68 @@ +import argparse +import asyncio +import logging +from contextlib import asynccontextmanager + +import uvicorn +from aiortc_bot import run_bot +from dotenv import load_dotenv +from fastapi import BackgroundTasks, FastAPI +from fastapi.responses import FileResponse + +from pipecat.transports.network.webrtc_connection import SmallWebRTCConnection + +# Load environment variables +load_dotenv(override=True) + +logger = logging.getLogger("pc") + +app = FastAPI() + +pcs = set() + + +@app.post("/api/offer") +async def offer(request: dict, background_tasks: BackgroundTasks): + pipecat_connection = SmallWebRTCConnection() + await pipecat_connection.initialize(sdp=request["sdp"], type=request["type"]) + + pcs.add(pipecat_connection) + + @pipecat_connection.on("closed") + async def handle_disconnected(): + logger.info("Discarding the peer connection.") + pcs.discard(pipecat_connection) + + background_tasks.add_task(run_bot, pipecat_connection) + + return pipecat_connection.get_answer() + + +@app.get("/") +async def serve_index(): + return FileResponse("index.html") + + +@asynccontextmanager +async def lifespan(app: FastAPI): + yield # Run app + coros = [pc.close() for pc in pcs] + await asyncio.gather(*coros) + pcs.clear() + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description="WebRTC demo") + parser.add_argument("--host", default="0.0.0.0", help="Host for HTTP server (default: 0.0.0.0)") + parser.add_argument( + "--port", type=int, default=7860, help="Port for HTTP server (default: 7860)" + ) + parser.add_argument("--verbose", "-v", action="count") + args = parser.parse_args() + + if args.verbose: + logging.basicConfig(level=logging.DEBUG) + else: + logging.basicConfig(level=logging.INFO) + + uvicorn.run(app, host=args.host, port=args.port)