Hello, I am using the Python Realtime Starter, in ...
# ask-ai
h
Hello, I am using the Python Realtime Starter, in the console i can speak with it, but if i want to connect to the Playground or my frontend; I try to use it with the dev mode, I get the following error: raise Exception("cannot access local participant before connecting") Exception: cannot access local participant before connecting Do you have any idea what I need to change? here i smy code: # stt -> openai # lmm -> openai # tss -> openai from dotenv import load_dotenv from livekit import agents from livekit.agents import AgentSession, Agent, RoomInputOptions from livekit.plugins import ( openai, noise_cancellation, silero ) #from livekit.plugins.turn_detector.multilingual import MultilingualModel load_dotenv() class Assistant(Agent): def __init__(self) -> None: super().__init__(instructions="You are a helpful voice AI assistant.") async def entrypoint(ctx: agents.JobContext): session = AgentSession( stt=openai.STT(model="gpt-4o-transcribe"), llm=openai.LLM(model="gpt-4o-mini"), tts = openai.TTS(model="gpt-4o-mini-tts", voice="ash", instructions="Speak in a friendly and conversational tone."), vad=silero.VAD.load(), #turn_detection=MultilingualModel() ) await session.start( room=ctx.room, agent=Assistant(), room_input_options=RoomInputOptions( # LiveKit Cloud enhanced noise cancellation # - If self-hosting, omit this parameter # - For telephony applications, use
BVCTelephony
for best results noise_cancellation=noise_cancellation.BVC(), ), ) await session.generate_reply( instructions="Greet the user and offer your assistance." ) if name == "__main__": agents.cli.run_app(agents.WorkerOptions(entrypoint_fnc=entrypoint))
t
Thanks for reaching out, Ju. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
The error "cannot access local participant before connecting" occurs when attempting to access the local participant before establishing a connection to the room. To resolve this, ensure that the
connect()
method is called before accessing the
local_participant
property. Here’s how you can fix it: 1. Call
room.connect()
with the required URL and token before accessing the local participant. 2. Ensure the connection is established before interacting with the
local_participant
. For example:
Copy code
room = rtc.Room()

# Connect to the room
await room.connect("<ws://localhost:7880>", "your_token")

# Access the local participant after connecting
local_participant = room.local_participant
Make sure your code follows this sequence to avoid the error. Sources: livekit-rtc/livekit/rtc/room.py
h
how would you cange then the starter code I have sent?
t
Hey there! To make sure your starter code connects properly before accessing the local participant, you can tweak the
entrypoint
function by adding the
connect()
method. Here’s the updated snippet for you:
Copy code
async def entrypoint(ctx: agents.JobContext):
    session = AgentSession(
        stt=openai.STT(model="gpt-4o-transcribe"),
        llm=openai.LLM(model="gpt-4o-mini"),
        tts=openai.TTS(model="gpt-4o-mini-tts", voice="ash", instructions="Speak in a friendly and conversational tone."),
        vad=silero.VAD.load(),
    )

    # Connect to the room before starting the session
    await ctx.room.connect("<ws://your-server-url>", "your_token")

    await session.start(
        room=ctx.room,
        agent=Assistant(),
        room_input_options=RoomInputOptions(
            noise_cancellation=noise_cancellation.BVC(),
        ),
    )

    await session.generate_reply(
        instructions="Greet the user and offer your assistance."
    )
Just a quick heads-up: don’t forget to swap out
"<ws://your-server-url>"
and
"your_token"
with your actual WebSocket URL and token from your LiveKit server. This little change will make sure your room is connected before the session kicks off, so you won’t run into that pesky error. Hope this helps!
h
from where do i get the token?
t
Hey there! To generate a token for connecting to a LiveKit room, you’ll need your LiveKit API credentials handy. Here’s a simple way to do it in Python: 1. First, make sure your
LIVEKIT_API_KEY
and
LIVEKIT_API_SECRET
are set up in your environment variables. 2. Then, you can use this function to create your token:
Copy code
from livekit import api
from dotenv import load_dotenv
import os

# Load environment variables
load_dotenv()

def generate_token(room_name, identity=None, name=None):
    """
    Generate a LiveKit access token for room access.
    
    Args:
        room_name (str): The name of the room to join
        identity (str, optional): The participant identity. Defaults to None.
        name (str, optional): The display name. Defaults to None.
    
    Returns:
        str: JWT token string
    """
    if not identity:
        identity = f"python-user-{room_name}"
    
    if not name:
        name = identity
        
    # Just a quick check to make sure your environment variables are set
    if not (LIVEKIT_API_KEY := os.getenv("LIVEKIT_API_KEY")) or not (LIVEKIT_API_SECRET := os.getenv("LIVEKIT_API_SECRET")):
        raise ValueError("LIVEKIT_API_KEY and LIVEKIT_API_SECRET must be set in .env file")
    
    # Now, let’s create the token with video permissions
    token = (
        api.AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET)
        .with_identity(identity)
        .with_name(name)
        .with_grants(
            api.VideoGrants(
                room_join=True,
                room=room_name,
            )
        )
        .to_jwt()
    )
    
    return token
Here’s how you might use it:
Copy code
# Generate a token for a participant
token = generate_token("my-room", identity="user123", name="Participant Name")

# Then, use this token to connect to your LiveKit room
Don’t forget to have your
.env
file set up like this:
Copy code
LIVEKIT_API_KEY=your_api_key
LIVEKIT_API_SECRET=your_api_secret
This will give you a JWT token ready to connect to your LiveKit room. If you hit any bumps or have questions, just give me a shout—I’m here to help! Sources: Generating tokens | LiveKit Docs | stream_audio.py | complex-agents/drive-thru/frontend/app/api/connection-details/route.ts
h
how to start then from dev mode?
t
Hey there! To get LiveKit up and running in development mode, here’s a quick guide for you: 1. Just start the LiveKit server with the
--dev
flag by running this command:
Copy code
livekit-server --dev
This will launch LiveKit using the default API key and secret:
devkey
and
secret
. Super simple! If you want to connect to the server from other devices on your network, no worries! Just add the
--bind
flag like this:
Copy code
livekit-server --dev --bind 0.0.0.0
If you hit any snags or want a hand with anything else, just give me a shout—I’m here to help! Sources: cmd/server/main.go | Running LiveKit locally | LiveKit Docs