|
| 1 | +# |
| 2 | +# Copyright (c) 2024–2025, Daily |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: BSD 2-Clause License |
| 5 | +# |
| 6 | + |
| 7 | + |
| 8 | +import os |
| 9 | + |
| 10 | +import aiohttp |
| 11 | +from dotenv import load_dotenv |
| 12 | +from loguru import logger |
| 13 | + |
| 14 | +from pipecat.audio.turn.smart_turn.base_smart_turn import SmartTurnParams |
| 15 | +from pipecat.audio.turn.smart_turn.local_smart_turn_v3 import LocalSmartTurnAnalyzerV3 |
| 16 | +from pipecat.audio.vad.silero import SileroVADAnalyzer |
| 17 | +from pipecat.audio.vad.vad_analyzer import VADParams |
| 18 | +from pipecat.frames.frames import LLMRunFrame |
| 19 | +from pipecat.pipeline.pipeline import Pipeline |
| 20 | +from pipecat.pipeline.runner import PipelineRunner |
| 21 | +from pipecat.pipeline.task import PipelineParams, PipelineTask |
| 22 | +from pipecat.processors.aggregators.llm_context import LLMContext |
| 23 | +from pipecat.processors.aggregators.llm_response_universal import LLMContextAggregatorPair |
| 24 | +from pipecat.runner.types import RunnerArguments |
| 25 | +from pipecat.runner.utils import create_transport |
| 26 | +from pipecat.services.deepgram.stt import DeepgramSTTService |
| 27 | +from pipecat.services.deepgram.tts import DeepgramHttpTTSService |
| 28 | +from pipecat.services.openai.llm import OpenAILLMService |
| 29 | +from pipecat.transports.base_transport import BaseTransport, TransportParams |
| 30 | +from pipecat.transports.daily.transport import DailyParams |
| 31 | +from pipecat.transports.websocket.fastapi import FastAPIWebsocketParams |
| 32 | + |
| 33 | +load_dotenv(override=True) |
| 34 | + |
| 35 | + |
| 36 | +# We store functions so objects (e.g. SileroVADAnalyzer) don't get |
| 37 | +# instantiated. The function will be called when the desired transport gets |
| 38 | +# selected. |
| 39 | +transport_params = { |
| 40 | + "daily": lambda: DailyParams( |
| 41 | + audio_in_enabled=True, |
| 42 | + audio_out_enabled=True, |
| 43 | + vad_analyzer=SileroVADAnalyzer(params=VADParams(stop_secs=0.2)), |
| 44 | + turn_analyzer=LocalSmartTurnAnalyzerV3(params=SmartTurnParams()), |
| 45 | + ), |
| 46 | + "twilio": lambda: FastAPIWebsocketParams( |
| 47 | + audio_in_enabled=True, |
| 48 | + audio_out_enabled=True, |
| 49 | + vad_analyzer=SileroVADAnalyzer(params=VADParams(stop_secs=0.2)), |
| 50 | + turn_analyzer=LocalSmartTurnAnalyzerV3(params=SmartTurnParams()), |
| 51 | + ), |
| 52 | + "webrtc": lambda: TransportParams( |
| 53 | + audio_in_enabled=True, |
| 54 | + audio_out_enabled=True, |
| 55 | + vad_analyzer=SileroVADAnalyzer(params=VADParams(stop_secs=0.2)), |
| 56 | + turn_analyzer=LocalSmartTurnAnalyzerV3(params=SmartTurnParams()), |
| 57 | + ), |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +async def run_bot(transport: BaseTransport, runner_args: RunnerArguments): |
| 62 | + logger.info(f"Starting bot") |
| 63 | + |
| 64 | + async with aiohttp.ClientSession() as session: |
| 65 | + stt = DeepgramSTTService(api_key=os.getenv("DEEPGRAM_API_KEY")) |
| 66 | + |
| 67 | + tts = DeepgramHttpTTSService( |
| 68 | + api_key=os.getenv("DEEPGRAM_API_KEY"), |
| 69 | + voice="aura-2-andromeda-en", |
| 70 | + aiohttp_session=session, |
| 71 | + ) |
| 72 | + |
| 73 | + llm = OpenAILLMService(api_key=os.getenv("OPENAI_API_KEY")) |
| 74 | + |
| 75 | + messages = [ |
| 76 | + { |
| 77 | + "role": "system", |
| 78 | + "content": "You are a helpful LLM in a WebRTC call. 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.", |
| 79 | + }, |
| 80 | + ] |
| 81 | + |
| 82 | + context = LLMContext(messages) |
| 83 | + context_aggregator = LLMContextAggregatorPair(context) |
| 84 | + |
| 85 | + pipeline = Pipeline( |
| 86 | + [ |
| 87 | + transport.input(), # Transport user input |
| 88 | + stt, # STT |
| 89 | + context_aggregator.user(), # User responses |
| 90 | + llm, # LLM |
| 91 | + tts, # TTS |
| 92 | + transport.output(), # Transport bot output |
| 93 | + context_aggregator.assistant(), # Assistant spoken responses |
| 94 | + ] |
| 95 | + ) |
| 96 | + |
| 97 | + task = PipelineTask( |
| 98 | + pipeline, |
| 99 | + params=PipelineParams( |
| 100 | + enable_metrics=True, |
| 101 | + enable_usage_metrics=True, |
| 102 | + ), |
| 103 | + idle_timeout_secs=runner_args.pipeline_idle_timeout_secs, |
| 104 | + ) |
| 105 | + |
| 106 | + @transport.event_handler("on_client_connected") |
| 107 | + async def on_client_connected(transport, client): |
| 108 | + logger.info(f"Client connected") |
| 109 | + # Kick off the conversation. |
| 110 | + messages.append({"role": "system", "content": "Please introduce yourself to the user."}) |
| 111 | + await task.queue_frames([LLMRunFrame()]) |
| 112 | + |
| 113 | + @transport.event_handler("on_client_disconnected") |
| 114 | + async def on_client_disconnected(transport, client): |
| 115 | + logger.info(f"Client disconnected") |
| 116 | + await task.cancel() |
| 117 | + |
| 118 | + runner = PipelineRunner(handle_sigint=runner_args.handle_sigint) |
| 119 | + |
| 120 | + await runner.run(task) |
| 121 | + |
| 122 | + |
| 123 | +async def bot(runner_args: RunnerArguments): |
| 124 | + """Main bot entry point compatible with Pipecat Cloud.""" |
| 125 | + transport = await create_transport(runner_args, transport_params) |
| 126 | + await run_bot(transport, runner_args) |
| 127 | + |
| 128 | + |
| 129 | +if __name__ == "__main__": |
| 130 | + from pipecat.runner.run import main |
| 131 | + |
| 132 | + main() |
0 commit comments