abundant-father-33863
08/04/2025, 10:15 AMtall-airline-16756
08/04/2025, 10:15 AMtall-airline-16756
08/04/2025, 10:16 AMChatContext 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:
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:
# 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.abundant-father-33863
08/04/2025, 10:21 AMtall-airline-16756
08/04/2025, 10:21 AMchat_ctx and see how many messages are in the history, here’s a simple way to do it:
1. Getting the Last Message:
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:
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:
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:
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!abundant-father-33863
08/04/2025, 1:34 PMtall-airline-16756
08/04/2025, 1:34 PMadd_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:
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!