How can I exclude system messages from ChatContext...
# ask-ai
a
How can I exclude system messages from ChatContext in python?? and only keep user and assistant messages in it?
t
Thanks for reaching out, Hiren. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To exclude system messages from
ChatContext
in Python and keep only user and assistant messages, you can use the
copy()
method with the
exclude_instructions
parameter. Here's how you can do it:
Copy code
filtered_ctx = chat_ctx.copy(exclude_instructions=True)
Explanation: • The
copy()
method creates a new
ChatContext
instance. • Setting
exclude_instructions=True
removes messages with the role "system" or "developer". • The resulting
ChatContext
contains only "user" and "assistant" messages. Example:
Copy code
# Assume chat_ctx contains system, user, and assistant messages
filtered_ctx = chat_ctx.copy(exclude_instructions=True)
# filtered_ctx now contains only user and assistant messages
This method works across different LLM providers supported by the
ChatContext
class, including OpenAI, Anthropic, AWS, and Google.
a
okay this is good and pretty straight forward, can you suggest me how can I check what's the last message in chat_ctx, means is it empty or is there a last message of system or user.. OR what's the count of total messages in history (chatcontext)
t
Hey there! If you want to check out the last message in
chat_ctx
and see how many messages are in the history, here’s a simple way to do it: 1. Getting the Last Message:
Copy code
last_message = chat_ctx.items[-1] if chat_ctx.items else None
last_message
will grab the last item in the chat context, or give you
None
if there’s nothing there yet. 2. Counting All Messages:
Copy code
total_messages = sum(1 for item in chat_ctx.items if item.type == "message")
• This counts up all the items where the type is `"message"`—pretty handy! 3. Here’s a Quick Example:
Copy code
if chat_ctx.items:
    last_message = chat_ctx.items[-1]
    print(f"Last message: {last_message.text_content}")
else:
    print("No messages in the chat context")

total_messages = sum(1 for item in chat_ctx.items if item.type == "message")
print(f"Total messages: {total_messages}")
4. Counting Messages from the User:
Copy code
user_messages = sum(1 for item in chat_ctx.items 
                   if item.type == "message" and item.role == "user")
These little snippets should help you keep track of your chat history easily. If you want me to walk you through anything else or have questions, just give me a shout!
a
Can you give me an example code which I can execute when participant lefts.. I want to save the transcript once there's no participant is connected
t
Hey there! To make sure you save the transcript when a participant leaves and no one else is connected, you can use the
add_shutdown_callback
method right in your agent's entrypoint function. This way, a function will automatically kick in to save the conversation history as the session shuts down—pretty handy, right? Example Code:
Copy code
from datetime import datetime
import json
from livekit.agents import JobContext

def entrypoint(ctx: JobContext):
    # Add the shutdown callback before connecting to the room
    async def write_transcript():
        try:
            current_date = datetime.now().strftime("%Y%m%d_%H%M%S")
            filename = f"/tmp/transcript_{ctx.room.name}_{current_date}.json"

            with open(filename, 'w') as f:
                json.dump(session.history.to_dict(), f, indent=2)

            print(f"Transcript saved to {filename}")
        except Exception as e:
            print(f"Error saving transcript: {e}")

    ctx.add_shutdown_callback(write_transcript)

    # Rest of your entrypoint code follows...
Key Points:Shutdown Callback: Using
add_shutdown_callback
makes sure your transcript gets saved once the room closes and everyone has left. • File Location: The example saves to
/tmp/
, but feel free to change this path to wherever works best for you. Just double-check the folder exists and you have write permissions. • Error Handling: We've included try-except blocks to catch any hiccups during file saving, so you’ll be alerted if something goes wrong. • Filename: It adds a timestamp to the filename to keep things neat and avoid overwriting previous transcripts. This method helps keep your transcripts safe and sound when no one's connected anymore. If you want me to walk you through anything else or have questions, just give me a shout!