Code review fixes + docstrings
This commit is contained in:
@@ -1,104 +0,0 @@
|
|||||||
#
|
|
||||||
# Copyright (c) 2024–2025, Daily
|
|
||||||
#
|
|
||||||
# SPDX-License-Identifier: BSD 2-Clause License
|
|
||||||
#
|
|
||||||
|
|
||||||
import asyncio
|
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
|
|
||||||
import aiohttp
|
|
||||||
from dotenv import load_dotenv
|
|
||||||
from loguru import logger
|
|
||||||
from runner import configure
|
|
||||||
|
|
||||||
from pipecat.audio.vad.silero import SileroVADAnalyzer
|
|
||||||
from pipecat.frames.frames import EndFrame
|
|
||||||
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.elevenlabs import ElevenLabsHttpTTSService
|
|
||||||
from pipecat.services.openai import OpenAILLMService
|
|
||||||
from pipecat.transports.services.daily import DailyParams, DailyTransport
|
|
||||||
|
|
||||||
load_dotenv(override=True)
|
|
||||||
|
|
||||||
logger.remove(0)
|
|
||||||
logger.add(sys.stderr, level="DEBUG")
|
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
|
||||||
async with aiohttp.ClientSession() as session:
|
|
||||||
(room_url, token) = await configure(session)
|
|
||||||
|
|
||||||
transport = DailyTransport(
|
|
||||||
room_url,
|
|
||||||
token,
|
|
||||||
"Respond bot",
|
|
||||||
DailyParams(
|
|
||||||
audio_out_enabled=True,
|
|
||||||
transcription_enabled=True,
|
|
||||||
vad_enabled=True,
|
|
||||||
vad_analyzer=SileroVADAnalyzer(),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
tts = ElevenLabsHttpTTSService(
|
|
||||||
api_key=os.getenv("ELEVENLABS_API_KEY", ""),
|
|
||||||
voice_id=os.getenv("ELEVENLABS_VOICE_ID", ""),
|
|
||||||
# params=ElevenLabsHttpTTSService.InputParams(language="en"),
|
|
||||||
)
|
|
||||||
|
|
||||||
llm = OpenAILLMService(api_key=os.getenv("OPENAI_API_KEY"), model="gpt-4o")
|
|
||||||
|
|
||||||
messages = [
|
|
||||||
{
|
|
||||||
"role": "system",
|
|
||||||
"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.",
|
|
||||||
},
|
|
||||||
]
|
|
||||||
|
|
||||||
context = OpenAILLMContext(messages)
|
|
||||||
context_aggregator = llm.create_context_aggregator(context)
|
|
||||||
|
|
||||||
pipeline = Pipeline(
|
|
||||||
[
|
|
||||||
transport.input(), # Transport user input
|
|
||||||
context_aggregator.user(), # User responses
|
|
||||||
llm, # LLM
|
|
||||||
tts, # TTS
|
|
||||||
transport.output(), # Transport bot output
|
|
||||||
context_aggregator.assistant(), # Assistant spoken responses
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
task = PipelineTask(
|
|
||||||
pipeline,
|
|
||||||
PipelineParams(
|
|
||||||
allow_interruptions=True,
|
|
||||||
enable_metrics=True,
|
|
||||||
enable_usage_metrics=True,
|
|
||||||
report_only_initial_ttfb=True,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
@transport.event_handler("on_first_participant_joined")
|
|
||||||
async def on_first_participant_joined(transport, participant):
|
|
||||||
await transport.capture_participant_transcription(participant["id"])
|
|
||||||
# Kick off the conversation.
|
|
||||||
messages.append({"role": "system", "content": "Please introduce yourself to the user."})
|
|
||||||
await task.queue_frames([context_aggregator.user().get_context_frame()])
|
|
||||||
|
|
||||||
@transport.event_handler("on_participant_left")
|
|
||||||
async def on_participant_left(transport, participant, reason):
|
|
||||||
await task.queue_frame(EndFrame())
|
|
||||||
|
|
||||||
runner = PipelineRunner()
|
|
||||||
|
|
||||||
await runner.run(task)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
asyncio.run(main())
|
|
||||||
@@ -430,6 +430,7 @@ class ElevenLabsHttpTTSService(TTSService):
|
|||||||
Args:
|
Args:
|
||||||
api_key: ElevenLabs API key
|
api_key: ElevenLabs API key
|
||||||
voice_id: ID of the voice to use
|
voice_id: ID of the voice to use
|
||||||
|
aiohttp_session: aiohttp ClientSession
|
||||||
model: Model ID (default: "eleven_flash_v2_5" for low latency)
|
model: Model ID (default: "eleven_flash_v2_5" for low latency)
|
||||||
base_url: API base URL
|
base_url: API base URL
|
||||||
output_format: Audio output format (PCM)
|
output_format: Audio output format (PCM)
|
||||||
@@ -449,20 +450,20 @@ class ElevenLabsHttpTTSService(TTSService):
|
|||||||
*,
|
*,
|
||||||
api_key: str,
|
api_key: str,
|
||||||
voice_id: str,
|
voice_id: str,
|
||||||
|
aiohttp_session: aiohttp.ClientSession,
|
||||||
model: str = "eleven_flash_v2_5",
|
model: str = "eleven_flash_v2_5",
|
||||||
base_url: str = "https://api.elevenlabs.io",
|
base_url: str = "https://api.elevenlabs.io",
|
||||||
output_format: ElevenLabsOutputFormat = "pcm_24000",
|
output_format: ElevenLabsOutputFormat = "pcm_24000",
|
||||||
params: InputParams = InputParams(),
|
params: InputParams = InputParams(),
|
||||||
**kwargs,
|
**kwargs,
|
||||||
):
|
):
|
||||||
sample_rate = sample_rate_from_output_format(output_format)
|
super().__init__(sample_rate=sample_rate_from_output_format(output_format), **kwargs)
|
||||||
super().__init__(sample_rate=sample_rate, **kwargs)
|
|
||||||
|
|
||||||
self._api_key = api_key
|
self._api_key = api_key
|
||||||
self._base_url = base_url
|
self._base_url = base_url
|
||||||
self._output_format = output_format
|
self._output_format = output_format
|
||||||
self._params = params
|
self._params = params
|
||||||
self._session: Optional[aiohttp.ClientSession] = None
|
self._session = aiohttp_session
|
||||||
|
|
||||||
self._settings = {
|
self._settings = {
|
||||||
"sample_rate": sample_rate_from_output_format(output_format),
|
"sample_rate": sample_rate_from_output_format(output_format),
|
||||||
@@ -484,6 +485,11 @@ class ElevenLabsHttpTTSService(TTSService):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
def _set_voice_settings(self) -> Optional[Dict[str, Union[float, bool]]]:
|
def _set_voice_settings(self) -> Optional[Dict[str, Union[float, bool]]]:
|
||||||
|
"""Configure voice settings if stability and similarity_boost are provided.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Dictionary of voice settings or None if required parameters are missing.
|
||||||
|
"""
|
||||||
voice_settings: Dict[str, Union[float, bool]] = {}
|
voice_settings: Dict[str, Union[float, bool]] = {}
|
||||||
if (
|
if (
|
||||||
self._settings["stability"] is not None
|
self._settings["stability"] is not None
|
||||||
@@ -507,27 +513,16 @@ class ElevenLabsHttpTTSService(TTSService):
|
|||||||
|
|
||||||
return voice_settings or None
|
return voice_settings or None
|
||||||
|
|
||||||
async def start(self, frame: StartFrame):
|
|
||||||
await super().start(frame)
|
|
||||||
self._session = aiohttp.ClientSession()
|
|
||||||
|
|
||||||
async def stop(self, frame: EndFrame):
|
|
||||||
await super().stop(frame)
|
|
||||||
if self._session:
|
|
||||||
await self._session.close()
|
|
||||||
self._session = None
|
|
||||||
|
|
||||||
async def cancel(self, frame: CancelFrame):
|
|
||||||
await super().cancel(frame)
|
|
||||||
if self._session:
|
|
||||||
await self._session.close()
|
|
||||||
self._session = None
|
|
||||||
|
|
||||||
async def run_tts(self, text: str) -> AsyncGenerator[Frame, None]:
|
async def run_tts(self, text: str) -> AsyncGenerator[Frame, None]:
|
||||||
logger.debug(f"Generating TTS: [{text}]")
|
"""Generate speech from text using ElevenLabs streaming API.
|
||||||
|
|
||||||
if not self._session:
|
Args:
|
||||||
self._session = aiohttp.ClientSession()
|
text: The text to convert to speech
|
||||||
|
|
||||||
|
Yields:
|
||||||
|
Frames containing audio data and status information
|
||||||
|
"""
|
||||||
|
logger.debug(f"Generating TTS: [{text}]")
|
||||||
|
|
||||||
url = f"{self._base_url}/v1/text-to-speech/{self._voice_id}/stream"
|
url = f"{self._base_url}/v1/text-to-speech/{self._voice_id}/stream"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user