how to stream voice agent pipeline
# ask-ai
p
how to stream voice agent pipeline
t
Thanks for reaching out, arpan. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd love to understand more about how you're planning to use the voice agent pipeline for streaming. Could you share a bit more about your goals? For example: • Are you hoping to connect a voice agent with LiveKit for real-time conversations? • Do you need a hand with setting up the pipeline, managing audio streams, or something else? • Are there particular technologies or tools you're working with in this setup? The more details you provide, the better I can support you. I'm here to help!
p
llstrem
t
I'm glad you're interested in the VoicePipelineAgent in LiveKit! It's thoughtfully designed to help manage voice-based AI interactions by smoothly orchestrating a pipeline of models like Speech-to-Text (STT), Large Language Model (LLM), and Text-to-Speech (TTS). Plus, it uses Voice Activity Detection (VAD) and turn detection to keep the conversation flowing naturally. Here’s a friendly example of how you might set it up in Node.js:
Copy code
const agent = new pipeline.VoicePipelineAgent(
  vad,
  new deepgram.STT(),
  new openai.LLM(),
  new elevenlabs.TTS(),
  { chatCtx: initialContext }
);
agent.start(ctx.room, participant);
await agent.say('Hey, how can I help you today', true);
This setup configures the agent with VAD, STT, LLM, and TTS models, and it kicks off the interaction as soon as a participant joins the room. LiveKit also offers examples in both Python and Node.js, and supports integrations with providers like Deepgram, OpenAI, and ElevenLabs. If you'd like, I’m here to help you with any specific configurations or to dive deeper into details—just let me know! Sources: agent/main.py | chain-of-thought-tts/agent.py | README.md | AI voice agents | LiveKit Docs | VoicePipelineAgent | LiveKit Docs
p
python strem useing LLM stream
t
To stream using an LLM in Python with LiveKit's VoicePipelineAgent, you can follow this example:
Copy code
import logging
from dotenv import load_dotenv

from livekit.agents import llm
from livekit.agents.pipeline import VoicePipelineAgent
from livekit.plugins import deepgram, openai, silero

# Set logging for debugging
logging.basicConfig(level=logging.INFO)

# Load environment variables
load_dotenv(".env")

# Define system prompt
initial_ctx = llm.ChatContext().append(
    role="system",
    text="You are a helpful voice assistant. Respond concisely."
)

# Define a callback to filter text before TTS
async def before_tts_cb(agent: VoicePipelineAgent, text: str):
    # Replace problematic words
    return text.replace("error", "issue").replace("down", "unavailable")

# Initialize the VoicePipelineAgent with Groq as the LLM provider
agent = VoicePipelineAgent(
    vad=silero.VAD.load(),
    stt=deepgram.STT(model="nova-2-general"),
    llm=openai.LLM.with_groq(model="llama3-70b-8192"), # example model
    tts=openai.TTS(), # or any other TTS provider
    before_tts_cb=before_tts_cb,
    chat_ctx=initial_ctx,
    allow_interruptions=True, # allow user to interrupt
)

# Start the agent
agent.start(room, participant)
This setup streams LLM responses in real time, using callbacks like
before_tts_cb
to customize text before it is synthesized into speech. Please feel free to reach out if you'd like help with specific configurations or if you'd like me to walk you through any part of this. I'm here to support you every step of the way! Sources: chain-of-thought-tts/agent.py | livekit-plugins/livekit-plugins-google/livekit/plugins/google/llm.py | VoicePipelineAgent | LiveKit Docs | OpenAI compatible LLMs | LiveKit Docs
p
LLMStream
t
The LLMStream in LiveKit is a key part of the VoicePipelineAgent framework, thoughtfully designed to manage real-time streaming of responses from an LLM. It works by asynchronously sending response chunks as they come in from the LLM, making it super easy to integrate with Text-to-Speech (TTS) for instant speech synthesis. Here’s a friendly overview of how it works: • Streaming Responses: The
LLM.chat()
method gives you an
LLMStream
object, which you can iterate over asynchronously to handle response chunks bit by bit. • Error Handling: The
LLMStream
class has strong error handling built in, turning provider-specific exceptions into standardized LiveKit exceptions so you get consistent behavior no matter which LLM provider you use. • Customization: You have the flexibility to use callbacks like
before_llm_cb
to tweak the input context before it’s sent to the LLM, and
before_tts_cb
to adjust the output text before it’s turned into speech. If you’d like, I’d be happy to share a specific example or dive deeper into how you can use
LLMStream
. Just let me know!
p
example
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!
p
LLMStream example
from livekit.agents.llm import LLMStream