async def *synthesize_voice*(_text_: str) -> Ch...
# ask-ai
b
async def *synthesize_voice*(_text_: str) -> ChunkedStream: tts = agents.tts.StreamAdapter( tts=openai.TTS(model="gpt-4o-mini-tts", speed=1) ) stream = tts.synthesize(text) how to make this stream from TTS ready to be published by a local participant via await room.local_participant.publish_track() or should I use a different method?
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.
I'm glad you're interested in publishing a TTS audio stream using
room.local_participant.publish_track
in LiveKit! Let me walk you through the steps in a simple and clear way: 1. Initialize the TTS engine: First, set up an instance of the TTS service you want to use, like OpenAI, making sure to provide the necessary parameters such as the model and speed. 2. Create an AudioSource and LocalAudioTrack: Next, create an
AudioSource
using the TTS engine’s sample rate and number of channels. Then, create a
LocalAudioTrack
from this source:
Copy code
source = rtc.AudioSource(tts.sample_rate, tts.num_channels)
track = rtc.LocalAudioTrack.create_audio_track("agent-mic", source)
3. Define TrackPublishOptions: Here, you specify the source of your track, like setting it to microphone for TTS audio:
Copy code
options = rtc.TrackPublishOptions()
options.source = rtc.TrackSource.SOURCE_MICROPHONE
4. Connect to the room and publish the track: Now, connect your participant to the room and publish the audio track with these commands:
Copy code
await job.connect(auto_subscribe=AutoSubscribe.SUBSCRIBE_NONE)
publication = await job.room.local_participant.publish_track(track, options)
await publication.wait_for_subscription()
5. Stream and play synthesized audio: Finally, use the TTS stream interface to send chunks of text and push the synthesized audio frames to the source:
Copy code
stream = tts.stream()

async def _playback_task():
    async for audio in stream:
        await source.capture_frame(audio.frame)

task = asyncio.create_task(_playback_task())

words = text.split()
for i in range(0, len(words), 2):
    chunk = " ".join(words[i : i + 2])
    stream.push_text(chunk + " ")

stream.flush()
stream.end_input()
await asyncio.gather(task)
This method lets you publish synthesized speech in real-time within a LiveKit room, whether you're sending the entire text at once or streaming it bit by bit. If you have any questions or need further help, feel free to ask—I'm here to support you every step of the way! Sources: examples/other/text-to-speech/sync_tts_transcription.py
b
and how would you do agent to agent testing?
t
I'm glad you're interested in performing agent-to-agent testing in LiveKit! To help you get started, here’s a friendly guide to configure your agents to interact smoothly with each other: 1. Enable agent-to-agent interaction: You can allow agents to listen to and interact with each other by using one of these methods: ◦ Set
listen_to_agents=True
in `RoomInputOptions`:
Copy code
room_io = RoomIO(session, room=ctx.room,
    input_options=RoomInputOptions(
        listen_to_agents=True
    ))
◦ Alternatively, explicitly include agent participants with `participant_kinds`:
Copy code
room_io = RoomIO(
    session,
    room=ctx.room,
    input_options=RoomInputOptions(
        participant_kinds=[
            ParticipantKind.STANDARD,
            ParticipantKind.AGENT  # Enable agent-to-agent interaction
        ]
    )
)
await room_io.start()
1. Initialize agent sessions: Make sure to configure your agent session with
RoomInputOptions
to include agent participants, like this:
Copy code
await session.start(
    agent=SimpleEvaluationAgent(),
    room=ctx.room,
    room_input_options=RoomInputOptions(
        participant_kinds=[
            rtc.ParticipantKind.PARTICIPANT_KIND_AGENT,
        ]
    ),
)
2. Best practices for testing: ◦ Define clear user personas and evaluation dimensions such as accuracy and latency. ◦ Run your tests in isolated rooms to avoid any interference. ◦ Consider using deterministic scenarios for regression testing and randomized ones for exploratory evaluation. 1. Structured evaluation workflow: It’s helpful to implement a workflow that includes: ◦ Creating an evaluation agent with predefined test scenarios. ◦ Using LLM tool calls to grade responses (like PASS/FAIL). ◦ Collecting important performance metrics such as response latency and turn-taking accuracy. ◦ Generating structured evaluation reports to keep track of results. 1. Testing frameworks: Tools like pytest can be very useful for behavioral tests, including fixtures, function tool mocking, and validating conversation flows. Following these steps will set you up for success with agent-to-agent testing in LiveKit. If you have any questions or need further assistance, feel free to ask—I'm here to help! Sources: How to test your agent using another agent | Operational Guidance & Best Practices | livekit/livekit_composite | DeepWik... | docs/index.yaml | Testing and evaluation | LiveKit Docs | Agent Testing & CI/CD | livekit/livekit_composite | DeepWiki