how can I send system messages to the LLM?
# ask-ai
c
how can I send system messages to the LLM?
t
Thanks for reaching out, Guillaume. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks for reaching out! Could you please help me understand a bit more about what you mean by "system messages" and "LLM" in this context? Are you talking about sending certain types of messages within a LiveKit room, or is this connected to another feature or integration? I'm here to help you through this!
c
using a livekit agent and using the python sdk for livekit agent, how could I send a system message to the LLM powering the agent
t
I'd love to help you send a system message to the LLM powering a LiveKit agent using the Python SDK. Here's a simple step-by-step guide to get you started: 1. Set an Initial System Message During Initialization:
Copy code
from livekit.agents import llm

initial_ctx = llm.ChatContext().append(
    role="system",
    text="You are a helpful assistant."
)
You can pass this
initial_ctx
to the agent when you create it. This sets the tone right from the start! 2. Update the System Message Dynamically:
Copy code
from livekit.agents import llm

def update_instructions(chat_ctx: llm.ChatContext, instructions: str):
    idx = chat_ctx.index_by_id(INSTRUCTIONS_MESSAGE_ID)
    if idx is not None:
        chat_ctx.items[idx] = llm.ChatMessage(
            id=INSTRUCTIONS_MESSAGE_ID,
            role="system",
            content=[instructions],
            created_at=chat_ctx.items[idx].created_at,
        )
    else:
        chat_ctx.items.insert(0, llm.ChatMessage(
            id=INSTRUCTIONS_MESSAGE_ID,
            role="system",
            content=[instructions]
        ))
This way, you can easily tweak the system message while your app is running. Pretty handy, right? 3. Insert System Messages for Specific Use Cases: For instance, if you're working on content moderation:
Copy code
moderation_ctx = llm.ChatContext([
    llm.ChatMessage(
        role="system",
        content=["You are a content moderator. Respond ONLY with 'APPROPRIATE' or 'INAPPROPRIATE'."]
    ),
    llm.ChatMessage(role="user", content=[f"Evaluate: {text}"])
])
This helps guide the LLM to respond exactly how you want. If you have any questions or want me to walk you through any part of this, just let me know—I'm here to help! Sources: Baseten LLM integration guide | LiveKit Docs | VoicePipelineAgent | LiveKit Docs | pipeline-llm/llm_powered_content_filter.py | examples/voice_agents/llamaindex-rag/retrieval.py | agent_extensions/agent_extensions/rag/handler.py
c
how would I access the ChatContext of the agent while it’s running?
t
To help you access the
ChatContext
of a LiveKit agent while it’s running, here are some friendly methods you can try: 1. Access the Full Conversation History: You can use the
session.history
property to get the entire conversation history like this:
Copy code
# Python Example
chat_ctx = agent_session.history
This gives you the global
ChatContext
instance, which updates in real-time as new conversation items come in. 2. Read-Only Access to Current Context: If you want to peek at the current chat context without changing it, try the
agent.chat_ctx
property:
Copy code
# Python Example
current_ctx = agent.chat_ctx
This is especially handy when you’re working within agent lifecycle methods like
on_user_turn_completed
. 3. Accessing from Within an Agent: When you’re writing your own custom logic, you can access the session directly, like so:
Copy code
# Python Example
async def on_user_turn_completed(self, turn_ctx, new_message):
    full_history = self.session.history
    # Use history for decision making
If you’d like me to walk you through any of these steps or provide more examples, just let me know—I’m here to help! Sources: Workflows | LiveKit Docs | agents/src/llm/remote_chat_context.test.ts | tests/test_agent_session.py