Getting Started
Core Features
How-To Guides
Configuration
Troubleshooting
Integrations
When interacting with an AI assistant, it's crucial for the AI to remember past conversations, preferences, and important details to provide a personalized and consistent experience. The "Search Memories" feature allows your AI to retrieve relevant information from its long-term memory based on the current conversation or user query.
This capability ensures that the AI doesn't "forget" previous interactions, enabling it to adapt to individual user needs, recall specific facts, or continue a conversation with full context. By searching memories, your AI can access stored information about a user's history, preferences, or specific topics discussed, making its responses much more accurate and helpful.
Before you can search for memories, you need to initialize the Memory object. This object manages all memory operations.
from mem0 import Memory
# Initialize the memory object
memory = Memory()To find memories, you'll use the search method of your Memory object. You need to provide a query and a . Optionally, you can specify a to control how many memories are returned.
Here's a complete example demonstrating how to search for memories and use them to enhance an AI's response in a simple chatbot.
from openai import OpenAI
from mem0 import Memory
# Initialize OpenAI client and Memory
openai_client = OpenAI()
In this example, the chat_with_memories function first searches for memories related to the user_input. It then uses these relevant_memories to construct a system_prompt for the AI model, ensuring the AI has the necessary context to provide a well-informed answer. Finally, the conversation turn is added back to memory, continuously enriching the AI's knowledge base.
user_idlimit# Define your search parameters
user_query = "What did we talk about regarding my favorite color?"
user_identifier = "default_user" # Or a specific user ID for your application
number_of_results = 3
# Perform the search
relevant_memories = memory.search(
query=user_query,
user_id=user_identifier,
limit=number_of_results
)query: This is the text or question you want to use to find relevant memories. It could be the user's current input, a summary of the conversation, or a specific topic.user_id: This identifier ensures that the search is performed within the context of a specific user's memories. It helps in retrieving personalized information.limit (Optional): This specifies the maximum number of relevant memories to retrieve. If not provided, a default number of memories will be returned.The search method returns a dictionary containing the results. The actual memory entries are typically found under the results key, where each entry is a dictionary that includes the memory content.
# Check if any memories were found
if relevant_memories and relevant_memories.get("results"):
print(f"Found {len(relevant_memories['results'])} relevant memories:")
for entry in relevant_memories["results"]:
print(f"- {entry['memory']}")
else:
print("No relevant memories found.")
# Example of how to format memories for an AI prompt
memories_str = "\n".join(f"- {entry['memory']}" for entry in relevant_memories["results"])
print("\nFormatted memories for AI prompt:")
print(memories_str)Once you retrieve relevant memories, you can incorporate them directly into your AI's system prompt or user messages. This provides the AI with the necessary context to generate informed and personalized responses, as shown in the example below.