for the context I'm using realtime model for my ag...
# ask-ai
b
for the context I'm using realtime model for my agent I want to do some testing. I'd like to use standalone TTS model to simulate user voice messages - is that possible?
t
Thanks for reaching out, mateuszkasprowicz. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Absolutely, you can use a standalone TTS model to simulate user voice messages in your realtime agent setup. It’s a great way to generate audio from text and then publish that audio stream to a LiveKit room, making it feel like it’s coming from a participant. Here’s a simple way to get started: • Create a standalone TTS instance using a supported model, such as OpenAI, Cartesia, or ElevenLabs. • Send the text to the TTS model to generate a stream of synthesized audio. • Publish the resulting audio stream to the LiveKit room using the
room.local_participant.publish_audio_track
method or through a custom agent participant. This approach lets you simulate user input, which is super helpful for testing or automation. Plus, you can customize the voice and audio features depending on the TTS provider you choose. If you’d like, I’m here to help with any part of the implementation or to provide more details—just let me know! Sources: pipeline-tts/cartesia_tts.py | Realtime model plugins | LiveKit Docs | OpenAI Realtime API plugin guide | LiveKit docs | Text-to-speech (TTS) models | LiveKit Docs | examples/voice_agents/realtime_load_chat_history.py
b
can you show me an example implementation?
t
Here’s a friendly example to help you simulate user voice messages using a standalone TTS model in LiveKit. This example uses the Rime TTS plugin for real-time audio streaming, and I hope it makes things a bit easier for you:
Copy code
import asyncio
import logging

from livekit import rtc
from livekit.agents.tts import StreamAdapter
from livekit.plugins import rime

async def simulate_voice():
    # Initialize Rime TTS
    tts = rime.TTS(model="arcana", speaker="astra")

    # Set up audio source and track
    source = rtc.AudioSource(tts.sample_rate, tts.num_channels)
    track = rtc.LocalAudioTrack.create_audio_track("agent-mic", source)

    # Publish the audio track to the room
    room = await rtc.connect_to_room("your_room_url", "your_token")
    await room.local_participant.publish_track(track)

    # Prepare text for streaming
    text = "Hello! This is a test of the TTS simulation in LiveKit."
    stream = StreamAdapter(tts=tts).stream()

    # Push text to TTS and stream audio
    stream.push_text(text)
    async for audio in stream:
        await source.capture_frame(audio.frame)

    # End the stream
    stream.end_input()

# Run the simulation
asyncio.run(simulate_voice())
This script sets up a TTS model, streams the synthesized audio to a LiveKit room, and simulates user voice messages. Just be sure to swap out the placeholders like
"your_room_url"
and
"your_token"
with your actual LiveKit room details. If you have any questions or would like me to walk you through a more specific example, please don’t hesitate to ask—I’m here to help! Sources: Baseten TTS plugin guide | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_activity.py | Text-to-speech (TTS) models | LiveKit Docs
b
and how to use it in tests. provide a simple, minimal implementation
t
Here’s a minimal implementation to use a standalone TTS model in tests for simulating user voice messages:
Copy code
import asyncio
from livekit import rtc
from livekit.agents.tts import StreamAdapter
from livekit.plugins import rime

async def test_tts():
    # Initialize TTS model
    tts = rime.TTS(model="arcana", speaker="astra")

    # Create a stream for TTS
    stream = StreamAdapter(tts=tts).stream()

    # Push text to simulate user input
    stream.push_text("Hello, this is a test message.")
    stream.push_text("This simulates user voice input.")

    # Collect synthesized audio frames
    audio_frames = []
    async for audio in stream:
        audio_frames.append(audio.frame)

    # End the stream
    stream.end_input()

    # Use audio_frames in your tests
    print(f"Generated {len(audio_frames)} audio frames.")

# Run the test
asyncio.run(test_tts())
This script gently guides you through initializing a TTS model, streaming synthesized audio, and collecting audio frames for your testing needs. Feel free to swap out the TTS provider and model to best fit your unique use case. If you have any questions or would like more examples, just let me know—I’m here to help! Sources: plugins/google/src/beta/gemini_tts.test.ts | Text-to-speech (TTS) models | LiveKit Docs | Node.js Agents Architecture | livekit/livekit_composite | DeepWiki
b
but how to test its interactions with my actual realtime agent
t
To help you test interactions between a standalone TTS model and your realtime agent, here’s a simple step-by-step guide you can follow: 1. Configure the Realtime Model: First, set your realtime model to return text-only responses by configuring its
response_format
to "text". This way, the agent will output text that the TTS model can easily process. 2. Integrate the TTS Model: Next, bring in a standalone TTS instance (like Cartesia or ElevenLabs) into your agent session. Here’s a quick example:
Copy code
from livekit.agents import AgentSession
from livekit.plugins import cartesia

tts = cartesia.TTS()
session = AgentSession(
    llm=realtime_model,
    tts=tts,
    # Add other configurations like STT if needed
)
1. Simulate User Input: Then, push text to the TTS model to generate audio and publish it to the LiveKit room. This helps simulate user voice messages for your agent to work with. 2. Monitor and Validate: Keep an eye on metrics events (like
MetricsCollected
) to check latency and synchronization. If you want, you can also transcribe the audio output using Whisper or another STT model to ensure accuracy. 3. Dynamic Testing: Feel free to test multiple TTS providers by switching between them during a session. For example, you can use the
handoff
mechanism to hand over control to a new agent with a different TTS model. If you’d like, I’m happy to provide a specific code example for any of these steps—just let me know! Sources: Realtime model plugins | LiveKit Docs | plugins/rime/src/tts.test.ts | pipeline-tts/tts_comparison/README.md | Text-to-speech (TTS) models | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_activity.py
b
how to create a test roomIO instance for a specific test
t
To help you create a test RoomIO instance for your specific test, here’s a simple step-by-step guide you can follow: 1. Use a Mock Room: For unit testing, you can easily create a mock room using the
create_mock_room()
function:
Copy code
from livekit.agents.ipc.mock_room import create_mock_room
from livekit.agents.voice import RoomIO

# Create a mock room
mock_room = create_mock_room()

# Initialize RoomIO with the mock room
room_io = RoomIO(
    session=your_agent_session,
    room=mock_room,
    input_options={"audio_enabled": True}
)

# Start the RoomIO instance
await room_io.start()
1. For Integration Testing: You can connect to a real LiveKit server using test credentials. Just generate tokens with the right permissions and pass the connected room to RoomIO. 2. Customize RoomIO: Feel free to adjust
input_options
and
output_options
to control how media behaves, like enabling audio or focusing on specific participants. If you’d like, I’m here to help with more details or examples tailored to your testing needs—just let me know! Sources: livekit-agents/livekit/agents/ipc/mock_room.py | Community & Support | livekit/livekit_composite | DeepWiki | livekit/tests/common/e2e/mod.rs | Tests/LiveKitTestSupport/Room.swift | agents/src/voice/agent_session.ts
b
so with create_mock_room would I actually be able to simulate agent and user and their interactions?
t
The
create_mock_room
function gives you a mocked room instance with essential properties like participants and connection state. However, it doesn’t automatically simulate active interactions between agents and users, such as voice or data exchange. To help you simulate agent-user interactions, here are a few approaches you might find useful: • Try agent-to-agent testing, where one agent acts as a simulated user. You can configure
RoomInputOptions
to listen to other agents. • Use mocking tools like
FakeSTT
,
FakeTTS
, and
FakeAudioInput
to simulate speech, text, and audio streams. • For a more comprehensive approach, the LiveKit CLI offers an agent load testing tool that simulates echo participants and helps evaluate agent performance under load. If you'd like, I’d be happy to walk you through an example of setting up agent-to-agent testing or show you how to use these tools. Just let me know! Sources: tests/test_agent_session.py | Agent Testing & CI/CD | livekit/livekit_composite | DeepWiki | README.md
b
let's say I want to agent to agent testing
t
To set up agent-to-agent testing in LiveKit, let me guide you through the steps: 1. Enable Agent-to-Agent Interaction: You’ll want to configure
RoomInputOptions
so agents can listen to each other. Here’s a simple example to get you started:
Copy code
from livekit.agents.voice import RoomIO
from livekit.agents.voice.room_io import RoomInputOptions

room_io = RoomIO(
    session=your_agent_session,
    room=ctx.room,
    input_options=RoomInputOptions(
        listen_to_agents=True  # Enables agent-to-agent interaction
    )
)
await room_io.start()
1. Develop a Test/Evaluation Agent: Try creating an agent that has predefined test scenarios along with grading logic. For instance, you can use LLM tool calls to assess responses from the target agent. 2. Use the LiveKit CLI for Load Testing: The
lk perf agent-load-test
command is super helpful to simulate multiple rooms and echo participants for performance checks. Here’s how you might run it:
Copy code
lk perf agent-load-test \
  --rooms 5 \
  --agent-name test-agent \
  --echo-speech-delay 10s \
  --duration 5m
1. Integrate with Testing Frameworks: Don’t forget to use
pytest
alongside LiveKit’s built-in testing helpers. This will help you validate agent behavior, tool calls, and keep your conversation flow smooth. If you’d like, I’m here to help with a specific example for any of these steps—just let me know! Sources: How to test your agent using another agent | README.md | docs/index.yaml | Testing and evaluation | LiveKit Docs | README.md
b
so let's say I'm running a test agent entrypoint I explictly create a room, generate a token for a test user and dispatch and agent there. how to connect this simulated user now?
t
Hi there! To help you connect a simulated user to a LiveKit room for testing with an agent, here’s a simple step-by-step guide you can follow: 1. Use the LiveKit CLI: You can run this command to simulate a user connecting to the room:
Copy code
livekit-cli agents console --url <livekit_url> --api-key <api_key> --api-secret <api_secret>
1. Programmatically Simulate a User: Try using the
simulate_job
method to create a room and connect a simulated user like this:
Copy code
await worker.simulate_job(SimulateJobInfo(room="test-room", participant_identity="simulated-user"))
1. Enable Agent-to-Agent Interaction: If your simulated user is actually another agent, you’ll want to configure
RoomInputOptions
to listen to agent participants:
Copy code
room_io = RoomIO(session, room=ctx.room,
    input_options=RoomInputOptions(listen_to_agents=True))
1. Alternative: Load Testing: You can also use the LiveKit CLI’s load testing tool to simulate multiple users and agents in a room with this command:
Copy code
lk perf agent-load-test \
  --rooms 1 \
  --agent-name test-agent \
  --echo-speech-delay 10s \
  --duration 5m
If you’d like, I’m here to provide more details or examples on any of these methods—just let me know! Sources: examples/avatar_agents/bey/README.md | Agent Testing & CI/CD | livekit/livekit_composite | DeepWiki | Community & Support | livekit/livekit_composite | DeepWiki | Web and mobile frontends | LiveKit Docs | pkg/loadtester/agentloadtester.go
b
how to access worker?
t
I'm glad you're exploring how to access a worker instance in LiveKit for testing or simulation! Here's a friendly guide to help you through the process: 1. Simulate a Worker: You can use the
SimulateAgentWorker
method from LiveKit server’s test utilities. This handy tool lets you create a simulated worker that connects to the LiveKit server and manages job availability and assignments smoothly. 2. Create a TestServer: Try instantiating a test server with
NewTestServer(bus)
or
NewTestServerWithService(s)
. This sets up an environment that mimics agent-service communication, making your testing more realistic. 3. Control Job Flow: You can programmatically manage job assignments during your simulation using helper methods like
Accept()
or
Reject()
on job requests. This gives you great control over how jobs are handled. This method is mainly designed for internal testing within the LiveKit server codebase. However, if you're looking to perform end-to-end testing in a deployed environment, the LiveKit CLI or sample applications are excellent tools to simulate participants and trigger agent dispatch. Please feel free to reach out if you'd like more details or examples—I’m here to help you get this set up smoothly! Sources: README.md | LiveKit CLI Setup | LiveKit Docs
b
show me the code
t
Here’s a friendly example to help you simulate a worker and connect it to a LiveKit room programmatically in Python:
Copy code
from livekit.agents import Worker
from livekit.agents.api import SimulateJobInfo

async def simulate_worker():
    # Initialize the worker
    worker = Worker(api_key="your_api_key", api_secret="your_api_secret")

    # Simulate a job by creating a room and participant
    await worker.simulate_job(SimulateJobInfo(
        room="test-room",
        participant_identity="simulated-user"
    ))

# Run the simulation
import asyncio
asyncio.run(simulate_worker())
This snippet sets up a simulated worker, creates a room, and connects a participant for testing purposes. Just be sure to swap out
"your_api_key"
and
"your_api_secret"
with your own LiveKit credentials. If you have any questions or want me to walk you through more examples, please feel free to ask—I’m here to help! Sources: README.md | livekit-agents/livekit/agents/worker.py | README.md | Worker lifecycle | LiveKit Docs | Worker options | LiveKit Docs