Livekit
This guide demonstrates how to create a memory-enabled voice assistant using LiveKit, Deepgram, OpenAI, and Mem0, focusing on creating an intelligent, context-aware travel planning agent.
Prerequisites
Before you begin, make sure you have:
- Installed Livekit Agents SDK with voice dependencies of silero and deepgram:
- Installed Mem0 SDK:
- Set up your API keys in a
.env
file:
Note: Make sure to have a Livekit and Deepgram account. You can find these variables
LIVEKIT_URL
,LIVEKIT_API_KEY
andLIVEKIT_API_SECRET
from LiveKit Cloud Console and for more information you can refer this website LiveKit Documentation. ForDEEPGRAM_API_KEY
you can get from Deepgram Console refer this website Deepgram Documentation for more details.
Code Breakdown
Let’s break down the key components of this implementation:
1. Setting Up Dependencies and Environment
This section handles:
- Importing required modules
- Loading environment variables
- Setting up logging
- Extracting user identification
- Initializing the Mem0 client
2. Memory Enrichment Function
This function:
- Stores user messages in Mem0
- Performs semantic search for relevant memories
- Augments the chat context with retrieved memories
- Enables contextually aware responses
3. Prewarm and Entrypoint Functions
The entrypoint function:
- Connects to LiveKit room
- Initializes Mem0 memory client
- Sets up initial system context
- Creates a VoicePipelineAgent with memory enrichment
- Starts the agent with an initial greeting
Create a Memory-Enabled Voice Agent
Now that we’ve explained each component, here’s the complete implementation that combines OpenAI Agents SDK for voice with Mem0’s memory capabilities:
Key Features of This Implementation
- Semantic Memory Retrieval: Uses Mem0 to store and retrieve contextually relevant memories
- Voice Interaction: Leverages LiveKit for voice communication
- Intelligent Context Management: Augments conversations with past interactions
- Travel Planning Specialization: Focused on creating a helpful travel guide assistant
Running the Example
To run this example:
- Install all required dependencies
- Set up your
.env
file with the necessary API keys - Ensure your microphone and audio setup are configured
- Run the script with Python 3.11 or newer and with the following command:
- After the script starts, you can interact with the voice agent using Livekit’s Agent Platform and Connect to the agent inorder to start conversations.
Best Practices for Voice Agents with Memory
- Context Preservation: Store enough context with each memory for effective retrieval
- Privacy Considerations: Implement secure memory management
- Relevant Memory Filtering: Use semantic search to retrieve only the most pertinent memories
- Error Handling: Implement robust error handling for memory operations
Debugging Function Tools
- To run the script in debug mode simply start the assistant with
dev
mode:
- When working with memory-enabled voice agents, use Python’s
logging
module for effective debugging: