If I am using the stt, llm, tts pipeline for audio...
# ask-ai
f
If I am using the stt, llm, tts pipeline for audio, but also wish to have text based input, how does the LLM differentiate between the text and voice audio? The audio is streamed realtime. For the text, I am trying to use room.localParticipant.sendText on the frontend, and I want the agent to be aware of the text, and then include it in the context before sending to llm to generate response. How do i do that? Can you show some code examples please? How do I make the LLM aware of the text input that I have written. The text could change dynamically, so I want the LLM to update the text state before invoking a call.
t
Thanks for reaching out, Mok. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! If you want to handle both text and audio input in your pipeline and smoothly include text input in the LLM context before getting a response, here are some friendly tips for you: • Tell Text and Audio Apart: Audio input goes through STT (Speech-to-Text) to become text. For direct text input, you can use
room.localParticipant.sendText
. To figure out where the input came from, check the
on_user_turn_completed
hook. • Change the LLM Context on the Fly: The
on_user_turn_completed
lifecycle hook is your buddy for tweaking or enriching user input before it reaches the LLM. You might want to add system messages or clean things up dynamically. • Add Text Input Manually: The
update_chat_ctx
method lets you pop text messages right into the context, so the LLM doesn’t miss a thing. Here’s a quick example showing how to adjust the LLM context dynamically:
Copy code
from livekit.agents import Agent, ChatContext, ChatMessage

class MyAgent(Agent):
    async def on_user_turn_completed(
        self, turn_ctx: ChatContext, new_message: ChatMessage
    ):
        # Just a little something: If the user says "urgent," add a system note
        if "urgent" in new_message.text_content.lower():
            turn_ctx.add_message(
                role="system",
                content="The user has indicated urgency. Respond quickly and concisely."
            )
        # Clean up the user message by removing "urgent"
        new_message.text_content = new_message.text_content.replace("urgent", "")
And if you want even more control, you can override the
llm_node
method to tweak the context right before sending it off to the LLM:
Copy code
async def llm_node(
    self,
    chat_ctx: ChatContext,
    tools: list[FunctionTool],
    model_settings: ModelSettings
):
    # Add some friendly instructions or context
    chat_ctx.add_message(
        role="system",
        content="Always respond in a friendly and helpful tone."
    )
    # Then pass it along to the default process
    async for chunk in self.default.llm_node(chat_ctx, tools, model_settings):
        yield chunk
With these handy methods, you’ll have both text and audio inputs working together smoothly in the LLM’s context to generate great responses. If you have any questions or want to chat more about this, I’m here to help! Sources: Agents 0.x migration guide | LiveKit Docs | Pipeline nodes and hooks | LiveKit Docs | agents/src/pipeline/pipeline_agent.ts
f
regarding the chat context, does it make it seem like the user has said the text? Also, how will the example change, if I use AgentSession? How do I defined the llm_node etc? do i put them within AgentSession? or within the function entrypoint?
t
When you add text input to the chat context, it’s treated just like the user has “said” that text. This way, both text and audio inputs blend smoothly into the conversation flow. About using
AgentSession
, you can create methods like
llm_node
right inside your
Agent
class. These methods will then be automatically called during the session. For instance, if you want to customize how the LLM behaves, you’d override the
llm_node
method in your agent class like this:
Copy code
from livekit.agents import Agent, ChatContext, ModelSettings

class MyAgent(Agent):
    async def llm_node(self, chat_ctx: ChatContext, tools: list, model_settings: ModelSettings):
        # Add dynamic instructions or context
        chat_ctx.add_message(
            role="system",
            content="Always respond in a friendly and helpful tone."
        )
        # Delegate to default implementation
        async for chunk in self.default.llm_node(chat_ctx, tools, model_settings):
            yield chunk
When you’re working with
AgentSession
, you just create an instance of your custom agent and pass it to the session. The overridden methods like
llm_node
will automatically be used throughout the session. Here’s a quick example:
Copy code
from livekit.agents import AgentSession

# Instantiate your custom agent
my_agent = MyAgent()

# Create a session with the agent
session = AgentSession(agent=my_agent)

# Start the session (example entry point)
await session.run()
By defining methods like
llm_node
in your agent class, you make sure they fit right into the session’s processing flow without any extra hassle. If you want me to walk you through anything else or share more examples, just let me know—I’m here to help! Sources: Agents v0.x migration guide - Node.js | LiveKit Docs | Agents 0.x migration guide | LiveKit Docs | Text and transcriptions | LiveKit Docs | examples/voice_agents/llamaindex-rag/chat_engine.py