How to use RealTimeModel example please
# ask-ai
w
How to use RealTimeModel example please
t
Thanks for reaching out, ajay. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm glad you're interested in using the
RealtimeModel
in LiveKit! To get started, you’ll initialize it with configurations like the AI model, voice, and optional parameters such as temperature or tool choice. This setup allows you to have real-time voice interactions by sending audio directly to the AI model and receiving responses in audio form. Here’s a simple example using the OpenAI Realtime Model to help you get a feel for it:
Copy code
import logging
from dotenv import load_dotenv
from livekit.agents import Agent, AgentSession, JobContext, WorkerOptions, cli, llm
from livekit.plugins import openai

# Load environment variables
load_dotenv()

# Define agent behavior
async def entrypoint(ctx: JobContext):
    chat_ctx = llm.ChatContext.empty()
    chat_ctx.add_message(role="user", content="Hey Sonic, tell me a children's story.")
    chat_ctx.add_message(role="assistant", content="Once upon a time in the sky lived a small fluffy cloud named Wispy...")

    llm_model = openai.realtime.RealtimeModel(
        voice="alloy",
        model="gpt-4o-mini-realtime-preview"
    )

    agent = Agent(
        instructions="You are a helpful voice AI assistant.",
        llm=llm_model,
        chat_ctx=chat_ctx,
    )

    session = AgentSession()
    await session.start(agent=agent, room=ctx.room)

if __name__ == "__main__":
    cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint))
This example walks you through initializing the
RealtimeModel
with a chosen voice and model, setting up an agent with a bit of conversation history, and starting a session to enjoy real-time voice interaction. If you’d like, I’m here to help with more details on advanced configurations or specific provider options—just let me know! Sources: Realtime model plugins | LiveKit Docs | Real-time AI Models & Streaming | livekit/livekit_composite | DeepWiki | examples/voice_agents/push_to_talk.py | Using the Testing Framework with Realtime Models