ElevenLabs
Create voice-based conversational AI agents with memory capabilities by integrating ElevenLabs and Mem0. This integration enables persistent, context-aware voice interactions that remember past conversations.
Overview
In this guide, we’ll build a voice agent that:
- Uses ElevenLabs Conversational AI for voice interaction
- Leverages Mem0 to store and retrieve memories from past conversations
- Provides personalized responses based on user history
Setup and Configuration
Install necessary libraries:
Configure your environment variables:
Integration Code Breakdown
Let’s break down the implementation into manageable parts:
1. Imports and Environment Setup
First, we import required libraries and set up the environment:
These imports provide:
- Standard Python libraries for system operations and signal handling
AsyncMemoryClient
from Mem0 for memory operations- ElevenLabs components for voice interaction
2. Environment Variables and Validation
Next, we validate the required environment variables:
This section:
- Retrieves required environment variables
- Performs validation to ensure required variables are present
- Exits the application with an error message if required variables are missing
- Sets the Mem0 API key in the environment for the Mem0 client to use
3. Client Initialization
Initialize both the ElevenLabs and Mem0 clients:
Here we:
- Create an ElevenLabs client with the API key
- Initialize a ClientTools object for registering function tools
- Create an AsyncMemoryClient instance for Mem0 interactions
4. Memory Function Definitions
Define the two key memory functions that will be registered as tools:
These functions:
add_memories
:
- Takes a message parameter containing information to remember
- Stores the message in Mem0 using the
add
method - Associates the memory with the specific USER_ID
- Returns a success message to the agent
retrieve_memories
:
- Takes a message parameter as the search query
- Sets up filters to only retrieve memories for the current user
- Uses semantic search to find relevant memories
- Joins all retrieved memories into a single text
- Prints retrieved memories to the console for debugging
- Returns the memories or a “No memories found” message if none are found
5. Registering Memory Functions as Tools
Register the memory functions with the ElevenLabs ClientTools system:
This allows the ElevenLabs agent to:
- Access these functions through function calling
- Wait for asynchronous results (is_async=True)
- Call these functions by name (“addMemories” and “retrieveMemories”)
6. Conversation Setup
Configure the conversation with ElevenLabs:
This sets up the conversation with:
- The ElevenLabs client and Agent ID
- Authentication requirements based on API key presence
- DefaultAudioInterface for handling audio I/O
- The client_tools with our memory functions
- Callback functions for:
- Displaying agent responses
- Showing corrected responses (when the agent self-corrects)
- Displaying user transcripts for debugging
- (Commented out) Latency measurements
7. Conversation Management
Start and manage the conversation:
This final section:
- Prints a message indicating the conversation has started
- Starts the conversation session
- Sets up a signal handler to gracefully end the session on Ctrl+C
- Waits for the session to end and gets the conversation ID
- Prints the conversation ID for reference
Memory Tools Overview
This integration provides two key memory functions to your conversational AI agent:
1. Adding Memories (addMemories
)
The addMemories
tool allows your agent to store important information during a conversation, including:
- User preferences
- Important facts shared by the user
- Decisions or commitments made during the conversation
- Action items to follow up on
When the agent identifies information worth remembering, it calls this function to store it in the Mem0 database with the appropriate user ID.
How it works:
- The agent identifies information that should be remembered
- It formats the information as a message string
- It calls the
addMemories
function with this message - The function stores the memory in Mem0 linked to the user’s ID
- Later conversations can retrieve this memory
Example usage in agent prompt:
2. Retrieving Memories (retrieveMemories
)
The retrieveMemories
tool allows your agent to search for and retrieve relevant memories from previous conversations. The agent can:
- Search for context related to the current topic
- Recall user preferences
- Remember previous interactions on similar topics
- Create continuity across multiple sessions
How it works:
- The agent needs context for the current conversation
- It calls
retrieveMemories
with the current conversation topic or question - The function performs a semantic search in Mem0
- Relevant memories are returned to the agent
- The agent incorporates these memories into its response
Example usage in agent prompt:
Configuring Your ElevenLabs Agent
To enable your agent to effectively use memory:
-
Add function calling capabilities to your agent in the ElevenLabs platform:
- Go to your agent settings in the ElevenLabs platform
- Navigate to the “Tools” section
- Enable function calling for your agent
- Add the memory tools as described below
-
Add the
addMemories
andretrieveMemories
tools to your agent with these specifications:
For addMemories
:
For retrieveMemories
:
- Update your agent’s prompt to instruct it to use these memory functions. For example:
Example Conversation Flow
Here’s how a typical conversation with memory might flow:
-
User speaks: “Hi, do you remember my favorite color?”
-
Agent retrieves memories:
-
Agent processes with context:
- If memories found: Prepares a personalized response
- If no memories: Prepares to ask and store the information
-
Agent responds:
- With memory: “Yes, your favorite color is blue!”
- Without memory: “I don’t think you’ve told me your favorite color before. What is it?”
-
User responds: “It’s actually green.”
-
Agent stores new information:
-
Agent confirms: “Thanks, I’ll remember that your favorite color is green.”
Example Use Cases
-
Personal Assistant - Remember user preferences, past requests, and important dates
-
Customer Support - Recall previous issues a customer has had
-
Educational AI - Track student progress and tailor teaching accordingly
-
Healthcare Assistant - Remember symptoms, medications, and health concerns
Troubleshooting
- Missing API Keys:
- Error: “API_KEY environment variable must be set”
- Solution: Ensure all environment variables are set correctly in your .env file or system environment
- Connection Issues:
- Error: “Failed to connect to API”
- Solution: Check your network connection and API key permissions. Verify the API keys are valid and have the necessary permissions.
- Empty Memory Results:
- Symptom: Agent always responds with “No memories found”
- Solution: This is normal for new users. The memory database builds up over time as conversations occur. It’s also possible your query isn’t semantically similar to stored memories - try different phrasing.
- Agent Not Using Memories:
- Symptom: The agent retrieves memories but doesn’t incorporate them in responses
- Solution: Update the agent’s prompt to explicitly instruct it to use the retrieved memories in its responses
Conclusion
By integrating ElevenLabs Conversational AI with Mem0, you can create voice agents that maintain context across conversations and provide personalized responses based on user history. This powerful combination enables:
- More natural, context-aware conversations
- Personalized user experiences that improve over time
- Reduced need for users to repeat information
- Long-term relationship building between users and AI agents
Help
- For more details on ElevenLabs, visit the ElevenLabs Conversational AI Documentation
- For Mem0 documentation, refer to the Mem0 Platform
- If you need further assistance, please feel free to reach out to us through the following methods: