jolly-eve-3916
08/01/2025, 8:29 PMtall-airline-16756
08/01/2025, 8:29 PMtall-airline-16756
08/01/2025, 8:30 PMAgentSession without affecting other sessions, follow these steps:
1. Create a ChatContext: Initialize a ChatContext object and populate it with the desired conversation history using the add_message method. Specify the role (e.g., "user" or "assistant") and content for each message.
2. Pass ChatContext to AgentSession: When starting an AgentSession, pass the ChatContext to the Agent instance. This ensures the session-specific context is used without affecting other sessions.
3. Use the update_chat_ctx Method: If you need to modify the context during a session, use the update_chat_ctx method to ensure changes are session-specific.
Here’s an example:
from livekit.agents import ChatContext, Agent, AgentSession
# Create ChatContext and add messages
chat_ctx = ChatContext()
chat_ctx.add_message(role="user", content="Hello!")
chat_ctx.add_message(role="assistant", content="How can I help you today?")
# Create Agent with ChatContext
agent = Agent(instructions="You are a helpful assistant.", chat_ctx=chat_ctx)
# Start AgentSession
session = AgentSession()
await session.start(agent=agent, room=...)
By following this approach, each user will have their own conversation history, ensuring no data leaks between sessions. Let me know if you need further clarification or assistance!
Sources: OpenAI Realtime API integration guide | LiveKit Docs | Workflows | LiveKit Docs | Testing and evaluation | LiveKit Docs | livekit-agents/livekit/agents/voice/agent.py