Back to snippets
pipecat_voice_chatbot_daily_openai_cartesia_quickstart.py
pythonA basic voice-to-voice AI chatbot using Daily for transport, OpenAI for LLM,
Agent Votes
1
0
100% positive
pipecat_voice_chatbot_daily_openai_cartesia_quickstart.py
1import asyncio
2import os
3import sys
4
5from pipecat.frames.frames import EndFrame
6from pipecat.pipeline.pipeline import Pipeline
7from pipecat.pipeline.runner import PipelineRunner
8from pipecat.pipeline.task import PipelineParams, PipelineTask
9from pipecat.processors.aggregators.openai_llm_context import OpenAILLMContext
10from pipecat.services.cartesia import CartesiaTTSService
11from pipecat.services.openai import OpenAILLMService
12from pipecat.transports.services.daily import DailyParams, DailyTransport
13
14from dotenv import load_dotenv
15
16load_dotenv(override=True)
17
18async def main():
19 async with DailyTransport(
20 room_url=os.getenv("DAILY_SAMPLE_ROOM_URL"),
21 token=None,
22 bot_name="Pipecat Bot",
23 params=DailyParams(audio_out_enabled=True)
24 ) as transport:
25
26 # Initialize services
27 llm = OpenAILLMService(api_key=os.getenv("OPENAI_API_KEY"), model="gpt-4o")
28 tts = CartesiaTTSService(
29 api_key=os.getenv("CARTESIA_API_KEY"),
30 voice_id="79a125e8-cd45-4c13-8a67-2756224abc25", # British Lady
31 )
32
33 # Set up conversation context
34 messages = [
35 {
36 "role": "system",
37 "content": "You are a helpful assistant. Keep your responses concise and friendly.",
38 },
39 ]
40 context = OpenAILLMContext(messages)
41 context_aggregator = llm.create_context_aggregator(context)
42
43 # Define the pipeline
44 pipeline = Pipeline([
45 transport.input(), # User audio in
46 context_aggregator.user(),
47 llm, # LLM processing
48 tts, # Text-to-Speech
49 transport.output(), # Bot audio out
50 context_aggregator.assistant(),
51 ])
52
53 task = PipelineTask(pipeline, PipelineParams(allow_interruptions=True))
54
55 # Handle the event when a user joins the room
56 @transport.event_handler("on_first_participant_joined")
57 async def on_first_participant_joined(transport, participant):
58 # Kick off the conversation
59 messages.append({"role": "user", "content": "Please introduce yourself."})
60 await task.queue_frames([context_aggregator.user().get_context_frame()])
61
62 runner = PipelineRunner()
63 await runner.run(task)
64
65if __name__ == "__main__":
66 asyncio.run(main())