how to override llm in runtime?
# ask-ai
a
how to override llm in runtime?
t
Thanks for reaching out, affandy. 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 override the LLM at runtime in LiveKit, you’ve got a few friendly options: • Override the
llm_node
Method:
You can create your own
Agent
subclass and tweak the
llm_node
method. This lets you control the LLM inference on the fly—like picking an LLM based on what’s happening right then or even changing the output stream. • Dynamic Assignment: Another way is to assign an LLM dynamically when a session starts, tailoring it to what you need. • Use OpenAI-Compatible Methods: You can also use handy methods like
with_groq()
or
with_ollama()
to switch between LLMs smoothly. If you want me to share some code examples or dive deeper into any of these, just say the word—I’m here to help! Sources: Docs::Agents | livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/llm.py
a
give me the override example
t
Here’s a friendly example of how you can override the
llm_node
method in your own
Agent
subclass to flexibly control the LLM behavior on the fly:
Copy code
from livekit.agents import Agent, llm, ModelSettings
from livekit.plugins import openai

class CustomAgent(Agent):
    async def llm_node(
        self,
        chat_ctx: llm.ChatContext,
        tools: list[llm.FunctionTool],
        model_settings: ModelSettings
    ) -> AsyncIterable[llm.ChatChunk | str]:
        # You can decide which LLM to use at runtime (maybe based on user input or config)
        use_groq = True  # This could come from session state
        current_llm = openai.LLM.with_groq(model="llama3-8b-8192") if use_groq else openai.LLM()

        async with current_llm.chat(chat_ctx=chat_ctx, tools=tools) as stream:
            async for chunk in stream:
                # Feel free to tweak or filter the output here
                if hasattr(chunk, 'delta') and hasattr(chunk.delta, 'content'):
                    chunk.delta.content = chunk.delta.content.replace("example", "customized")
                yield chunk
So, what’s happening here: • The
llm_node
method is customized to pick an LLM dynamically (like using
with_groq
for a particular model). • You have the option to adjust the output stream, such as swapping out or filtering content on the go. If anything’s unclear or you want me to walk you through more examples, just let me know—I’m here to help!
a
when defining cusotm agent, in
__init__()
there is a parameter for llm. can I change the used llm by inserting custom llm there?
t
Absolutely, you can change the LLM by inserting a custom LLM in the
__init__()
method of a custom
Agent
. The
Agent
class lets you pass an
llm
parameter in its constructor, so you can easily plug in your own LLM instance. For example, here’s how you might use the OpenAI plugin to specify a custom LLM:
Copy code
from livekit.agents import Agent\nfrom livekit.plugins import openai\n\nclass CustomAgent(Agent):\n    def __init__(self):\n        super().__init__(\n            instructions="You are a helpful agent.",\n            llm=openai.LLM.with_groq(model="llama3-8b-8192"),\n            tts=openai.TTS(instructions="Speak in a natural tone."),\n        )\n
In this example, the
llm
parameter is set to use a specific LLM (like Groq) dynamically. Feel free to swap it out with any compatible LLM instance, such as Cerebras, by using the right
with_*
method. If you want me to walk you through it more or need extra examples, just let me know—I’m here to help! Sources: Using Custom LLM Providers with the Agent | livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/llm.py | Modifying LLM output before TTS | LiveKit Docs | OpenAI compatible LLMs | LiveKit Docs | Docs::Agents
a
ok, I had the whole pipeline defined using stt-llm-tts pipeline. but there is a sub agent which I want to use realtime models instead. I've set the llm to realtime model. but then, I got this warning below
Copy code
turn_detection is set to 'vad', but the LLM is a RealtimeModel and server-side turn detection enabled, ignoring the turn_detection setting
how I can override the turn detection setting as well?
t
Hey there! If you want to override the turn detection settings when using a RealtimeModel LLM, you’ve got two main ways to go about it: 1. Use the RealtimeModel's Built-in Turn Detection: You can tweak the built-in turn detection by adjusting parameters like
threshold
,
prefix_padding_ms
, and
silence_duration_ms
. Here’s a quick example:
Copy code
from livekit.plugins.openai import realtime
from openai.types.beta.realtime.session import TurnDetection

session = AgentSession(
    llm=realtime.RealtimeModel(
        turn_detection=TurnDetection(
            type="server_vad",
            threshold=0.5,
            prefix_padding_ms=300,
            silence_duration_ms=500,
            create_response=True,
            interrupt_response=True,
        )
    )
)
1. Use LiveKit’s Custom Turn Detection Model: Another option is to turn off the RealtimeModel's built-in turn detection and use a separate streaming STT model instead. Like this:
Copy code
session = AgentSession(
    vad=livekit.turnDetector.EnglishModel(),
    stt=deepgram.STT(),
    llm=openai.realtime.RealtimeModel(
        turn_detection=None,
        input_audio_transcription=None,
    )
)
Just a heads up: sticking with the built-in turn detection usually saves you some costs. If you want me to walk you through anything else or give more examples, just let me know—I’m here to help! Sources: OpenAI Realtime API integration guide | LiveKit Docs | OpenAI Realtime API integration guide | LiveKit Docs | Gemini Live API integration guide | LiveKit Docs | agents/src/voice/agent_activity.ts | Turn detection and interruptions | LiveKit Docs