Creating a Tavus example using the DailyTransport.

This commit is contained in:
Filipi Fuchter
2025-05-23 23:03:40 -03:00
parent 6346ca1a84
commit 8221dd594e

View File

@@ -7,9 +7,9 @@
import asyncio import asyncio
import os import os
import sys import sys
from typing import Any, Mapping
import aiohttp import aiohttp
from daily_runner import configure
from dotenv import load_dotenv from dotenv import load_dotenv
from loguru import logger from loguru import logger
@@ -20,7 +20,7 @@ from pipecat.pipeline.task import PipelineParams, PipelineTask
from pipecat.processors.aggregators.openai_llm_context import OpenAILLMContext from pipecat.processors.aggregators.openai_llm_context import OpenAILLMContext
from pipecat.services.cartesia.tts import CartesiaTTSService from pipecat.services.cartesia.tts import CartesiaTTSService
from pipecat.services.deepgram.stt import DeepgramSTTService from pipecat.services.deepgram.stt import DeepgramSTTService
from pipecat.services.openai.llm import OpenAILLMService from pipecat.services.google.llm import GoogleLLMService
from pipecat.services.tavus.video import TavusVideoService from pipecat.services.tavus.video import TavusVideoService
from pipecat.transports.services.daily import DailyParams, DailyTransport from pipecat.transports.services.daily import DailyParams, DailyTransport
@@ -32,23 +32,20 @@ logger.add(sys.stderr, level="DEBUG")
async def main(): async def main():
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
tavus = TavusVideoService( (room_url, token) = await configure(session)
api_key=os.getenv("TAVUS_API_KEY"),
replica_id=os.getenv("TAVUS_REPLICA_ID"),
session=session,
)
# get persona, look up persona_name, set this as the bot name to ignore
persona_name = await tavus.get_persona_name()
room_url = await tavus.initialize()
transport = DailyTransport( transport = DailyTransport(
room_url=room_url, room_url,
token=None, token,
bot_name="Pipecat bot", "Pipecat bot",
params=DailyParams( DailyParams(
audio_in_enabled=True, audio_in_enabled=True,
audio_out_enabled=True,
video_out_enabled=True,
video_out_is_live=True,
vad_analyzer=SileroVADAnalyzer(), vad_analyzer=SileroVADAnalyzer(),
video_out_width=1280,
video_out_height=720,
), ),
) )
@@ -59,7 +56,13 @@ async def main():
voice_id="a167e0f3-df7e-4d52-a9c3-f949145efdab", voice_id="a167e0f3-df7e-4d52-a9c3-f949145efdab",
) )
llm = OpenAILLMService(model="gpt-4o-mini") llm = GoogleLLMService(api_key=os.getenv("GOOGLE_API_KEY"))
tavus = TavusVideoService(
api_key=os.getenv("TAVUS_API_KEY"),
replica_id=os.getenv("TAVUS_REPLICA_ID"),
session=session,
)
messages = [ messages = [
{ {
@@ -87,10 +90,8 @@ async def main():
task = PipelineTask( task = PipelineTask(
pipeline, pipeline,
params=PipelineParams( params=PipelineParams(
# We just use 16000 because that's what Tavus is expecting and
# we avoid resampling.
audio_in_sample_rate=16000, audio_in_sample_rate=16000,
audio_out_sample_rate=16000, audio_out_sample_rate=24000,
allow_interruptions=True, allow_interruptions=True,
enable_metrics=True, enable_metrics=True,
enable_usage_metrics=True, enable_usage_metrics=True,
@@ -98,33 +99,22 @@ async def main():
), ),
) )
@transport.event_handler("on_participant_joined") @transport.event_handler("on_first_participant_joined")
async def on_participant_joined( async def on_first_participant_joined(transport, participant):
transport: DailyTransport, participant: Mapping[str, Any] # Kick off the conversation.
) -> None: messages.append(
# Ignore the Tavus replica's microphone {
if participant.get("info", {}).get("userName", "") == persona_name: "role": "system",
logger.debug(f"Ignoring {participant['id']}'s microphone") "content": "Start by greeting the user and ask how you can help.",
await transport.update_subscriptions( }
participant_settings={ )
participant["id"]: { await task.queue_frames([context_aggregator.user().get_context_frame()])
"media": {"microphone": "unsubscribed"},
}
}
)
if participant.get("info", {}).get("userName", "") != persona_name:
# 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") @transport.event_handler("on_participant_left")
async def on_participant_left(transport, participant, reason): async def on_participant_left(transport, participant, reason):
await task.cancel() await task.cancel()
runner = PipelineRunner() runner = PipelineRunner(handle_sigint=False)
await runner.run(task) await runner.run(task)