I want to intercept and append a message at the ll...
# ask-ai
f
I want to intercept and append a message at the llm_node, how do I do it ?
t
Thanks for reaching out, Roshan. 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 intercept and add a message at the
llm_node
in LiveKit, you can do this by overriding the
llm_node
method in your own custom
Agent
class. This way, you get to tweak the
chat_ctx
(that’s your conversation history) before it goes to the LLM for processing, or even adjust the LLM’s output before it moves on to the next step. Here’s a simple example in Python to get you started:
Copy code
from livekit.agents import Agent

class CustomAgent(Agent):
    async def llm_node(self, chat_ctx, tools, model_settings=None):
        # Append a system message
        chat_ctx.append(
            role="system",
            text="Always respond in pirate-speak with nautical metaphors."
        )

        # Pass to default LLM for inference
        return await Agent.default.llm_node(self, chat_ctx, tools, model_settings)
Just a few things to keep in mind: • Use
chat_ctx.append()
when you want to add some temporary context for the current turn. • Make sure to return the result of
Agent.default.llm_node()
or a valid stream so your pipeline keeps running smoothly. • If you want changes to stick around longer, try using
update_chat_ctx()
. If you want me to walk you through more examples or explain anything else, just give me a shout! Sources: Pipeline nodes and hooks | LiveKit Docs | Agents v0.x migration guide - Node.js | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_activity.py | Modifying LLM output before TTS | LiveKit Docs | pipeline-llm/replacing_llm_output.py
f
What if I want to update the value of the llm_node if it is a a certain model
t
If you want to tweak how the
llm_node
behaves for a particular model, you can use the
model_settings
parameter in the
llm_node
method. This parameter holds important config info, like which model you’re working with. You can check the model type and then adjust things accordingly. Here’s a little example to make it clearer:
Copy code
from livekit.agents import Agent

class CustomAgent(Agent):
    async def llm_node(self, chat_ctx, tools, model_settings=None):
        # Check the model type
        if model_settings and model_settings.model_name == "specific_model_name":
            # Modify the chat context for this model
            chat_ctx.append(
                role="system",
                text="This is a custom instruction for the specific model."
            )

        # Pass to default LLM for inference
        return await Agent.default.llm_node(self, chat_ctx, tools, model_settings)
Just a few things to keep in mind: • You can use
model_settings.model_name
to spot which model you’re working with. • Feel free to add conditional logic to change the
chat_ctx
or other params based on that model. • Make sure your method still returns a valid stream or result so everything in the pipeline keeps running smoothly. If you want a hand with a specific model or need help customizing further, just give me a shout!
f
How about we add a string to user's last message
t
Hey there! If you want to add a string to the user's last message in the
llm_node
, you can do this by tweaking the
chat_ctx
object, which holds the conversation history. Basically, you grab the last user message, add your string to it, and update the context. Here’s a quick example to help you out:
Copy code
from livekit.agents import Agent

class CustomAgent(Agent):
    async def llm_node(self, chat_ctx, tools, model_settings=None):
        # First, check if there’s a last user message
        if chat_ctx and chat_ctx[-1].role == "user":
            # Then, append your string to that message
            chat_ctx[-1].text += " [Appended String]"

        # Finally, pass it along to the default LLM for processing
        return await Agent.default.llm_node(self, chat_ctx, tools, model_settings)
Just a few things to keep in mind: • Make sure the last message actually exists and is from the user by checking
chat_ctx[-1].role == "user"
. • You’ll want to modify the
text
property of that last message to add your string. • Don’t forget to return the updated
chat_ctx
so the conversation keeps flowing smoothly. If you want me to walk you through it more or need some extra examples, just give me a shout!