# Configurations Source: https://docs.mem0.ai/components/embedders/config Config in mem0 is a dictionary that specifies the settings for your embedding models. It allows you to customize the behavior and connection details of your chosen embedder. ## How to define configurations? The config is defined as an object (or dictionary) with two main keys: * `embedder`: Specifies the embedder provider and its configuration * `provider`: The name of the embedder (e.g., "openai", "ollama") * `config`: A nested object or dictionary containing provider-specific settings ## How to use configurations? Here's a general example of how to use the config with mem0: ```python Python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "sk-xx" config = { "embedder": { "provider": "your_chosen_provider", "config": { # Provider-specific settings go here } } } m = Memory.from_config(config) m.add("Your text here", user_id="user", metadata={"category": "example"}) ``` ```typescript TypeScript theme={null} import { Memory } from 'mem0ai/oss'; const config = { embedder: { provider: 'openai', config: { apiKey: process.env.OPENAI_API_KEY || '', model: 'text-embedding-3-small', // Provider-specific settings go here }, }, }; const memory = new Memory(config); await memory.add("Your text here", { userId: "user", metadata: { category: "example" } }); ``` ## Why is Config Needed? Config is essential for: 1. Specifying which embedding model to use. 2. Providing necessary connection details (e.g., model, api\_key, embedding\_dims). 3. Ensuring proper initialization and connection to your chosen embedder. ## Master List of All Params in Config Here's a comprehensive list of all parameters that can be used across different embedders: | Parameter | Description | Provider | | ------------------------------ | ----------------------------------------------------------- | ------------ | | `model` | Embedding model to use | All | | `api_key` | API key of the provider | All | | `embedding_dims` | Dimensions of the embedding model | All | | `http_client_proxies` | Allow proxy server settings | All | | `ollama_base_url` | Base URL for the Ollama embedding model | Ollama | | `model_kwargs` | Key-Value arguments for the Huggingface embedding model | Huggingface | | `azure_kwargs` | Key-Value arguments for the AzureOpenAI embedding model | Azure OpenAI | | `openai_base_url` | Base URL for OpenAI API | OpenAI | | `vertex_credentials_json` | Path to the Google Cloud credentials JSON file for VertexAI | VertexAI | | `memory_add_embedding_type` | The type of embedding to use for the add memory action | VertexAI | | `memory_update_embedding_type` | The type of embedding to use for the update memory action | VertexAI | | `memory_search_embedding_type` | The type of embedding to use for the search memory action | VertexAI | | `lmstudio_base_url` | Base URL for LM Studio API | LM Studio | | Parameter | Description | Provider | | --------------- | --------------------------------- | -------- | | `model` | Embedding model to use | All | | `apiKey` | API key of the provider | All | | `embeddingDims` | Dimensions of the embedding model | All | ## Supported Embedding Models For detailed information on configuring specific embedders, please visit the [Embedding Models](./models) section. There you'll find information for each supported embedder with provider-specific usage examples and configuration details. # Azure OpenAI Source: https://docs.mem0.ai/components/embedders/models/azure_openai To use Azure OpenAI embedding models, set the `EMBEDDING_AZURE_OPENAI_API_KEY`, `EMBEDDING_AZURE_DEPLOYMENT`, `EMBEDDING_AZURE_ENDPOINT` and `EMBEDDING_AZURE_API_VERSION` environment variables. You can obtain the Azure OpenAI API key from the Azure. ### Usage ```python Python theme={null} import os from mem0 import Memory os.environ["EMBEDDING_AZURE_OPENAI_API_KEY"] = "your-api-key" os.environ["EMBEDDING_AZURE_DEPLOYMENT"] = "your-deployment-name" os.environ["EMBEDDING_AZURE_ENDPOINT"] = "your-api-base-url" os.environ["EMBEDDING_AZURE_API_VERSION"] = "version-to-use" os.environ["OPENAI_API_KEY"] = "your_api_key" # For LLM config = { "embedder": { "provider": "azure_openai", "config": { "model": "text-embedding-3-large", "azure_kwargs": { "api_version": "", "azure_deployment": "", "azure_endpoint": "", "api_key": "", "default_headers": { "CustomHeader": "your-custom-header", } } } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="john") ``` ```typescript TypeScript theme={null} import { Memory } from 'mem0ai/oss'; const config = { embedder: { provider: "azure_openai", config: { model: "text-embedding-3-large", modelProperties: { endpoint: "your-api-base-url", deployment: "your-deployment-name", apiVersion: "version-to-use", } } } } const memory = new Memory(config); const messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] await memory.add(messages, { userId: "john" }); ``` As an alternative to using an API key, the Azure Identity credential chain can be used to authenticate with [Azure OpenAI role-based security](https://learn.microsoft.com/en-us/azure/ai-foundry/openai/how-to/role-based-access-control). If an API key is provided, it will be used for authentication over an Azure Identity Below is a sample configuration for using Mem0 with Azure OpenAI and Azure Identity: ```python theme={null} import os from mem0 import Memory # You can set the values directly in the config dictionary or use environment variables os.environ["LLM_AZURE_DEPLOYMENT"] = "your-deployment-name" os.environ["LLM_AZURE_ENDPOINT"] = "your-api-base-url" os.environ["LLM_AZURE_API_VERSION"] = "version-to-use" config = { "llm": { "provider": "azure_openai_structured", "config": { "model": "your-deployment-name", "temperature": 0.1, "max_tokens": 2000, "azure_kwargs": { "azure_deployment": "", "api_version": "", "azure_endpoint": "", "default_headers": { "CustomHeader": "your-custom-header", } } } } } ``` Refer to [Azure Identity troubleshooting tips](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/identity/azure-identity/TROUBLESHOOTING.md#troubleshoot-environmentcredential-authentication-issues) for setting up an Azure Identity credential. ### Config Here are the parameters available for configuring Azure OpenAI embedder: | Parameter | Description | Default Value | | ---------------- | -------------------------------------- | ------------------------ | | `model` | The name of the embedding model to use | `text-embedding-3-small` | | `embedding_dims` | Dimensions of the embedding model | `1536` | | `azure_kwargs` | The Azure OpenAI configs | `config_keys` | | Parameter | Description | Default Value | | ----------------- | --------------------------------------------- | ---------------------------- | | `model` | The name of the embedding model to use | `text-embedding-3-small` | | `embeddingDims` | Dimensions of the embedding model | `1536` | | `apiKey` | Azure OpenAI API key | `None` | | `modelProperties` | Object containing endpoint and other settings | `{ endpoint: "",...rest }` | # Hugging Face Source: https://docs.mem0.ai/components/embedders/models/huggingface You can use embedding models from Huggingface to run Mem0 locally. ### Usage ```python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "your_api_key" # For LLM config = { "embedder": { "provider": "huggingface", "config": { "model": "multi-qa-MiniLM-L6-cos-v1" } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="john") ``` ### Using Text Embeddings Inference (TEI) You can also use Hugging Face's Text Embeddings Inference service for faster and more efficient embeddings: ```python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "your_api_key" # For LLM # Using HuggingFace Text Embeddings Inference API config = { "embedder": { "provider": "huggingface", "config": { "huggingface_base_url": "http://localhost:3000/v1" } } } m = Memory.from_config(config) m.add("This text will be embedded using the TEI service.", user_id="john") ``` To run the TEI service, you can use Docker: ```bash theme={null} docker run -d -p 3000:80 -v huggingfacetei:/data --platform linux/amd64 \ ghcr.io/huggingface/text-embeddings-inference:cpu-1.6 \ --model-id BAAI/bge-small-en-v1.5 ``` ### Config Here are the parameters available for configuring Huggingface embedder: | Parameter | Description | Default Value | | ---------------------- | ----------------------------------------------------- | --------------------------- | | `model` | The name of the model to use | `multi-qa-MiniLM-L6-cos-v1` | | `embedding_dims` | Dimensions of the embedding model | `selected_model_dimensions` | | `model_kwargs` | Additional arguments for the model | `None` | | `huggingface_base_url` | URL to connect to Text Embeddings Inference (TEI) API | `None` | # null Source: https://docs.mem0.ai/components/embedders/models/ollama You can use embedding models from Ollama to run Mem0 locally. ### Usage ```python Python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "your_api_key" # For LLM config = { "embedder": { "provider": "ollama", "config": { "model": "mxbai-embed-large" } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="john") ``` ```typescript TypeScript theme={null} import { Memory } from 'mem0ai/oss'; const config = { embedder: { provider: 'ollama', config: { model: 'nomic-embed-text:latest', // or any other Ollama embedding model url: 'http://localhost:11434', // Ollama server URL }, }, }; const memory = new Memory(config); const messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] await memory.add(messages, { userId: "john" }); ``` ### Config Here are the parameters available for configuring Ollama embedder: | Parameter | Description | Default Value | | ----------------- | ----------------------------------- | ------------------ | | `model` | The name of the Ollama model to use | `nomic-embed-text` | | `embedding_dims` | Dimensions of the embedding model | `512` | | `ollama_base_url` | Base URL for ollama connection | `None` | | Parameter | Description | Default Value | | --------------- | ----------------------------------- | ------------------------- | | `model` | The name of the Ollama model to use | `nomic-embed-text:latest` | | `url` | Base URL for Ollama server | `http://localhost:11434` | | `embeddingDims` | Dimensions of the embedding model | 768 | # OpenAI Source: https://docs.mem0.ai/components/embedders/models/openai To use OpenAI embedding models, set the `OPENAI_API_KEY` environment variable. You can obtain the OpenAI API key from the [OpenAI Platform](https://platform.openai.com/account/api-keys). ### Usage ```python Python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "your_api_key" config = { "embedder": { "provider": "openai", "config": { "model": "text-embedding-3-large" } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="john") ``` ```typescript TypeScript theme={null} import { Memory } from 'mem0ai/oss'; const config = { embedder: { provider: 'openai', config: { apiKey: 'your-openai-api-key', model: 'text-embedding-3-large', }, }, }; const memory = new Memory(config); await memory.add("I'm visiting Paris", { userId: "john" }); ``` ### Config Here are the parameters available for configuring OpenAI embedder: | Parameter | Description | Default Value | | ---------------- | -------------------------------------- | ------------------------ | | `model` | The name of the embedding model to use | `text-embedding-3-small` | | `embedding_dims` | Dimensions of the embedding model | `1536` | | `api_key` | The OpenAI API key | `None` | | Parameter | Description | Default Value | | --------------- | -------------------------------------- | ------------------------ | | `model` | The name of the embedding model to use | `text-embedding-3-small` | | `embeddingDims` | Dimensions of the embedding model | `1536` | | `apiKey` | The OpenAI API key | `None` | # null Source: https://docs.mem0.ai/components/embedders/models/vertexai ### Vertex AI To use Google Cloud's Vertex AI for text embedding models, set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to point to the path of your service account's credentials JSON file. These credentials can be created in the [Google Cloud Console](https://console.cloud.google.com/). ### Usage ```python theme={null} import os from mem0 import Memory # Set the path to your Google Cloud credentials JSON file os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/path/to/your/credentials.json" os.environ["OPENAI_API_KEY"] = "your_api_key" # For LLM config = { "embedder": { "provider": "vertexai", "config": { "model": "text-embedding-004", "memory_add_embedding_type": "RETRIEVAL_DOCUMENT", "memory_update_embedding_type": "RETRIEVAL_DOCUMENT", "memory_search_embedding_type": "RETRIEVAL_QUERY" } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="john") ``` The embedding types can be one of the following: * SEMANTIC\_SIMILARITY * CLASSIFICATION * CLUSTERING * RETRIEVAL\_DOCUMENT, RETRIEVAL\_QUERY, QUESTION\_ANSWERING, FACT\_VERIFICATION * CODE\_RETRIEVAL\_QUERY\ Check out the [Vertex AI documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/embeddings/task-types#supported_task_types) for more information. ### Config Here are the parameters available for configuring the Vertex AI embedder: | Parameter | Description | Default Value | | ------------------------------ | --------------------------------------------------------- | -------------------- | | `model` | The name of the Vertex AI embedding model to use | `text-embedding-004` | | `vertex_credentials_json` | Path to the Google Cloud credentials JSON file | `None` | | `embedding_dims` | Dimensions of the embedding model | `256` | | `memory_add_embedding_type` | The type of embedding to use for the add memory action | `RETRIEVAL_DOCUMENT` | | `memory_update_embedding_type` | The type of embedding to use for the update memory action | `RETRIEVAL_DOCUMENT` | | `memory_search_embedding_type` | The type of embedding to use for the search memory action | `RETRIEVAL_QUERY` | # Overview Source: https://docs.mem0.ai/components/embedders/overview Mem0 offers support for various embedding models, allowing users to choose the one that best suits their needs. ## Supported Embedders See the list of supported embedders below. The following embedders are supported in the Python implementation. The TypeScript implementation currently only supports OpenAI. ## Usage To utilize an embedding model, you must provide a configuration to customize its usage. If no configuration is supplied, a default configuration will be applied, and `OpenAI` will be used as the embedding model. For a comprehensive list of available parameters for embedding model configuration, please refer to [Config](./config). # Configurations Source: https://docs.mem0.ai/components/llms/config ## How to define configurations? The `config` is defined as a Python dictionary with two main keys: * `llm`: Specifies the llm provider and its configuration * `provider`: The name of the llm (e.g., "openai", "groq") * `config`: A nested dictionary containing provider-specific settings The `config` is defined as a TypeScript object with these keys: * `llm`: Specifies the LLM provider and its configuration (required) * `provider`: The name of the LLM (e.g., "openai", "groq") * `config`: A nested object containing provider-specific settings * `embedder`: Specifies the embedder provider and its configuration (optional) * `vectorStore`: Specifies the vector store provider and its configuration (optional) * `historyDbPath`: Path to the history database file (optional) ### Config Values Precedence Config values are applied in the following order of precedence (from highest to lowest): 1. Values explicitly set in the `config` object/dictionary 2. Environment variables (e.g., `OPENAI_API_KEY`, `OPENAI_BASE_URL`) 3. Default values defined in the LLM implementation This means that values specified in the `config` will override corresponding environment variables, which in turn override default values. ## How to Use Config Here's a general example of how to use the config with Mem0: ```python Python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "sk-xx" # for embedder config = { "llm": { "provider": "your_chosen_provider", "config": { # Provider-specific settings go here } } } m = Memory.from_config(config) m.add("Your text here", user_id="user", metadata={"category": "example"}) ``` ```typescript TypeScript theme={null} import { Memory } from 'mem0ai/oss'; // Minimal configuration with just the LLM settings const config = { llm: { provider: 'your_chosen_provider', config: { // Provider-specific settings go here } } }; const memory = new Memory(config); await memory.add("Your text here", { userId: "user123", metadata: { category: "example" } }); ``` ## Why is Config Needed? Config is essential for: 1. Specifying which LLM to use. 2. Providing necessary connection details (e.g., model, api\_key, temperature). 3. Ensuring proper initialization and connection to your chosen LLM. ## Master List of All Params in Config Here's a comprehensive list of all parameters that can be used across different LLMs: | Parameter | Description | Provider | | --------------------- | -------------------------------------------- | ----------- | | `model` | Embedding model to use | All | | `temperature` | Temperature of the model | All | | `api_key` | API key to use | All | | `max_tokens` | Tokens to generate | All | | `top_p` | Probability threshold for nucleus sampling | All | | `top_k` | Number of highest probability tokens to keep | All | | `http_client_proxies` | Allow proxy server settings | AzureOpenAI | | `models` | List of models | Openrouter | | `route` | Routing strategy | Openrouter | | `openrouter_base_url` | Base URL for Openrouter API | Openrouter | | `site_url` | Site URL | Openrouter | | `app_name` | Application name | Openrouter | | `ollama_base_url` | Base URL for Ollama API | Ollama | | `openai_base_url` | Base URL for OpenAI API | OpenAI | | `azure_kwargs` | Azure LLM args for initialization | AzureOpenAI | | `deepseek_base_url` | Base URL for DeepSeek API | DeepSeek | | `xai_base_url` | Base URL for XAI API | XAI | | `sarvam_base_url` | Base URL for Sarvam API | Sarvam | | `reasoning_effort` | Reasoning level (low, medium, high) | Sarvam | | `frequency_penalty` | Penalize frequent tokens (-2.0 to 2.0) | Sarvam | | `presence_penalty` | Penalize existing tokens (-2.0 to 2.0) | Sarvam | | `seed` | Seed for deterministic sampling | Sarvam | | `stop` | Stop sequences (max 4) | Sarvam | | `lmstudio_base_url` | Base URL for LM Studio API | LM Studio | | `response_callback` | LLM response callback function | OpenAI | | Parameter | Description | Provider | | --------------- | -------------------------------------------- | -------- | | `model` | Embedding model to use | All | | `temperature` | Temperature of the model | All | | `apiKey` | API key to use | All | | `maxTokens` | Tokens to generate | All | | `topP` | Probability threshold for nucleus sampling | All | | `topK` | Number of highest probability tokens to keep | All | | `openaiBaseUrl` | Base URL for OpenAI API | OpenAI | ## Supported LLMs For detailed information on configuring specific LLMs, please visit the [LLMs](./models) section. There you'll find information for each supported LLM with provider-specific usage examples and configuration details. # Anthropic Source: https://docs.mem0.ai/components/llms/models/anthropic To use Anthropic's models, please set the `ANTHROPIC_API_KEY` which you find on their [Account Settings Page](https://console.anthropic.com/account/keys). ## Usage ```python Python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "your-api-key" # used for embedding model os.environ["ANTHROPIC_API_KEY"] = "your-api-key" config = { "llm": { "provider": "anthropic", "config": { "model": "claude-sonnet-4-20250514", "temperature": 0.1, "max_tokens": 2000, } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` ```typescript TypeScript theme={null} import { Memory } from 'mem0ai/oss'; const config = { llm: { provider: 'anthropic', config: { apiKey: process.env.ANTHROPIC_API_KEY || '', model: 'claude-sonnet-4-20250514', temperature: 0.1, maxTokens: 2000, }, }, }; const memory = new Memory(config); const messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] await memory.add(messages, { userId: "alice", metadata: { category: "movies" } }); ``` ## Config All available parameters for the `anthropic` config are present in [Master List of All Params in Config](../config). # AWS Bedrock Source: https://docs.mem0.ai/components/llms/models/aws_bedrock ### Setup * Before using the AWS Bedrock LLM, make sure you have the appropriate model access from [Bedrock Console](https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/modelaccess). * You will also need to authenticate the `boto3` client by using a method in the [AWS documentation](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html#configuring-credentials) * You will have to export `AWS_REGION`, `AWS_ACCESS_KEY`, and `AWS_SECRET_ACCESS_KEY` to set environment variables. ### Usage ```python theme={null} import os from mem0 import Memory os.environ['AWS_REGION'] = 'us-west-2' os.environ["AWS_ACCESS_KEY_ID"] = "xx" os.environ["AWS_SECRET_ACCESS_KEY"] = "xx" config = { "llm": { "provider": "aws_bedrock", "config": { "model": "anthropic.claude-3-5-haiku-20241022-v1:0", "temperature": 0.2, "max_tokens": 2000, } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` ### Config All available parameters for the `aws_bedrock` config are present in [Master List of All Params in Config](../config). # Azure OpenAI Source: https://docs.mem0.ai/components/llms/models/azure_openai Mem0 Now Supports Azure OpenAI Models in TypeScript SDK To use Azure OpenAI models, you have to set the `LLM_AZURE_OPENAI_API_KEY`, `LLM_AZURE_ENDPOINT`, `LLM_AZURE_DEPLOYMENT` and `LLM_AZURE_API_VERSION` environment variables. You can obtain the Azure API key from the [Azure](https://azure.microsoft.com/). Optionally, you can use Azure Identity to authenticate with Azure OpenAI, which allows you to use managed identities or service principals for production and Azure CLI login for development instead of an API key. If an Azure Identity is to be used, ***do not*** set the `LLM_AZURE_OPENAI_API_KEY` environment variable or the api\_key in the config dictionary. > **Note**: The following are currently unsupported with reasoning models `Parallel tool calling`,`temperature`, `top_p`, `presence_penalty`, `frequency_penalty`, `logprobs`, `top_logprobs`, `logit_bias`, `max_tokens` ## Usage ```python Python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "your-api-key" # used for embedding model os.environ["LLM_AZURE_OPENAI_API_KEY"] = "your-api-key" os.environ["LLM_AZURE_DEPLOYMENT"] = "your-deployment-name" os.environ["LLM_AZURE_ENDPOINT"] = "your-api-base-url" os.environ["LLM_AZURE_API_VERSION"] = "version-to-use" config = { "llm": { "provider": "azure_openai", "config": { "model": "your-deployment-name", "temperature": 0.1, "max_tokens": 2000, "azure_kwargs": { "azure_deployment": "", "api_version": "", "azure_endpoint": "", "api_key": "", "default_headers": { "CustomHeader": "your-custom-header", } } } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` ```typescript TypeScript theme={null} import { Memory } from 'mem0ai/oss'; const config = { llm: { provider: 'azure_openai', config: { apiKey: process.env.AZURE_OPENAI_API_KEY || '', modelProperties: { endpoint: 'https://your-api-base-url', deployment: 'your-deployment-name', modelName: 'your-model-name', apiVersion: 'version-to-use', // Any other parameters you want to pass to the Azure OpenAI API }, }, }, }; const memory = new Memory(config); const messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] await memory.add(messages, { userId: "alice", metadata: { category: "movies" } }); ``` We also support the new [OpenAI structured-outputs](https://platform.openai.com/docs/guides/structured-outputs/introduction) model. Typescript SDK does not support the `azure_openai_structured` model yet. ```python theme={null} import os from mem0 import Memory os.environ["LLM_AZURE_OPENAI_API_KEY"] = "your-api-key" os.environ["LLM_AZURE_DEPLOYMENT"] = "your-deployment-name" os.environ["LLM_AZURE_ENDPOINT"] = "your-api-base-url" os.environ["LLM_AZURE_API_VERSION"] = "version-to-use" config = { "llm": { "provider": "azure_openai_structured", "config": { "model": "your-deployment-name", "temperature": 0.1, "max_tokens": 2000, "azure_kwargs": { "azure_deployment": "", "api_version": "", "azure_endpoint": "", "api_key": "", "default_headers": { "CustomHeader": "your-custom-header", } } } } } ``` As an alternative to using an API key, the Azure Identity credential chain can be used to authenticate with [Azure OpenAI role-based security](https://learn.microsoft.com/en-us/azure/ai-foundry/openai/how-to/role-based-access-control). If an API key is provided, it will be used for authentication over an Azure Identity Below is a sample configuration for using Mem0 with Azure OpenAI and Azure Identity: ```python theme={null} import os from mem0 import Memory # You can set the values directly in the config dictionary or use environment variables os.environ["LLM_AZURE_DEPLOYMENT"] = "your-deployment-name" os.environ["LLM_AZURE_ENDPOINT"] = "your-api-base-url" os.environ["LLM_AZURE_API_VERSION"] = "version-to-use" config = { "llm": { "provider": "azure_openai_structured", "config": { "model": "your-deployment-name", "temperature": 0.1, "max_tokens": 2000, "azure_kwargs": { "azure_deployment": "", "api_version": "", "azure_endpoint": "", "default_headers": { "CustomHeader": "your-custom-header", } } } } } ``` Refer to [Azure Identity troubleshooting tips](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/identity/azure-identity/TROUBLESHOOTING.md#troubleshoot-environmentcredential-authentication-issues) for setting up an Azure Identity credential. ## Config All available parameters for the `azure_openai` config are present in [Master List of All Params in Config](../config). # DeepSeek Source: https://docs.mem0.ai/components/llms/models/deepseek To use DeepSeek LLM models, you have to set the `DEEPSEEK_API_KEY` environment variable. You can also optionally set `DEEPSEEK_API_BASE` if you need to use a different API endpoint (defaults to "[https://api.deepseek.com](https://api.deepseek.com)"). ## Usage ```python theme={null} import os from mem0 import Memory os.environ["DEEPSEEK_API_KEY"] = "your-api-key" os.environ["OPENAI_API_KEY"] = "your-api-key" # for embedder model config = { "llm": { "provider": "deepseek", "config": { "model": "deepseek-chat", # default model "temperature": 0.2, "max_tokens": 2000, "top_p": 1.0 } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` You can also configure the API base URL in the config: ```python theme={null} config = { "llm": { "provider": "deepseek", "config": { "model": "deepseek-chat", "deepseek_base_url": "https://your-custom-endpoint.com", "api_key": "your-api-key" # alternatively to using environment variable } } } ``` ## Config All available parameters for the `deepseek` config are present in [Master List of All Params in Config](../config). # Google AI Source: https://docs.mem0.ai/components/llms/models/google_AI To use the Gemini model, set the `GOOGLE_API_KEY` environment variable. You can obtain the Google/Gemini API key from [Google AI Studio](https://aistudio.google.com/app/apikey). > **Note:** As of the latest release, Mem0 uses the new `google.genai` SDK instead of the deprecated `google.generativeai`. All message formatting and model interaction now use the updated `types` module from `google.genai`. > **Note:** Some Gemini models are being deprecated and will retire soon. It is recommended to migrate to the latest stable models like `"gemini-2.0-flash-001"` or `"gemini-2.0-flash-lite-001"` to ensure ongoing support and improvements. ## Usage ```python Python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "your-openai-api-key" # Used for embedding model os.environ["GOOGLE_API_KEY"] = "your-gemini-api-key" config = { "llm": { "provider": "gemini", "config": { "model": "gemini-2.0-flash-001", "temperature": 0.2, "max_tokens": 2000, "top_p": 1.0 } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I’m not a big fan of thrillers, but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thrillers and suggest sci-fi movies instead."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` ```typescript TypeScript theme={null} import { Memory } from "mem0ai/oss"; const config = { llm: { // You can also use "google" as provider ( for backward compatibility ) provider: "gemini", config: { model: "gemini-2.0-flash-001", temperature: 0.1 } } } const memory = new Memory(config); const messages = [ { role: "user", content: "I'm planning to watch a movie tonight. Any recommendations?" }, { role: "assistant", content: "How about thriller movies? They can be quite engaging." }, { role: "user", content: "I’m not a big fan of thrillers, but I love sci-fi movies." }, { role: "assistant", content: "Got it! I'll avoid thrillers and suggest sci-fi movies instead." } ] await memory.add(messages, { userId: "alice", metadata: { category: "movies" } }); ``` ## Config All available parameters for the `Gemini` config are present in [Master List of All Params in Config](../config). # Groq Source: https://docs.mem0.ai/components/llms/models/groq [Groq](https://groq.com/) is the creator of the world's first Language Processing Unit (LPU), providing exceptional speed performance for AI workloads running on their LPU Inference Engine. In order to use LLMs from Groq, go to their [platform](https://console.groq.com/keys) and get the API key. Set the API key as `GROQ_API_KEY` environment variable to use the model as given below in the example. ## Usage ```python Python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "your-api-key" # used for embedding model os.environ["GROQ_API_KEY"] = "your-api-key" config = { "llm": { "provider": "groq", "config": { "model": "mixtral-8x7b-32768", "temperature": 0.1, "max_tokens": 2000, } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` ```typescript TypeScript theme={null} import { Memory } from 'mem0ai/oss'; const config = { llm: { provider: 'groq', config: { apiKey: process.env.GROQ_API_KEY || '', model: 'mixtral-8x7b-32768', temperature: 0.1, maxTokens: 1000, }, }, }; const memory = new Memory(config); const messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] await memory.add(messages, { userId: "alice", metadata: { category: "movies" } }); ``` ## Config All available parameters for the `groq` config are present in [Master List of All Params in Config](../config). # LangChain Source: https://docs.mem0.ai/components/llms/models/langchain Mem0 supports LangChain as a provider to access a wide range of LLM models. LangChain is a framework for developing applications powered by language models, making it easy to integrate various LLM providers through a consistent interface. For a complete list of available chat models supported by LangChain, refer to the [LangChain Chat Models documentation](https://python.langchain.com/docs/integrations/chat). ## Usage ```python Python theme={null} import os from mem0 import Memory from langchain_openai import ChatOpenAI # Set necessary environment variables for your chosen LangChain provider os.environ["OPENAI_API_KEY"] = "your-api-key" # Initialize a LangChain model directly openai_model = ChatOpenAI( model="gpt-4.1-nano-2025-04-14", temperature=0.2, max_tokens=2000 ) # Pass the initialized model to the config config = { "llm": { "provider": "langchain", "config": { "model": openai_model } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` ```typescript TypeScript theme={null} import { Memory } from 'mem0ai/oss'; import { ChatOpenAI } from "@langchain/openai"; // Initialize a LangChain model directly const openaiModel = new ChatOpenAI({ modelName: "gpt-4", temperature: 0.2, maxTokens: 2000, apiKey: process.env.OPENAI_API_KEY, }); const config = { llm: { provider: 'langchain', config: { model: openaiModel, }, }, }; const memory = new Memory(config); const messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] await memory.add(messages, { userId: "alice", metadata: { category: "movies" } }); ``` ## Supported LangChain Providers LangChain supports a wide range of LLM providers, including: * OpenAI (`ChatOpenAI`) * Anthropic (`ChatAnthropic`) * Google (`ChatGoogleGenerativeAI`, `ChatGooglePalm`) * Mistral (`ChatMistralAI`) * Ollama (`ChatOllama`) * Azure OpenAI (`AzureChatOpenAI`) * HuggingFace (`HuggingFaceChatEndpoint`) * And many more You can use any of these model instances directly in your configuration. For a complete and up-to-date list of available providers, refer to the [LangChain Chat Models documentation](https://python.langchain.com/docs/integrations/chat). ## Provider-Specific Configuration When using LangChain as a provider, you'll need to: 1. Set the appropriate environment variables for your chosen LLM provider 2. Import and initialize the specific model class you want to use 3. Pass the initialized model instance to the config Make sure to install the necessary LangChain packages and any provider-specific dependencies. ## Config All available parameters for the `langchain` config are present in [Master List of All Params in Config](../config). # null Source: https://docs.mem0.ai/components/llms/models/litellm [Litellm](https://litellm.vercel.app/docs/) is compatible with over 100 large language models (LLMs), all using a standardized input/output format. You can explore the [available models](https://litellm.vercel.app/docs/providers) to use with Litellm. Ensure you set the `API_KEY` for the model you choose to use. ## Usage ```python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "your-api-key" config = { "llm": { "provider": "litellm", "config": { "model": "gpt-4.1-nano-2025-04-14", "temperature": 0.2, "max_tokens": 2000, } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` ## Config All available parameters for the `litellm` config are present in [Master List of All Params in Config](../config). # LM Studio Source: https://docs.mem0.ai/components/llms/models/lmstudio To use LM Studio with Mem0, you'll need to have LM Studio running locally with its server enabled. LM Studio provides a way to run local LLMs with an OpenAI-compatible API. ## Usage ```python Python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "your-api-key" # used for embedding model config = { "llm": { "provider": "lmstudio", "config": { "model": "lmstudio-community/Meta-Llama-3.1-70B-Instruct-GGUF/Meta-Llama-3.1-70B-Instruct-IQ2_M.gguf", "temperature": 0.2, "max_tokens": 2000, "lmstudio_base_url": "http://localhost:1234/v1", # default LM Studio API URL "lmstudio_response_format": {"type": "json_schema", "json_schema": {"type": "object", "schema": {}}}, } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` ### Running Completely Locally You can also use LM Studio for both LLM and embedding to run Mem0 entirely locally: ```python theme={null} from mem0 import Memory # No external API keys needed! config = { "llm": { "provider": "lmstudio" }, "embedder": { "provider": "lmstudio" } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice123", metadata={"category": "movies"}) ``` When using LM Studio for both LLM and embedding, make sure you have: 1. An LLM model loaded for generating responses 2. An embedding model loaded for vector embeddings 3. The server enabled with the correct endpoints accessible To use LM Studio, you need to: 1. Download and install [LM Studio](https://lmstudio.ai/) 2. Start a local server from the "Server" tab 3. Set the appropriate `lmstudio_base_url` in your configuration (default is usually [http://localhost:1234/v1](http://localhost:1234/v1)) ## Config All available parameters for the `lmstudio` config are present in [Master List of All Params in Config](../config). # Mistral AI Source: https://docs.mem0.ai/components/llms/models/mistral_AI To use mistral's models, please obtain the Mistral AI api key from their [console](https://console.mistral.ai/). Set the `MISTRAL_API_KEY` environment variable to use the model as given below in the example. ## Usage ```python Python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "your-api-key" # used for embedding model os.environ["MISTRAL_API_KEY"] = "your-api-key" config = { "llm": { "provider": "litellm", "config": { "model": "open-mixtral-8x7b", "temperature": 0.1, "max_tokens": 2000, } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` ```typescript TypeScript theme={null} import { Memory } from 'mem0ai/oss'; const config = { llm: { provider: 'mistral', config: { apiKey: process.env.MISTRAL_API_KEY || '', model: 'mistral-tiny-latest', // Or 'mistral-small-latest', 'mistral-medium-latest', etc. temperature: 0.1, maxTokens: 2000, }, }, }; const memory = new Memory(config); const messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] await memory.add(messages, { userId: "alice", metadata: { category: "movies" } }); ``` ## Config All available parameters for the `litellm` config are present in [Master List of All Params in Config](../config). # Ollama Source: https://docs.mem0.ai/components/llms/models/ollama You can use LLMs from Ollama to run Mem0 locally. These [models](https://ollama.com/search?c=tools) support tool calling. ## Usage ```python Python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "your-api-key" # for embedder config = { "llm": { "provider": "ollama", "config": { "model": "mixtral:8x7b", "temperature": 0.1, "max_tokens": 2000, } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` ```typescript TypeScript theme={null} import { Memory } from 'mem0ai/oss'; const config = { llm: { provider: 'ollama', config: { model: 'llama3.1:8b', // or any other Ollama model url: 'http://localhost:11434', // Ollama server URL temperature: 0.1, }, }, }; const memory = new Memory(config); const messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] await memory.add(messages, { userId: "alice", metadata: { category: "movies" } }); ``` ## Config All available parameters for the `ollama` config are present in [Master List of All Params in Config](../config). # OpenAI Source: https://docs.mem0.ai/components/llms/models/openai To use OpenAI LLM models, you have to set the `OPENAI_API_KEY` environment variable. You can obtain the OpenAI API key from the [OpenAI Platform](https://platform.openai.com/account/api-keys). > **Note**: The following are currently unsupported with reasoning models `Parallel tool calling`,`temperature`, `top_p`, `presence_penalty`, `frequency_penalty`, `logprobs`, `top_logprobs`, `logit_bias`, `max_tokens` ## Usage ```python Python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "your-api-key" config = { "llm": { "provider": "openai", "config": { "model": "gpt-4.1-nano-2025-04-14", "temperature": 0.2, "max_tokens": 2000, } } } # Use Openrouter by passing it's api key # os.environ["OPENROUTER_API_KEY"] = "your-api-key" # config = { # "llm": { # "provider": "openai", # "config": { # "model": "meta-llama/llama-3.1-70b-instruct", # } # } # } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` ```typescript TypeScript theme={null} import { Memory } from 'mem0ai/oss'; const config = { llm: { provider: 'openai', config: { apiKey: process.env.OPENAI_API_KEY || '', model: 'gpt-4-turbo-preview', temperature: 0.2, maxTokens: 1500, }, }, }; const memory = new Memory(config); const messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] await memory.add(messages, { userId: "alice", metadata: { category: "movies" } }); ``` We also support the new [OpenAI structured-outputs](https://platform.openai.com/docs/guides/structured-outputs/introduction) model. ```python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "your-api-key" config = { "llm": { "provider": "openai_structured", "config": { "model": "gpt-4.1-nano-2025-04-14", "temperature": 0.0, } } } m = Memory.from_config(config) ``` ## Config All available parameters for the `openai` config are present in [Master List of All Params in Config](../config). # Sarvam AI Source: https://docs.mem0.ai/components/llms/models/sarvam **Sarvam AI** is an Indian AI company developing language models with a focus on Indian languages and cultural context. Their latest model **Sarvam-M** is designed to understand and generate content in multiple Indian languages while maintaining high performance in English. To use Sarvam AI's models, please set the `SARVAM_API_KEY` which you can get from their [platform](https://dashboard.sarvam.ai/). ## Usage ```python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "your-api-key" # used for embedding model os.environ["SARVAM_API_KEY"] = "your-api-key" config = { "llm": { "provider": "sarvam", "config": { "model": "sarvam-m", "temperature": 0.7, } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alex") ``` ## Advanced Usage with Sarvam-Specific Features ```python theme={null} import os from mem0 import Memory config = { "llm": { "provider": "sarvam", "config": { "model": { "name": "sarvam-m", "reasoning_effort": "high", # Enable advanced reasoning "frequency_penalty": 0.1, # Reduce repetition "seed": 42 # For deterministic outputs }, "temperature": 0.3, "max_tokens": 2000, "api_key": "your-sarvam-api-key" } } } m = Memory.from_config(config) # Example with Hindi conversation messages = [ {"role": "user", "content": "मैं SBI में joint account खोलना चाहता हूँ।"}, {"role": "assistant", "content": "SBI में joint account खोलने के लिए आपको कुछ documents की जरूरत होगी। क्या आप जानना चाहते हैं कि कौन से documents चाहिए?"} ] m.add(messages, user_id="rajesh", metadata={"language": "hindi", "topic": "banking"}) ``` ## Config All available parameters for the `sarvam` config are present in [Master List of All Params in Config](../config). # Together Source: https://docs.mem0.ai/components/llms/models/together To use Together LLM models, you have to set the `TOGETHER_API_KEY` environment variable. You can obtain the Together API key from their [Account settings page](https://api.together.xyz/settings/api-keys). ## Usage ```python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "your-api-key" # used for embedding model os.environ["TOGETHER_API_KEY"] = "your-api-key" config = { "llm": { "provider": "together", "config": { "model": "mistralai/Mixtral-8x7B-Instruct-v0.1", "temperature": 0.2, "max_tokens": 2000, } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` ## Config All available parameters for the `together` config are present in [Master List of All Params in Config](../config). # vLLM Source: https://docs.mem0.ai/components/llms/models/vllm [vLLM](https://docs.vllm.ai/) is a high-performance inference engine for large language models that provides significant performance improvements for local inference. It's designed to maximize throughput and memory efficiency for serving LLMs. ## Prerequisites 1. **Install vLLM**: ```bash theme={null} pip install vllm ``` 2. **Start vLLM server**: ```bash theme={null} # For testing with a small model vllm serve microsoft/DialoGPT-medium --port 8000 # For production with a larger model (requires GPU) vllm serve Qwen/Qwen2.5-32B-Instruct --port 8000 ``` ## Usage ```python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "your-api-key" # used for embedding model config = { "llm": { "provider": "vllm", "config": { "model": "Qwen/Qwen2.5-32B-Instruct", "vllm_base_url": "http://localhost:8000/v1", "temperature": 0.1, "max_tokens": 2000, } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thrillers, but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thrillers and suggest sci-fi movies instead."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` ## Configuration Parameters | Parameter | Description | Default | Environment Variable | | --------------- | --------------------------------- | ----------------------------- | -------------------- | | `model` | Model name running on vLLM server | `"Qwen/Qwen2.5-32B-Instruct"` | - | | `vllm_base_url` | vLLM server URL | `"http://localhost:8000/v1"` | `VLLM_BASE_URL` | | `api_key` | API key (dummy for local) | `"vllm-api-key"` | `VLLM_API_KEY` | | `temperature` | Sampling temperature | `0.1` | - | | `max_tokens` | Maximum tokens to generate | `2000` | - | ## Environment Variables You can set these environment variables instead of specifying them in config: ```bash theme={null} export VLLM_BASE_URL="http://localhost:8000/v1" export VLLM_API_KEY="your-vllm-api-key" export OPENAI_API_KEY="your-openai-api-key" # for embeddings ``` ## Benefits * **High Performance**: 2-24x faster inference than standard implementations * **Memory Efficient**: Optimized memory usage with PagedAttention * **Local Deployment**: Keep your data private and reduce API costs * **Easy Integration**: Drop-in replacement for other LLM providers * **Flexible**: Works with any model supported by vLLM ## Troubleshooting 1. **Server not responding**: Make sure vLLM server is running ```bash theme={null} curl http://localhost:8000/health ``` 2. **404 errors**: Ensure correct base URL format ```python theme={null} "vllm_base_url": "http://localhost:8000/v1" # Note the /v1 ``` 3. **Model not found**: Check model name matches server 4. **Out of memory**: Try smaller models or reduce `max_model_len` ```bash theme={null} vllm serve Qwen/Qwen2.5-32B-Instruct --max-model-len 4096 ``` ## Config All available parameters for the `vllm` config are present in [Master List of All Params in Config](../config). # xAI Source: https://docs.mem0.ai/components/llms/models/xAI [xAI](https://x.ai/) is a new AI company founded by Elon Musk that develops large language models, including Grok. Grok is trained on real-time data from X (formerly Twitter) and aims to provide accurate, up-to-date responses with a touch of wit and humor. In order to use LLMs from xAI, go to their [platform](https://console.x.ai) and get the API key. Set the API key as `XAI_API_KEY` environment variable to use the model as given below in the example. ## Usage ```python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "your-api-key" # used for embedding model os.environ["XAI_API_KEY"] = "your-api-key" config = { "llm": { "provider": "xai", "config": { "model": "grok-3-beta", "temperature": 0.1, "max_tokens": 2000, } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` ## Config All available parameters for the `xai` config are present in [Master List of All Params in Config](../config). # Overview Source: https://docs.mem0.ai/components/llms/overview Mem0 includes built-in support for various popular large language models. Memory can utilize the LLM provided by the user, ensuring efficient use for specific needs. ## Usage To use a llm, you must provide a configuration to customize its usage. If no configuration is supplied, a default configuration will be applied, and `OpenAI` will be used as the llm. For a comprehensive list of available parameters for llm configuration, please refer to [Config](./config). ## Supported LLMs See the list of supported LLMs below. All LLMs are supported in Python. The following LLMs are also supported in TypeScript: **OpenAI**, **Anthropic**, and **Groq**. ## Structured vs Unstructured Outputs Mem0 supports two types of OpenAI LLM formats, each with its own strengths and use cases: ### Structured Outputs Structured outputs are LLMs that align with OpenAI's structured outputs model: * **Optimized for:** Returning structured responses (e.g., JSON objects) * **Benefits:** Precise, easily parseable data * **Ideal for:** Data extraction, form filling, API responses * **Learn more:** [OpenAI Structured Outputs Guide](https://platform.openai.com/docs/guides/structured-outputs/introduction) ### Unstructured Outputs Unstructured outputs correspond to OpenAI's standard, free-form text model: * **Flexibility:** Returns open-ended, natural language responses * **Customization:** Use the `response_format` parameter to guide output * **Trade-off:** Less efficient than structured outputs for specific data needs * **Best for:** Creative writing, explanations, general conversation Choose the format that best suits your application's requirements for optimal performance and usability. # Configurations Source: https://docs.mem0.ai/components/vectordbs/config ## How to define configurations? The `config` is defined as an object with two main keys: * `vector_store`: Specifies the vector database provider and its configuration * `provider`: The name of the vector database (e.g., "chroma", "pgvector", "qdrant", "milvus", "upstash\_vector", "azure\_ai\_search", "vertex\_ai\_vector\_search", "valkey") * `config`: A nested dictionary containing provider-specific settings ## How to Use Config Here's a general example of how to use the config with mem0: ```python Python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "sk-xx" config = { "vector_store": { "provider": "your_chosen_provider", "config": { # Provider-specific settings go here } } } m = Memory.from_config(config) m.add("Your text here", user_id="user", metadata={"category": "example"}) ``` ```typescript TypeScript theme={null} // Example for in-memory vector database (Only supported in TypeScript) import { Memory } from 'mem0ai/oss'; const configMemory = { vector_store: { provider: 'memory', config: { collectionName: 'memories', dimension: 1536, }, }, }; const memory = new Memory(configMemory); await memory.add("Your text here", { userId: "user", metadata: { category: "example" } }); ``` The in-memory vector database is only supported in the TypeScript implementation. ## Why is Config Needed? Config is essential for: 1. Specifying which vector database to use. 2. Providing necessary connection details (e.g., host, port, credentials). 3. Customizing database-specific settings (e.g., collection name, path). 4. Ensuring proper initialization and connection to your chosen vector store. ## Master List of All Params in Config Here's a comprehensive list of all parameters that can be used across different vector databases: | Parameter | Description | | ---------------------------- | ------------------------------------------------------- | | `collection_name` | Name of the collection | | `embedding_model_dims` | Dimensions of the embedding model | | `client` | Custom client for the database | | `path` | Path for the database | | `host` | Host where the server is running | | `port` | Port where the server is running | | `user` | Username for database connection | | `password` | Password for database connection | | `dbname` | Name of the database | | `url` | Full URL for the server | | `api_key` | API key for the server | | `on_disk` | Enable persistent storage | | `endpoint_id` | Endpoint ID (vertex\_ai\_vector\_search) | | `index_id` | Index ID (vertex\_ai\_vector\_search) | | `deployment_index_id` | Deployment index ID (vertex\_ai\_vector\_search) | | `project_id` | Project ID (vertex\_ai\_vector\_search) | | `project_number` | Project number (vertex\_ai\_vector\_search) | | `vector_search_api_endpoint` | Vector search API endpoint (vertex\_ai\_vector\_search) | | `connection_string` | PostgreSQL connection string (for Supabase/PGVector) | | `index_method` | Vector index method (for Supabase) | | `index_measure` | Distance measure for similarity search (for Supabase) | | Parameter | Description | | -------------------- | ------------------------------------------------------- | | `collectionName` | Name of the collection | | `embeddingModelDims` | Dimensions of the embedding model | | `dimension` | Dimensions of the embedding model (for memory provider) | | `host` | Host where the server is running | | `port` | Port where the server is running | | `url` | URL for the server | | `apiKey` | API key for the server | | `path` | Path for the database | | `onDisk` | Enable persistent storage | | `redisUrl` | URL for the Redis server | | `username` | Username for database connection | | `password` | Password for database connection | ## Customizing Config Each vector database has its own specific configuration requirements. To customize the config for your chosen vector store: 1. Identify the vector database you want to use from [supported vector databases](./dbs). 2. Refer to the `Config` section in the respective vector database's documentation. 3. Include only the relevant parameters for your chosen database in the `config` dictionary. ## Supported Vector Databases For detailed information on configuring specific vector databases, please visit the [Supported Vector Databases](./dbs) section. There you'll find individual pages for each supported vector store with provider-specific usage examples and configuration details. # Azure AI Search Source: https://docs.mem0.ai/components/vectordbs/dbs/azure [Azure AI Search](https://learn.microsoft.com/azure/search/search-what-is-azure-search/) (formerly known as "Azure Cognitive Search") provides secure information retrieval at scale over user-owned content in traditional and generative AI search applications. ## Usage ```python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "sk-xx" # This key is used for embedding purpose config = { "vector_store": { "provider": "azure_ai_search", "config": { "service_name": "", "api_key": "", "collection_name": "mem0", "embedding_model_dims": 1536 } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` ## Using binary compression for large vector collections ```python theme={null} config = { "vector_store": { "provider": "azure_ai_search", "config": { "service_name": "", "api_key": "", "collection_name": "mem0", "embedding_model_dims": 1536, "compression_type": "binary", "use_float16": True # Use half precision for storage efficiency } } } ``` ## Using hybrid search ```python theme={null} config = { "vector_store": { "provider": "azure_ai_search", "config": { "service_name": "", "api_key": "", "collection_name": "mem0", "embedding_model_dims": 1536, "hybrid_search": True, "vector_filter_mode": "postFilter" } } } ``` ## Using Azure Identity for Authentication As an alternative to using an API key, the Azure Identity credential chain can be used to authenticate with Azure OpenAI. The list below shows the order of precedence for credential application: 1. **Environment Credential:** Azure client ID, secret, tenant ID, or certificate in environment variables for service principal authentication. 2. **Workload Identity Credential:** Utilizes Azure Workload Identity (relevant for Kubernetes and Azure workloads). 3. **Managed Identity Credential:** Authenticates as a Managed Identity (for apps/services hosted in Azure with Managed Identity enabled), this is the most secure production credential. 4. **Shared Token Cache Credential / Visual Studio Credential (Windows only):** Uses cached credentials from Visual Studio sign-ins (and sometimes VS Code if SSO is enabled). 5. **Azure CLI Credential:** Uses the currently logged-in user from the Azure CLI (`az login`), this is the most common development credential. 6. **Azure PowerShell Credential:** Uses the identity from Azure PowerShell (`Connect-AzAccount`). 7. **Azure Developer CLI Credential:** Uses the session from Azure Developer CLI (`azd auth login`). If an API is provided, it will be used for authentication over an Azure Identity To enable Role-Based Access Control (RBAC) for Azure AI Search, follow these steps: 1. In the Azure Portal, navigate to your **Azure AI Search** service. 2. In the left menu, select **Settings** > **Keys**. 3. Change the authentication setting to **Role-based access control**, or **Both** if you need API key compatibility. The default is “Key-based authentication”—you must switch it to use Azure roles. 4. **Go to Access Control (IAM):** * In the Azure Portal, select your Search service. * Click **Access Control (IAM)** on the left. 5. **Add a Role Assignment:** * Click **Add** > **Add role assignment**. 6. **Choose Role:** * Mem0 requires the **Search Index Data Contributor** and **Search Service Contributor** role. 7. **Choose Member** * To assign to a User, Group, Service Principal or Managed Identity: * For production it is recommended to use a service principal or managed identity. * For a service principal: select **User, group, or service principal** and search for the service principal. * For a managed identity: select **Managed identity** and choose the managed identity. * For development, you can assign the role to a user account. * For development: select **User, group, or service principal** and pick an Azure Entra ID account (the same used with `az login`). 8. **Complete the Assignment:** * Click **Review + Assign**. If you are using Azure Identity, do not set the `api_key` in the configuration. ```python theme={null} config = { "vector_store": { "provider": "azure_ai_search", "config": { "service_name": "", "collection_name": "mem0", "embedding_model_dims": 1536, "compression_type": "binary", "use_float16": True # Use half precision for storage efficiency } } } ``` ### Environment Variables to Use Azure Identity Credential * For an Environment Credential, you will need to setup a Service Principal and set the following environment variables: * `AZURE_TENANT_ID`: Your Azure Active Directory tenant ID. * `AZURE_CLIENT_ID`: The client ID of your service principal or managed identity. * `AZURE_CLIENT_SECRET`: The client secret of your service principal. * For a User-Assigned Managed Identity, you will need to set the following environment variable: * `AZURE_CLIENT_ID`: The client ID of the user-assigned managed identity. * For a System-Assigned Managed Identity, no additional environment variables are needed. ### Developer Logins for Azure Identity Credential * For an Azure CLI Credential, you need to have the Azure CLI installed and logged in with `az login`. * For an Azure PowerShell Credential, you need to have the Azure PowerShell module installed and logged in with `Connect-AzAccount`. * For an Azure Developer CLI Credential, you need to have the Azure Developer CLI installed and logged in with `azd auth login`. Troubleshooting tips for [Azure Identity](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/identity/azure-identity/TROUBLESHOOTING.md#troubleshoot-environmentcredential-authentication-issues). ## Configuration Parameters | Parameter | Description | Default Value | Options | | ---------------------- | ------------------------------------------------- | ------------- | ------------------------------------------------------------------------------------------------------------ | | `service_name` | Azure AI Search service name | Required | - | | `api_key` | API key of the Azure AI Search service | Optional | If not present, the [Azure Identity](#using-azure-identity-for-authentication) credential chain will be used | | `collection_name` | The name of the collection/index to store vectors | `mem0` | Any valid index name | | `embedding_model_dims` | Dimensions of the embedding model | `1536` | Any integer value | | `compression_type` | Type of vector compression to use | `none` | `none`, `scalar`, `binary` | | `use_float16` | Store vectors in half precision (Edm.Half) | `False` | `True`, `False` | | `vector_filter_mode` | Vector filter mode to use | `preFilter` | `postFilter`, `preFilter` | | `hybrid_search` | Use hybrid search | `False` | `True`, `False` | ## Notes on Configuration Options * **compression\_type**: * `none`: No compression, uses full vector precision * `scalar`: Scalar quantization with reasonable balance of speed and accuracy * `binary`: Binary quantization for maximum compression with some accuracy trade-off * **vector\_filter\_mode**: * `preFilter`: Applies filters before vector search (faster) * `postFilter`: Applies filters after vector search (may provide better relevance) * **use\_float16**: Using half precision (float16) reduces storage requirements but may slightly impact accuracy. Useful for very large vector collections. * **Filterable Fields**: The implementation automatically extracts `user_id`, `run_id`, and `agent_id` fields from payloads for filtering. # Azure MySQL Source: https://docs.mem0.ai/components/vectordbs/dbs/azure_mysql [Azure Database for MySQL](https://azure.microsoft.com/products/mysql) is a fully managed relational database service that provides enterprise-grade reliability and security. It supports JSON-based vector storage for semantic search capabilities in AI applications. ### Usage ```python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "sk-xx" config = { "vector_store": { "provider": "azure_mysql", "config": { "host": "your-server.mysql.database.azure.com", "port": 3306, "user": "your_username", "password": "your_password", "database": "mem0_db", "collection_name": "memories", } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` #### Using Azure Managed Identity For production deployments, use Azure Managed Identity instead of passwords: ```python theme={null} config = { "vector_store": { "provider": "azure_mysql", "config": { "host": "your-server.mysql.database.azure.com", "user": "your_username", "database": "mem0_db", "collection_name": "memories", "use_azure_credential": True, # Uses DefaultAzureCredential "ssl_disabled": False } } } ``` When `use_azure_credential` is enabled, the password is obtained via Azure DefaultAzureCredential (supports Managed Identity, Azure CLI, etc.) ### Config Here are the parameters available for configuring Azure MySQL: | Parameter | Description | Default Value | | ---------------------- | -------------------------------------------------- | ------------- | | `host` | MySQL server hostname | Required | | `port` | MySQL server port | `3306` | | `user` | Database user | Required | | `password` | Database password (optional with Azure credential) | `None` | | `database` | Database name | Required | | `collection_name` | Table name for storing vectors | `"mem0"` | | `embedding_model_dims` | Dimensions of embedding vectors | `1536` | | `use_azure_credential` | Use Azure DefaultAzureCredential | `False` | | `ssl_ca` | Path to SSL CA certificate | `None` | | `ssl_disabled` | Disable SSL (not recommended) | `False` | | `minconn` | Minimum connections in pool | `1` | | `maxconn` | Maximum connections in pool | `5` | ### Setup #### Create MySQL Flexible Server using Azure CLI: ```bash theme={null} # Create resource group az group create --name mem0-rg --location eastus # Create MySQL Flexible Server az mysql flexible-server create \ --resource-group mem0-rg \ --name mem0-mysql-server \ --location eastus \ --admin-user myadmin \ --admin-password \ --version 8.0.21 # Create database az mysql flexible-server db create \ --resource-group mem0-rg \ --server-name mem0-mysql-server \ --database-name mem0_db # Configure firewall az mysql flexible-server firewall-rule create \ --resource-group mem0-rg \ --name mem0-mysql-server \ --rule-name AllowMyIP \ --start-ip-address \ --end-ip-address ``` #### Enable Azure AD Authentication: 1. In Azure Portal, navigate to your MySQL Flexible Server 2. Go to **Security** > **Authentication** and enable Azure AD 3. Add your application's managed identity as a MySQL user: ```sql theme={null} CREATE AADUSER 'your-app-identity' IDENTIFIED BY 'your-client-id'; GRANT ALL PRIVILEGES ON mem0_db.* TO 'your-app-identity'@'%'; FLUSH PRIVILEGES; ``` For production, use [Managed Identity](https://learn.microsoft.com/azure/active-directory/managed-identities-azure-resources/) to eliminate password management. # Baidu VectorDB (Mochow) Source: https://docs.mem0.ai/components/vectordbs/dbs/baidu [Baidu VectorDB](https://cloud.baidu.com/doc/VDB/index.html) is an enterprise-level distributed vector database service developed by Baidu Intelligent Cloud. It is powered by Baidu's proprietary "Mochow" vector database kernel, providing high performance, availability, and security for vector search. ### Usage ```python theme={null} import os from mem0 import Memory config = { "vector_store": { "provider": "baidu", "config": { "endpoint": "http://your-mochow-endpoint:8287", "account": "root", "api_key": "your-api-key", "database_name": "mem0", "table_name": "mem0_table", "embedding_model_dims": 1536, "metric_type": "COSINE" } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about a thriller movie? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` ### Config Here are the parameters available for configuring Baidu VectorDB: | Parameter | Description | Default Value | | ---------------------- | --------------------------------------------- | ------------- | | `endpoint` | Endpoint URL for your Baidu VectorDB instance | Required | | `account` | Baidu VectorDB account name | `root` | | `api_key` | API key for accessing Baidu VectorDB | Required | | `database_name` | Name of the database | `mem0` | | `table_name` | Name of the table | `mem0_table` | | `embedding_model_dims` | Dimensions of the embedding model | `1536` | | `metric_type` | Distance metric for similarity search | `L2` | ### Distance Metrics The following distance metrics are supported: * `L2`: Euclidean distance (default) * `IP`: Inner product * `COSINE`: Cosine similarity ### Index Configuration The vector index is automatically configured with the following HNSW parameters: * `m`: 16 (number of connections per element) * `efconstruction`: 200 (size of the dynamic candidate list) * `auto_build`: true (automatically build index) * `auto_build_index_policy`: Incremental build with 10000 rows increment # Apache Cassandra Source: https://docs.mem0.ai/components/vectordbs/dbs/cassandra [Apache Cassandra](https://cassandra.apache.org/) is a highly scalable, distributed NoSQL database designed for handling large amounts of data across many commodity servers with no single point of failure. It supports vector storage for semantic search capabilities in AI applications and can scale to massive datasets with linear performance improvements. ### Usage ```python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "sk-xx" config = { "vector_store": { "provider": "cassandra", "config": { "contact_points": ["127.0.0.1"], "port": 9042, "username": "cassandra", "password": "cassandra", "keyspace": "mem0", "collection_name": "memories", } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` #### Using DataStax Astra DB For managed Cassandra with DataStax Astra DB: ```python theme={null} config = { "vector_store": { "provider": "cassandra", "config": { "contact_points": ["dummy"], # Not used with secure connect bundle "username": "token", "password": "AstraCS:...", # Your Astra DB application token "keyspace": "mem0", "collection_name": "memories", "secure_connect_bundle": "/path/to/secure-connect-bundle.zip" } } } ``` When using DataStax Astra DB, provide the secure connect bundle path. The contact\_points parameter is ignored when a secure connect bundle is provided. ### Config Here are the parameters available for configuring Apache Cassandra: | Parameter | Description | Default Value | | ----------------------- | -------------------------------------- | ------------- | | `contact_points` | List of contact point IP addresses | Required | | `port` | Cassandra port | `9042` | | `username` | Database username | `None` | | `password` | Database password | `None` | | `keyspace` | Keyspace name | `"mem0"` | | `collection_name` | Table name for storing vectors | `"memories"` | | `embedding_model_dims` | Dimensions of embedding vectors | `1536` | | `secure_connect_bundle` | Path to Astra DB secure connect bundle | `None` | | `protocol_version` | CQL protocol version | `4` | | `load_balancing_policy` | Custom load balancing policy | `None` | ### Setup #### Option 1: Local Cassandra Setup using Docker: ```bash theme={null} # Pull and run Cassandra container docker run --name mem0-cassandra \ -p 9042:9042 \ -e CASSANDRA_CLUSTER_NAME="Mem0Cluster" \ -d cassandra:latest # Wait for Cassandra to start (may take 1-2 minutes) docker exec -it mem0-cassandra cqlsh # Create keyspace CREATE KEYSPACE IF NOT EXISTS mem0 WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}; ``` #### Option 2: DataStax Astra DB (Managed Cloud): 1. Sign up at [DataStax Astra](https://astra.datastax.com/) 2. Create a new database 3. Download the secure connect bundle 4. Generate an application token For production deployments, use DataStax Astra DB for fully managed Cassandra with automatic scaling, backups, and security. #### Option 3: Install Cassandra Locally: **Ubuntu/Debian:** ```bash theme={null} # Add Apache Cassandra repository echo "deb https://downloads.apache.org/cassandra/debian 40x main" | sudo tee -a /etc/apt/sources.list.d/cassandra.sources.list curl https://downloads.apache.org/cassandra/KEYS | sudo apt-key add - # Install Cassandra sudo apt-get update sudo apt-get install cassandra # Start Cassandra sudo systemctl start cassandra # Verify installation nodetool status ``` **macOS:** ```bash theme={null} # Using Homebrew brew install cassandra # Start Cassandra brew services start cassandra # Connect to CQL shell cqlsh ``` ### Python Client Installation Install the required Python package: ```bash theme={null} pip install cassandra-driver ``` ### Performance Considerations * **Replication Factor**: For production, use replication factor of at least 3 * **Consistency Level**: Balance between consistency and performance (QUORUM recommended) * **Partitioning**: Cassandra automatically distributes data across nodes * **Scaling**: Add nodes to linearly increase capacity and performance ### Advanced Configuration ```python theme={null} from cassandra.policies import DCAwareRoundRobinPolicy config = { "vector_store": { "provider": "cassandra", "config": { "contact_points": ["node1.example.com", "node2.example.com", "node3.example.com"], "port": 9042, "username": "mem0_user", "password": "secure_password", "keyspace": "mem0_prod", "collection_name": "memories", "protocol_version": 4, "load_balancing_policy": DCAwareRoundRobinPolicy(local_dc='DC1') } } } ``` For production use, configure appropriate replication strategies and consistency levels based on your availability and consistency requirements. # null Source: https://docs.mem0.ai/components/vectordbs/dbs/chroma [Chroma](https://www.trychroma.com/) is an AI-native open-source vector database that simplifies building LLM apps by providing tools for storing, embedding, and searching embeddings with a focus on simplicity and speed. It supports both local deployment and cloud hosting through ChromaDB Cloud. ### Usage #### Local Installation ```python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "sk-xx" config = { "vector_store": { "provider": "chroma", "config": { "collection_name": "test", "path": "db", # Optional: ChromaDB Cloud configuration # "api_key": "your-chroma-cloud-api-key", # "tenant": "your-chroma-cloud-tenant-id", } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` ### Config Here are the parameters available for configuring Chroma: | Parameter | Description | Default Value | | ----------------- | ------------------------------------------- | ------------- | | `collection_name` | The name of the collection | `mem0` | | `client` | Custom client for Chroma | `None` | | `path` | Path for the Chroma database | `db` | | `host` | The host where the Chroma server is running | `None` | | `port` | The port where the Chroma server is running | `None` | | `api_key` | ChromaDB Cloud API key (for cloud usage) | `None` | | `tenant` | ChromaDB Cloud tenant ID (for cloud usage) | `None` | # null Source: https://docs.mem0.ai/components/vectordbs/dbs/databricks [Databricks Vector Search](https://docs.databricks.com/en/generative-ai/vector-search.html) is a serverless similarity search engine that allows you to store a vector representation of your data, including metadata, in a vector database. With Vector Search, you can create auto-updating vector search indexes from Delta tables managed by Unity Catalog and query them with a simple API to return the most similar vectors. ### Usage ```python theme={null} import os from mem0 import Memory config = { "vector_store": { "provider": "databricks", "config": { "workspace_url": "https://your-workspace.databricks.com", "access_token": "your-access-token", "endpoint_name": "your-vector-search-endpoint", "index_name": "catalog.schema.index_name", "source_table_name": "catalog.schema.source_table", "embedding_dimension": 1536 } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` ### Config Here are the parameters available for configuring Databricks Vector Search: | Parameter | Description | Default Value | | --------------------------------- | --------------------------------------------------------------------------- | ------------- | | `workspace_url` | The URL of your Databricks workspace | **Required** | | `access_token` | Personal Access Token for authentication | `None` | | `service_principal_client_id` | Service principal client ID (alternative to access\_token) | `None` | | `service_principal_client_secret` | Service principal client secret (required with client\_id) | `None` | | `endpoint_name` | Name of the Vector Search endpoint | **Required** | | `index_name` | Name of the vector index (Unity Catalog format: catalog.schema.index) | **Required** | | `source_table_name` | Name of the source Delta table (Unity Catalog format: catalog.schema.table) | **Required** | | `embedding_dimension` | Dimension of self-managed embeddings | `1536` | | `embedding_source_column` | Column name for text when using Databricks-computed embeddings | `None` | | `embedding_model_endpoint_name` | Databricks serving endpoint for embeddings | `None` | | `embedding_vector_column` | Column name for self-managed embedding vectors | `embedding` | | `endpoint_type` | Type of endpoint (`STANDARD` or `STORAGE_OPTIMIZED`) | `STANDARD` | | `sync_computed_embeddings` | Whether to sync computed embeddings automatically | `True` | ### Authentication Databricks Vector Search supports two authentication methods: #### Service Principal (Recommended for Production) ```python theme={null} config = { "vector_store": { "provider": "databricks", "config": { "workspace_url": "https://your-workspace.databricks.com", "service_principal_client_id": "your-service-principal-id", "service_principal_client_secret": "your-service-principal-secret", "endpoint_name": "your-endpoint", "index_name": "catalog.schema.index_name", "source_table_name": "catalog.schema.source_table" } } } ``` #### Personal Access Token (for Development) ```python theme={null} config = { "vector_store": { "provider": "databricks", "config": { "workspace_url": "https://your-workspace.databricks.com", "access_token": "your-personal-access-token", "endpoint_name": "your-endpoint", "index_name": "catalog.schema.index_name", "source_table_name": "catalog.schema.source_table" } } } ``` ### Embedding Options #### Self-Managed Embeddings (Default) Use your own embedding model and provide vectors directly: ```python theme={null} config = { "vector_store": { "provider": "databricks", "config": { # ... authentication config ... "embedding_dimension": 768, # Match your embedding model "embedding_vector_column": "embedding" } } } ``` #### Databricks-Computed Embeddings Let Databricks compute embeddings from text using a serving endpoint: ```python theme={null} config = { "vector_store": { "provider": "databricks", "config": { # ... authentication config ... "embedding_source_column": "text", "embedding_model_endpoint_name": "e5-small-v2" } } } ``` ### Important Notes * **Delta Sync Index**: This implementation uses Delta Sync Index, which automatically syncs with your source Delta table. Direct vector insertion/deletion/update operations will log warnings as they're not supported with Delta Sync. * **Unity Catalog**: Both the source table and index must be in Unity Catalog format (`catalog.schema.table_name`). * **Endpoint Auto-Creation**: If the specified endpoint doesn't exist, it will be created automatically. * **Index Auto-Creation**: If the specified index doesn't exist, it will be created automatically with the provided configuration. * **Filter Support**: Supports filtering by metadata fields, with different syntax for STANDARD vs STORAGE\_OPTIMIZED endpoints. # null Source: https://docs.mem0.ai/components/vectordbs/dbs/elasticsearch [Elasticsearch](https://www.elastic.co/) is a distributed, RESTful search and analytics engine that can efficiently store and search vector data using dense vectors and k-NN search. ### Installation Elasticsearch support requires additional dependencies. Install them with: ```bash theme={null} pip install elasticsearch>=8.0.0 ``` ### Usage ```python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "sk-xx" config = { "vector_store": { "provider": "elasticsearch", "config": { "collection_name": "mem0", "host": "localhost", "port": 9200, "embedding_model_dims": 1536 } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` ### Config Here are the parameters available for configuring Elasticsearch: | Parameter | Description | Default Value | | ---------------------- | -------------------------------------------------- | ------------- | | `collection_name` | The name of the index to store the vectors | `mem0` | | `embedding_model_dims` | Dimensions of the embedding model | `1536` | | `host` | The host where the Elasticsearch server is running | `localhost` | | `port` | The port where the Elasticsearch server is running | `9200` | | `cloud_id` | Cloud ID for Elastic Cloud deployment | `None` | | `api_key` | API key for authentication | `None` | | `user` | Username for basic authentication | `None` | | `password` | Password for basic authentication | `None` | | `verify_certs` | Whether to verify SSL certificates | `True` | | `auto_create_index` | Whether to automatically create the index | `True` | | `custom_search_query` | Function returning a custom search query | `None` | | `headers` | Custom headers to include in requests | `None` | ### Features * Efficient vector search using Elasticsearch's native k-NN search * Support for both local and cloud deployments (Elastic Cloud) * Multiple authentication methods (Basic Auth, API Key) * Automatic index creation with optimized mappings for vector search * Memory isolation through payload filtering * Custom search query function to customize the search query ### Custom Search Query The `custom_search_query` parameter allows you to customize the search query when `Memory.search` is called. **Example** ```python theme={null} import os from typing import List, Optional, Dict from mem0 import Memory def custom_search_query(query: List[float], limit: int, filters: Optional[Dict]) -> Dict: return { "knn": { "field": "vector", "query_vector": query, "k": limit, "num_candidates": limit * 2 } } os.environ["OPENAI_API_KEY"] = "sk-xx" config = { "vector_store": { "provider": "elasticsearch", "config": { "collection_name": "mem0", "host": "localhost", "port": 9200, "embedding_model_dims": 1536, "custom_search_query": custom_search_query } } } ``` It should be a function that takes the following parameters: * `query`: a query vector used in `Memory.search` * `limit`: a number of results used in `Memory.search` * `filters`: a dictionary of key-value pairs used in `Memory.search`. You can add custom pairs for the custom search query. The function should return a query body for the Elasticsearch search API. # null Source: https://docs.mem0.ai/components/vectordbs/dbs/faiss [FAISS](https://github.com/facebookresearch/faiss) is a library for efficient similarity search and clustering of dense vectors. It is designed to work with large-scale datasets and provides a high-performance search engine for vector data. FAISS is optimized for memory usage and search speed, making it an excellent choice for production environments. ### Usage ```python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "sk-xx" config = { "vector_store": { "provider": "faiss", "config": { "collection_name": "test", "path": "/tmp/faiss_memories", "distance_strategy": "euclidean" } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` ### Installation To use FAISS in your mem0 project, you need to install the appropriate FAISS package for your environment: ```bash theme={null} # For CPU version pip install faiss-cpu # For GPU version (requires CUDA) pip install faiss-gpu ``` ### Config Here are the parameters available for configuring FAISS: | Parameter | Description | Default Value | | ------------------- | ---------------------------------------------------------------------------------- | ------------------------------ | | `collection_name` | The name of the collection | `mem0` | | `path` | Path to store FAISS index and metadata | `/tmp/faiss/` | | `distance_strategy` | Distance metric strategy to use (options: 'euclidean', 'inner\_product', 'cosine') | `euclidean` | | `normalize_L2` | Whether to normalize L2 vectors (only applicable for euclidean distance) | `False` | ### Performance Considerations FAISS offers several advantages for vector search: 1. **Efficiency**: FAISS is optimized for memory usage and speed, making it suitable for large-scale applications. 2. **Offline Support**: FAISS works entirely locally, with no need for external servers or API calls. 3. **Storage Options**: Vectors can be stored in-memory for maximum speed or persisted to disk. 4. **Multiple Index Types**: FAISS supports different index types optimized for various use cases (though mem0 currently uses the basic flat index). ### Distance Strategies FAISS in mem0 supports three distance strategies: * **euclidean**: L2 distance, suitable for most embedding models * **inner\_product**: Dot product similarity, useful for some specialized embeddings * **cosine**: Cosine similarity, best for comparing semantic similarity regardless of vector magnitude When using `cosine` or `inner_product` with normalized vectors, you may want to set `normalize_L2=True` for better results. # LangChain Source: https://docs.mem0.ai/components/vectordbs/dbs/langchain Mem0 supports LangChain as a provider for vector store integration. LangChain provides a unified interface to various vector databases, making it easy to integrate different vector store providers through a consistent API. When using LangChain as your vector store provider, you must set the collection name to "mem0". This is a required configuration for proper integration with Mem0. ## Usage ```python Python theme={null} import os from mem0 import Memory from langchain_community.vectorstores import Chroma from langchain_openai import OpenAIEmbeddings # Initialize a LangChain vector store embeddings = OpenAIEmbeddings() vector_store = Chroma( persist_directory="./chroma_db", embedding_function=embeddings, collection_name="mem0" # Required collection name ) # Pass the initialized vector store to the config config = { "vector_store": { "provider": "langchain", "config": { "client": vector_store } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` ```typescript TypeScript theme={null} import { Memory } from "mem0ai"; import { OpenAIEmbeddings } from "@langchain/openai"; import { MemoryVectorStore as LangchainMemoryStore } from "langchain/vectorstores/memory"; const embeddings = new OpenAIEmbeddings(); const vectorStore = new LangchainVectorStore(embeddings); const config = { "vector_store": { "provider": "langchain", "config": { "client": vectorStore } } } const memory = new Memory(config); const messages = [ { role: "user", content: "I'm planning to watch a movie tonight. Any recommendations?" }, { role: "assistant", content: "How about thriller movies? They can be quite engaging." }, { role: "user", content: "I'm not a big fan of thriller movies but I love sci-fi movies." }, { role: "assistant", content: "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future." } ] await memory.add(messages, { userId: "alice", metadata: { category: "movies" } }); ``` ## Supported LangChain Vector Stores LangChain supports a wide range of vector store providers, including: * Chroma * FAISS * Pinecone * Weaviate * Milvus * Qdrant * And many more You can use any of these vector store instances directly in your configuration. For a complete and up-to-date list of available providers, refer to the [LangChain Vector Stores documentation](https://python.langchain.com/docs/integrations/vectorstores). ## Limitations When using LangChain as a vector store provider, there are some limitations to be aware of: 1. **Bulk Operations**: The `get_all` and `delete_all` operations are not supported when using LangChain as the vector store provider. This is because LangChain's vector store interface doesn't provide standardized methods for these bulk operations across all providers. 2. **Provider-Specific Features**: Some advanced features may not be available depending on the specific vector store implementation you're using through LangChain. ## Provider-Specific Configuration When using LangChain as a vector store provider, you'll need to: 1. Set the appropriate environment variables for your chosen vector store provider 2. Import and initialize the specific vector store class you want to use 3. Pass the initialized vector store instance to the config Make sure to install the necessary LangChain packages and any provider-specific dependencies. ## Config All available parameters for the `langchain` vector store config are present in [Master List of All Params in Config](../config). # null Source: https://docs.mem0.ai/components/vectordbs/dbs/milvus [Milvus](https://milvus.io/) is an open-source vector database that suits AI applications of every size, from running a demo chatbot in a Jupyter notebook to building web-scale search that serves billions of users. ### Usage ```python theme={null} import os from mem0 import Memory config = { "vector_store": { "provider": "milvus", "config": { "collection_name": "test", "embedding_model_dims": 1536, "url": "127.0.0.1", "token": "8e4b8ca8cf2c67", "db_name": "my_database", } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` ### Config Here are the parameters available for configuring Milvus: | Parameter | Description | Default Value | | ---------------------- | ----------------------------------------------------------- | ------------------------ | | `url` | Full URL/Uri for Milvus/Zilliz server | `http://localhost:19530` | | `token` | Token for Zilliz server / for local setup defaults to None. | `None` | | `collection_name` | The name of the collection | `mem0` | | `embedding_model_dims` | Dimensions of the embedding model | `1536` | | `metric_type` | Metric type for similarity search | `L2` | | `db_name` | Name of the database | `""` | # null Source: https://docs.mem0.ai/components/vectordbs/dbs/mongodb # MongoDB [MongoDB](https://www.mongodb.com/) is a versatile document database that supports vector search capabilities, allowing for efficient high-dimensional similarity searches over large datasets with robust scalability and performance. ## Usage ```python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "sk-xx" config = { "vector_store": { "provider": "mongodb", "config": { "db_name": "mem0-db", "collection_name": "mem0-collection", "mongo_uri":"mongodb://username:password@localhost:27017" } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` ## Config Here are the parameters available for configuring MongoDB: | Parameter | Description | Default Value | | ---------------------- | ----------------------------------- | --------------------------------------------- | | db\_name | Name of the MongoDB database | `"mem0_db"` | | collection\_name | Name of the MongoDB collection | `"mem0_collection"` | | embedding\_model\_dims | Dimensions of the embedding vectors | `1536` | | mongo\_uri | The MongoDB URI connection string | `mongodb://username:password@localhost:27017` | > **Note**: If `mongo_uri` is not provided, it will default to `mongodb://username:password@localhost:27017`. # null Source: https://docs.mem0.ai/components/vectordbs/dbs/neptune_analytics # Neptune Analytics Vector Store [Neptune Analytics](https://docs.aws.amazon.com/neptune-analytics/latest/userguide/what-is-neptune-analytics.html/) is a memory-optimized graph database engine for analytics. With Neptune Analytics, you can get insights and find trends by processing large amounts of graph data in seconds, including vector search. ## Installation ```bash theme={null} pip install mem0ai[vector_stores] ``` ## Usage ```python theme={null} config = { "vector_store": { "provider": "neptune", "config": { "collection_name": "mem0", "endpoint": f"neptune-graph://my-graph-identifier", }, }, } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` ## Parameters Let's see the available parameters for the `neptune` config: | Parameter | Description | Default Value | | ----------------- | ------------------------------------------------ | ------------------------------------- | | `collection_name` | The name of the collection to store the vectors | `mem0` | | `endpoint` | Connection URL for the Neptune Analytics service | `neptune-graph://my-graph-identifier` | # null Source: https://docs.mem0.ai/components/vectordbs/dbs/opensearch [OpenSearch](https://opensearch.org/) is an enterprise-grade search and observability suite that brings order to unstructured data at scale. OpenSearch supports k-NN (k-Nearest Neighbors) and allows you to store and retrieve high-dimensional vector embeddings efficiently. ### Installation OpenSearch support requires additional dependencies. Install them with: ```bash theme={null} pip install opensearch-py ``` ### Prerequisites Before using OpenSearch with Mem0, you need to set up a collection in AWS OpenSearch Service. #### AWS OpenSearch Service You can create a collection through the AWS Console: * Navigate to [OpenSearch Service Console](https://console.aws.amazon.com/aos/home) * Click "Create collection" * Select "Serverless collection" and then enable "Vector search" capabilities * Once created, note the endpoint URL (host) for your configuration ### Usage ```python theme={null} import os from mem0 import Memory import boto3 from opensearchpy import OpenSearch, RequestsHttpConnection, AWSV4SignerAuth # For AWS OpenSearch Service with IAM authentication region = 'us-west-2' service = 'aoss' credentials = boto3.Session().get_credentials() auth = AWSV4SignerAuth(credentials, region, service) config = { "vector_store": { "provider": "opensearch", "config": { "collection_name": "mem0", "host": "your-domain.us-west-2.aoss.amazonaws.com", "port": 443, "http_auth": auth, "embedding_model_dims": 1024, "connection_class": RequestsHttpConnection, "pool_maxsize": 20, "use_ssl": True, "verify_certs": True } } } ``` ### Add Memories ```python theme={null} m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` ### Search Memories ```python theme={null} results = m.search("What kind of movies does Alice like?", user_id="alice") ``` ### Features * Fast and Efficient Vector Search * Can be deployed on-premises, in containers, or on cloud platforms like AWS OpenSearch Service * Multiple authentication and security methods (Basic Authentication, API Keys, LDAP, SAML, and OpenID Connect) * Automatic index creation with optimized mappings for vector search * Memory optimization through disk-based vector search and quantization * Real-time analytics and observability # null Source: https://docs.mem0.ai/components/vectordbs/dbs/pgvector [pgvector](https://github.com/pgvector/pgvector) is an open-source vector similarity search extension for Postgres. After connecting to Postgres, run `CREATE EXTENSION IF NOT EXISTS vector;` to create the vector extension. ### Usage ```python Python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "sk-xx" config = { "vector_store": { "provider": "pgvector", "config": { "user": "test", "password": "123", "host": "127.0.0.1", "port": "5432", } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` ```typescript TypeScript theme={null} import { Memory } from 'mem0ai/oss'; const config = { vectorStore: { provider: 'pgvector', config: { collectionName: 'memories', embeddingModelDims: 1536, user: 'test', password: '123', host: '127.0.0.1', port: 5432, dbname: 'vector_store', // Optional, defaults to 'postgres' diskann: false, // Optional, requires pgvectorscale extension hnsw: false, // Optional, for HNSW indexing }, }, }; const memory = new Memory(config); const messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] await memory.add(messages, { userId: "alice", metadata: { category: "movies" } }); ``` ### Config Here are the parameters available for configuring pgvector: | Parameter | Description | Default Value | | ---------------------- | --------------------------------------------------------------------------------------- | ------------- | | `dbname` | The name of the database | `postgres` | | `collection_name` | The name of the collection | `mem0` | | `embedding_model_dims` | Dimensions of the embedding model | `1536` | | `user` | User name to connect to the database | `None` | | `password` | Password to connect to the database | `None` | | `host` | The host where the Postgres server is running | `None` | | `port` | The port where the Postgres server is running | `None` | | `diskann` | Whether to use diskann for vector similarity search (requires pgvectorscale) | `True` | | `hnsw` | Whether to use hnsw for vector similarity search | `False` | | `sslmode` | SSL mode for PostgreSQL connection (e.g., 'require', 'prefer', 'disable') | `None` | | `connection_string` | PostgreSQL connection string (overrides individual connection parameters) | `None` | | `connection_pool` | psycopg2 connection pool object (overrides connection string and individual parameters) | `None` | **Note**: The connection parameters have the following priority: 1. `connection_pool` (highest priority) 2. `connection_string` 3. Individual connection parameters (`user`, `password`, `host`, `port`, `sslmode`) # null Source: https://docs.mem0.ai/components/vectordbs/dbs/pinecone [Pinecone](https://www.pinecone.io/) is a fully managed vector database designed for machine learning applications, offering high performance vector search with low latency at scale. It's particularly well-suited for semantic search, recommendation systems, and other AI-powered applications. > **New**: Pinecone integration now supports custom namespaces! Use the `namespace` parameter to logically separate data within the same index. This is especially useful for multi-tenant or multi-user applications. > **Note**: Before configuring Pinecone, you need to select an embedding model (e.g., OpenAI, Cohere, or custom models) and ensure the `embedding_model_dims` in your config matches your chosen model's dimensions. For example, OpenAI's text-embedding-3-small uses 1536 dimensions. ### Usage ```python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "sk-xx" os.environ["PINECONE_API_KEY"] = "your-api-key" # Example using serverless configuration config = { "vector_store": { "provider": "pinecone", "config": { "collection_name": "testing", "embedding_model_dims": 1536, # Matches OpenAI's text-embedding-3-small "namespace": "my-namespace", # Optional: specify a namespace for multi-tenancy "serverless_config": { "cloud": "aws", # Choose between 'aws' or 'gcp' or 'azure' "region": "us-east-1" }, "metric": "cosine" } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` ### Config Here are the parameters available for configuring Pinecone: | Parameter | Description | Default Value | | ---------------------- | -------------------------------------------------------------------------- | ---------------------------------------- | | `collection_name` | Name of the index/collection | Required | | `embedding_model_dims` | Dimensions of the embedding model (must match your chosen embedding model) | Required | | `client` | Existing Pinecone client instance | `None` | | `api_key` | API key for Pinecone | Environment variable: `PINECONE_API_KEY` | | `environment` | Pinecone environment | `None` | | `serverless_config` | Configuration for serverless deployment (AWS or GCP or Azure) | `None` | | `pod_config` | Configuration for pod-based deployment | `None` | | `hybrid_search` | Whether to enable hybrid search | `False` | | `metric` | Distance metric for vector similarity | `"cosine"` | | `batch_size` | Batch size for operations | `100` | | `namespace` | Namespace for the collection, useful for multi-tenancy. | `None` | > **Important**: You must choose either `serverless_config` or `pod_config` for your deployment, but not both. #### Serverless Config Example ```python theme={null} config = { "vector_store": { "provider": "pinecone", "config": { "collection_name": "memory_index", "embedding_model_dims": 1536, # For OpenAI's text-embedding-3-small "namespace": "my-namespace", # Optional: custom namespace "serverless_config": { "cloud": "aws", # or "gcp" or "azure" "region": "us-east-1" # Choose appropriate region } } } } ``` #### Pod Config Example ```python theme={null} config = { "vector_store": { "provider": "pinecone", "config": { "collection_name": "memory_index", "embedding_model_dims": 1536, # For OpenAI's text-embedding-ada-002 "namespace": "my-namespace", # Optional: custom namespace "pod_config": { "environment": "gcp-starter", "replicas": 1, "pod_type": "starter" } } } } ``` # null Source: https://docs.mem0.ai/components/vectordbs/dbs/qdrant [Qdrant](https://qdrant.tech/) is an open-source vector search engine. It is designed to work with large-scale datasets and provides a high-performance search engine for vector data. ### Usage ```python Python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "sk-xx" config = { "vector_store": { "provider": "qdrant", "config": { "collection_name": "test", "host": "localhost", "port": 6333, } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` ```typescript TypeScript theme={null} import { Memory } from 'mem0ai/oss'; const config = { vectorStore: { provider: 'qdrant', config: { collectionName: 'memories', embeddingModelDims: 1536, host: 'localhost', port: 6333, }, }, }; const memory = new Memory(config); const messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] await memory.add(messages, { userId: "alice", metadata: { category: "movies" } }); ``` ### Config Let's see the available parameters for the `qdrant` config: | Parameter | Description | Default Value | | ---------------------- | ----------------------------------------------- | ------------- | | `collection_name` | The name of the collection to store the vectors | `mem0` | | `embedding_model_dims` | Dimensions of the embedding model | `1536` | | `client` | Custom client for qdrant | `None` | | `host` | The host where the qdrant server is running | `None` | | `port` | The port where the qdrant server is running | `None` | | `path` | Path for the qdrant database | `/tmp/qdrant` | | `url` | Full URL for the qdrant server | `None` | | `api_key` | API key for the qdrant server | `None` | | `on_disk` | For enabling persistent storage | `False` | | Parameter | Description | Default Value | | -------------------- | ----------------------------------------------- | ------------- | | `collectionName` | The name of the collection to store the vectors | `mem0` | | `embeddingModelDims` | Dimensions of the embedding model | `1536` | | `host` | The host where the Qdrant server is running | `None` | | `port` | The port where the Qdrant server is running | `None` | | `path` | Path for the Qdrant database | `/tmp/qdrant` | | `url` | Full URL for the Qdrant server | `None` | | `apiKey` | API key for the Qdrant server | `None` | | `onDisk` | For enabling persistent storage | `False` | # null Source: https://docs.mem0.ai/components/vectordbs/dbs/redis [Redis](https://redis.io/) is a scalable, real-time database that can store, search, and analyze vector data. ### Installation ```bash theme={null} pip install redis redisvl ``` Redis Stack using Docker: ```bash theme={null} docker run -d --name redis-stack -p 6379:6379 -p 8001:8001 redis/redis-stack:latest ``` ### Usage ```python Python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "sk-xx" config = { "vector_store": { "provider": "redis", "config": { "collection_name": "mem0", "embedding_model_dims": 1536, "redis_url": "redis://localhost:6379" } }, "version": "v1.1" } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` ```typescript TypeScript theme={null} import { Memory } from 'mem0ai/oss'; const config = { vectorStore: { provider: 'redis', config: { collectionName: 'memories', embeddingModelDims: 1536, redisUrl: 'redis://localhost:6379', username: 'your-redis-username', password: 'your-redis-password', }, }, }; const memory = new Memory(config); const messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] await memory.add(messages, { userId: "alice", metadata: { category: "movies" } }); ``` ### Config Let's see the available parameters for the `redis` config: | Parameter | Description | Default Value | | ---------------------- | ----------------------------------------------- | ------------- | | `collection_name` | The name of the collection to store the vectors | `mem0` | | `embedding_model_dims` | Dimensions of the embedding model | `1536` | | `redis_url` | The URL of the Redis server | `None` | | Parameter | Description | Default Value | | -------------------- | ----------------------------------------------- | ------------- | | `collectionName` | The name of the collection to store the vectors | `mem0` | | `embeddingModelDims` | Dimensions of the embedding model | `1536` | | `redisUrl` | The URL of the Redis server | `None` | | `username` | Username for Redis connection | `None` | | `password` | Password for Redis connection | `None` | # Amazon S3 Vectors Source: https://docs.mem0.ai/components/vectordbs/dbs/s3_vectors [Amazon S3 Vectors](https://aws.amazon.com/s3/features/vectors/) is a purpose-built, cost-optimized vector storage and query service for semantic search and AI applications. It provides S3-level elasticity and durability with sub-second query performance. ### Installation S3 Vectors support requires additional dependencies. Install them with: ```bash theme={null} pip install boto3 ``` ### Usage To use Amazon S3 Vectors with Mem0, you need to have an AWS account and the necessary IAM permissions (`s3vectors:*`). Ensure your environment is configured with AWS credentials (e.g., via `~/.aws/credentials` or environment variables). ```python theme={null} import os from mem0 import Memory # Ensure your AWS credentials are configured in your environment # e.g., by setting AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_DEFAULT_REGION config = { "vector_store": { "provider": "s3_vectors", "config": { "vector_bucket_name": "my-mem0-vector-bucket", "collection_name": "my-memories-index", "embedding_model_dims": 1536, "distance_metric": "cosine", "region_name": "us-east-1" } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about a thriller movie? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` ### Config Here are the parameters available for configuring Amazon S3 Vectors: | Parameter | Description | Default Value | | ---------------------- | -------------------------------------------------------------------------------- | ------------------------------------- | | `vector_bucket_name` | The name of the S3 Vector bucket to use. It will be created if it doesn't exist. | Required | | `collection_name` | The name of the vector index within the bucket. | `mem0` | | `embedding_model_dims` | Dimensions of the embedding model. Must match your embedder. | `1536` | | `distance_metric` | Distance metric for similarity search. Options: `cosine`, `euclidean`. | `cosine` | | `region_name` | The AWS region where the bucket and index reside. | `None` (uses default from AWS config) | ### IAM Permissions Your AWS identity (user or role) needs permissions to perform actions on S3 Vectors. A minimal policy would look like this: ```json theme={null} { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "s3vectors:*", "Resource": "*" } ] } ``` For production, it is recommended to scope down the resource ARN to your specific buckets and indexes. # null Source: https://docs.mem0.ai/components/vectordbs/dbs/supabase [Supabase](https://supabase.com/) is an open-source Firebase alternative that provides a PostgreSQL database with pgvector extension for vector similarity search. It offers a powerful and scalable solution for storing and querying vector embeddings. Create a [Supabase](https://supabase.com/dashboard/projects) account and project, then get your connection string from Project Settings > Database. See the [docs](https://supabase.github.io/vecs/hosting/) for details. ### Usage ```python Python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "sk-xx" config = { "vector_store": { "provider": "supabase", "config": { "connection_string": "postgresql://user:password@host:port/database", "collection_name": "memories", "index_method": "hnsw", # Optional: defaults to "auto" "index_measure": "cosine_distance" # Optional: defaults to "cosine_distance" } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` ```typescript Typescript theme={null} import { Memory } from "mem0ai/oss"; const config = { vectorStore: { provider: "supabase", config: { collectionName: "memories", embeddingModelDims: 1536, supabaseUrl: process.env.SUPABASE_URL || "", supabaseKey: process.env.SUPABASE_KEY || "", tableName: "memories", }, }, } const memory = new Memory(config); const messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] await memory.add(messages, { userId: "alice", metadata: { category: "movies" } }); ``` ### SQL Migrations for TypeScript Implementation The following SQL migrations are required to enable the vector extension and create the memories table: ```sql theme={null} -- Enable the vector extension create extension if not exists vector; -- Create the memories table create table if not exists memories ( id text primary key, embedding vector(1536), metadata jsonb, created_at timestamp with time zone default timezone('utc', now()), updated_at timestamp with time zone default timezone('utc', now()) ); -- Create the vector similarity search function create or replace function match_vectors( query_embedding vector(1536), match_count int, filter jsonb default '{}'::jsonb ) returns table ( id text, similarity float, metadata jsonb ) language plpgsql as $$ begin return query select t.id::text, 1 - (t.embedding <=> query_embedding) as similarity, t.metadata from memories t where case when filter::text = '{}'::text then true else t.metadata @> filter end order by t.embedding <=> query_embedding limit match_count; end; $$; ``` Go to [Supabase](https://supabase.com/dashboard/projects) and run the above SQL migrations in the SQL Editor. ### Config Here are the parameters available for configuring Supabase: | Parameter | Description | Default Value | | ---------------------- | --------------------------------------- | ----------------- | | `connection_string` | PostgreSQL connection string (required) | None | | `collection_name` | Name for the vector collection | `mem0` | | `embedding_model_dims` | Dimensions of the embedding model | `1536` | | `index_method` | Vector index method to use | `auto` | | `index_measure` | Distance measure for similarity search | `cosine_distance` | | Parameter | Description | Default Value | | -------------------- | --------------------------------- | ------------- | | `collectionName` | Name for the vector collection | `mem0` | | `embeddingModelDims` | Dimensions of the embedding model | `1536` | | `supabaseUrl` | Supabase URL | None | | `supabaseKey` | Supabase key | None | | `tableName` | Name for the vector table | `memories` | ### Index Methods The following index methods are supported: * `auto`: Automatically selects the best available index method * `hnsw`: Hierarchical Navigable Small World graph index (faster search, more memory usage) * `ivfflat`: Inverted File Flat index (good balance of speed and memory) ### Distance Measures Available distance measures for similarity search: * `cosine_distance`: Cosine similarity (recommended for most embedding models) * `l2_distance`: Euclidean distance * `l1_distance`: Manhattan distance * `max_inner_product`: Maximum inner product similarity ### Best Practices 1. **Index Method Selection**: * Use `hnsw` for fastest search performance when memory is not a constraint * Use `ivfflat` for a good balance of search speed and memory usage * Use `auto` if unsure, it will select the best method based on your data 2. **Distance Measure Selection**: * Use `cosine_distance` for most embedding models (OpenAI, Hugging Face, etc.) * Use `max_inner_product` if your vectors are normalized * Use `l2_distance` or `l1_distance` if working with raw feature vectors 3. **Connection String**: * Always use environment variables for sensitive information in the connection string * Format: `postgresql://user:password@host:port/database` # null Source: https://docs.mem0.ai/components/vectordbs/dbs/upstash-vector [Upstash Vector](https://upstash.com/docs/vector) is a serverless vector database with built-in embedding models. ### Usage with Upstash embeddings You can enable the built-in embedding models by setting `enable_embeddings` to `True`. This allows you to use Upstash's embedding models for vectorization. ```python theme={null} import os from mem0 import Memory os.environ["UPSTASH_VECTOR_REST_URL"] = "..." os.environ["UPSTASH_VECTOR_REST_TOKEN"] = "..." config = { "vector_store": { "provider": "upstash_vector", "enable_embeddings": True, } } m = Memory.from_config(config) m.add("Likes to play cricket on weekends", user_id="alice", metadata={"category": "hobbies"}) ``` Setting `enable_embeddings` to `True` will bypass any external embedding provider you have configured. ### Usage with external embedding providers ```python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "..." os.environ["UPSTASH_VECTOR_REST_URL"] = "..." os.environ["UPSTASH_VECTOR_REST_TOKEN"] = "..." config = { "vector_store": { "provider": "upstash_vector", }, "embedder": { "provider": "openai", "config": { "model": "text-embedding-3-large" }, } } m = Memory.from_config(config) m.add("Likes to play cricket on weekends", user_id="alice", metadata={"category": "hobbies"}) ``` ### Config Here are the parameters available for configuring Upstash Vector: | Parameter | Description | Default Value | | ------------------- | ---------------------------------- | ------------- | | `url` | URL for the Upstash Vector index | `None` | | `token` | Token for the Upstash Vector index | `None` | | `client` | An `upstash_vector.Index` instance | `None` | | `collection_name` | The default namespace used | `""` | | `enable_embeddings` | Whether to use Upstash embeddings | `False` | When `url` and `token` are not provided, the `UPSTASH_VECTOR_REST_URL` and `UPSTASH_VECTOR_REST_TOKEN` environment variables are used. # null Source: https://docs.mem0.ai/components/vectordbs/dbs/valkey # Valkey Vector Store [Valkey](https://valkey.io/) is an open source (BSD) high-performance key/value datastore that supports a variety of workloads and rich datastructures including vector search. ## Installation ```bash theme={null} pip install mem0ai[vector_stores] ``` ## Usage ```python theme={null} config = { "vector_store": { "provider": "valkey", "config": { "collection_name": "test", "valkey_url": "valkey://localhost:6379", "embedding_model_dims": 1536, "index_type": "flat" } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` ## Parameters Here are the parameters available for configuring Valkey: | Parameter | Description | Default Value | | ---------------------- | ----------------------------------------------- | ------------------------- | | `collection_name` | The name of the collection to store the vectors | `mem0` | | `valkey_url` | Connection URL for the Valkey server | `valkey://localhost:6379` | | `embedding_model_dims` | Dimensions of the embedding model | `1536` | | `index_type` | Vector index algorithm (`hnsw` or `flat`) | `hnsw` | | `hnsw_m` | Number of bi-directional links for HNSW | `16` | | `hnsw_ef_construction` | Size of dynamic candidate list for HNSW | `200` | | `hnsw_ef_runtime` | Size of dynamic candidate list for search | `10` | | `distance_metric` | Distance metric for vector similarity | `cosine` | # null Source: https://docs.mem0.ai/components/vectordbs/dbs/vectorize [Cloudflare Vectorize](https://developers.cloudflare.com/vectorize/) is a vector database offering from Cloudflare, allowing you to build AI-powered applications with vector embeddings. ### Usage ```typescript TypeScript theme={null} import { Memory } from 'mem0ai/oss'; const config = { vectorStore: { provider: 'vectorize', config: { indexName: 'my-memory-index', accountId: 'your-cloudflare-account-id', apiKey: 'your-cloudflare-api-key', dimension: 1536, // Optional: defaults to 1536 }, }, }; const memory = new Memory(config); const messages = [ {"role": "user", "content": "I'm looking for a good book to read."}, {"role": "assistant", "content": "Sure, what genre are you interested in?"}, {"role": "user", "content": "I enjoy fantasy novels with strong world-building."}, {"role": "assistant", "content": "Great! I'll keep that in mind for future recommendations."} ] await memory.add(messages, { userId: "bob", metadata: { interest: "books" } }); ``` ### Config Here are the parameters available for configuring Vectorize: | Parameter | Description | Default Value | | ----------- | --------------------------------- | ----------------- | | `indexName` | The name of the Vectorize index | `None` (Required) | | `accountId` | Your Cloudflare account ID | `None` (Required) | | `apiKey` | Your Cloudflare API token | `None` (Required) | | `dimension` | Dimensions of the embedding model | `1536` | # Vertex AI Vector Search Source: https://docs.mem0.ai/components/vectordbs/dbs/vertex_ai ### Usage To use Google Cloud Vertex AI Vector Search with `mem0`, you need to configure the `vector_store` in your `mem0` config: ```python theme={null} import os from mem0 import Memory os.environ["GOOGLE_API_KEY"] = "sk-xx" config = { "vector_store": { "provider": "vertex_ai_vector_search", "config": { "endpoint_id": "YOUR_ENDPOINT_ID", # Required: Vector Search endpoint ID "index_id": "YOUR_INDEX_ID", # Required: Vector Search index ID "deployment_index_id": "YOUR_DEPLOYMENT_INDEX_ID", # Required: Deployment-specific ID "project_id": "YOUR_PROJECT_ID", # Required: Google Cloud project ID "project_number": "YOUR_PROJECT_NUMBER", # Required: Google Cloud project number "region": "YOUR_REGION", # Optional: Defaults to GOOGLE_CLOUD_REGION "credentials_path": "path/to/credentials.json", # Optional: Defaults to GOOGLE_APPLICATION_CREDENTIALS "vector_search_api_endpoint": "YOUR_API_ENDPOINT" # Required for get operations } } } m = Memory.from_config(config) m.add("Your text here", user_id="user", metadata={"category": "example"}) ``` ### Required Parameters | Parameter | Description | Required | | ---------------------------- | ----------------------------------- | ------------------------------------------------- | | `endpoint_id` | Vector Search endpoint ID | Yes | | `index_id` | Vector Search index ID | Yes | | `deployment_index_id` | Deployment-specific index ID | Yes | | `project_id` | Google Cloud project ID | Yes | | `project_number` | Google Cloud project number | Yes | | `vector_search_api_endpoint` | Vector search API endpoint | Yes (for get operations) | | `region` | Google Cloud region | No (defaults to GOOGLE\_CLOUD\_REGION) | | `credentials_path` | Path to service account credentials | No (defaults to GOOGLE\_APPLICATION\_CREDENTIALS) | # null Source: https://docs.mem0.ai/components/vectordbs/dbs/weaviate [Weaviate](https://weaviate.io/) is an open-source vector search engine. It allows efficient storage and retrieval of high-dimensional vector embeddings, enabling powerful search and retrieval capabilities. ### Installation ```bash theme={null} pip install weaviate weaviate-client ``` ### Usage ```python Python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "sk-xx" config = { "vector_store": { "provider": "weaviate", "config": { "collection_name": "test", "cluster_url": "http://localhost:8080", "auth_client_secret": None, } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about a thriller movie? They can be quite engaging."}, {"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` ### Config Here are the parameters available for configuring Weaviate: | Parameter | Description | Default Value | | ---------------------- | ----------------------------------------------- | ------------- | | `collection_name` | The name of the collection to store the vectors | `mem0` | | `embedding_model_dims` | Dimensions of the embedding model | `1536` | | `cluster_url` | URL for the Weaviate server | `None` | | `auth_client_secret` | API key for Weaviate authentication | `None` | # Overview Source: https://docs.mem0.ai/components/vectordbs/overview Mem0 includes built-in support for various popular databases. Memory can utilize the database provided by the user, ensuring efficient use for specific needs. ## Supported Vector Databases See the list of supported vector databases below. The following vector databases are supported in the Python implementation. The TypeScript implementation currently only supports Qdrant, Redis, Valkey, Vectorize and in-memory vector database. ## Usage To utilize a vector database, you must provide a configuration to customize its usage. If no configuration is supplied, a default configuration will be applied, and `Qdrant` will be used as the vector database. For a comprehensive list of available parameters for vector database configuration, please refer to [Config](./config). ## Common issues ### Using Model with Different Dimensions If you are using a customized model with different dimensions other than 1536 (for example, 768), you may encounter the following error: `ValueError: shapes (0,1536) and (768,) not aligned: 1536 (dim 1) != 768 (dim 0)` You can add `"embedding_model_dims": 768,` to the config of the vector\_store to resolve this issue. # Add Memory Source: https://docs.mem0.ai/core-concepts/memory-operations/add Add memory into the Mem0 platform by storing user-assistant interactions and facts for later retrieval. # How Mem0 Adds Memory Adding memory is how Mem0 captures useful details from a conversation so your agents can reuse them later. Think of it as saving the important sentences from a chat transcript into a structured notebook your agent can search. **Why it matters** * Preserves user preferences, goals, and feedback across sessions. * Powers personalization and decision-making in downstream conversations. * Keeps context consistent between managed Platform and OSS deployments. ## Key terms * **Messages** – The ordered list of user/assistant turns you send to `add`. * **Infer** – Controls whether Mem0 extracts structured memories (`infer=True`, default) or stores raw messages. * **Metadata** – Optional filters (e.g., `{"category": "movie_recommendations"}`) that improve retrieval later. * **User / Session identifiers** – `user_id`, `session_id`, or `run_id` that scope the memory for future searches. ## How does it work? Mem0 offers two flows: * **Mem0 Platform** – Fully managed API with dashboard, scaling, and graph features. * **Mem0 Open Source** – Local SDK that you run in your own environment. Both flows take the same payload and pass it through the same pipeline. Mem0 sends the messages through an LLM that pulls out key facts, decisions, or preferences to remember. Existing memories are checked for duplicates or contradictions so the latest truth wins. The resulting memories land in managed vector storage (and optional graph storage) so future searches return them quickly. Duplicate protection only runs during that conflict-resolution step when you let Mem0 infer memories (`infer=True`, the default). If you switch to `infer=False`, Mem0 stores your payload exactly as provided, so duplicates will land. Mixing both modes for the same fact will save it twice. You trigger this pipeline with a single `add` call—no manual orchestration needed. ## Add with Mem0 Platform ```python Python theme={null} from mem0 import MemoryClient client = MemoryClient(api_key="your-api-key") messages = [ {"role": "user", "content": "I'm planning a trip to Tokyo next month."}, {"role": "assistant", "content": "Great! I’ll remember that for future suggestions."} ] client.add( messages=messages, user_id="alice", ) ``` ```javascript JavaScript theme={null} import { MemoryClient } from "mem0ai"; const client = new MemoryClient({apiKey: "your-api-key"}); const messages = [ { role: "user", content: "I'm planning a trip to Tokyo next month." }, { role: "assistant", content: "Great! I’ll remember that for future suggestions." } ]; await client.add({ messages, user_id: "alice", version: "v2", }); ``` Expect a `memory_id` (or list of IDs) in the response. Check the Mem0 dashboard to confirm the new entry under the correct user. ## Add with Mem0 Open Source ```python Python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "your-api-key" m = Memory() messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] # Store inferred memories (default behavior) result = m.add(messages, user_id="alice", metadata={"category": "movie_recommendations"}) # Optionally store raw messages without inference result = m.add(messages, user_id="alice", metadata={"category": "movie_recommendations"}, infer=False) ``` ```javascript JavaScript theme={null} import { Memory } from 'mem0ai/oss'; const memory = new Memory(); const messages = [ { role: "user", content: "I like to drink coffee in the morning and go for a walk" } ]; const result = memory.add(messages, { userId: "alice", metadata: { category: "preferences" } }); ``` Use `infer=False` only when you need to store raw transcripts. Most workflows benefit from Mem0 extracting structured memories automatically. If you do choose `infer=False`, keep it consistent. Raw inserts skip conflict resolution, so a later `infer=True` call with the same content will create a second memory instead of updating the first. ## When Should You Add Memory? Add memory whenever your agent learns something useful: * A new user preference is shared * A decision or suggestion is made * A goal or task is completed * A new entity is introduced * A user gives feedback or clarification Storing this context allows the agent to reason better in future interactions. ### More Details For full list of supported fields, required formats, and advanced options, see the [Add Memory API Reference](/api-reference/memory/add-memories). ## Managed vs OSS differences | Capability | Mem0 Platform | Mem0 OSS | | -------------------- | ---------------------------------------- | ----------------------------------------------- | | Conflict resolution | Automatic with dashboard visibility | SDK handles merges locally; you control storage | | Graph writes | Toggle per request (`enable_graph=True`) | Requires configuring a graph provider | | Rate limits | Managed quotas per workspace | Limited by your hardware and provider APIs | | Dashboard visibility | Yes — inspect memories visually | Inspect via CLI, logs, or custom UI | ## Put it into practice * Review the Advanced Memory Operations guide to layer metadata, rerankers, and graph toggles. * Explore the Add Memories API reference for every request/response field. ## See it live * Support Inbox with Mem0 shows add + search powering a support flow. * AI Tutor with Mem0 uses add to personalize lesson plans. # Delete Memory Source: https://docs.mem0.ai/core-concepts/memory-operations/delete Remove memories from Mem0 either individually, in bulk, or via filters. # Remove Memories Safely Deleting memories is how you honor compliance requests, undo bad data, or clean up expired sessions. Mem0 lets you delete a specific memory, a list of IDs, or everything that matches a filter. **Why it matters** * Satisfies user erasure (GDPR/CCPA) without touching the rest of your data. * Keeps knowledge bases accurate by removing stale or incorrect facts. * Works for both the managed Platform API and the OSS SDK. ## Key terms * **memory\_id** – Unique ID returned by `add`/`search` identifying the record to delete. * **batch\_delete** – API call that removes up to 1000 memories in one request. * **delete\_all** – Filter-based deletion by user, agent, run, or metadata. * **immutable** – Flagged memories that cannot be updated; delete + re-add instead. ## How the delete flow works Decide whether you’re removing a single memory, a list, or everything that matches a filter. Call `delete`, `batch_delete`, or `delete_all` with the required IDs or filters. Confirm the response message, then re-run `search` or check the dashboard/logs to ensure the memory is gone. ## Delete a single memory (Platform) ```python Python theme={null} from mem0 import MemoryClient client = MemoryClient(api_key="your-api-key") memory_id = "your_memory_id" client.delete(memory_id=memory_id) ``` ```javascript JavaScript theme={null} import MemoryClient from 'mem0ai'; const client = new MemoryClient({ apiKey: "your-api-key" }); client.delete("your_memory_id") .then(result => console.log(result)) .catch(error => console.error(error)); ``` You’ll receive a confirmation payload. The dashboard reflects the removal within seconds. ## Batch delete multiple memories (Platform) ```python Python theme={null} from mem0 import MemoryClient client = MemoryClient(api_key="your-api-key") delete_memories = [ {"memory_id": "id1"}, {"memory_id": "id2"} ] response = client.batch_delete(delete_memories) print(response) ``` ```javascript JavaScript theme={null} import MemoryClient from 'mem0ai'; const client = new MemoryClient({ apiKey: "your-api-key" }); const deleteMemories = [ { memory_id: "id1" }, { memory_id: "id2" } ]; client.batchDelete(deleteMemories) .then(response => console.log('Batch delete response:', response)) .catch(error => console.error(error)); ``` ## Delete memories by filter (Platform) ```python Python theme={null} from mem0 import MemoryClient client = MemoryClient(api_key="your-api-key") # Delete all memories for a specific user client.delete_all(user_id="alice") ``` ```javascript JavaScript theme={null} import MemoryClient from 'mem0ai'; const client = new MemoryClient({ apiKey: "your-api-key" }); client.deleteAll({ user_id: "alice" }) .then(result => console.log(result)) .catch(error => console.error(error)); ``` You can also filter by other parameters such as: * `agent_id` * `run_id` * `metadata` (as JSON string) `delete_all` requires at least one filter (user, agent, run, or metadata). Calling it with no filters raises an error to prevent accidental data loss. ## Delete with Mem0 OSS ```python Python theme={null} from mem0 import Memory memory = Memory() memory.delete(memory_id="mem_123") memory.delete_all(user_id="alice") ``` The OSS JavaScript SDK does not yet expose deletion helpers—use the REST API or Python SDK when self-hosting. ## Use cases recap * Forget a user’s preferences at their request. * Remove outdated or incorrect facts before they spread. * Clean up memories after session expiration or retention deadlines. * Comply with privacy legislation (GDPR, CCPA) and internal policies. ## Method comparison | Method | Use when | IDs required | Filters | | --------------------- | ----------------------------------- | ------------ | ------- | | `delete(memory_id)` | You know the exact record | ✔️ | ✖️ | | `batch_delete([...])` | You have a list of IDs to purge | ✔️ | ✖️ | | `delete_all(...)` | You need to forget a user/agent/run | ✖️ | ✔️ | ## Put it into practice * Review the Delete Memory API reference, plus Batch Delete and Filtered Delete. * Pair deletes with Expiration Policies to automate retention. ## See it live * Support Inbox with Mem0 demonstrates compliance-driven deletes. * Data Management tooling shows how deletes fit into broader lifecycle flows. # Search Memory Source: https://docs.mem0.ai/core-concepts/memory-operations/search Retrieve relevant memories from Mem0 using powerful semantic and filtered search capabilities. # How Mem0 Searches Memory Mem0's search operation lets agents ask natural-language questions and get back the memories that matter most. Like a smart librarian, it finds exactly what you need from everything you've stored. **Why it matters** * Retrieves the right facts without rebuilding prompts from scratch. * Supports both managed Platform and OSS so you can test locally and deploy at scale. * Keeps results relevant with filters, rerankers, and thresholds. ## Key terms * **Query** – Natural-language question or statement you pass to `search`. * **Filters** – JSON logic (AND/OR, comparison operators) that narrows results by user, categories, dates, etc. * **top\_k / threshold** – Controls how many memories return and the minimum similarity score. * **Rerank** – Optional second pass that boosts precision when a reranker is configured. ## Architecture Mem0 cleans and enriches your natural-language query so the downstream embedding search is accurate. Embeddings locate the closest memories using cosine similarity across your scoped dataset. Logical filters narrow candidates; rerankers or thresholds fine-tune ordering. Formatted memories (with metadata and timestamps) return to your agent or calling service. This pipeline runs the same way for the hosted Platform API and the OSS SDK. ## How does it work? Search converts your natural language question into a vector embedding, then finds memories with similar embeddings in your database. The results are ranked by similarity score and can be further refined with filters or reranking. ```python theme={null} # Minimal example that shows the concept in action # Platform API client.search("What are Alice's hobbies?", filters={"user_id": "alice"}) # OSS m.search("What are Alice's hobbies?", user_id="alice") ``` Always provide at least a `user_id` filter to scope searches to the right user's memories. This prevents cross-contamination between users. ## When should you use it? * **Context retrieval** - When your agent needs past context to generate better responses * **Personalization** - To recall user preferences, history, or past interactions * **Fact checking** - To verify information against stored memories before responding * **Decision support** - When agents need relevant background information to make decisions ## Platform vs OSS usage | Capability | Mem0 Platform | Mem0 OSS | | --------------------- | -------------------------------------------------------------------- | --------------------------------------------------- | | **user\_id usage** | In `filters={"user_id": "alice"}` for search/get\_all | As parameter `user_id="alice"` for all operations | | **Filter syntax** | Logical operators (`AND`, `OR`, comparisons) with field-level access | Basic field filters, extend via Python hooks | | **Reranking** | Toggle `rerank=True` with managed reranker catalog | Requires configuring local or third-party rerankers | | **Thresholds** | Request-level configuration (`threshold`, `top_k`) | Controlled via SDK parameters | | **Response metadata** | Includes confidence scores, timestamps, dashboard visibility | Determined by your storage backend | ## Search with Mem0 Platform ```python Python theme={null} from mem0 import MemoryClient client = MemoryClient(api_key="your-api-key") query = "What do you know about me?" filters = { "OR": [ {"user_id": "alice"}, {"agent_id": {"in": ["travel-assistant", "customer-support"]}} ] } results = client.search(query, filters=filters) ``` ```javascript JavaScript theme={null} import { MemoryClient } from "mem0ai"; const client = new MemoryClient({apiKey: "your-api-key"}); const query = "I'm craving some pizza. Any recommendations?"; const filters = { AND: [ { user_id: "alice" } ] }; const results = await client.search(query, { filters }); ``` ## Search with Mem0 Open Source ```python Python theme={null} from mem0 import Memory m = Memory() # Simple search related_memories = m.search("Should I drink coffee or tea?", user_id="alice") # Search with filters memories = m.search( "food preferences", user_id="alice", filters={"categories": {"contains": "diet"}} ) ``` ```javascript JavaScript theme={null} import { Memory } from 'mem0ai/oss'; const memory = new Memory(); // Simple search const relatedMemories = memory.search("Should I drink coffee or tea?", { userId: "alice" }); // Search with filters (if supported) const memories = memory.search("food preferences", { userId: "alice", filters: { categories: { contains: "diet" } } }); ``` Expect an array of memory documents. Platform responses include vectors, metadata, and timestamps; OSS returns your stored schema. ## Filter patterns Filters help narrow down search results. Common use cases: **Filter by Session Context:** *Platform API:* ```python theme={null} # Get memories from a specific agent session client.search("query", filters={ "AND": [ {"user_id": "alice"}, {"agent_id": "chatbot"}, {"run_id": "session-123"} ] }) ``` *OSS:* ```python theme={null} # Get memories from a specific agent session m.search("query", user_id="alice", agent_id="chatbot", run_id="session-123") ``` **Filter by Date Range:** ```python theme={null} # Platform only - date filtering client.search("recent memories", filters={ "AND": [ {"user_id": "alice"}, {"created_at": {"gte": "2024-07-01"}} ] }) ``` **Filter by Categories:** ```python theme={null} # Platform only - category filtering client.search("preferences", filters={ "AND": [ {"user_id": "alice"}, {"categories": {"contains": "food"}} ] }) ``` ## Tips for better search * **Use natural language**: Mem0 understands intent, so describe what you're looking for naturally * **Scope with user ID**: Always provide `user_id` to scope search to relevant memories * **Platform API**: Use `filters={"user_id": "alice"}` * **OSS**: Use `user_id="alice"` as parameter * **Combine filters**: Use AND/OR logic to create precise queries (Platform) * **Consider wildcard filters**: Use wildcard filters (e.g., `run_id: "*"`) for broader matches * **Tune parameters**: Adjust `top_k` for result count, `threshold` for relevance cutoff * **Enable reranking**: Use `rerank=True` (default) when you have a reranker configured ### More Details For the full list of filter logic, comparison operators, and optional search parameters, see the [Search Memory API Reference](/api-reference/memory/search-memories). ## Put it into practice * Revisit the Add Memory guide to ensure you capture the context you expect to retrieve. * Configure rerankers and filters in Advanced Retrieval for higher precision. ## See it live * Support Inbox with Mem0 demonstrates scoped search with rerankers. * Tavily Search with Mem0 shows hybrid search in action. # Update Memory Source: https://docs.mem0.ai/core-concepts/memory-operations/update Modify an existing memory by updating its content or metadata. # Keep Memories Accurate with Update Mem0’s update operation lets you fix or enrich an existing memory without deleting it. When a user changes their preference or clarifies a fact, use update to keep the knowledge base fresh. **Why it matters** * Corrects outdated or incorrect memories immediately. * Adds new metadata so filters and rerankers stay sharp. * Works for both one-off edits and large batches (up to 1000 memories). ## Key terms * **memory\_id** – Unique identifier returned by `add` or `search` results. * **text** / **data** – New content that replaces the stored memory value. * **metadata** – Optional key-value pairs you update alongside the text. * **batch\_update** – Platform API that edits multiple memories in a single request. * **immutable** – Flagged memories that must be deleted and re-added instead of updated. ## How the update flow works Use `search` or dashboard inspection to capture the `memory_id` you want to change. Call `update` (or `batch_update`) with new text and optional metadata. Mem0 overwrites the stored value and adjusts indexes. Check the response or re-run `search` to ensure the revised memory appears with the new content. ## Single memory update (Platform) ```python Python theme={null} from mem0 import MemoryClient client = MemoryClient(api_key="your-api-key") memory_id = "your_memory_id" client.update( memory_id=memory_id, text="Updated memory content about the user", metadata={"category": "profile-update"} ) ``` ```javascript JavaScript theme={null} import MemoryClient from 'mem0ai'; const client = new MemoryClient({ apiKey: "your-api-key" }); const memory_id = "your_memory_id"; await client.update(memory_id, { text: "Updated memory content about the user", metadata: { category: "profile-update" } }); ``` Expect a confirmation message and the updated memory to appear in the dashboard almost instantly. ## Batch update (Platform) Update up to 1000 memories in one call. ```python Python theme={null} from mem0 import MemoryClient client = MemoryClient(api_key="your-api-key") update_memories = [ {"memory_id": "id1", "text": "Watches football"}, {"memory_id": "id2", "text": "Likes to travel"} ] response = client.batch_update(update_memories) print(response) ``` ```javascript JavaScript theme={null} import MemoryClient from 'mem0ai'; const client = new MemoryClient({ apiKey: "your-api-key" }); const updateMemories = [ { memoryId: "id1", text: "Watches football" }, { memoryId: "id2", text: "Likes to travel" } ]; client.batchUpdate(updateMemories) .then(response => console.log('Batch update response:', response)) .catch(error => console.error(error)); ``` ## Update with Mem0 OSS ```python Python theme={null} from mem0 import Memory memory = Memory() memory.update( memory_id="mem_123", data="Alex now prefers decaf coffee", ) ``` ``` ``` OSS JavaScript SDK does not expose `update` yet—use the REST API or Python SDK when self-hosting. ## Tips * Update both `text` **and** `metadata` together to keep filters accurate. * Batch updates are ideal after large imports or when syncing CRM corrections. * Immutable memories must be deleted and re-added instead of updated. * Pair updates with feedback signals (thumbs up/down) to self-heal memories automatically. ## Managed vs OSS differences | Capability | Mem0 Platform | Mem0 OSS | | -------------------- | ------------------------------------------- | ------------------------------------ | | Update call | `client.update(memory_id, {...})` | `memory.update(memory_id, data=...)` | | Batch updates | `client.batch_update` (up to 1000 memories) | Script your own loop or bulk job | | Dashboard visibility | Inspect updates in the UI | Inspect via logs or custom tooling | | Immutable handling | Returns descriptive error | Raises exception—delete and re-add | ## Put it into practice * Review the Update Memory API reference for request/response details. * Combine updates with Feedback Mechanism to automate corrections. ## See it live * Support Inbox with Mem0 uses updates to refine customer profiles. * AI Tutor with Mem0 demonstrates user preference corrections mid-course. # Memory Types Source: https://docs.mem0.ai/core-concepts/memory-types See how Mem0 layers conversation, session, and user memories to keep agents contextual. # How Mem0 Organizes Memory Mem0 separates memory into layers so agents remember the right detail at the right time. Think of it like a notebook: a sticky note for the current task, a daily journal for the session, and an archive for everything a user has shared. **Why it matters** * Keeps conversations coherent without repeating instructions. * Lets agents personalize responses based on long-term preferences. * Avoids over-fetching data by scoping memory to the correct layer. ## Key terms * **Conversation memory** – In-flight messages inside a single turn (what was just said). * **Session memory** – Short-lived facts that apply for the current task or channel. * **User memory** – Long-lived knowledge tied to a person, account, or workspace. * **Organizational memory** – Shared context available to multiple agents or teams. ```mermaid theme={null} graph LR A[Conversation turn] --> B[Session memory] B --> C[User memory] C --> D[Org memory] C --> E[Mem0 retrieval layer] ``` ## Short-term vs long-term memory Short-term memory keeps the current conversation coherent. It includes: * **Conversation history** – recent turns in order so the agent remembers what was just said. * **Working memory** – temporary state such as tool outputs or intermediate calculations. * **Attention context** – the immediate focus of the assistant, similar to what a person holds in mind mid-sentence. Long-term memory preserves knowledge across sessions. It captures: * **Factual memory** – user preferences, account details, and domain facts. * **Episodic memory** – summaries of past interactions or completed tasks. * **Semantic memory** – relationships between concepts so agents can reason about them later. Mem0 maps these classic categories onto its layered storage so you can decide what should fade quickly versus what should last for months. ## How does it work? Mem0 stores each layer separately and merges them when you query: 1. **Capture** – Messages enter the conversation layer while the turn is active. 2. **Promote** – Relevant details persist to session or user memory based on your `user_id`, `session_id`, and metadata. 3. **Retrieve** – The search pipeline pulls from all layers, ranking user memories first, then session notes, then raw history. ```python theme={null} import os from mem0 import Memory memory = Memory(api_key=os.environ["MEM0_API_KEY"]) # Sticky note: conversation memory memory.add( ["I'm Alex and I prefer boutique hotels."], user_id="alex", session_id="trip-planning-2025", ) # Later in the session, pull long-term + session context results = memory.search( "Any hotel preferences?", user_id="alex", session_id="trip-planning-2025", ) ``` Use `session_id` when you want short-term context to expire automatically; rely on `user_id` for lasting personalization. ## When should you use each layer? * **Conversation memory** – Tool calls or chain-of-thought that only matter within the current turn. * **Session memory** – Multi-step tasks (onboarding flows, debugging sessions) that should reset once complete. * **User memory** – Personal preferences, account state, or compliance details that must persist across interactions. * **Organizational memory** – Shared FAQs, product catalogs, or policies that every agent should recall. ## How it compares | Layer | Lifetime | Short or long term | Best for | Trade-offs | | ------------ | ------------------- | ------------------ | --------------------- | ---------------------------- | | Conversation | Single response | Short-term | Tool execution detail | Lost after the turn finishes | | Session | Minutes to hours | Short-term | Multi-step flows | Clear it manually when done | | User | Weeks to forever | Long-term | Personalization | Requires consent/governance | | Org | Configured globally | Long-term | Shared knowledge | Needs owner to keep current | Avoid storing secrets or unredacted PII in user or org memories—Mem0 is retrievable by design. Encrypt or hash sensitive values first. ## Put it into practice * Use the Add Memory guide to persist user preferences. * Follow Advanced Memory Operations to tune metadata and graph writes. ## See it live * AI Tutor with Mem0 shows session vs user memories in action. * Support Inbox with Mem0 demonstrates shared org memory. # Welcome to Mem0 Source: https://docs.mem0.ai/introduction Memory layer for AI agents

Build with mem0

Universal, Self-improving memory layer for LLM applications.

Write your first memory

Mem0 Products

Developer Resources

# API Reference Changes Source: https://docs.mem0.ai/migration/api-changes Complete API changes between v0.x and v1.0.0 Beta ## Overview This page documents all API changes between Mem0 v0.x and v1.0.0 Beta, organized by component and method. ## Memory Class Changes ### Constructor #### v0.x ```python theme={null} from mem0 import Memory # Basic initialization m = Memory() # With configuration config = { "version": "v1.0", # Supported in v0.x "vector_store": {...} } m = Memory.from_config(config) ``` #### v1.0.0 ```python theme={null} from mem0 import Memory # Basic initialization (same) m = Memory() # With configuration config = { "version": "v1.1", # v1.1+ only "vector_store": {...}, # New optional features "reranker": { "provider": "cohere", "config": {...} } } m = Memory.from_config(config) ``` ### add() Method #### v0.x Signature ```python theme={null} def add( self, messages, user_id: str = None, agent_id: str = None, run_id: str = None, metadata: dict = None, filters: dict = None, output_format: str = None, # ❌ REMOVED version: str = None # ❌ REMOVED ) -> Union[List[dict], dict] ``` #### v1.0.0 Signature ```python theme={null} def add( self, messages, user_id: str = None, agent_id: str = None, run_id: str = None, metadata: dict = None, filters: dict = None, infer: bool = True # ✅ NEW: Control memory inference ) -> dict # Always returns dict with "results" key ``` #### Changes Summary | Parameter | v0.x | v1.0.0 | Change | | --------------- | ---- | ------ | ----------- | | `messages` | ✅ | ✅ | Unchanged | | `user_id` | ✅ | ✅ | Unchanged | | `agent_id` | ✅ | ✅ | Unchanged | | `run_id` | ✅ | ✅ | Unchanged | | `metadata` | ✅ | ✅ | Unchanged | | `filters` | ✅ | ✅ | Unchanged | | `output_format` | ✅ | ❌ | **REMOVED** | | `version` | ✅ | ❌ | **REMOVED** | | `infer` | ❌ | ✅ | **NEW** | #### Response Format Changes **v0.x Response (variable format):** ```python theme={null} # With output_format="v1.0" [ { "id": "mem_123", "memory": "User loves pizza", "event": "ADD" } ] # With output_format="v1.1" { "results": [ { "id": "mem_123", "memory": "User loves pizza", "event": "ADD" } ] } ``` **v1.0.0 Response (standardized):** ```python theme={null} # Always returns this format { "results": [ { "id": "mem_123", "memory": "User loves pizza", "metadata": {...}, "event": "ADD" } ] } ``` ### search() Method #### v0.x Signature ```python theme={null} def search( self, query: str, user_id: str = None, agent_id: str = None, run_id: str = None, limit: int = 100, filters: dict = None, # Basic key-value only output_format: str = None, # ❌ REMOVED version: str = None # ❌ REMOVED ) -> Union[List[dict], dict] ``` #### v1.0.0 Signature ```python theme={null} def search( self, query: str, user_id: str = None, agent_id: str = None, run_id: str = None, limit: int = 100, filters: dict = None, # ✅ ENHANCED: Advanced operators rerank: bool = True # ✅ NEW: Reranking support ) -> dict # Always returns dict with "results" key ``` #### Enhanced Filtering **v0.x Filters (basic):** ```python theme={null} # Simple key-value filtering only filters = { "category": "food", "user_id": "alice" } ``` **v1.0.0 Filters (enhanced):** ```python theme={null} # Advanced filtering with operators filters = { "AND": [ {"category": "food"}, {"score": {"gte": 0.8}}, { "OR": [ {"priority": "high"}, {"urgent": True} ] } ] } # Comparison operators filters = { "score": {"gt": 0.5}, # Greater than "priority": {"gte": 5}, # Greater than or equal "rating": {"lt": 3}, # Less than "confidence": {"lte": 0.9}, # Less than or equal "status": {"eq": "active"}, # Equal "archived": {"ne": True}, # Not equal "tags": {"in": ["work", "personal"]}, # In list "category": {"nin": ["spam", "deleted"]} # Not in list } ``` ### get\_all() Method #### v0.x Signature ```python theme={null} def get_all( self, user_id: str = None, agent_id: str = None, run_id: str = None, filters: dict = None, output_format: str = None, # ❌ REMOVED version: str = None # ❌ REMOVED ) -> Union[List[dict], dict] ``` #### v1.0.0 Signature ```python theme={null} def get_all( self, user_id: str = None, agent_id: str = None, run_id: str = None, filters: dict = None # ✅ ENHANCED: Advanced operators ) -> dict # Always returns dict with "results" key ``` ### update() Method #### No Breaking Changes ```python theme={null} # Same signature in both versions def update( self, memory_id: str, data: str ) -> dict ``` ### delete() Method #### No Breaking Changes ```python theme={null} # Same signature in both versions def delete( self, memory_id: str ) -> dict ``` ### delete\_all() Method #### No Breaking Changes ```python theme={null} # Same signature in both versions def delete_all( self, user_id: str ) -> dict ``` ## Platform Client (MemoryClient) Changes ### async\_mode Default Changed #### v0.x ```python theme={null} from mem0 import MemoryClient client = MemoryClient(api_key="your-key") # async_mode had to be explicitly set or had different default result = client.add("content", user_id="alice", async_mode=True) ``` #### v1.0.0 ```python theme={null} from mem0 import MemoryClient client = MemoryClient(api_key="your-key") # async_mode defaults to True now (better performance) result = client.add("content", user_id="alice") # Uses async_mode=True by default # Can still override if needed result = client.add("content", user_id="alice", async_mode=False) ``` ## Configuration Changes ### Memory Configuration #### v0.x Config Options ```python theme={null} config = { "vector_store": {...}, "llm": {...}, "embedder": {...}, "graph_store": {...}, "version": "v1.0", # ❌ v1.0 no longer supported "history_db_path": "...", "custom_fact_extraction_prompt": "..." } ``` #### v1.0.0 Config Options ```python theme={null} config = { "vector_store": {...}, "llm": {...}, "embedder": {...}, "graph_store": {...}, "reranker": { # ✅ NEW: Reranker support "provider": "cohere", "config": {...} }, "version": "v1.1", # ✅ v1.1+ only "history_db_path": "...", "custom_fact_extraction_prompt": "...", "custom_update_memory_prompt": "..." # ✅ NEW: Custom update prompt } ``` ### New Configuration Options #### Reranker Configuration ```python theme={null} # Cohere reranker "reranker": { "provider": "cohere", "config": { "model": "rerank-english-v3.0", "api_key": "your-api-key", "top_k": 10 } } # Sentence Transformer reranker "reranker": { "provider": "sentence_transformer", "config": { "model": "cross-encoder/ms-marco-MiniLM-L-6-v2", "device": "cuda" } } # Hugging Face reranker "reranker": { "provider": "huggingface", "config": { "model": "BAAI/bge-reranker-base", "device": "cuda" } } # LLM-based reranker "reranker": { "provider": "llm_reranker", "config": { "llm": { "provider": "openai", "config": { "model": "gpt-4", "api_key": "your-api-key" } } } } ``` ## Error Handling Changes ### New Error Types #### v0.x Errors ```python theme={null} # Generic exceptions try: result = m.add("content", user_id="alice", version="v1.0") except Exception as e: print(f"Error: {e}") ``` #### v1.0.0 Errors ```python theme={null} # More specific error handling try: result = m.add("content", user_id="alice") except ValueError as e: if "v1.0 API format is no longer supported" in str(e): # Handle version compatibility error pass elif "Invalid filter operator" in str(e): # Handle filter syntax error pass except TypeError as e: # Handle parameter errors pass except Exception as e: # Handle unexpected errors pass ``` ### Validation Changes #### Stricter Parameter Validation **v0.x (Lenient):** ```python theme={null} # Unknown parameters might be ignored result = m.add("content", user_id="alice", unknown_param="value") ``` **v1.0.0 (Strict):** ```python theme={null} # Unknown parameters raise TypeError try: result = m.add("content", user_id="alice", unknown_param="value") except TypeError as e: print(f"Invalid parameter: {e}") ``` ## Response Schema Changes ### Memory Object Schema #### v0.x Schema ```python theme={null} { "id": "mem_123", "memory": "User loves pizza", "user_id": "alice", "metadata": {...}, "created_at": "2024-01-01T00:00:00Z", "updated_at": "2024-01-01T00:00:00Z", "score": 0.95 # In search results } ``` #### v1.0.0 Schema (Enhanced) ```python theme={null} { "id": "mem_123", "memory": "User loves pizza", "user_id": "alice", "agent_id": "assistant", # ✅ More context "run_id": "session_001", # ✅ More context "metadata": {...}, "categories": ["food"], # ✅ NEW: Auto-categorization "immutable": false, # ✅ NEW: Immutability flag "created_at": "2024-01-01T00:00:00Z", "updated_at": "2024-01-01T00:00:00Z", "score": 0.95, # In search results "rerank_score": 0.98 # ✅ NEW: If reranking used } ``` ## Migration Code Examples ### Simple Migration #### Before (v0.x) ```python theme={null} from mem0 import Memory m = Memory() # Add with deprecated parameters result = m.add( "I love pizza", user_id="alice", output_format="v1.1", version="v1.0" ) # Handle variable response format if isinstance(result, list): memories = result else: memories = result.get("results", []) for memory in memories: print(memory["memory"]) ``` #### After (v1.0.0 ) ```python theme={null} from mem0 import Memory m = Memory() # Add without deprecated parameters result = m.add( "I love pizza", user_id="alice" ) # Always dict format with "results" key for memory in result["results"]: print(memory["memory"]) ``` ### Advanced Migration #### Before (v0.x) ```python theme={null} # Basic filtering results = m.search( "food preferences", user_id="alice", filters={"category": "food"}, output_format="v1.1" ) ``` #### After (v1.0.0 ) ```python theme={null} # Enhanced filtering with reranking results = m.search( "food preferences", user_id="alice", filters={ "AND": [ {"category": "food"}, {"score": {"gte": 0.8}} ] }, rerank=True ) ``` ## Summary | Component | v0.x | v1.0.0 | Status | | ------------------ | ----------------- | ------------------------------ | ------------- | | `add()` method | Variable response | Standardized response | ⚠️ Breaking | | `search()` method | Basic filtering | Enhanced filtering + reranking | ⚠️ Breaking | | `get_all()` method | Variable response | Standardized response | ⚠️ Breaking | | Response format | Variable | Always `{"results": [...]}` | ⚠️ Breaking | | Reranking | ❌ Not available | ✅ Full support | ✅ New feature | | Advanced filtering | ❌ Basic only | ✅ Full operators | ✅ Enhancement | | Error handling | Generic | Specific error types | ✅ Improvement | Use this reference to systematically update your codebase. Test each change thoroughly before deploying to production. # Breaking Changes in v1.0.0 Source: https://docs.mem0.ai/migration/breaking-changes Complete list of breaking changes when upgrading from v0.x to v1.0.0 **Important:** This page lists all breaking changes. Please review carefully before upgrading. ## API Version Changes ### Removed v1.0 API Support **Breaking Change:** The v1.0 API format is completely removed and no longer supported. #### Before (v0.x) ```python theme={null} # This was supported in v0.x config = { "version": "v1.0" # ❌ No longer supported } result = m.add( "memory content", user_id="alice" ) ``` #### After (v1.0.0 ) ```python theme={null} # v1.1 is the minimum supported version config = { "version": "v1.1" # ✅ Required minimum } result = m.add( "memory content", user_id="alice" ) ``` **Error Message:** ``` ValueError: The v1.0 API format is no longer supported in mem0ai 1.0.0+. Please use v1.1 format which returns a dict with 'results' key. ``` ## Parameter Removals ### 1. version Parameter in Method Calls **Breaking Change:** Version parameter removed from method calls. #### Before (v0.x) ```python theme={null} result = m.add("content", user_id="alice", version="v1.0") ``` #### After (v1.0.0 ) ```python theme={null} result = m.add("content", user_id="alice") ``` ### 2. async\_mode Parameter (Platform Client) **Change:** For `MemoryClient` (Platform API), `async_mode` now defaults to `True` but can still be configured. #### Before (v0.x) ```python theme={null} from mem0 import MemoryClient client = MemoryClient(api_key="your-key") result = client.add("content", user_id="alice", async_mode=True) result = client.add("content", user_id="alice", async_mode=False) ``` #### After (v1.0.0 ) ```python theme={null} from mem0 import MemoryClient client = MemoryClient(api_key="your-key") # async_mode now defaults to True, but you can still override it result = client.add("content", user_id="alice") # Uses async_mode=True by default # You can still explicitly set it to False if needed result = client.add("content", user_id="alice", async_mode=False) ``` ## Response Format Changes ### Standardized Response Structure **Breaking Change:** All responses now return a standardized dictionary format. #### Before (v0.x) ```python theme={null} # Could return different formats based on version configuration result = m.add("content", user_id="alice") # With v1.0: Returns [{"id": "...", "memory": "...", "event": "ADD"}] # With v1.1: Returns {"results": [{"id": "...", "memory": "...", "event": "ADD"}]} ``` #### After (v1.0.0 ) ```python theme={null} # Always returns standardized format result = m.add("content", user_id="alice") # Always returns: {"results": [{"id": "...", "memory": "...", "event": "ADD"}]} # Access results consistently for memory in result["results"]: print(memory["memory"]) ``` ## Configuration Changes ### Version Configuration **Breaking Change:** Default API version changed. #### Before (v0.x) ```python theme={null} # v1.0 was supported config = { "version": "v1.0" # ❌ No longer supported } ``` #### After (v1.0.0 ) ```python theme={null} # v1.1 is minimum, v1.1 is default config = { "version": "v1.1" # ✅ Minimum supported } # Or omit for default config = { # version defaults to v1.1 } ``` ### Memory Configuration **Breaking Change:** Some configuration options have changed defaults. #### Before (v0.x) ```python theme={null} from mem0 import Memory # Default configuration in v0.x m = Memory() # Used default settings suitable for v0.x ``` #### After (v1.0.0 ) ```python theme={null} from mem0 import Memory # Default configuration optimized for v1.0.0 m = Memory() # Uses v1.1+ optimized defaults # Explicit configuration recommended config = { "version": "v1.1", "vector_store": { "provider": "qdrant", "config": { "host": "localhost", "port": 6333 } } } m = Memory.from_config(config) ``` ## Method Signature Changes ### Search Method **Enhanced but backward compatible:** #### Before (v0.x) ```python theme={null} results = m.search( "query", user_id="alice", filters={"key": "value"} # Simple key-value only ) ``` #### After (v1.0.0 ) ```python theme={null} # Basic usage remains the same results = m.search("query", user_id="alice") # Enhanced filtering available (optional) results = m.search( "query", user_id="alice", filters={ "AND": [ {"key": "value"}, {"score": {"gte": 0.8}} ] }, rerank=True # New parameter ) ``` ## Error Handling Changes ### New Error Types **Breaking Change:** More specific error types and messages. #### Before (v0.x) ```python theme={null} try: result = m.add("content", user_id="alice", version="v1.0") except Exception as e: print(f"Generic error: {e}") ``` #### After (v1.0.0 ) ```python theme={null} try: result = m.add("content", user_id="alice") except ValueError as e: if "v1.0 API format is no longer supported" in str(e): # Handle version error specifically print("Please upgrade your code to use v1.1+ format") else: print(f"Value error: {e}") except Exception as e: print(f"Unexpected error: {e}") ``` ### Validation Changes **Breaking Change:** Stricter parameter validation. #### Before (v0.x) ```python theme={null} # Some invalid parameters might have been ignored result = m.add( "content", user_id="alice", invalid_param="ignored" # Might have been silently ignored ) ``` #### After (v1.0.0 ) ```python theme={null} # Strict validation - unknown parameters cause errors try: result = m.add( "content", user_id="alice", invalid_param="value" # ❌ Will raise TypeError ) except TypeError as e: print(f"Invalid parameter: {e}") ``` ## Import Changes ### No Breaking Changes in Imports **Good News:** Import statements remain the same. ```python theme={null} # These imports work in both v0.x and v1.0.0 from mem0 import Memory, AsyncMemory from mem0 import MemoryConfig ``` ## Dependency Changes ### Minimum Python Version **Potential Breaking Change:** Check Python version requirements. #### Before (v0.x) * Python 3.8+ supported #### After (v1.0.0 ) * Python 3.9+ required (check current requirements) ### Package Dependencies **Breaking Change:** Some dependencies updated with potential breaking changes. ```bash theme={null} # Check for conflicts after upgrade pip install --upgrade mem0ai pip check # Verify no dependency conflicts ``` ## Data Migration ### Database Schema **Good News:** No database schema changes required. * Existing memories remain compatible * No data migration required * Vector store data unchanged ### Memory Format **Good News:** Memory storage format unchanged. * Existing memories work with v1.0.0 * Search continues to work with old memories * No re-indexing required ## Testing Changes ### Test Updates Required **Breaking Change:** Update tests for new response format. #### Before (v0.x) ```python theme={null} def test_add_memory(): result = m.add("content", user_id="alice") assert isinstance(result, list) # ❌ No longer true assert len(result) > 0 ``` #### After (v1.0.0 ) ```python theme={null} def test_add_memory(): result = m.add("content", user_id="alice") assert isinstance(result, dict) # ✅ Always dict assert "results" in result # ✅ Always has results key assert len(result["results"]) > 0 ``` ## Rollback Considerations ### Safe Rollback Process If you need to rollback: ```bash theme={null} # 1. Rollback package pip install mem0ai==0.1.20 # Last stable v0.x # 2. Revert code changes git checkout previous_commit # 3. Test functionality python test_mem0_functionality.py ``` ### Data Safety * **Safe:** Memories stored in v0.x format work with v1.0.0 * **Safe:** Rollback doesn't lose data * **Safe:** Vector store data remains intact ## Next Steps 1. **Review all breaking changes** in your codebase 2. **Update method calls** to remove deprecated parameters 3. **Update response handling** to use standardized format 4. **Test thoroughly** with your existing data 5. **Update error handling** for new error types Step-by-step migration instructions Complete API reference changes **Need Help?** If you encounter issues during migration, check our [GitHub Discussions](https://github.com/mem0ai/mem0/discussions) or community support channels. # Migrating from v0.x to v1.0.0 Source: https://docs.mem0.ai/migration/v0-to-v1 Complete guide to upgrade your Mem0 implementation to version 1.0.0 **Breaking Changes Ahead!** Mem0 1.0.0 introduces several breaking changes. Please read this guide carefully before upgrading. ## Overview Mem0 1.0.0 is a major release that modernizes the API, improves performance, and adds powerful new features. This guide will help you migrate your existing v0.x implementation to the new version. ## Key Changes Summary | Feature | v0.x | v1.0.0 | Migration Required | | ---------------------------- | --------------- | -------------------------------- | ------------------ | | API Version | v1.0 supported | v1.0 **removed**, v1.1+ only | ✅ Yes | | Async Mode (Platform Client) | Optional/manual | Defaults to `True`, configurable | ⚠️ Partial | | Metadata Filtering | Basic | Enhanced with operators | ⚠️ Optional | | Reranking | Not available | Full support | ⚠️ Optional | ## Step-by-Step Migration ### 1. Update Installation ```bash theme={null} # Update to the latest version pip install --upgrade mem0ai ``` ### 2. Remove Deprecated Parameters #### Before (v0.x) ```python theme={null} from mem0 import Memory # These parameters are no longer supported m = Memory() result = m.add( "I love pizza", user_id="alice", version="v1.0" # ❌ REMOVED ) ``` #### After (v1.0.0 ) ```python theme={null} from mem0 import Memory # Clean, simplified API m = Memory() result = m.add( "I love pizza", user_id="alice" # version parameter removed ) ``` ### 3. Update Configuration #### Before (v0.x) ```python theme={null} config = { "vector_store": { "provider": "qdrant", "config": { "host": "localhost", "port": 6333 } }, "version": "v1.0" # ❌ No longer supported } m = Memory.from_config(config) ``` #### After (v1.0.0 ) ```python theme={null} config = { "vector_store": { "provider": "qdrant", "config": { "host": "localhost", "port": 6333 } }, "version": "v1.1" # ✅ v1.1 is the minimum supported version } m = Memory.from_config(config) ``` ### 4. Handle Response Format Changes #### Before (v0.x) ```python theme={null} # Response could be a list or dict depending on version result = m.add("I love coffee", user_id="alice") if isinstance(result, list): # Handle list format for item in result: print(item["memory"]) else: # Handle dict format print(result["results"]) ``` #### After (v1.0.0 ) ```python theme={null} # Response is always a standardized dict with "results" key result = m.add("I love coffee", user_id="alice") # Always access via "results" key for item in result["results"]: print(item["memory"]) ``` ### 5. Update Search Operations #### Before (v0.x) ```python theme={null} # Basic search results = m.search("What do I like?", user_id="alice") # With filters results = m.search( "What do I like?", user_id="alice", filters={"category": "food"} ) ``` #### After (v1.0.0 ) ```python theme={null} # Same basic search API results = m.search("What do I like?", user_id="alice") # Enhanced filtering with operators (optional upgrade) results = m.search( "What do I like?", user_id="alice", filters={ "AND": [ {"category": "food"}, {"rating": {"gte": 8}} ] } ) # New: Reranking support (optional) results = m.search( "What do I like?", user_id="alice", rerank=True # Requires reranker configuration ) ``` ### 6. Platform Client async\_mode Default Changed **Change:** For `MemoryClient`, the `async_mode` parameter now defaults to `True` for better performance. #### Before (v0.x) ```python theme={null} from mem0 import MemoryClient client = MemoryClient(api_key="your-key") # Had to explicitly set async_mode result = client.add("I enjoy hiking", user_id="alice", async_mode=True) ``` #### After (v1.0.0 ) ```python theme={null} from mem0 import MemoryClient client = MemoryClient(api_key="your-key") # async_mode now defaults to True (best performance) result = client.add("I enjoy hiking", user_id="alice") # You can still override if needed for synchronous processing result = client.add("I enjoy hiking", user_id="alice", async_mode=False) ``` ## Configuration Migration ### Basic Configuration #### Before (v0.x) ```python theme={null} config = { "vector_store": { "provider": "qdrant", "config": { "host": "localhost", "port": 6333 } }, "llm": { "provider": "openai", "config": { "model": "gpt-3.5-turbo", "api_key": "your-key" } }, "version": "v1.0" } ``` #### After (v1.0.0 ) ```python theme={null} config = { "vector_store": { "provider": "qdrant", "config": { "host": "localhost", "port": 6333 } }, "llm": { "provider": "openai", "config": { "model": "gpt-3.5-turbo", "api_key": "your-key" } }, "version": "v1.1", # Minimum supported version # New optional features "reranker": { "provider": "cohere", "config": { "model": "rerank-english-v3.0", "api_key": "your-cohere-key" } } } ``` ### Enhanced Features (Optional) ```python theme={null} # Take advantage of new features config = { "vector_store": { "provider": "qdrant", "config": { "host": "localhost", "port": 6333 } }, "llm": { "provider": "openai", "config": { "model": "gpt-4", "api_key": "your-key" } }, "embedder": { "provider": "openai", "config": { "model": "text-embedding-3-small", "api_key": "your-key" } }, "reranker": { "provider": "sentence_transformer", "config": { "model": "cross-encoder/ms-marco-MiniLM-L-6-v2" } }, "version": "v1.1" } ``` ## Error Handling Migration ### Before (v0.x) ```python theme={null} try: result = m.add("memory", user_id="alice", version="v1.0") except Exception as e: print(f"Error: {e}") ``` ### After (v1.0.0 ) ```python theme={null} try: result = m.add("memory", user_id="alice") except ValueError as e: if "v1.0 API format is no longer supported" in str(e): print("Please upgrade your code to use v1.1+ format") else: print(f"Error: {e}") except Exception as e: print(f"Unexpected error: {e}") ``` ## Testing Your Migration ### 1. Basic Functionality Test ```python theme={null} def test_basic_functionality(): m = Memory() # Test add result = m.add("I love testing", user_id="test_user") assert "results" in result assert len(result["results"]) > 0 # Test search search_results = m.search("testing", user_id="test_user") assert "results" in search_results # Test get_all all_memories = m.get_all(user_id="test_user") assert "results" in all_memories print("✅ Basic functionality test passed") test_basic_functionality() ``` ### 2. Enhanced Features Test ```python theme={null} def test_enhanced_features(): config = { "reranker": { "provider": "sentence_transformer", "config": { "model": "cross-encoder/ms-marco-MiniLM-L-6-v2" } } } m = Memory.from_config(config) # Test reranking m.add("I love advanced features", user_id="test_user") results = m.search("features", user_id="test_user", rerank=True) assert "results" in results # Test enhanced filtering results = m.search( "features", user_id="test_user", filters={"user_id": {"eq": "test_user"}} ) assert "results" in results print("✅ Enhanced features test passed") test_enhanced_features() ``` ## Common Migration Issues ### Issue 1: Version Error **Error:** ``` ValueError: The v1.0 API format is no longer supported in mem0ai 1.0.0+ ``` **Solution:** ```python theme={null} # Remove version parameters or set to v1.1+ config = { # ... other config "version": "v1.1" # or remove entirely for default } ``` ### Issue 2: Response Format Error **Error:** ``` KeyError: 'results' ``` **Solution:** ```python theme={null} # Always access response via "results" key result = m.add("memory", user_id="alice") memories = result["results"] # Not result directly ``` ### Issue 3: Parameter Error **Error:** ``` TypeError: add() got an unexpected keyword argument 'output_format' ``` **Solution:** ```python theme={null} # Remove deprecated parameters result = m.add( "memory", user_id="alice" # Remove: version ) ``` ## Rollback Plan If you encounter issues during migration: ### 1. Immediate Rollback ```bash theme={null} # Downgrade to last v0.x version pip install mem0ai==0.1.20 # Replace with your last working version ``` ### 2. Gradual Migration ```python theme={null} # Test both versions side by side import mem0_v0 # Your old version import mem0 # New version def compare_results(query, user_id): old_results = mem0_v0.search(query, user_id=user_id) new_results = mem0.search(query, user_id=user_id) print("Old format:", old_results) print("New format:", new_results["results"]) ``` ## Performance Improvements ### Before (v0.x) ```python theme={null} # Sequential operations result1 = m.add("memory 1", user_id="alice") result2 = m.add("memory 2", user_id="alice") result3 = m.search("query", user_id="alice") ``` ### After (v1.0.0 ) ```python theme={null} # Better async performance async def batch_operations(): async_memory = AsyncMemory() # Concurrent operations results = await asyncio.gather( async_memory.add("memory 1", user_id="alice"), async_memory.add("memory 2", user_id="alice"), async_memory.search("query", user_id="alice") ) return results ``` ## Next Steps 1. **Complete the migration** using this guide 2. **Test thoroughly** with your existing data 3. **Explore new features** like enhanced filtering and reranking 4. **Update your documentation** to reflect the new API 5. **Monitor performance** and optimize as needed Detailed list of all breaking changes Complete API reference changes Need help with migration? Check our [GitHub Discussions](https://github.com/mem0ai/mem0/discussions) or reach out to our community for support. # Configure the OSS Stack Source: https://docs.mem0.ai/open-source/configuration Wire up Mem0 OSS with your preferred LLM, vector store, embedder, and reranker. # Configure Mem0 OSS Components **Prerequisites** * Python 3.10+ with `pip` available * Running vector database (e.g., Qdrant, Postgres + pgvector) or access credentials for a managed store * API keys for your chosen LLM, embedder, and reranker providers Start from the Python quickstart if you still need the base CLI and repository. ## Install dependencies ```bash theme={null} pip install mem0ai ``` ```bash theme={null} pip install qdrant-client openai ``` ```bash theme={null} git clone https://github.com/mem0ai/mem0.git cd mem0/examples/docker-compose ``` ```bash theme={null} pip install -r requirements.txt ``` ## Define your configuration ```python theme={null} from mem0 import Memory config = { "vector_store": { "provider": "qdrant", "config": {"host": "localhost", "port": 6333}, }, "llm": { "provider": "openai", "config": {"model": "gpt-4.1-mini", "temperature": 0.1}, }, "embedder": { "provider": "vertexai", "config": {"model": "textembedding-gecko@003"}, }, "reranker": { "provider": "cohere", "config": {"model": "rerank-english-v3.0"}, }, } memory = Memory.from_config(config) ``` ```bash theme={null} export QDRANT_API_KEY="..." export OPENAI_API_KEY="..." export COHERE_API_KEY="..." ``` ```yaml theme={null} vector_store: provider: qdrant config: host: localhost port: 6333 llm: provider: azure_openai config: api_key: ${AZURE_OPENAI_KEY} deployment_name: gpt-4.1-mini embedder: provider: ollama config: model: nomic-embed-text reranker: provider: zero_entropy config: api_key: ${ZERO_ENTROPY_KEY} ``` ```python theme={null} from mem0 import Memory memory = Memory.from_config_file("config.yaml") ``` Run `memory.add(["Remember my favorite cafe in Tokyo."], user_id="alex")` and then `memory.search("favorite cafe", user_id="alex")`. You should see the Qdrant collection populate and the reranker mark the memory as a top hit. ## Tune component settings Name collections explicitly in production (`collection_name`) to isolate tenants and enable per-tenant retention policies. Keep extraction temperatures ≤0.2 so advanced memories stay deterministic. Raise it only when you see missing facts. Limit `top_k` to 10–20 results; sending more adds latency without meaningful gains. Mixing managed and self-hosted components? Make sure every outbound provider call happens through a secure network path. Managed rerankers often require outbound internet even if your vector store is on-prem. ## Quick recovery * Qdrant connection errors → confirm port `6333` is exposed and API key (if set) matches. * Empty search results → verify the embedder model name; a mismatch causes dimension errors. * `Unknown reranker` → update the SDK (`pip install --upgrade mem0ai`) to load the latest provider registry. # Async Memory Source: https://docs.mem0.ai/open-source/features/async-memory Run Mem0 operations without blocking your event loop. `AsyncMemory` gives you a non-blocking interface to Mem0’s storage layer so Python applications can add, search, and manage memories directly from async code. Use it when you embed Mem0 inside FastAPI services, background workers, or any workflow that relies on `asyncio`. **You’ll use this when…** * Your agent already runs in an async framework and you need memory calls to await cleanly. * You want to embed Mem0’s storage locally without sending requests through the synchronous client. * You plan to mix memory operations with other async APIs (OpenAI, HTTP calls, databases). `AsyncMemory` expects a running event loop. Always call it inside `async def` functions or through helpers like `asyncio.run()` to avoid runtime errors. Working in TypeScript? The Node SDK still uses synchronous calls—use `Memory` there and rely on Python’s `AsyncMemory` when you need awaited operations. ## Feature anatomy * **Direct storage access:** `AsyncMemory` talks to the same backends as the synchronous client but keeps everything in-process for lower latency. * **Method parity:** Each memory operation (`add`, `search`, `get_all`, `delete`, etc.) mirrors the synchronous API, letting you reuse payload shapes. * **Concurrent execution:** Non-blocking I/O lets you schedule multiple memory tasks with `asyncio.gather`. * **Scoped organization:** Continue using `user_id`, `agent_id`, and `run_id` to separate memories across sessions and agents. | Operation | Async signature | Notes | | --------------- | ---------------------------------------------- | --------------------------------------------- | | Create memories | `await memory.add(...)` | Same arguments as synchronous `Memory.add`. | | Search memories | `await memory.search(...)` | Returns dict with `results`, identical shape. | | List memories | `await memory.get_all(...)` | Filter by `user_id`, `agent_id`, `run_id`. | | Retrieve memory | `await memory.get(memory_id=...)` | Raises `ValueError` if ID is invalid. | | Update memory | `await memory.update(memory_id=..., data=...)` | Accepts partial updates. | | Delete memory | `await memory.delete(memory_id=...)` | Returns confirmation payload. | | Delete in bulk | `await memory.delete_all(...)` | Requires at least one scope filter. | | History | `await memory.history(memory_id=...)` | Fetches change log for auditing. | *** ## Configure it ### Initialize the client ```python theme={null} import asyncio from mem0 import AsyncMemory # Default configuration memory = AsyncMemory() # Custom configuration from mem0.configs.base import MemoryConfig custom_config = MemoryConfig( # Your custom configuration here ) memory = AsyncMemory(config=custom_config) ``` Run `await memory.search(...)` once right after initialization. If it returns memories without errors, your configuration works. Keep configuration objects close to the async client so you can reuse them across workers without recreating vector store connections. ### Manage lifecycle and concurrency ```python theme={null} import asyncio from contextlib import asynccontextmanager from mem0 import AsyncMemory @asynccontextmanager async def get_memory(): memory = AsyncMemory() try: yield memory finally: # Clean up resources if needed pass async def safe_memory_usage(): async with get_memory() as memory: return await memory.search("test query", user_id="alice") ``` Wrap the client in an async context manager when you need a clean shutdown (for example, inside FastAPI startup/shutdown hooks). ```python theme={null} async def batch_operations(): memory = AsyncMemory() tasks = [ memory.add( messages=[{"role": "user", "content": f"Message {i}"}], user_id=f"user_{i}" ) for i in range(5) ] results = await asyncio.gather(*tasks, return_exceptions=True) for i, result in enumerate(results): if isinstance(result, Exception): print(f"Task {i} failed: {result}") else: print(f"Task {i} completed successfully") ``` When concurrency works correctly, successful tasks return memory IDs while failures surface as exceptions in the `results` list. ### Add resilience with retries ```python theme={null} import asyncio from mem0 import AsyncMemory async def with_timeout_and_retry(operation, max_retries=3, timeout=10.0): for attempt in range(max_retries): try: return await asyncio.wait_for(operation(), timeout=timeout) except asyncio.TimeoutError: print(f"Timeout on attempt {attempt + 1}") except Exception as exc: print(f"Error on attempt {attempt + 1}: {exc}") if attempt < max_retries - 1: await asyncio.sleep(2 ** attempt) raise Exception(f"Operation failed after {max_retries} attempts") async def robust_memory_search(): memory = AsyncMemory() async def search_operation(): return await memory.search("test query", user_id="alice") return await with_timeout_and_retry(search_operation) ``` Always cap retries—runaway loops can keep the event loop busy and block other tasks. *** ## See it in action ### Core operations ```python theme={null} # Create memories result = await memory.add( messages=[ {"role": "user", "content": "I'm travelling to SF"}, {"role": "assistant", "content": "That's great to hear!"} ], user_id="alice" ) # Search memories results = await memory.search( query="Where am I travelling?", user_id="alice" ) # List memories all_memories = await memory.get_all(user_id="alice") # Get a specific memory specific_memory = await memory.get(memory_id="memory-id-here") # Update a memory updated_memory = await memory.update( memory_id="memory-id-here", data="I'm travelling to Seattle" ) # Delete a memory await memory.delete(memory_id="memory-id-here") # Delete scoped memories await memory.delete_all(user_id="alice") ``` Confirm each call returns the same response fields as the synchronous client (IDs, `results`, or confirmation objects). Missing keys usually mean the coroutine wasn’t awaited. `delete_all` requires at least one of `user_id`, `agent_id`, or `run_id`. Provide all three to narrow deletion to a single session. ### Scoped organization ```python theme={null} await memory.add( messages=[{"role": "user", "content": "I prefer vegetarian food"}], user_id="alice", agent_id="diet-assistant", run_id="consultation-001" ) all_user_memories = await memory.get_all(user_id="alice") agent_memories = await memory.get_all(user_id="alice", agent_id="diet-assistant") session_memories = await memory.get_all(user_id="alice", run_id="consultation-001") specific_memories = await memory.get_all( user_id="alice", agent_id="diet-assistant", run_id="consultation-001" ) history = await memory.history(memory_id="memory-id-here") ``` Use `history` when you need audit trails for compliance or debugging update logic. ### Blend with other async APIs ```python theme={null} import asyncio from openai import AsyncOpenAI from mem0 import AsyncMemory async_openai_client = AsyncOpenAI() async_memory = AsyncMemory() async def chat_with_memories(message: str, user_id: str = "default_user") -> str: search_result = await async_memory.search(query=message, user_id=user_id, limit=3) relevant_memories = search_result["results"] memories_str = "\n".join(f"- {entry['memory']}" for entry in relevant_memories) system_prompt = ( "You are a helpful AI. Answer the question based on query and memories.\n" f"User Memories:\n{memories_str}" ) messages = [ {"role": "system", "content": system_prompt}, {"role": "user", "content": message}, ] response = await async_openai_client.chat.completions.create( model="gpt-4.1-nano-2025-04-14", messages=messages ) assistant_response = response.choices[0].message.content messages.append({"role": "assistant", "content": assistant_response}) await async_memory.add(messages, user_id=user_id) return assistant_response ``` When everything is wired correctly, the OpenAI response should incorporate recent memories and the follow-up `add` call should persist the new assistant turn. ### Handle errors gracefully ```python theme={null} from mem0 import AsyncMemory from mem0.configs.base import MemoryConfig async def handle_initialization_errors(): try: config = MemoryConfig( vector_store={"provider": "chroma", "config": {"path": "./chroma_db"}}, llm={"provider": "openai", "config": {"model": "gpt-4.1-nano-2025-04-14"}} ) AsyncMemory(config=config) print("AsyncMemory initialized successfully") except ValueError as err: print(f"Configuration error: {err}") except ConnectionError as err: print(f"Connection error: {err}") async def handle_memory_operation_errors(): memory = AsyncMemory() try: await memory.get(memory_id="non-existent-id") except ValueError as err: print(f"Invalid memory ID: {err}") try: await memory.search(query="", user_id="alice") except ValueError as err: print(f"Invalid search query: {err}") ``` Catch and log `ValueError` exceptions from invalid inputs—async stack traces can otherwise disappear inside background tasks. ### Serve through FastAPI ```python theme={null} from fastapi import FastAPI, HTTPException from mem0 import AsyncMemory app = FastAPI() memory = AsyncMemory() @app.post("/memories/") async def add_memory(messages: list, user_id: str): try: result = await memory.add(messages=messages, user_id=user_id) return {"status": "success", "data": result} except Exception as exc: raise HTTPException(status_code=500, detail=str(exc)) @app.get("/memories/search") async def search_memories(query: str, user_id: str, limit: int = 10): try: result = await memory.search(query=query, user_id=user_id, limit=limit) return {"status": "success", "data": result} except Exception as exc: raise HTTPException(status_code=500, detail=str(exc)) ``` Create one `AsyncMemory` instance per process when using FastAPI—startup hooks are a good place to configure and reuse it. ### Instrument logging ```python theme={null} import logging import time from functools import wraps logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) def log_async_operation(operation_name): def decorator(func): @wraps(func) async def wrapper(*args, **kwargs): start_time = time.time() logger.info(f"Starting {operation_name}") try: result = await func(*args, **kwargs) duration = time.time() - start_time logger.info(f"{operation_name} completed in {duration:.2f}s") return result except Exception as exc: duration = time.time() - start_time logger.error(f"{operation_name} failed after {duration:.2f}s: {exc}") raise return wrapper return decorator @log_async_operation("Memory Add") async def logged_memory_add(memory, messages, user_id): return await memory.add(messages=messages, user_id=user_id) ``` Logged durations give you the baseline needed to spot regressions once AsyncMemory is in production. *** ## Verify the feature is working * Run a quick add/search cycle and confirm the returned memory content matches your input. * Inspect application logs to ensure async tasks complete without blocking the event loop. * In FastAPI or other frameworks, hit health endpoints to verify the shared client handles concurrent requests. * Monitor retry counters—unexpected spikes indicate configuration or connectivity issues. *** ## Best practices 1. **Keep operations awaited:** Forgetting `await` is the fastest way to miss writes—lint for it or add helper wrappers. 2. **Scope deletions carefully:** Always supply `user_id`, `agent_id`, or `run_id` to avoid purging too much data. 3. **Batch writes thoughtfully:** Use `asyncio.gather` for throughput but cap concurrency based on backend capacity. 4. **Log errors with context:** Capture user and agent scopes to triage failures quickly. 5. **Reuse clients:** Instantiate `AsyncMemory` once per worker to avoid repeated backend handshakes. *** ## Troubleshooting | Issue | Possible causes | Fix | | -------------------- | ------------------------------------ | ----------------------------------------------------------- | | Initialization fails | Missing dependencies, invalid config | Validate `MemoryConfig` settings and environment variables. | | Slow operations | Large datasets, network latency | Cache heavy queries and tune vector store parameters. | | Memory not found | Invalid ID or deleted record | Check ID source and handle soft-deleted states. | | Connection timeouts | Network issues, overloaded backend | Apply retries/backoff and inspect infrastructure health. | | Out-of-memory errors | Oversized batches | Reduce concurrency or chunk operations into smaller sets. | *** Review how add, search, update, and delete behave across synchronous and async clients. Follow a full workflow that mixes AsyncMemory with OpenAI tool-call automation. # Custom Fact Extraction Prompt Source: https://docs.mem0.ai/open-source/features/custom-fact-extraction-prompt Tailor fact extraction so Mem0 stores only the details you care about. Custom fact extraction prompts let you decide exactly which facts Mem0 records from a conversation. Define a focused prompt, give a few examples, and Mem0 will add only the memories that match your use case. **You’ll use this when…** * A project needs domain-specific facts (order numbers, customer info) without storing casual chatter. * You already have a clear schema for memories and want the LLM to follow it. * You must prevent irrelevant details from entering long-term storage. Prompts that are too broad cause unrelated facts to slip through. Keep instructions tight and test them with real transcripts. *** ## Feature anatomy * **Prompt instructions:** Describe which entities or phrases to keep. Specific guidance keeps the extractor focused. * **Few-shot examples:** Show positive and negative cases so the model copies the right format. * **Structured output:** Responses return JSON with a `facts` array that Mem0 converts into individual memories. * **LLM configuration:** `custom_fact_extraction_prompt` (Python) or `customPrompt` (TypeScript) lives alongside your model settings. 1. State the allowed fact types. 2. Include short examples that mirror production messages. 3. Show both empty (`[]`) and populated outputs. 4. Remind the model to return JSON with a `facts` key only. *** ## Configure it ### Write the custom prompt ```python Python theme={null} custom_fact_extraction_prompt = """ Please only extract entities containing customer support information, order details, and user information. Here are some few shot examples: Input: Hi. Output: {"facts" : []} Input: The weather is nice today. Output: {"facts" : []} Input: My order #12345 hasn't arrived yet. Output: {"facts" : ["Order #12345 not received"]} Input: I'm John Doe, and I'd like to return the shoes I bought last week. Output: {"facts" : ["Customer name: John Doe", "Wants to return shoes", "Purchase made last week"]} Input: I ordered a red shirt, size medium, but received a blue one instead. Output: {"facts" : ["Ordered red shirt, size medium", "Received blue shirt instead"]} Return the facts and customer information in a json format as shown above. """ ``` ```ts TypeScript theme={null} const customPrompt = ` Please only extract entities containing customer support information, order details, and user information. Here are some few shot examples: Input: Hi. Output: {"facts" : []} Input: The weather is nice today. Output: {"facts" : []} Input: My order #12345 hasn't arrived yet. Output: {"facts" : ["Order #12345 not received"]} Input: I am John Doe, and I would like to return the shoes I bought last week. Output: {"facts" : ["Customer name: John Doe", "Wants to return shoes", "Purchase made last week"]} Input: I ordered a red shirt, size medium, but received a blue one instead. Output: {"facts" : ["Ordered red shirt, size medium", "Received blue shirt instead"]} Return the facts and customer information in a json format as shown above. `; ``` Keep example pairs short and mirror the capitalization, punctuation, and tone you see in real user messages. ### Load the prompt in configuration ```python Python theme={null} from mem0 import Memory config = { "llm": { "provider": "openai", "config": { "model": "gpt-4.1-nano-2025-04-14", "temperature": 0.2, "max_tokens": 2000, } }, "custom_fact_extraction_prompt": custom_fact_extraction_prompt, "version": "v1.1" } m = Memory.from_config(config_dict=config) ``` ```ts TypeScript theme={null} import { Memory } from "mem0ai/oss"; const config = { version: "v1.1", llm: { provider: "openai", config: { apiKey: process.env.OPENAI_API_KEY ?? "", model: "gpt-4-turbo-preview", temperature: 0.2, maxTokens: 1500, }, }, customPrompt: customPrompt, }; const memory = new Memory(config); ``` After initialization, run a quick `add` call with a known example and confirm the response splits into separate facts. *** ## See it in action ### Example: Order support memory ```python Python theme={null} m.add("Yesterday, I ordered a laptop, the order id is 12345", user_id="alice") ``` ```ts TypeScript theme={null} await memory.add("Yesterday, I ordered a laptop, the order id is 12345", { userId: "user123" }); ``` ```json Output theme={null} { "results": [ {"memory": "Ordered a laptop", "event": "ADD"}, {"memory": "Order ID: 12345", "event": "ADD"}, {"memory": "Order placed yesterday", "event": "ADD"} ], "relations": [] } ``` The output contains only the facts described in your prompt, each stored as a separate memory entry. ### Example: Irrelevant message filtered out ```python Python theme={null} m.add("I like going to hikes", user_id="alice") ``` ```ts TypeScript theme={null} await memory.add("I like going to hikes", { userId: "user123" }); ``` ```json Output theme={null} { "results": [], "relations": [] } ``` Empty `results` show the prompt successfully ignored content outside your target domain. *** ## Verify the feature is working * Log every call during rollout and confirm the `facts` array matches your schema. * Check that unrelated messages return an empty `results` array. * Run regression samples whenever you edit the prompt to ensure previously accepted facts still pass. *** ## Best practices 1. **Be precise:** Call out the exact categories or fields you want to capture. 2. **Show negative cases:** Include examples that should produce `[]` so the model learns to skip them. 3. **Keep JSON strict:** Avoid extra keys; only return `facts` to simplify downstream parsing. 4. **Version prompts:** Track prompt changes with a version number so you can roll back quickly. 5. **Review outputs regularly:** Spot-check stored memories to catch drift early. *** Refresh how Mem0 stores memories and how prompts influence fact creation. Apply custom extraction to route customer requests in a full workflow. # Custom Update Memory Prompt Source: https://docs.mem0.ai/open-source/features/custom-update-memory-prompt Decide how Mem0 adds, updates, or deletes memories using your own rules. The custom update memory prompt tells Mem0 how to handle changes when new facts arrive. Craft the prompt so the LLM can compare incoming facts with existing memories and choose the right action. **You’ll use this when…** * Stored memories need to stay consistent as users change preferences or correct past statements. * Your product has clear rules for when to add, update, delete, or leave a memory untouched. * You want traceable decisions (ADD, UPDATE, DELETE, NONE) for auditing or compliance. Prompts that mix instructions or omit examples can lead to wrong actions like deleting valid memories. Keep the language simple and test each action path. *** ## Feature anatomy * **Action verbs:** The prompt teaches the model to return `ADD`, `UPDATE`, `DELETE`, or `NONE` for every memory entry. * **ID retention:** Updates reuse the original memory ID so downstream systems maintain history. * **Old vs. new text:** Updates include `old_memory` so you can track what changed. * **Decision table:** Your prompt should explain when to use each action and show concrete examples. | Action | When to choose it | Output details | | -------- | -------------------------------------------------------------- | ------------------------------------------- | | `ADD` | Fact is new and not stored yet | Generate a new ID and set `event: "ADD"`. | | `UPDATE` | Fact replaces older info about the same topic | Keep the original ID, include `old_memory`. | | `DELETE` | Fact contradicts the stored memory or you explicitly remove it | Keep ID, set `event: "DELETE"`. | | `NONE` | Fact matches existing memory or is irrelevant | Keep ID with `event: "NONE"`. | *** ## Configure it ### Author the prompt ```python Python theme={null} UPDATE_MEMORY_PROMPT = """You are a smart memory manager which controls the memory of a system. You can perform four operations: (1) add into the memory, (2) update the memory, (3) delete from the memory, and (4) no change. Based on the above four operations, the memory will change. Compare newly retrieved facts with the existing memory. For each new fact, decide whether to: - ADD: Add it to the memory as a new element - UPDATE: Update an existing memory element - DELETE: Delete an existing memory element - NONE: Make no change (if the fact is already present or irrelevant) There are specific guidelines to select which operation to perform: 1. **Add**: If the retrieved facts contain new information not present in the memory, then you have to add it by generating a new ID in the id field. - **Example**: - Old Memory: [ { "id" : "0", "text" : "User is a software engineer" } ] - Retrieved facts: ["Name is John"] - New Memory: { "memory" : [ { "id" : "0", "text" : "User is a software engineer", "event" : "NONE" }, { "id" : "1", "text" : "Name is John", "event" : "ADD" } ] } 2. **Update**: If the retrieved facts contain information that is already present in the memory but the information is totally different, then you have to update it. If the retrieved fact contains information that conveys the same thing as the elements present in the memory, then you have to keep the fact which has the most information. Example (a) -- if the memory contains "User likes to play cricket" and the retrieved fact is "Loves to play cricket with friends", then update the memory with the retrieved facts. Example (b) -- if the memory contains "Likes cheese pizza" and the retrieved fact is "Loves cheese pizza", then you do not need to update it because they convey the same information. If the direction is to update the memory, then you have to update it. Please keep in mind while updating you have to keep the same ID. Please note to return the IDs in the output from the input IDs only and do not generate any new ID. - **Example**: - Old Memory: [ { "id" : "0", "text" : "I really like cheese pizza" }, { "id" : "1", "text" : "User is a software engineer" }, { "id" : "2", "text" : "User likes to play cricket" } ] - Retrieved facts: ["Loves chicken pizza", "Loves to play cricket with friends"] - New Memory: { "memory" : [ { "id" : "0", "text" : "Loves cheese and chicken pizza", "event" : "UPDATE", "old_memory" : "I really like cheese pizza" }, { "id" : "1", "text" : "User is a software engineer", "event" : "NONE" }, { "id" : "2", "text" : "Loves to play cricket with friends", "event" : "UPDATE", "old_memory" : "User likes to play cricket" } ] } 3. **Delete**: If the retrieved facts contain information that contradicts the information present in the memory, then you have to delete it. Or if the direction is to delete the memory, then you have to delete it. Please note to return the IDs in the output from the input IDs only and do not generate any new ID. - **Example**: - Old Memory: [ { "id" : "0", "text" : "Name is John" }, { "id" : "1", "text" : "Loves cheese pizza" } ] - Retrieved facts: ["Dislikes cheese pizza"] - New Memory: { "memory" : [ { "id" : "0", "text" : "Name is John", "event" : "NONE" }, { "id" : "1", "text" : "Loves cheese pizza", "event" : "DELETE" } ] } 4. **No Change**: If the retrieved facts contain information that is already present in the memory, then you do not need to make any changes. - **Example**: - Old Memory: [ { "id" : "0", "text" : "Name is John" }, { "id" : "1", "text" : "Loves cheese pizza" } ] - Retrieved facts: ["Name is John"] - New Memory: { "memory" : [ { "id" : "0", "text" : "Name is John", "event" : "NONE" }, { "id" : "1", "text" : "Loves cheese pizza", "event" : "NONE" } ] } """ ``` ### Define the expected output format ```json Add theme={null} { "memory": [ { "id": "0", "text": "This information is new", "event": "ADD" } ] } ``` ```json Update theme={null} { "memory": [ { "id": "0", "text": "This information replaces the old information", "event": "UPDATE", "old_memory": "Old information" } ] } ``` ```json Delete theme={null} { "memory": [ { "id": "0", "text": "This information will be deleted", "event": "DELETE" } ] } ``` ```json No Change theme={null} { "memory": [ { "id": "0", "text": "No changes for this information", "event": "NONE" } ] } ``` Consistent JSON structure makes it easy to parse decisions downstream or log them for auditing. *** ## See it in action * Run reconciliation jobs that compare retrieved facts to existing memories. * Feed both sources into the custom prompt, then apply the returned actions (add new entries, update text, delete outdated facts). * Log each decision so product teams can review why a change happened. The prompt works alongside `custom_fact_extraction_prompt`—fact extraction identifies candidate facts, and the update prompt decides how to merge them into long-term storage. *** ## Verify the feature is working * Test all four actions with targeted examples, including edge cases where facts differ only slightly. * Confirm update responses keep the original IDs and include `old_memory`. * Ensure delete actions only trigger when contradictions appear or when you explicitly request removal. *** ## Best practices 1. **Keep instructions brief:** Remove redundant wording so the LLM focuses on the decision logic. 2. **Document your schema:** Share the prompt and examples with your team so everyone knows how memories evolve. 3. **Track prompt versions:** When rules change, bump a version number and archive the prior prompt. 4. **Review outputs regularly:** Skim audit logs weekly to spot drift or repeated mistakes. 5. **Pair with monitoring:** Visualize counts of each action to detect spikes in deletes or updates. *** ## Compare prompts | Feature | `custom_update_memory_prompt` | `custom_fact_extraction_prompt` | | ----------- | ---------------------------------------------- | ------------------------------------------- | | Primary job | Decide memory actions (ADD/UPDATE/DELETE/NONE) | Pull facts from user and assistant messages | | Inputs | Retrieved facts + existing memory entries | Raw conversation turns | | Output | Structured memory array with events | Array of extracted facts | *** Coordinate both prompts so fact extraction feeds clean inputs into the update flow. See how update prompts keep customer profiles current in a working automation. # Graph Memory Source: https://docs.mem0.ai/open-source/features/graph-memory Layer relationships onto Mem0 search so agents remember who did what, when, and with whom. Graph Memory extends Mem0 by persisting nodes and edges alongside embeddings, so recalls stitch together people, places, and events instead of just keywords. **You’ll use this when…** * Conversation history mixes multiple actors and objects that vectors alone blur together * Compliance or auditing demands a graph of who said what and when * Agent teams need shared context without duplicating every memory in each run ## How Graph Memory Maps Context Mem0 extracts entities and relationships from every memory write, stores embeddings in your vector database, and mirrors relationships in a graph backend. On retrieval, vector search narrows candidates while the graph returns related context alongside the results. ```mermaid theme={null} graph LR A[Conversation] --> B(Extraction LLM) B --> C[Vector Store] B --> D[Graph Store] E[Query] --> C C --> F[Candidate Memories] F --> D D --> G[Contextual Recall] ``` ## How It Works Mem0’s extraction LLM identifies entities, relationships, and timestamps from the conversation payload you send to `memory.add`. Embeddings land in your configured vector database while nodes and edges flow into a Bolt-compatible graph backend (Neo4j, Memgraph, Neptune, or Kuzu). `memory.search` performs vector similarity (optionally reranked by your configured reranker) and returns the results list. Graph Memory runs in parallel and adds related entities in the `relations` array—it does not reorder the vector hits automatically. ## Quickstart (Neo4j Aura) **Time to implement:** \~10 minutes · **Prerequisites:** Python 3.10+, Node.js 18+, Neo4j Aura DB (free tier) Provision a free [Neo4j Aura](https://neo4j.com/product/auradb/) instance, copy the Bolt URI, username, and password, then follow the language tab that matches your stack. ```bash theme={null} pip install "mem0ai[graph]" ``` ```bash theme={null} export NEO4J_URL="neo4j+s://.databases.neo4j.io" export NEO4J_USERNAME="neo4j" export NEO4J_PASSWORD="your-password" ``` ```python theme={null} import os from mem0 import Memory config = { "graph_store": { "provider": "neo4j", "config": { "url": os.environ["NEO4J_URL"], "username": os.environ["NEO4J_USERNAME"], "password": os.environ["NEO4J_PASSWORD"], "database": "neo4j", } } } memory = Memory.from_config(config) conversation = [ {"role": "user", "content": "Alice met Bob at GraphConf 2025 in San Francisco."}, {"role": "assistant", "content": "Great! Logging that connection."}, ] memory.add(conversation, user_id="demo-user") results = memory.search( "Who did Alice meet at GraphConf?", user_id="demo-user", limit=3, rerank=True, ) for hit in results["results"]: print(hit["memory"]) ``` ```bash theme={null} npm install mem0ai ``` ```bash theme={null} export NEO4J_URL="neo4j+s://.databases.neo4j.io" export NEO4J_USERNAME="neo4j" export NEO4J_PASSWORD="your-password" ``` ```typescript theme={null} import { Memory } from "mem0ai/oss"; const config = { enableGraph: true, graphStore: { provider: "neo4j", config: { url: process.env.NEO4J_URL!, username: process.env.NEO4J_USERNAME!, password: process.env.NEO4J_PASSWORD!, database: "neo4j", }, }, }; const memory = new Memory(config); const conversation = [ { role: "user", content: "Alice met Bob at GraphConf 2025 in San Francisco." }, { role: "assistant", content: "Great! Logging that connection." }, ]; await memory.add(conversation, { userId: "demo-user" }); const results = await memory.search( "Who did Alice meet at GraphConf?", { userId: "demo-user", limit: 3, rerank: true } ); results.results.forEach((hit) => { console.log(hit.memory); }); ``` Expect to see **Alice met Bob at GraphConf 2025** in the output. In Neo4j Browser run `MATCH (p:Person)-[r]->(q:Person) RETURN p,r,q LIMIT 5;` to confirm the edge exists. Graph Memory enriches responses by adding related entities in the `relations` key. The ordering of `results` always comes from vector search (plus any reranker you configure); graph edges do not reorder those hits automatically. ## Operate Graph Memory Day-to-Day Guide which relationships become nodes and edges. ```python Python theme={null} import os from mem0 import Memory config = { "graph_store": { "provider": "neo4j", "config": { "url": os.environ["NEO4J_URL"], "username": os.environ["NEO4J_USERNAME"], "password": os.environ["NEO4J_PASSWORD"], }, "custom_prompt": "Please only capture people, organisations, and project links.", } } memory = Memory.from_config(config_dict=config) ``` ```typescript TypeScript theme={null} import { Memory } from "mem0ai/oss"; const config = { enableGraph: true, graphStore: { provider: "neo4j", config: { url: process.env.NEO4J_URL!, username: process.env.NEO4J_USERNAME!, password: process.env.NEO4J_PASSWORD!, }, customPrompt: "Please only capture people, organisations, and project links.", } }; const memory = new Memory(config); ``` Keep noisy edges out of the graph by demanding higher extraction confidence. ```python theme={null} config["graph_store"]["config"]["threshold"] = 0.75 ``` Disable graph writes or reads when you only want vector behaviour. ```python theme={null} memory.add(messages, user_id="demo-user", enable_graph=False) results = memory.search("marketing partners", user_id="demo-user", enable_graph=False) ``` Separate or share context across agents and sessions with `user_id`, `agent_id`, and `run_id`. ```typescript TypeScript theme={null} memory.add("I prefer Italian cuisine", { userId: "bob", agentId: "food-assistant" }); memory.add("I'm allergic to peanuts", { userId: "bob", agentId: "health-assistant" }); memory.add("I live in Seattle", { userId: "bob" }); const food = await memory.search("What food do I like?", { userId: "bob", agentId: "food-assistant" }); const allergies = await memory.search("What are my allergies?", { userId: "bob", agentId: "health-assistant" }); const location = await memory.search("Where do I live?", { userId: "bob" }); ``` Monitor graph growth, especially on free tiers, by periodically cleaning dormant nodes: `MATCH (n) WHERE n.lastSeen < date() - duration('P90D') DETACH DELETE n`. ## Troubleshooting Confirm Bolt connectivity is enabled, credentials match Aura, and your IP is allow-listed. Retry after confirming the URI format is `neo4j+s://...`. Ensure the graph identifier matches the vector dimension used by your embedder and that the IAM role allows `neptune-graph:*DataViaQuery` actions. Catch the provider error and retry with `enable_graph=False` so vector-only search keeps serving responses while the graph backend recovers. ## Decision Points * Select the graph store that fits your deployment (managed Aura vs. self-hosted Neo4j vs. AWS Neptune vs. local Kuzu). * Decide when to enable graph writes per request; routine conversations may stay vector-only to save latency. * Set a policy for pruning stale relationships so your graph stays fast and affordable. ## Provider setup Choose your backend and expand the matching panel for configuration details and links. Install the APOC plugin for self-hosted deployments, then configure Mem0: ```typescript theme={null} import { Memory } from "mem0ai/oss"; const config = { enableGraph: true, graphStore: { provider: "neo4j", config: { url: "neo4j+s://", username: "neo4j", password: "", } } }; const memory = new Memory(config); ``` Additional docs: [Neo4j Aura Quickstart](https://neo4j.com/docs/aura/), [APOC installation](https://neo4j.com/docs/apoc/current/installation/). Run Memgraph Mage locally with schema introspection enabled: ```bash theme={null} docker run -p 7687:7687 memgraph/memgraph-mage:latest --schema-info-enabled=True ``` Then point Mem0 at the instance: ```python theme={null} from mem0 import Memory config = { "graph_store": { "provider": "memgraph", "config": { "url": "bolt://localhost:7687", "username": "memgraph", "password": "your-password", }, }, } m = Memory.from_config(config_dict=config) ``` Learn more: [Memgraph Docs](https://memgraph.com/docs). Match vector dimensions between Neptune and your embedder, enable public connectivity (if needed), and grant IAM permissions: ```python theme={null} from mem0 import Memory config = { "graph_store": { "provider": "neptune", "config": { "endpoint": "neptune-graph://", }, }, } m = Memory.from_config(config_dict=config) ``` Reference: [Neptune Analytics Guide](https://docs.aws.amazon.com/neptune/latest/analytics/). Create a Neptune cluster, enable the public endpoint if you operate outside the VPC, and point Mem0 at the host: ```python theme={null} from mem0 import Memory config = { "graph_store": { "provider": "neptunedb", "config": { "collection_name": "", "endpoint": "neptune-graph://", }, }, } m = Memory.from_config(config_dict=config) ``` Reference: [Accessing Data in Neptune DB](https://docs.aws.amazon.com/neptune/latest/userguide/). Kuzu runs in-process, so supply a path (or `:memory:`) for the database file: ```python theme={null} config = { "graph_store": { "provider": "kuzu", "config": { "db": "/tmp/mem0-example.kuzu" } } } ``` Kuzu will clear its state when using `:memory:` once the process exits. See the [Kuzu documentation](https://kuzudb.com/docs/) for advanced settings. # Enhanced Metadata Filtering Source: https://docs.mem0.ai/open-source/features/metadata-filtering Fine-grained metadata queries for precise OSS memory retrieval. Enhanced metadata filtering in Mem0 1.0.0 lets you run complex queries across memory metadata. Combine comparisons, logical operators, and wildcard matches to zero in on the exact memories your agent needs. **You’ll use this when…** * Retrieval must respect multiple metadata conditions before returning context. * You need to mix numeric, boolean, and string filters in a single query. * Agents rely on deterministic filtering instead of broad semantic search alone. Enhanced filtering requires Mem0 1.0.0 or later and a vector store that supports the operators you enable. Unsupported operators fall back to simple equality filters. The TypeScript SDK accepts the same filter shape shown here—transpose the dictionaries to objects and reuse the keys unchanged. *** ## Feature anatomy | Operator | Meaning | When to use it | | ------------------------ | ------------------------------------------------- | ---------------------------------------------------------------------- | | `eq` / `ne` | Equals / not equals | Exact matches on strings, numbers, or booleans. | | `gt` / `gte` | Greater than / greater than or equal | Rank results by score, confidence, or any numeric field. | | `lt` / `lte` | Less than / less than or equal | Cap numeric values (e.g., ratings, timestamps). | | `in` / `nin` | In list / not in list | Pre-approve or block sets of values without chaining multiple filters. | | `contains` / `icontains` | Case-sensitive / case-insensitive substring match | Scan text fields for keywords. | | `*` | Wildcard | Require that a field exists, regardless of value. | | `AND` / `OR` / `NOT` | Combine filters | Build logic trees so multiple conditions work together. | ### Metadata selectors Start with key-value filters when you need direct matches on metadata fields. ```python theme={null} from mem0 import Memory m = Memory() # Search with simple metadata filters results = m.search( "What are my preferences?", user_id="alice", filters={"category": "preferences"} ) ``` Expect only memories tagged with `category="preferences"` to return for the given `user_id`. ### Comparison operators Layer greater-than/less-than comparisons to rank results by score, confidence, or any numeric field. Equality helpers (`eq`, `ne`) keep string and boolean checks explicit. ```python theme={null} # Greater than / Less than results = m.search( "recent activities", user_id="alice", filters={ "score": {"gt": 0.8}, "priority": {"gte": 5}, "confidence": {"lt": 0.9}, "rating": {"lte": 3} } ) # Equality operators results = m.search( "specific content", user_id="alice", filters={ "status": {"eq": "active"}, "archived": {"ne": True} } ) ``` ### List-based operators Use `in` and `nin` when you want to pre-approve or exclude specific values without writing multiple equality checks. ```python theme={null} # In / Not in operators results = m.search( "multi-category search", user_id="alice", filters={ "category": {"in": ["food", "travel", "entertainment"]}, "status": {"nin": ["deleted", "archived"]} } ) ``` Verify the response includes only memories in the whitelisted categories and omits any with archived or deleted status. ### String operators `contains` and `icontains` capture substring matches, making it easy to scan descriptions or tags for keywords without retrieving irrelevant memories. ```python theme={null} # Text matching operators results = m.search( "content search", user_id="alice", filters={ "title": {"contains": "meeting"}, "description": {"icontains": "important"}, "tags": {"contains": "urgent"} } ) ``` ### Wildcard matching Allow any value for a field while still requiring the field to exist—handy when the mere presence of a field matters. ```python theme={null} # Match any value for a field results = m.search( "all with category", user_id="alice", filters={ "category": "*" } ) ``` ### Logical combinations Combine filters with `AND`, `OR`, and `NOT` to express complex decision trees. Nest logical operators to encode multi-branch workflows. ```python theme={null} # Logical AND results = m.search( "complex query", user_id="alice", filters={ "AND": [ {"category": "work"}, {"priority": {"gte": 7}}, {"status": {"ne": "completed"}} ] } ) # Logical OR results = m.search( "flexible query", user_id="alice", filters={ "OR": [ {"category": "urgent"}, {"priority": {"gte": 9}}, {"deadline": {"contains": "today"}} ] } ) # Logical NOT results = m.search( "exclusion query", user_id="alice", filters={ "NOT": [ {"category": "archived"}, {"status": "deleted"} ] } ) # Complex nested logic results = m.search( "advanced query", user_id="alice", filters={ "AND": [ { "OR": [ {"category": "work"}, {"category": "personal"} ] }, {"priority": {"gte": 5}}, { "NOT": [ {"status": "archived"} ] } ] } ) ``` Inspect the response metadata—each returned memory should satisfy the combined logic tree exactly. If results look too broad, log the raw filters sent to your vector store. *** ## Configure it Tune your vector store so filter-heavy queries stay fast. Index fields you frequently filter on and keep complex checks for later in the evaluation order. ```python theme={null} # Ensure your vector store supports indexing on filtered fields config = { "vector_store": { "provider": "qdrant", "config": { "host": "localhost", "port": 6333, "indexed_fields": ["category", "priority", "status", "user_id"] } } } ``` After enabling indexing, benchmark the same query—latency should drop once the store can prune documents on indexed fields before vector scoring. Put simple key=value filters on indexed fields before your range or text conditions so the store trims results early. ```python theme={null} # More efficient: Filter on indexed fields first good_filters = { "AND": [ {"user_id": "alice"}, {"category": "work"}, {"content": {"contains": "meeting"}} ] } # Less efficient: Complex operations first avoid_filters = { "AND": [ {"description": {"icontains": "complex text search"}}, {"user_id": "alice"} ] } ``` When you reorder filters so indexed fields come first (`good_filters` example), queries typically return faster than the `avoid_filters` pattern where expensive text searches run before simple checks. Vector store support varies. Confirm operator coverage before shipping: Full comparison, list, and logical support. Handles deeply nested boolean logic efficiently. Equality and basic comparisons only. Limited nesting—break large trees into smaller calls. Comparisons plus `in`/`nin`. Text operators are constrained; rely on tags where possible. Full operator coverage with advanced text filters. Best option when you need hybrid text + metadata queries. If an operator is unsupported, most stores silently ignore that branch. Add validation before execution so you can fall back to simpler queries instead of returning empty results. ### Migrate from earlier filters ```python theme={null} # Before (v0.x) - simple key-value filtering only results = m.search( "query", user_id="alice", filters={"category": "work", "status": "active"} ) # After (v1.0.0) - enhanced filtering with operators results = m.search( "query", user_id="alice", filters={ "AND": [ {"category": "work"}, {"status": {"ne": "archived"}}, {"priority": {"gte": 5}} ] } ) ``` Existing equality filters continue to work; add new operator branches gradually so agents can adopt richer queries without downtime. *** ## See it in action ### Project management filtering ```python theme={null} # Find high-priority active tasks results = m.search( "What tasks need attention?", user_id="project_manager", filters={ "AND": [ {"project": {"in": ["alpha", ""]}}, {"priority": {"gte": 8}}, {"status": {"ne": "completed"}}, { "OR": [ {"assignee": "alice"}, {"assignee": "bob"} ] } ] } ) ``` Tasks returned should belong to the targeted projects, remain incomplete, and be assigned to one of the listed teammates. ### Customer support filtering ```python theme={null} # Find recent unresolved tickets results = m.search( "pending support issues", agent_id="support_bot", filters={ "AND": [ {"ticket_status": {"ne": "resolved"}}, {"priority": {"in": ["high", "critical"]}}, {"created_date": {"gte": "2024-01-01"}}, { "NOT": [ {"category": "spam"} ] } ] } ) ``` Pair `agent_id` filters with ticket-specific metadata so shared support bots return only the tickets they can act on in the current session. ### Content recommendation filtering ```python theme={null} # Personalized content filtering results = m.search( "recommend content", user_id="reader123", filters={ "AND": [ { "OR": [ {"genre": {"in": ["sci-fi", "fantasy"]}}, {"author": {"contains": "favorite"}} ] }, {"rating": {"gte": 4.0}}, {"read_status": {"ne": "completed"}}, {"language": "english"} ] } ) ``` Confirm personalized feeds show only unread titles that meet the rating and language criteria. ### Handle invalid operators ```python theme={null} try: results = m.search( "test query", user_id="alice", filters={ "invalid_operator": {"unknown": "value"} } ) except ValueError as e: print(f"Filter error: {e}") results = m.search( "test query", user_id="alice", filters={"category": "general"} ) ``` Validate filters before executing searches so you can catch typos or unsupported operators during development instead of at runtime. *** ## Verify the feature is working * Log the filters sent to your vector store and confirm the response metadata matches every clause. * Benchmark queries before and after indexing to ensure latency improvements materialize. * Add analytics or debug logging to track how often fallbacks execute when operators fail validation. *** ## Best practices 1. **Use indexed fields first:** Order filters so equality checks run before complex string operations. 2. **Combine operators intentionally:** Keep logical trees readable—large nests are harder to debug. 3. **Test performance regularly:** Benchmark critical queries with production-like payloads. 4. **Plan graceful degradation:** Provide fallback filters when an operator isn’t available. 5. **Validate syntax early:** Catch malformed filters during development to protect agents at runtime. *** Compare operator coverage and indexing strategies across supported stores. Practice building workflows that label and retrieve memories with clear metadata filters. # Multimodal Support Source: https://docs.mem0.ai/open-source/features/multimodal-support Capture and recall memories from both text and images. Multimodal support lets Mem0 extract facts from images alongside regular text. Add screenshots, receipts, or product photos and Mem0 will store the insights as searchable memories so agents can recall them later. **You’ll use this when…** * Users share screenshots, menus, or documents and you want the details to become memories. * You already collect text conversations but need visual context for better answers. * You want a single workflow that handles both URLs and local image files. Images larger than 20 MB are rejected. Compress or resize files before sending them to avoid errors. *** ## Feature anatomy * **Vision processing:** Mem0 runs the image through a vision model that extracts text and key details. * **Memory creation:** Extracted information is stored as standard memories so search, filters, and analytics continue to work. * **Context linking:** Visual and textual turns in the same conversation stay linked, giving agents richer context. * **Flexible inputs:** Accept publicly accessible URLs or base64-encoded local files in both Python and JavaScript SDKs. | Format | Used for | Notes | | ---------- | --------------------------- | ------------------------------------------ | | JPEG / JPG | Photos and screenshots | Default option for camera captures. | | PNG | Images with transparency | Keeps sharp text and UI elements crisp. | | WebP | Web-optimized images | Smaller payloads for faster uploads. | | GIF | Static or animated graphics | Works for simple graphics and short loops. | *** ## Configure it ### Add image messages from URLs ```python Python theme={null} from mem0 import Memory client = Memory() messages = [ {"role": "user", "content": "Hi, my name is Alice."}, { "role": "user", "content": { "type": "image_url", "image_url": { "url": "https://example.com/menu.jpg" } } } ] client.add(messages, user_id="alice") ``` ```ts TypeScript theme={null} import { Memory } from "mem0ai"; const client = new Memory(); const messages = [ { role: "user", content: "Hi, my name is Alice." }, { role: "user", content: { type: "image_url", image_url: { url: "https://example.com/menu.jpg" } } } ]; await client.add(messages, { user_id: "alice" }); ``` Inspect the response payload—the memories list should include entries extracted from the menu image as well as the text turns. ### Upload local images as base64 ```python Python theme={null} import base64 from mem0 import Memory def encode_image(image_path): with open(image_path, "rb") as image_file: return base64.b64encode(image_file.read()).decode("utf-8") client = Memory() base64_image = encode_image("path/to/your/image.jpg") messages = [ { "role": "user", "content": [ {"type": "text", "text": "What's in this image?"}, { "type": "image_url", "image_url": { "url": f"data:image/jpeg;base64,{base64_image}" } } ] } ] client.add(messages, user_id="alice") ``` ```ts TypeScript theme={null} import fs from "fs"; import { Memory } from "mem0ai"; function encodeImage(imagePath: string) { const buffer = fs.readFileSync(imagePath); return buffer.toString("base64"); } const client = new Memory(); const base64Image = encodeImage("path/to/your/image.jpg"); const messages = [ { role: "user", content: [ { type: "text", text: "What's in this image?" }, { type: "image_url", image_url: { url: `data:image/jpeg;base64,${base64Image}` } } ] } ]; await client.add(messages, { user_id: "alice" }); ``` Keep base64 payloads under 5 MB to speed up uploads and avoid hitting the 20 MB limit. *** ## See it in action ### Restaurant menu memory ```python theme={null} from mem0 import Memory client = Memory() messages = [ { "role": "user", "content": "Help me remember which dishes I liked." }, { "role": "user", "content": { "type": "image_url", "image_url": { "url": "https://example.com/restaurant-menu.jpg" } } }, { "role": "user", "content": "I’m allergic to peanuts and prefer vegetarian meals." } ] result = client.add(messages, user_id="user123") print(result) ``` The response should capture both the allergy note and menu items extracted from the photo so future searches can combine them. ### Document capture ```python theme={null} messages = [ { "role": "user", "content": "Store this receipt information for expenses." }, { "role": "user", "content": { "type": "image_url", "image_url": { "url": "https://example.com/receipt.jpg" } } } ] client.add(messages, user_id="user123") ``` Combine the receipt upload with structured metadata (tags, categories) if you need to filter expenses later. ### Error handling ```python Python theme={null} from mem0 import Memory from mem0.exceptions import InvalidImageError, FileSizeError client = Memory() try: messages = [{ "role": "user", "content": { "type": "image_url", "image_url": {"url": "https://example.com/image.jpg"} } }] client.add(messages, user_id="user123") print("Image processed successfully") except InvalidImageError: print("Invalid image format or corrupted file") except FileSizeError: print("Image file too large") except Exception as exc: print(f"Unexpected error: {exc}") ``` ```ts TypeScript theme={null} import { Memory } from "mem0ai"; const client = new Memory(); try { const messages = [{ role: "user", content: { type: "image_url", image_url: { url: "https://example.com/image.jpg" } } }]; await client.add(messages, { user_id: "user123" }); console.log("Image processed successfully"); } catch (error: any) { if (error.type === "invalid_image") { console.log("Invalid image format or corrupted file"); } else if (error.type === "file_size_exceeded") { console.log("Image file too large"); } else { console.log(`Unexpected error: ${error.message}`); } } ``` Fail fast on invalid formats so you can prompt users to re-upload before losing their context. *** ## Verify the feature is working * After calling `add`, inspect the returned memories and confirm they include image-derived text (menu items, receipt totals, etc.). * Run a follow-up `search` for a detail from the image; the memory should surface alongside related text. * Monitor image upload latency—large files should still complete under your acceptable response time. * Log file size and URL sources to troubleshoot repeated failures. *** ## Best practices 1. **Ask for intent:** Prompt users to explain why they sent an image so the memory includes the right context. 2. **Keep images readable:** Encourage clear photos without heavy filters or shadows for better extraction. 3. **Split bulk uploads:** Send multiple images as separate `add` calls to isolate failures and improve reliability. 4. **Watch privacy:** Avoid uploading sensitive documents unless your environment is secured for that data. 5. **Validate file size early:** Check file size before encoding to save bandwidth and time. *** ## Troubleshooting | Issue | Cause | Fix | | ------------------------- | ---------------------------- | ---------------------------------------------------------------------- | | Upload rejected | File larger than 20 MB | Compress or resize before sending. | | Memory missing image data | Low-quality or blurry image | Retake the photo with better lighting. | | Invalid format error | Unsupported file type | Convert to JPEG or PNG first. | | Slow processing | High-resolution images | Downscale or compress to under 5 MB. | | Base64 errors | Incorrect prefix or encoding | Ensure `data:image/;base64,` is present and the string is valid. | *** Review supported vision-capable models and configuration details. Follow an end-to-end workflow pairing text and image memories. # OpenAI Compatibility Source: https://docs.mem0.ai/open-source/features/openai_compatibility Use Mem0 with the same chat-completions flow you already built for OpenAI. Mem0 mirrors the OpenAI client interface so you can plug memories into existing chat-completion code with minimal changes. Point your OpenAI-compatible client at Mem0, keep the same request shape, and gain persistent memory between calls. **You’ll use this when…** * Your app already relies on OpenAI chat completions and you want Mem0 to feel familiar. * You need to reuse existing middleware that expects OpenAI-compatible responses. * You plan to switch between Mem0 Platform and the self-hosted client without rewriting code. ## Feature * **Drop-in client:** `client.chat.completions.create(...)` works the same as OpenAI’s method signatures. * **Shared parameters:** Mem0 accepts `messages`, `model`, and optional memory-scoping fields (`user_id`, `agent_id`, `run_id`). * **Memory-aware responses:** Each call saves relevant facts so future prompts automatically reflect past conversations. * **OSS parity:** Use the same API surface whether you call the hosted proxy or the OSS configuration. Run one request with `user_id` set. If the next call references that ID and its reply uses the stored memory, compatibility is confirmed. *** ## Configure it ### Call the managed Mem0 proxy ```python theme={null} from mem0.proxy.main import Mem0 client = Mem0(api_key="m0-xxx") messages = [ {"role": "user", "content": "I love Indian food but I cannot eat pizza since I'm allergic to cheese."} ] chat_completion = client.chat.completions.create( messages=messages, model="gpt-4.1-nano-2025-04-14", user_id="alice" ) ``` Reuse the same identifiers your OpenAI client already sends so you can switch between providers without branching logic. ### Use the OpenAI-compatible OSS client ```python theme={null} from mem0.proxy.main import Mem0 config = { "vector_store": { "provider": "qdrant", "config": { "host": "localhost", "port": 6333 } } } client = Mem0(config=config) chat_completion = client.chat.completions.create( messages=[{"role": "user", "content": "What's the capital of France?"}], model="gpt-4.1-nano-2025-04-14" ) ``` ## See it in action ### Memory-aware restaurant recommendation ```python theme={null} from mem0.proxy.main import Mem0 client = Mem0(api_key="m0-xxx") # Store preferences client.chat.completions.create( messages=[{"role": "user", "content": "I love Indian food but I'm allergic to cheese."}], model="gpt-4.1-nano-2025-04-14", user_id="alice" ) # Later conversation reuses the memory response = client.chat.completions.create( messages=[{"role": "user", "content": "Suggest dinner options in San Francisco."}], model="gpt-4.1-nano-2025-04-14", user_id="alice" ) print(response.choices[0].message.content) ``` The second response should call out Indian restaurants and avoid cheese, proving Mem0 recalled the stored preference. *** ## Verify the feature is working * Compare responses from Mem0 vs. OpenAI for identical prompts—both should return the same structure (`choices`, `usage`, etc.). * Inspect stored memories after each request to confirm the fact extraction captured the right details. * Test switching between hosted (`Mem0(api_key=...)`) and OSS configurations to ensure both respect the same request body. *** ## Best practices 1. **Scope context intentionally:** Pass identifiers only when you want conversations to persist; skip them for one-off calls. 2. **Log memory usage:** Inspect `response.metadata.memories` (if enabled) to see which facts the model recalled. 3. **Reuse middleware:** Point your existing OpenAI client wrappers to the Mem0 proxy URL to avoid code drift. 4. **Handle fallbacks:** Keep a code path for plain OpenAI calls in case Mem0 is unavailable, then resync memory later. *** ## Parameter reference | Parameter | Type | Purpose | | ---------- | ------ | --------------------------------------------------------------- | | `user_id` | `str` | Associates the conversation with a user so memories persist. | | `agent_id` | `str` | Optional agent or bot identifier for multi-agent scenarios. | | `run_id` | `str` | Optional session/run identifier for short-lived flows. | | `metadata` | `dict` | Store extra fields alongside each memory entry. | | `filters` | `dict` | Restrict retrieval to specific memories while responding. | | `limit` | `int` | Cap how many memories Mem0 pulls into the context (default 10). | Other request fields mirror OpenAI’s chat completion API. *** Review LLM options that support OpenAI-compatible calls in Mem0. See a full workflow that layers Mem0 memories on top of tool-calling agents. # Overview Source: https://docs.mem0.ai/open-source/features/overview Self-hosting features that extend Mem0 beyond basic memory storage # Self-Hosting Features Overview Mem0 Open Source ships with capabilities that adapt memory behavior for production workloads—async operations, graph relationships, multimodal inputs, and fine-tuned retrieval. Configure these features with code or YAML to match your application's needs. Start with the Python quickstart to validate basic memory operations, then enable the features below when you need them. ## Choose your path Store entity relationships for multi-hop recall. Query with logical operators and nested conditions. Boost search relevance with specialized models. Non-blocking operations for high-throughput apps. Process images, audio, and video memories. Tailor how facts are extracted from text. Control memory refinement with custom instructions. HTTP endpoints for language-agnostic integrations. Drop-in replacement for OpenAI chat endpoints. Looking for managed features instead? Compare self-hosting vs managed in the Platform vs OSS guide. ## Keep going # Reranker-Enhanced Search Source: https://docs.mem0.ai/open-source/features/reranker-search Boost relevance by reordering vector hits with reranking models. Reranker-enhanced search adds a second scoring pass after vector retrieval so Mem0 can return the most relevant memories first. Enable it when keyword similarity alone misses nuance or when you need the highest-confidence context for an agent decision. **You’ll use this when…** * Queries are nuanced and require semantic understanding beyond vector distance. * Large memory collections produce too many near matches to review manually. * You want consistent scoring across providers by delegating ranking to a dedicated model. Reranking raises latency and, for hosted models, API spend. Benchmark with production traffic and define a fallback path for latency-sensitive requests. All configuration snippets translate directly to the TypeScript SDK—swap dictionaries for objects while keeping the same keys (`provider`, `config`, `rerank` flags). *** ## Feature anatomy * **Initial vector search:** Retrieve candidate memories by similarity. * **Reranker pass:** A specialized model scores each candidate against the original query. * **Reordered results:** Mem0 sorts responses using the reranker’s scores before returning them. * **Optional fallbacks:** Toggle reranking per request or disable it entirely if performance or cost becomes a concern. - **[Cohere](/components/rerankers/models/cohere)** – Multilingual hosted reranker with API-based scoring. - **[Sentence Transformer](/components/rerankers/models/sentence_transformer)** – Local Hugging Face cross-encoders for GPU or CPU. - **[Hugging Face](/components/rerankers/models/huggingface)** – Bring any hosted or on-prem reranker model ID. - **[LLM Reranker](/components/rerankers/models/llm_reranker)** – Use your preferred LLM (OpenAI, etc.) for prompt-driven scoring. - **[Zero Entropy](/components/rerankers/models/zero_entropy)** – High-quality neural reranking tuned for retrieval tasks. | Provider | Latency | Quality | Cost | Local deploy | | -------------------- | ---------- | --------- | -------- | ------------ | | Cohere | Medium | High | API cost | ❌ | | Sentence Transformer | Low | Good | Free | ✅ | | Hugging Face | Low–Medium | Variable | Free | ✅ | | LLM Reranker | High | Very high | API cost | Depends | *** ## Configure it ### Basic setup ```python theme={null} from mem0 import Memory config = { "reranker": { "provider": "cohere", "config": { "model": "rerank-english-v3.0", "api_key": "your-cohere-api-key" } } } m = Memory.from_config(config) ``` Confirm `results["results"][0]["score"]` reflects the reranker output—if the field is missing, the reranker was not applied. Set `top_k` to the smallest candidate pool that still captures relevant hits. Smaller pools keep reranking costs down. ### Provider-specific options ```python theme={null} # Cohere reranker config = { "reranker": { "provider": "cohere", "config": { "model": "rerank-english-v3.0", "api_key": "your-cohere-api-key", "top_k": 10, "return_documents": True } } } # Sentence Transformer reranker config = { "reranker": { "provider": "sentence_transformer", "config": { "model": "cross-encoder/ms-marco-MiniLM-L-6-v2", "device": "cuda", "max_length": 512 } } } # Hugging Face reranker config = { "reranker": { "provider": "huggingface", "config": { "model": "BAAI/bge-reranker-base", "device": "cuda", "batch_size": 32 } } } # LLM-based reranker config = { "reranker": { "provider": "llm_reranker", "config": { "llm": { "provider": "openai", "config": { "model": "gpt-4", "api_key": "your-openai-api-key" } }, "top_k": 5 } } } ``` Keep authentication keys in environment variables when you plug these configs into production projects. ### Full stack example ```python theme={null} config = { "vector_store": { "provider": "qdrant", "config": { "host": "localhost", "port": 6333 } }, "llm": { "provider": "openai", "config": { "model": "gpt-4", "api_key": "your-openai-api-key" } }, "embedder": { "provider": "openai", "config": { "model": "text-embedding-3-small", "api_key": "your-openai-api-key" } }, "reranker": { "provider": "cohere", "config": { "model": "rerank-english-v3.0", "api_key": "your-cohere-api-key", "top_k": 15, "return_documents": True } } } m = Memory.from_config(config) ``` A quick search should now return results with both vector and reranker scores, letting you compare improvements immediately. ### Async support ```python theme={null} from mem0 import AsyncMemory async_memory = AsyncMemory.from_config(config) async def search_with_rerank(): return await async_memory.search( "What are my preferences?", user_id="alice", rerank=True ) import asyncio results = asyncio.run(search_with_rerank()) ``` Inspect the async response to confirm reranking still applies; the scores should match the synchronous implementation. ### Tune performance and cost ```python theme={null} # GPU-friendly local reranker configuration config = { "reranker": { "provider": "sentence_transformer", "config": { "model": "cross-encoder/ms-marco-MiniLM-L-6-v2", "device": "cuda", "batch_size": 32, "top_k": 10, "max_length": 256 } } } # Smart toggle for hosted rerankers def smart_search(query, user_id, use_rerank=None): if use_rerank is None: use_rerank = len(query.split()) > 3 return m.search(query, user_id=user_id, rerank=use_rerank) ``` Use heuristics (query length, user tier) to decide when to rerank so high-signal queries benefit without taxing every request. ### Handle failures gracefully ```python theme={null} try: results = m.search("test query", user_id="alice", rerank=True) except Exception as exc: print(f"Reranking failed: {exc}") results = m.search("test query", user_id="alice", rerank=False) ``` Always fall back to vector-only search—dropped queries introduce bigger accuracy issues than slightly less relevant ordering. ### Migrate from v0.x ```python theme={null} # Before: basic vector search results = m.search("query", user_id="alice") # After: same API with reranking enabled via config config = { "reranker": { "provider": "sentence_transformer", "config": { "model": "cross-encoder/ms-marco-MiniLM-L-6-v2" } } } m = Memory.from_config(config) results = m.search("query", user_id="alice") ``` *** ## See it in action ### Basic reranked search ```python theme={null} results = m.search( "What are my food preferences?", user_id="alice" ) for result in results["results"]: print(f"Memory: {result['memory']}") print(f"Score: {result['score']}") ``` Expect each result to list the reranker-adjusted score so you can compare ordering against baseline vector results. ### Toggle reranking per request ```python theme={null} results_with_rerank = m.search( "What movies do I like?", user_id="alice", rerank=True ) results_without_rerank = m.search( "What movies do I like?", user_id="alice", rerank=False ) ``` Log the reranked vs. non-reranked lists during rollout so stakeholders can see the improvement before enforcing it everywhere. You should see the same memories in both lists, but the reranked response will reorder them based on semantic relevance. ### Combine with metadata filters ```python theme={null} results = m.search( "important work tasks", user_id="alice", filters={ "AND": [ {"category": "work"}, {"priority": {"gte": 7}} ] }, rerank=True, limit=20 ) ``` Verify filtered reranked searches still respect every metadata clause—reranking only reorders candidates, it never bypasses filters. ### Real-world playbooks #### Customer support ```python theme={null} config = { "reranker": { "provider": "cohere", "config": { "model": "rerank-english-v3.0", "api_key": "your-cohere-api-key" } } } m = Memory.from_config(config) results = m.search( "customer having login issues with mobile app", agent_id="support_bot", filters={"category": "technical_support"}, rerank=True ) ``` Top results should highlight tickets matching the login issue context so agents can respond faster. #### Content recommendation ```python theme={null} results = m.search( "science fiction books with space exploration themes", user_id="reader123", filters={"content_type": "book_recommendation"}, rerank=True, limit=10 ) for result in results["results"]: print(f"Recommendation: {result['memory']}") print(f"Relevance: {result['score']:.3f}") ``` Expect high-scoring recommendations that match both the requested theme and any metadata limits you applied. #### Personal assistant ```python theme={null} results = m.search( "What restaurants did I enjoy last month that had good vegetarian options?", user_id="foodie_user", filters={ "AND": [ {"category": "dining"}, {"rating": {"gte": 4}}, {"date": {"gte": "2024-01-01"}} ] }, rerank=True ) ``` Reuse this pattern for other lifestyle queries—swap the filters and prompt text without changing the rerank configuration. Each workflow keeps the same `m.search(...)` signature, so you can template these queries across agents with only the prompt and filters changing. *** ## Verify the feature is working * Inspect result payloads for both `score` (vector) and reranker scores; mismatched fields indicate the reranker didn’t execute. * Track latency before and after enabling reranking to ensure SLAs hold. * Review provider logs or dashboards for throttling or quota warnings. * Run A/B comparisons (rerank on/off) to validate improved relevance before defaulting to reranked responses. *** ## Best practices 1. **Start local:** Try Sentence Transformer models to prove value before paying for hosted APIs. 2. **Monitor latency:** Add metrics around reranker duration so you notice regressions quickly. 3. **Control spend:** Use `top_k` and selective toggles to cap hosted reranker costs. 4. **Keep a fallback:** Always catch reranker failures and continue with vector-only ordering. 5. **Experiment often:** Swap providers or models to find the best fit for your domain and language mix. *** Review provider fields, defaults, and environment variables before going live. Extend scoring with prompt-tuned LLM rerankers for niche workflows. # REST API Server Source: https://docs.mem0.ai/open-source/features/rest-api Reach every Mem0 OSS capability through a FastAPI-powered REST layer. The Mem0 REST API server exposes every OSS memory operation over HTTP. Run it alongside your stack to add, search, update, and delete memories from any language that speaks REST. **You’ll use this when…** * Your services already talk to REST APIs and you want Mem0 to match that style. * Teams on languages without the Mem0 SDK still need access to memories. * You plan to explore or debug endpoints through the built-in OpenAPI page at `/docs`. Add your own authentication and HTTPS before exposing the server to anything beyond your internal network. The default image does not include auth. *** ## Feature * **CRUD endpoints:** Create, retrieve, search, update, delete, and reset memories by `user_id`, `agent_id`, or `run_id`. * **Status health check:** Access base routes to confirm the server is online. * **OpenAPI explorer:** Visit `/docs` for interactive testing and schema reference. *** ## Configure it ### Run with Docker Compose (development) 1. Create `server/.env` with your keys: ```bash theme={null} OPENAI_API_KEY=your-openai-api-key ``` 2. Start the stack: ```bash theme={null} cd server docker compose up ``` 3. Reach the API at `http://localhost:8888`. Edits to the server or library auto-reload. ### Run with Docker ```bash theme={null} docker pull mem0/mem0-api-server ``` ```bash theme={null} docker build -t mem0-api-server . ``` 1. Create a `.env` file with `OPENAI_API_KEY`. 2. Run the container: ```bash theme={null} docker run -p 8000:8000 --env-file .env mem0-api-server ``` 3. Visit `http://localhost:8000`. ### Run directly (no Docker) ```bash theme={null} pip install -r requirements.txt uvicorn main:app --reload ``` Use a process manager such as `systemd`, Supervisor, or PM2 when deploying the FastAPI server for production resilience. The REST server reads the same configuration you use locally, so you can point it at your preferred LLM, vector store, graph backend, and reranker without changing code. *** ## See it in action ### Create and search memories via HTTP ```bash theme={null} curl -X POST http://localhost:8000/memories \ -H "Content-Type: application/json" \ -d '{ "messages": [ {"role": "user", "content": "I love fresh vegetable pizza."} ], "user_id": "alice" }' ``` Expect a JSON response containing the new memory IDs and events (`ADD`, etc.). ```bash theme={null} curl "http://localhost:8000/memories/search?user_id=alice&query=vegetable" ``` ### Explore with OpenAPI docs 1. Navigate to `http://localhost:8000/docs`. 2. Pick an endpoint (e.g., `POST /memories/search`). 3. Fill in parameters and click **Execute** to try requests in-browser. Export the generated `curl` snippets from the OpenAPI UI to bootstrap integration tests. *** ## Verify the feature is working * Hit the root route and `/docs` to confirm the server is reachable. * Run a full cycle: `POST /memories` → `GET /memories/{id}` → `DELETE /memories/{id}`. * Watch server logs for import errors or provider misconfigurations during startup. * Confirm environment variables (API keys, vector store credentials) load correctly when containers restart. *** ## Best practices 1. **Add authentication:** Protect endpoints with API gateways, proxies, or custom FastAPI middleware. 2. **Use HTTPS:** Terminate TLS at your load balancer or reverse proxy. 3. **Monitor uptime:** Track request rates, latency, and error codes per endpoint. 4. **Version configs:** Keep environment files and Docker Compose definitions in source control. 5. **Limit exposure:** Bind to private networks unless you explicitly need public access. *** Fine-tune LLMs, vector stores, and graph backends that power the REST server. See how services call the REST endpoints as part of an automation pipeline. # Node SDK Quickstart Source: https://docs.mem0.ai/open-source/node-quickstart Store and search Mem0 memories from a TypeScript or JavaScript app in minutes. Spin up Mem0 with the Node SDK in just a few steps. You’ll install the package, initialize the client, add a memory, and confirm retrieval with a single search. ## Prerequisites * Node.js 18 or higher * (Optional) OpenAI API key stored in your environment when you want to customize providers ## Install and run your first memory ```bash theme={null} npm install mem0ai ``` ```ts theme={null} import { Memory } from "mem0ai/oss"; const memory = new Memory(); ``` ```ts theme={null} const messages = [ { role: "user", content: "I'm planning to watch a movie tonight. Any recommendations?" }, { role: "assistant", content: "How about thriller movies? They can be quite engaging." }, { role: "user", content: "I'm not a big fan of thriller movies but I love sci-fi movies." }, { role: "assistant", content: "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future." } ]; await memory.add(messages, { userId: "alice", metadata: { category: "movie_recommendations" } }); ``` ```ts theme={null} const results = await memory.search("What do you know about me?", { userId: "alice" }); console.log(results); ``` **Output** ```json theme={null} { "results": [ { "id": "892db2ae-06d9-49e5-8b3e-585ef9b85b8e", "memory": "User is planning to watch a movie tonight.", "score": 0.38920719231944799, "metadata": { "category": "movie_recommendations" }, "userId": "alice" } ] } ``` By default the Node SDK uses local-friendly settings (OpenAI `gpt-4.1-nano-2025-04-14`, `text-embedding-3-small`, in-memory vector store, and SQLite history). Swap components by passing a config as shown below. ## Configure for production ```ts theme={null} import { Memory } from "mem0ai/oss"; const memory = new Memory({ version: "v1.1", embedder: { provider: "openai", config: { apiKey: process.env.OPENAI_API_KEY || "", model: "text-embedding-3-small" } }, vectorStore: { provider: "memory", config: { collectionName: "memories", dimension: 1536 } }, llm: { provider: "openai", config: { apiKey: process.env.OPENAI_API_KEY || "", model: "gpt-4-turbo-preview" } }, historyDbPath: "memory.db" }); ``` ## Manage memories (optional) ```ts Get all memories theme={null} const allMemories = await memory.getAll({ userId: "alice" }); console.log(allMemories); ``` ```ts Get one memory theme={null} const singleMemory = await memory.get("892db2ae-06d9-49e5-8b3e-585ef9b85b8e"); console.log(singleMemory); ``` ```ts Search memories theme={null} const result = await memory.search("What do you know about me?", { userId: "alice" }); console.log(result); ``` ```ts Update a memory theme={null} const updateResult = await memory.update( "892db2ae-06d9-49e5-8b3e-585ef9b85b8e", "I love India, it is my favorite country." ); console.log(updateResult); ``` ```ts theme={null} // Audit history const history = await memory.history("892db2ae-06d9-49e5-8b3e-585ef9b85b8e"); console.log(history); // Delete specific or scoped memories await memory.delete("892db2ae-06d9-49e5-8b3e-585ef9b85b8e"); await memory.deleteAll({ userId: "alice" }); // Reset everything await memory.reset(); ``` ## Use a custom history store The Node SDK supports Supabase (or other providers) when you need serverless-friendly history storage. ```ts Supabase provider theme={null} import { Memory } from "mem0ai/oss"; const memory = new Memory({ historyStore: { provider: "supabase", config: { supabaseUrl: process.env.SUPABASE_URL || "", supabaseKey: process.env.SUPABASE_KEY || "", tableName: "memory_history" } } }); ``` ```ts Disable history theme={null} import { Memory } from "mem0ai/oss"; const memory = new Memory({ disableHistory: true }); ``` Create the Supabase table with: ```sql theme={null} create table memory_history ( id text primary key, memory_id text not null, previous_value text, new_value text, action text not null, created_at timestamp with time zone default timezone('utc', now()), updated_at timestamp with time zone, is_deleted integer default 0 ); ``` ## Configuration parameters Mem0 offers granular configuration across vector stores, LLMs, embedders, and history stores. | Parameter | Description | Default | | ---------- | ---------------------------------------- | ------------- | | `provider` | Vector store provider (e.g., `"memory"`) | `"memory"` | | `host` | Host address | `"localhost"` | | `port` | Port number | `undefined` | | Parameter | Description | Provider | | --------------- | ---------------------------------------------- | -------- | | `provider` | LLM provider (e.g., `"openai"`, `"anthropic"`) | All | | `model` | Model to use | All | | `temperature` | Temperature value | All | | `apiKey` | API key | All | | `maxTokens` | Max tokens to generate | All | | `topP` | Probability threshold | All | | `topK` | Token count to keep | All | | `openaiBaseUrl` | Base URL override | OpenAI | | Parameter | Description | Default | | ---------- | -------------------------------------- | ---------------------------- | | `provider` | Graph store provider (e.g., `"neo4j"`) | `"neo4j"` | | `url` | Connection URL | `process.env.NEO4J_URL` | | `username` | Username | `process.env.NEO4J_USERNAME` | | `password` | Password | `process.env.NEO4J_PASSWORD` | | Parameter | Description | Default | | ---------- | ------------------ | -------------------------- | | `provider` | Embedding provider | `"openai"` | | `model` | Embedding model | `"text-embedding-3-small"` | | `apiKey` | API key | `undefined` | | Parameter | Description | Default | | --------------- | ------------------------ | ------------------------- | | `historyDbPath` | Path to history database | `"{mem0_dir}/history.db"` | | `version` | API version | `"v1.0"` | | `customPrompt` | Custom processing prompt | `undefined` | | Parameter | Description | Default | | ---------------- | ---------------------- | ----------- | | `provider` | History provider | `"sqlite"` | | `config` | Provider configuration | `undefined` | | `disableHistory` | Disable history store | `false` | ```ts theme={null} const config = { version: "v1.1", embedder: { provider: "openai", config: { apiKey: process.env.OPENAI_API_KEY || "", model: "text-embedding-3-small" } }, vectorStore: { provider: "memory", config: { collectionName: "memories", dimension: 1536 } }, llm: { provider: "openai", config: { apiKey: process.env.OPENAI_API_KEY || "", model: "gpt-4-turbo-preview" } }, historyStore: { provider: "supabase", config: { supabaseUrl: process.env.SUPABASE_URL || "", supabaseKey: process.env.SUPABASE_KEY || "", tableName: "memories" } }, disableHistory: false, customPrompt: "I'm a virtual assistant. I'm here to help you with your queries." }; ``` ## What's next? Review CRUD patterns, filters, and advanced retrieval across the OSS stack. Swap in your preferred LLM, vector store, and history provider for production use. See a full Node-based workflow that layers Mem0 memories onto tool-calling agents. If you have any questions, please feel free to reach out: # Overview Source: https://docs.mem0.ai/open-source/overview Self-host Mem0 with full control over your infrastructure and data # Mem0 Open Source Overview Mem0 Open Source delivers the same adaptive memory engine as the platform, but packaged for teams that need to run everything on their own infrastructure. You own the stack, the data, and the customizations. Mem0 v1.0.0 brought rerankers, async-by-default clients, and Azure OpenAI support. See the release notes for the full rundown before upgrading. ## What Mem0 OSS provides * **Full control**: Tune every component, from LLMs to vector stores, inside your environment. * **Offline ready**: Keep memory on your own network when compliance or privacy demands it. * **Extendable codebase**: Fork the repo, add providers, and ship custom automations. Begin with the Python quickstart (or the Node.js variant) to clone the repo, configure dependencies, and validate memory reads/writes locally. ## Choose your path Bootstrap CLI and verify add/search loop. Install TypeScript SDK and run starter script. LLM, embedder, vector store, reranker setup. Relationship-aware recall with Neo4j, Memgraph. Hybrid retrieval and reranker controls. Reference deployment with REST endpoints. Async add/search flows and automation. Need a managed alternative? Compare hosting models in the Platform vs OSS guide or switch tabs to the Platform documentation. | Benefit | What you get | | --------------------------- | ------------------------------------------------------------------------------ | | Full infrastructure control | Host on your own servers with complete access to configuration and deployment. | | Complete customization | Modify the implementation, extend functionality, and tailor it to your stack. | | Local development | Perfect for development, testing, and offline environments. | | No vendor lock-in | Keep ownership of your data, providers, and pipelines. | | Community driven | Contribute improvements and tap into a growing ecosystem. | ## Default components Mem0 OSS works out of the box with sensible defaults: * LLM: OpenAI `gpt-4.1-nano-2025-04-14` (via `OPENAI_API_KEY`) * Embeddings: OpenAI `text-embedding-3-small` * Vector store: Local Qdrant instance storing data at `/tmp/qdrant` * History store: SQLite database at `~/.mem0/history.db` * Reranker: Disabled until you configure a provider Override any component with `Memory.from_config`. ## Keep going Need a managed alternative? Compare hosting models in the Platform vs OSS guide or switch tabs to the Platform documentation. # Python SDK Quickstart Source: https://docs.mem0.ai/open-source/python-quickstart Get started with Mem0 quickly! Get started with Mem0's Python SDK in under 5 minutes. This guide shows you how to install Mem0 and store your first memory. ## Prerequisites * Python 3.10 or higher * OpenAI API key ([Get one here](https://platform.openai.com/api-keys)) Set your OpenAI API key: ```bash theme={null} export OPENAI_API_KEY="your-openai-api-key" ``` Uses OpenAI by default. Want to use Ollama, Anthropic, or local models? See [Configuration](/open-source/configuration). ## Installation ```bash theme={null} pip install mem0ai ``` ```python theme={null} from mem0 import Memory m = Memory() ``` ```python theme={null} messages = [ {"role": "user", "content": "Hi, I'm Alex. I love basketball and gaming."}, {"role": "assistant", "content": "Hey Alex! I'll remember your interests."} ] m.add(messages, user_id="alex") ``` ```python theme={null} results = m.search("What do you know about me?", filters={"user_id": "alex"}) print(results) ``` **Output:** ```json theme={null} { "results": [ { "id": "mem_123abc", "memory": "Name is Alex. Enjoys basketball and gaming.", "user_id": "alex", "categories": ["personal_info"], "created_at": "2025-10-22T04:40:22.864647-07:00", "score": 0.89 } ] } ``` By default `Memory()` wires up: * OpenAI `gpt-4.1-nano-2025-04-14` for fact extraction and updates * OpenAI `text-embedding-3-small` embeddings (1536 dimensions) * Qdrant vector store with on-disk data at `/tmp/qdrant` * SQLite history at `~/.mem0/history.db` * No reranker (add one in the config when you need it) ## What's Next? Learn how to search, update, and manage memories with full CRUD operations Customize Mem0 with different LLMs, vector stores, and embedders for production use Explore async support, graph memory, and multi-agent memory organization ## Additional Resources * **[OpenAI Compatibility](/open-source/features/openai_compatibility)** - Use Mem0 with OpenAI-compatible chat completions * **[Contributing Guide](/contributing/development)** - Learn how to contribute to Mem0 * **[Examples](/cookbooks/companions/local-companion-ollama)** - See Mem0 in action with Ollama and other integrations # Advanced Memory Operations Source: https://docs.mem0.ai/platform/advanced-memory-operations Run richer add/search/update/delete flows on the managed platform with metadata, rerankers, and per-request controls. # Make Platform Memory Operations Smarter **Prerequisites** * Platform workspace with API key * Python 3.10+ and Node.js 18+ * Async memories enabled in your dashboard (Settings → Memory Options) Need a refresher on the core concepts first? Review the Add Memory overview, then come back for the advanced flow. ## Install and authenticate ```bash theme={null} pip install "mem0ai[async]" ``` ```bash theme={null} export MEM0_API_KEY="sk-platform-..." ``` ```python theme={null} import os from mem0 import AsyncMemoryClient memory = AsyncMemoryClient(api_key=os.environ["MEM0_API_KEY"]) ``` ```bash theme={null} npm install mem0ai ``` ```bash theme={null} export MEM0_API_KEY="sk-platform-..." ``` ```typescript theme={null} import { Memory } from "mem0ai"; const memory = new Memory({ apiKey: process.env.MEM0_API_KEY!, async: true }); ``` ## Add memories with metadata and graph context ```python theme={null} conversation = [ {"role": "user", "content": "I'm Morgan, planning a 3-week trip to Japan in May."}, {"role": "assistant", "content": "Great! I'll track dietary notes and cities you mention."}, {"role": "user", "content": "Please remember I avoid shellfish and prefer boutique hotels in Tokyo."}, ] result = await memory.add( conversation, user_id="traveler-42", metadata={"trip": "japan-2025", "preferences": ["boutique", "no-shellfish"]}, enable_graph=True, run_id="planning-call-1", ) ``` ```typescript theme={null} const conversation = [ { role: "user", content: "I'm Morgan, planning a 3-week trip to Japan in May." }, { role: "assistant", content: "Great! I'll track dietary notes and cities you mention." }, { role: "user", content: "Please remember I avoid shellfish and love boutique hotels in Tokyo." }, ]; const result = await memory.add(conversation, { userId: "traveler-42", metadata: { trip: "japan-2025", preferences: ["boutique", "no-shellfish"] }, enableGraph: true, runId: "planning-call-1", }); ``` Successful calls return memories tagged with the metadata you passed. In the dashboard, confirm a graph edge between “Morgan” and “Tokyo” and verify the `trip=japan-2025` tag exists. ## Retrieve and refine ```python theme={null} matches = await memory.search( "Any food alerts?", user_id="traveler-42", filters={"metadata.trip": "japan-2025"}, rerank=True, include_vectors=False, ) ``` ```python theme={null} await memory.update( memory_id=matches["results"][0]["id"], content="Morgan avoids shellfish and prefers boutique hotels in central Tokyo.", ) ``` ```typescript theme={null} const matches = await memory.search("Any food alerts?", { userId: "traveler-42", filters: { "metadata.trip": "japan-2025" }, rerank: true, includeVectors: false, }); ``` ```typescript theme={null} await memory.update(matches.results[0].id, { content: "Morgan avoids shellfish and prefers boutique hotels in central Tokyo.", }); ``` Need to pause graph writes on a per-request basis? Pass `enableGraph: false` (TypeScript) or `enable_graph=False` (Python) when latency matters more than relationship building. ## Clean up ```python theme={null} await memory.delete_all(user_id="traveler-42", run_id="planning-call-1") ``` ```typescript theme={null} await memory.deleteAll({ userId: "traveler-42", runId: "planning-call-1" }); ``` ## Quick recovery * `Missing required key enableGraph`: update the SDK to `mem0ai>=0.4.0`. * `Graph backend unavailable`: retry with `enableGraph=False` and inspect your graph provider status. * Empty results with filters: log `filters` values and confirm metadata keys match (case-sensitive). Metadata keys become part of your filtering schema. Stick to lowercase snake\_case (`trip_id`, `preferences`) to avoid collisions down the road. # Contribution Hub Source: https://docs.mem0.ai/platform/contribute Follow the shared playbook for writing and updating Mem0 documentation. # Build Mem0 Docs the Right Way **Who this is for** * Contributors and LLM assistants updating the docs * Reviewers vetting new pages before publication * Maintainers syncing live docs with the template library Check your team’s latest checklist or guidance so the update keeps the right navigation flow, CTA pattern, and language coverage. Select the doc type you are writing (quickstart, feature guide, migration, etc.) and copy the skeleton from the template library below. Fill the skeleton completely, include inline verification callouts, and jot down any open questions for maintainers before opening a PR. When previewing locally, confirm the page ends with exactly two CTA cards, includes both Python and TypeScript examples when they exist, and keeps all Mintlify icons (no emojis). ## Template Library Choose the document type you need. Each card links directly to the canonical template inside this repo. ## Contribution Checklist Confirm you copied the exact skeleton (`✅ COPY THIS` block) and removed every placeholder. Keep the DO-NOT-COPY guidance out of the published doc. Use Mintlify icons, include `` after runnable steps, and ensure Tabs show both Python and TypeScript (or justify the absence with ``). Flag blockers or follow-up work in your PR description so reviewers know what to look for and can update project trackers as needed. # FAQs Source: https://docs.mem0.ai/platform/faqs Mem0 utilizes a sophisticated hybrid database system to efficiently manage and retrieve memories for AI agents and assistants. Each memory is linked to a unique identifier, such as a user ID or agent ID, enabling Mem0 to organize and access memories tailored to specific individuals or contexts. When a message is added to Mem0 via the `add` method, the system extracts pertinent facts and preferences, distributing them across various data stores: a vector database and a graph database. This hybrid strategy ensures that diverse types of information are stored optimally, facilitating swift and effective searches. When an AI agent or LLM needs to access memories, it employs the `search` method. Mem0 conducts a comprehensive search across these data stores, retrieving relevant information from each. The retrieved memories can be seamlessly integrated into the system prompt as required, enhancing the personalization and relevance of responses. * **User, Session, and AI Agent Memory**: Retains information across sessions and interactions for users and AI agents, ensuring continuity and context. * **Adaptive Personalization**: Continuously updates memories based on user interactions and feedback. * **Developer-Friendly API**: Offers a straightforward API for seamless integration into various applications. * **Platform Consistency**: Ensures consistent behavior and data across different platforms and devices. * **Managed Service**: Provides a hosted solution for easy deployment and maintenance. * **Cost Savings**: Saves costs by adding relevant memories instead of complete transcripts to context window Mem0's memory implementation for Large Language Models (LLMs) offers several advantages over Retrieval-Augmented Generation (RAG): * **Entity Relationships**: Mem0 can understand and relate entities across different interactions, unlike RAG which retrieves information from static documents. This leads to a deeper understanding of context and relationships. * **Contextual Continuity**: Mem0 retains information across sessions, maintaining continuity in conversations and interactions, which is essential for long-term engagement applications like virtual companions or personalized learning assistants. * **Adaptive Learning**: Mem0 improves its personalization based on user interactions and feedback, making the memory more accurate and tailored to individual users over time. * **Dynamic Updates**: Mem0 can dynamically update its memory with new information and interactions, unlike RAG which relies on static data. This allows for real-time adjustments and improvements, enhancing the user experience. These advanced memory capabilities make Mem0 a powerful tool for developers aiming to create personalized and context-aware AI applications. * **Personalized Learning Assistants**: Long-term memory allows learning assistants to remember user preferences, strengths and weaknesses, and progress, providing a more tailored and effective learning experience. * **Customer Support AI Agents**: By retaining information from previous interactions, customer support bots can offer more accurate and context-aware assistance, improving customer satisfaction and reducing resolution times. * **Healthcare Assistants**: Long-term memory enables healthcare assistants to keep track of patient history, medication schedules, and treatment plans, ensuring personalized and consistent care. * **Virtual Companions**: Virtual companions can use long-term memory to build deeper relationships with users by remembering personal details, preferences, and past conversations, making interactions more delightful. * **Productivity Tools**: Long-term memory helps productivity tools remember user habits, frequently used documents, and task history, streamlining workflows and enhancing efficiency. * **Gaming AI**: In gaming, AI with long-term memory can create more immersive experiences by remembering player choices, strategies, and progress, adapting the game environment accordingly. Mem0 uses a sophisticated classification system to determine which parts of text should be extracted as memories. Not all text content will generate memories, as the system is designed to identify specific types of memorable information. There are several scenarios where mem0 may return an empty list of memories: * When users input definitional questions (e.g., "What is backpropagation?") * For general concept explanations that don't contain personal or experiential information * Technical definitions and theoretical explanations * General knowledge statements without personal context * Abstract or theoretical content Example Scenarios ``` Input: "What is machine learning?" No memories extracted - Content is definitional and does not meet memory classification criteria. Input: "Yesterday I learned about machine learning in class" Memory extracted - Contains personal experience and temporal context. ``` Best Practices To ensure successful memory extraction: * Include temporal markers (when events occurred) * Add personal context or experiences * Frame information in terms of real-world applications or experiences * Include specific examples or cases rather than general definitions When deploying Mem0 on AWS Lambda, you'll need to modify the storage directory configuration due to Lambda's file system restrictions. By default, Lambda only allows writing to the `/tmp` directory. To configure Mem0 for AWS Lambda, set the `MEM0_DIR` environment variable to point to a writable directory in `/tmp`: ```bash theme={null} MEM0_DIR=/tmp/.mem0 ``` If you're not using environment variables, you'll need to modify the storage path in your code: ```python theme={null} # Change from home_dir = os.path.expanduser("~") mem0_dir = os.environ.get("MEM0_DIR") or os.path.join(home_dir, ".mem0") # To mem0_dir = os.environ.get("MEM0_DIR", "/tmp/.mem0") ``` Note that the `/tmp` directory in Lambda has a size limit of 512MB and its contents are not persistent between function invocations. Metadata is the recommended approach for incorporating additional information with Mem0. You can store any type of structured data as metadata during the `add` method, such as location, timestamp, weather conditions, user state, or application context. This enriches your memories with valuable contextual information that can be used for more precise retrieval and filtering. During retrieval, you have two main approaches for using metadata: 1. **Pre-filtering**: Include metadata parameters in your initial search query to narrow down the memory pool 2. **Post-processing**: Retrieve a broader set of memories based on query, then apply metadata filters to refine the results Examples of useful metadata you might store: * **Contextual information**: Location, time, device type, application state * **User attributes**: Preferences, skill levels, demographic information * **Interaction details**: Conversation topics, sentiment, urgency levels * **Custom tags**: Any domain-specific categorization relevant to your application This flexibility allows you to create highly contextually aware AI applications that can adapt to specific user needs and situations. Metadata provides an additional dimension for memory retrieval, enabling more precise and relevant responses. To disable telemetry in Mem0, you can set the `MEM0_TELEMETRY` environment variable to `False`: ```bash theme={null} MEM0_TELEMETRY=False ``` You can also disable telemetry programmatically in your code: ```python theme={null} import os os.environ["MEM0_TELEMETRY"] = "False" ``` Setting this environment variable will prevent Mem0 from collecting and sending any usage data, ensuring complete privacy for your application. # Advanced Retrieval Source: https://docs.mem0.ai/platform/features/advanced-retrieval Advanced memory search with keyword expansion, intelligent reranking, and precision filtering ## What is Advanced Retrieval? Advanced Retrieval gives you precise control over how memories are found and ranked. While basic search uses semantic similarity, these advanced options help you find exactly what you need, when you need it. ## Search Enhancement Options ### Keyword Search Expands results to include memories with specific terms, names, and technical keywords. * Searching for specific entities, names, or technical terms * Need comprehensive coverage of a topic * Want broader recall even if some results are less relevant * Working with domain-specific terminology ```python Python theme={null} # Find memories containing specific food-related terms results = client.search( query="What foods should I avoid?", keyword_search=True, user_id="user123" ) # Results might include: # ✓ "Allergic to peanuts and shellfish" # ✓ "Lactose intolerant - avoid dairy" # ✓ "Mentioned avoiding gluten last week" ``` * **Latency**: \~10ms additional * **Recall**: Significantly increased * **Precision**: Slightly decreased * **Best for**: Entity search, comprehensive coverage ### Reranking Reorders results using deep semantic understanding to put the most relevant memories first. * Need the most relevant result at the top * Result order is critical for your application * Want consistent quality across different queries * Building user-facing features where accuracy matters ```python Python theme={null} # Get the most relevant travel plans first results = client.search( query="What are my upcoming travel plans?", rerank=True, user_id="user123" ) # Before reranking: After reranking: # 1. "Went to Paris" → 1. "Tokyo trip next month" # 2. "Tokyo trip next" → 2. "Need to book hotel in Tokyo" # 3. "Need hotel" → 3. "Went to Paris last year" ``` * **Latency**: 150-200ms additional * **Accuracy**: Significantly improved * **Ordering**: Much more relevant * **Best for**: Top-N precision, user-facing results ### Memory Filtering Filters results to keep only the most precisely relevant memories. * Need highly specific, focused results * Working with large datasets where noise is problematic * Quality over quantity is essential * Building production or safety-critical applications ```python Python theme={null} # Get only the most relevant dietary restrictions results = client.search( query="What are my dietary restrictions?", filter_memories=True, user_id="user123" ) # Before filtering: After filtering: # • "Allergic to nuts" → • "Allergic to nuts" # • "Likes Italian food" → • "Vegetarian diet" # • "Vegetarian diet" → # • "Eats dinner at 7pm" → ``` * **Latency**: 200-300ms additional * **Precision**: Maximized * **Recall**: May be reduced * **Best for**: Focused queries, production systems ## Real-World Use Cases ```python Python theme={null} # Smart home assistant finding device preferences results = client.search( query="How do I like my bedroom temperature?", keyword_search=True, # Find specific temperature mentions rerank=True, # Get most recent preferences first user_id="user123" ) # Finds: "Keep bedroom at 68°F", "Too cold last night at 65°F", etc. ``` ```python Python theme={null} # Find specific product issues with high precision results = client.search( query="Problems with premium subscription billing", keyword_search=True, # Find "premium", "billing", "subscription" filter_memories=True, # Only billing-related issues user_id="customer456" ) # Returns only relevant billing problems, not general questions ``` ```python Python theme={null} # Critical medical information needs perfect accuracy results = client.search( query="Patient allergies and contraindications", rerank=True, # Most important info first filter_memories=True, # Only medical restrictions user_id="patient789" ) # Ensures critical allergy info appears first and filters out non-medical data ``` ```python Python theme={null} # Find learning progress for specific topics results = client.search( query="Python programming progress and difficulties", keyword_search=True, # Find "Python", "programming", specific concepts rerank=True, # Recent progress first user_id="student123" ) # Gets comprehensive view of Python learning journey ``` ## Choosing the Right Combination ### Recommended Configurations ```python Python theme={null} # Fast and broad - good for exploration def quick_search(query, user_id): return client.search( query=query, keyword_search=True, user_id=user_id ) # Balanced - good for most applications def standard_search(query, user_id): return client.search( query=query, keyword_search=True, rerank=True, user_id=user_id ) # High precision - good for critical applications def precise_search(query, user_id): return client.search( query=query, rerank=True, filter_memories=True, user_id=user_id ) ``` ```javascript JavaScript theme={null} // Fast and broad - good for exploration function quickSearch(query, userId) { return client.search(query, { user_id: userId, keyword_search: true }); } // Balanced - good for most applications function standardSearch(query, userId) { return client.search(query, { user_id: userId, keyword_search: true, rerank: true }); } // High precision - good for critical applications function preciseSearch(query, userId) { return client.search(query, { user_id: userId, rerank: true, filter_memories: true }); } ``` ## Best Practices ### Do * Start simple with just one enhancement and measure impact * Use keyword search for entity-heavy queries (names, places, technical terms) * Use reranking when the top result quality matters most * Use filtering for production systems where precision is critical * Handle empty results gracefully when filtering is too aggressive * Monitor latency and adjust based on your application's needs ### Don't * Enable all options by default without measuring necessity * Use filtering for broad exploratory queries * Ignore latency impact in real-time applications * Forget to handle cases where filtering returns no results * Use advanced retrieval for simple, fast lookup scenarios ## Performance Guidelines ### Latency Expectations ```python Python theme={null} # Performance monitoring example import time start_time = time.time() results = client.search( query="user preferences", keyword_search=True, # +10ms rerank=True, # +150ms filter_memories=True, # +250ms user_id="user123" ) latency = time.time() - start_time print(f"Search completed in {latency:.2f}s") # ~0.41s expected ``` ### Optimization Tips 1. **Cache frequent queries** to avoid repeated advanced processing 2. **Use session-specific search** with `run_id` to reduce search space 3. **Implement fallback logic** when filtering returns empty results 4. **Monitor and alert** on search latency patterns # Async Client Source: https://docs.mem0.ai/platform/features/async-client Asynchronous client for Mem0 The `AsyncMemoryClient` is an asynchronous client for interacting with the Mem0 API. It provides similar functionality to the synchronous `MemoryClient` but allows for non-blocking operations, which can be beneficial in applications that require high concurrency. ## Initialization To use the async client, you first need to initialize it: ```python Python theme={null} import os from mem0 import AsyncMemoryClient os.environ["MEM0_API_KEY"] = "your-api-key" client = AsyncMemoryClient() ``` ```javascript JavaScript theme={null} const { MemoryClient } = require('mem0ai'); const client = new MemoryClient({ apiKey: 'your-api-key'}); ``` ## Methods The `AsyncMemoryClient` provides the following methods: ### Add Add a new memory asynchronously. ```python Python theme={null} messages = [ {"role": "user", "content": "Alice loves playing badminton"}, {"role": "assistant", "content": "That's great! Alice is a fitness freak"}, ] await client.add(messages, user_id="alice") ``` ```javascript JavaScript theme={null} const messages = [ {"role": "user", "content": "Alice loves playing badminton"}, {"role": "assistant", "content": "That's great! Alice is a fitness freak"}, ]; await client.add(messages, { user_id: "alice" }); ``` ### Search Search for memories based on a query asynchronously. ```python Python theme={null} await client.search("What is Alice's favorite sport?", user_id="alice") ``` ```javascript JavaScript theme={null} await client.search("What is Alice's favorite sport?", { user_id: "alice" }); ``` ### Get All Retrieve all memories for a user asynchronously. `get_all()` now requires filters to be specified. ```python Python theme={null} await client.get_all(filters={"AND": [{"user_id": "alice"}]}) ``` ```javascript JavaScript theme={null} await client.getAll({ filters: {"AND": [{"user_id": "alice"}]} }); ``` ### Delete Delete a specific memory asynchronously. ```python Python theme={null} await client.delete(memory_id="memory-id-here") ``` ```javascript JavaScript theme={null} await client.delete("memory-id-here"); ``` ### Delete All Delete all memories for a user asynchronously. ```python Python theme={null} await client.delete_all(user_id="alice") ``` ```javascript JavaScript theme={null} await client.deleteAll({ user_id: "alice" }); ``` ### History Get the history of a specific memory asynchronously. ```python Python theme={null} await client.history(memory_id="memory-id-here") ``` ```javascript JavaScript theme={null} await client.history("memory-id-here"); ``` ### Users Get all users, agents, and runs which have memories associated with them asynchronously. ```python Python theme={null} await client.users() ``` ```javascript JavaScript theme={null} await client.users(); ``` ### Reset Reset the client, deleting all users and memories asynchronously. ```python Python theme={null} await client.reset() ``` ```javascript JavaScript theme={null} await client.reset(); ``` ## Conclusion The `AsyncMemoryClient` provides a powerful way to interact with the Mem0 API asynchronously, allowing for more efficient and responsive applications. By using this client, you can perform memory operations without blocking your application's execution. If you have any questions or need further assistance, please don't hesitate to reach out: # Async Mode Default Change Source: https://docs.mem0.ai/platform/features/async-mode-default-change Important update to Memory Addition API behavior **Important Change** The `async_mode` parameter defaults to `true` for all memory additions, changing the default API behavior to asynchronous processing. ## Overview The Memory Addition API processes all memory additions asynchronously by default. This change improves performance and scalability by queuing memory operations in the background, allowing your application to continue without waiting for memory processing to complete. ## What's Changing The parameter `async_mode` will default to `true` instead of `false`. This means memory additions will be **processed asynchronously** by default - queued for background execution instead of waiting for processing to complete. ## Behavior Comparison ### Old Default Behavior (async\_mode = false) When `async_mode` was set to `false`, the API returned fully processed memory objects immediately: ```json theme={null} { "results": [ { "id": "de0ee948-af6a-436c-835c-efb6705207de", "event": "ADD", "memory": "User Order #1234 was for a 'Nova 2000'", "structured_attributes": { "day": 13, "hour": 16, "year": 2025, "month": 10, "minute": 59, "quarter": 4, "is_weekend": false, "day_of_week": "monday", "day_of_year": 286, "week_of_year": 42 } } ] } ``` ### New Default Behavior (async\_mode = true) With `async_mode` defaulting to `true`, memory processing is queued in the background and the API returns immediately: ```json theme={null} { "results": [ { "message": "Memory processing has been queued for background execution", "status": "PENDING", "event_id": "d7b5282a-0031-4cc2-98ba-5a02d8531e17" } ] } ``` ## Migration Guide ### If You Need Synchronous Processing If your integration relies on receiving the processed memory object immediately, you can explicitly set `async_mode` to `false` in your requests: ```python Python theme={null} from mem0 import MemoryClient client = MemoryClient(api_key="your-api-key") # Explicitly set async_mode=False to preserve synchronous behavior messages = [ {"role": "user", "content": "I ordered a Nova 2000"} ] result = client.add( messages, user_id="user-123", async_mode=False # This ensures synchronous processing ) ``` ```javascript JavaScript theme={null} const { MemoryClient } = require('mem0ai'); const client = new MemoryClient({ apiKey: 'your-api-key' }); // Explicitly set async_mode: false to preserve synchronous behavior const messages = [ { role: "user", content: "I ordered a Nova 2000" } ]; const result = await client.add(messages, { user_id: "user-123", async_mode: false // This ensures synchronous processing }); ``` ```bash cURL theme={null} curl -X POST https://api.mem0.ai/v1/memories/ \ -H "Authorization: Token your-api-key" \ -H "Content-Type: application/json" \ -d '{ "messages": [ {"role": "user", "content": "I ordered a Nova 2000"} ], "user_id": "user-123", "async_mode": false }' ``` ### If You Want to Adopt Asynchronous Processing If you want to benefit from the improved performance of asynchronous processing: 1. **Remove** any explicit `async_mode=False` parameters from your code 2. **Use webhooks** to receive notifications when memory processing completes Learn more about [Webhooks](/platform/features/webhooks) for real-time notifications about memory events. ## Benefits of Asynchronous Processing Switching to asynchronous processing provides several advantages: * **Faster API Response Times**: Your application doesn't wait for memory processing * **Better Scalability**: Handle more memory additions concurrently * **Improved User Experience**: Reduced latency in your application * **Resource Efficiency**: Background processing optimizes server resources ## Important Notes * The default behavior is now `async_mode=true` for asynchronous processing * Explicitly set `async_mode=false` if you need synchronous behavior * Use webhooks to receive notifications when memories are processed ## Monitoring Memory Processing When using asynchronous mode, use webhooks to receive notifications about memory events: Learn how to set up webhooks for memory processing events You can also retrieve all processed memories at any time: ```python Python theme={null} # Retrieve all memories for a user # Note: get_all now requires filters memories = client.get_all(filters={"AND": [{"user_id": "user-123"}]}) ``` ```javascript JavaScript theme={null} // Retrieve all memories for a user // Note: getAll now requires filters const memories = await client.getAll({ filters: {"AND": [{"user_id": "user-123"}]} }); ``` ## Need Help? If you have questions about this change or need assistance updating your integration: ## Related Documentation Learn about the asynchronous client for Mem0 View the complete API reference for adding memories Configure webhooks for memory processing events Understand memory addition operations # Contextual Memory Creation Source: https://docs.mem0.ai/platform/features/contextual-add Add messages with automatic context management - no manual history tracking required ## What is Contextual Memory Creation? Contextual memory creation automatically manages message history, allowing you to focus on building AI experiences without manually tracking interactions. Simply send new messages, and Mem0 handles the context automatically. ```python Python theme={null} # Just send new messages - Mem0 handles the context messages = [ {"role": "user", "content": "I love Italian food, especially pasta"}, {"role": "assistant", "content": "Great! I'll remember your preference for Italian cuisine."} ] client.add(messages, user_id="user123") ``` ```javascript JavaScript theme={null} // Just send new messages - Mem0 handles the context const messages = [ {"role": "user", "content": "I love Italian food, especially pasta"}, {"role": "assistant", "content": "Great! I'll remember your preference for Italian cuisine."} ]; await client.add(messages, { user_id: "user123", version: "v2" }); ``` ## Why Use Contextual Memory Creation? * **Simple**: Send only new messages, no manual history tracking * **Efficient**: Smaller payloads and faster processing * **Automatic**: Context management handled by Mem0 * **Reliable**: No risk of missing interaction history * **Scalable**: Works seamlessly as your application grows ## How It Works ### Basic Usage ```python Python theme={null} # First interaction messages1 = [ {"role": "user", "content": "Hi, I'm Sarah from New York"}, {"role": "assistant", "content": "Hello Sarah! Nice to meet you."} ] client.add(messages1, user_id="sarah") # Later interaction - just send new messages messages2 = [ {"role": "user", "content": "I'm planning a trip to Italy next month"}, {"role": "assistant", "content": "How exciting! Italy is beautiful this time of year."} ] client.add(messages2, user_id="sarah") # Mem0 automatically knows Sarah is from New York and can use this context ``` ```javascript JavaScript theme={null} // First interaction const messages1 = [ {"role": "user", "content": "Hi, I'm Sarah from New York"}, {"role": "assistant", "content": "Hello Sarah! Nice to meet you."} ]; await client.add(messages1, { user_id: "sarah", version: "v2" }); // Later interaction - just send new messages const messages2 = [ {"role": "user", "content": "I'm planning a trip to Italy next month"}, {"role": "assistant", "content": "How exciting! Italy is beautiful this time of year."} ]; await client.add(messages2, { user_id: "sarah", version: "v2" }); // Mem0 automatically knows Sarah is from New York and can use this context ``` ## Organization Strategies Choose the right approach based on your application's needs: ### User-Level Memories (`user_id` only) **Best for:** Personal preferences, profile information, long-term user data ```python Python theme={null} # Persistent user memories across all interactions messages = [ {"role": "user", "content": "I'm allergic to nuts and dairy"}, {"role": "assistant", "content": "I've noted your allergies for future reference."} ] client.add(messages, user_id="user123") # This allergy info will be available in ALL future interactions ``` ```javascript JavaScript theme={null} // Persistent user memories across all interactions const messages = [ {"role": "user", "content": "I'm allergic to nuts and dairy"}, {"role": "assistant", "content": "I've noted your allergies for future reference."} ]; await client.add(messages, { user_id: "user123", version: "v2" }); // This allergy info will be available in ALL future interactions ``` ### Session-Specific Memories (`user_id` + `run_id`) **Best for:** Task-specific context, separate interaction threads, project-based sessions ```python Python theme={null} # Trip planning session messages1 = [ {"role": "user", "content": "I want to plan a 5-day trip to Tokyo"}, {"role": "assistant", "content": "Perfect! Let's plan your Tokyo adventure."} ] client.add(messages1, user_id="user123", run_id="tokyo-trip-2024") # Later in the same trip planning session messages2 = [ {"role": "user", "content": "I prefer staying near Shibuya"}, {"role": "assistant", "content": "Great choice! Shibuya is very convenient."} ] client.add(messages2, user_id="user123", run_id="tokyo-trip-2024") # Different session for work project (separate context) work_messages = [ {"role": "user", "content": "Let's discuss the Q4 marketing strategy"}, {"role": "assistant", "content": "Sure! What are your main goals for Q4?"} ] client.add(work_messages, user_id="user123", run_id="q4-marketing") ``` ```javascript JavaScript theme={null} // Trip planning session const messages1 = [ {"role": "user", "content": "I want to plan a 5-day trip to Tokyo"}, {"role": "assistant", "content": "Perfect! Let's plan your Tokyo adventure."} ]; await client.add(messages1, { user_id: "user123", run_id: "tokyo-trip-2024", version: "v2" }); // Later in the same trip planning session const messages2 = [ {"role": "user", "content": "I prefer staying near Shibuya"}, {"role": "assistant", "content": "Great choice! Shibuya is very convenient."} ]; await client.add(messages2, { user_id: "user123", run_id: "tokyo-trip-2024", version: "v2" }); // Different session for work project (separate context) const workMessages = [ {"role": "user", "content": "Let's discuss the Q4 marketing strategy"}, {"role": "assistant", "content": "Sure! What are your main goals for Q4?"} ]; await client.add(workMessages, { user_id: "user123", run_id: "q4-marketing", version: "v2" }); ``` ## Real-World Use Cases ```python Python theme={null} # Support ticket context - keeps interaction focused messages = [ {"role": "user", "content": "My subscription isn't working"}, {"role": "assistant", "content": "I can help with that. What specific issue are you experiencing?"}, {"role": "user", "content": "I can't access premium features even though I paid"} ] # Each support ticket gets its own run_id client.add(messages, user_id="customer123", run_id="ticket-2024-001" ) ``` ```python Python theme={null} # Personal preferences (persistent across all interactions) preference_messages = [ {"role": "user", "content": "I prefer morning workouts and vegetarian meals"}, {"role": "assistant", "content": "Got it! I'll keep your fitness and dietary preferences in mind."} ] client.add(preference_messages, user_id="user456") # Daily planning session (session-specific) planning_messages = [ {"role": "user", "content": "Help me plan tomorrow's schedule"}, {"role": "assistant", "content": "Of course! I'll consider your morning workout preference."} ] client.add(planning_messages, user_id="user456", run_id="daily-plan-2024-01-15" ) ``` ```python Python theme={null} # Student profile (persistent) profile_messages = [ {"role": "user", "content": "I'm studying computer science and struggle with math"}, {"role": "assistant", "content": "I'll tailor explanations to help with math concepts."} ] client.add(profile_messages, user_id="student789") # Specific lesson session lesson_messages = [ {"role": "user", "content": "Can you explain algorithms?"}, {"role": "assistant", "content": "Sure! I'll explain algorithms with math-friendly examples."} ] client.add(lesson_messages, user_id="student789", run_id="algorithms-lesson-1" ) ``` ## Best Practices ### ✅ Do * **Organize by context scope**: Use `user_id` only for persistent data, add `run_id` for session-specific context * **Keep messages focused** on the current interaction * **Test with real interaction flows** to ensure context works as expected ### ❌ Don't * Send duplicate messages or interaction history * Skip identifiers like `user_id` or `run_id` that scope the memory * Mix contextual and non-contextual approaches in the same application ## Troubleshooting | Issue | Solution | | ------------------------------- | --------------------------------------------------------------------------------- | | **Context not working** | Ensure each call uses the same `user_id` / `run_id` combo; version is automatic | | **Wrong context retrieved** | Check if you need separate `run_id` values for different interaction topics | | **Missing interaction history** | Verify all messages in the interaction thread use the same `user_id` and `run_id` | | **Too much irrelevant context** | Use more specific `run_id` values to separate different interaction types | # Criteria Retrieval Source: https://docs.mem0.ai/platform/features/criteria-retrieval Mem0's Criteria Retrieval feature allows you to retrieve memories based on your defined criteria. It goes beyond generic semantic relevance and ranks memories based on what matters to your application: emotional tone, intent, behavioral signals, or other custom traits. Instead of just searching for "how similar a memory is to this query," you can define what relevance truly means for your project. For example: * Prioritize joyful memories when building a wellness assistant * Downrank negative memories in a productivity-focused agent * Highlight curiosity in a tutoring agent You define criteria: custom attributes like "joy", "negativity", "confidence", or "urgency", and assign weights to control how they influence scoring. When you search, Mem0 uses these to re-rank semantically relevant memories, favoring those that better match your intent. This gives you nuanced, intent-aware memory search that adapts to your use case. ## When to Use Criteria Retrieval Use Criteria Retrieval if: * You’re building an agent that should react to **emotions** or **behavioral signals** * You want to guide memory selection based on **context**, not just content * You have domain-specific signals like "risk", "positivity", "confidence", etc. that shape recall ## Setting Up Criteria Retrieval Let’s walk through how to configure and use Criteria Retrieval step by step. ### Initialize the Client Before defining any criteria, make sure to initialize the `MemoryClient` with your credentials and project ID: ```python theme={null} from mem0 import MemoryClient client = MemoryClient( api_key="your_mem0_api_key", org_id="your_organization_id", project_id="your_project_id" ) ``` ### Define Your Criteria Each criterion includes: * A `name` (used in scoring) * A `description` (interpreted by the LLM) * A `weight` (how much it influences the final score) ```python theme={null} retrieval_criteria = [ { "name": "joy", "description": "Measure the intensity of positive emotions such as happiness, excitement, or amusement expressed in the sentence. A higher score reflects greater joy.", "weight": 3 }, { "name": "curiosity", "description": "Assess the extent to which the sentence reflects inquisitiveness, interest in exploring new information, or asking questions. A higher score reflects stronger curiosity.", "weight": 2 }, { "name": "emotion", "description": "Evaluate the presence and depth of sadness or negative emotional tone, including expressions of disappointment, frustration, or sorrow. A higher score reflects greater sadness.", "weight": 1 } ] ``` ### Apply Criteria to Your Project Once defined, register the criteria to your project: ```python theme={null} client.project.update(retrieval_criteria=retrieval_criteria) ``` Criteria apply project-wide. Once set, they affect all searches automatically. ## Example Walkthrough After setting up your criteria, you can use them to filter and retrieve memories. Here's an example: ### Add Memories ```python theme={null} messages = [ {"role": "user", "content": "What a beautiful sunny day! I feel so refreshed and ready to take on anything!"}, {"role": "user", "content": "I've always wondered how storms form—what triggers them in the atmosphere?"}, {"role": "user", "content": "It's been raining for days, and it just makes everything feel heavier."}, {"role": "user", "content": "Finally I get time to draw something today, after a long time!! I am super happy today."} ] client.add(messages, user_id="alice") ``` ### Run Standard vs. Criteria-Based Search ```python theme={null} # Search with criteria enabled filters = {"user_id": "alice"} results_with_criteria = client.search( query="Why I am feeling happy today?", filters=filters ) # To disable criteria for a specific search results_without_criteria = client.search( query="Why I am feeling happy today?", filters=filters, use_criteria=False # Disable criteria-based scoring ) ``` ### Compare Results ### Search Results (with Criteria) ```python theme={null} [ {"memory": "User feels refreshed and ready to take on anything on a beautiful sunny day", "score": 0.666, ...}, {"memory": "User finally has time to draw something after a long time", "score": 0.616, ...}, {"memory": "User is happy today", "score": 0.500, ...}, {"memory": "User is curious about how storms form and what triggers them in the atmosphere.", "score": 0.400, ...}, {"memory": "It has been raining for days, making everything feel heavier.", "score": 0.116, ...} ] ``` ### Search Results (without Criteria) ```python theme={null} [ {"memory": "User is happy today", "score": 0.607, ...}, {"memory": "User feels refreshed and ready to take on anything on a beautiful sunny day", "score": 0.512, ...}, {"memory": "It has been raining for days, making everything feel heavier.", "score": 0.4617, ...}, {"memory": "User is curious about how storms form and what triggers them in the atmosphere.", "score": 0.340, ...}, {"memory": "User finally has time to draw something after a long time", "score": 0.336, ...}, ] ``` ## Search Results Comparison 1. **Memory Ordering**: With criteria, memories with high joy scores (like feeling refreshed and drawing) are ranked higher. Without criteria, the most relevant memory ("User is happy today") comes first. 2. **Score Distribution**: With criteria, scores are more spread out (0.116 to 0.666) and reflect the criteria weights. Without criteria, scores are more clustered (0.336 to 0.607) and based purely on relevance. 3. **Trait Sensitivity**: "Rainy day" content is penalized due to negative tone, while "Storm curiosity" is recognized and scored accordingly. ## Key Differences vs. Standard Search | Aspect | Standard Search | Criteria Retrieval | | ---------------------- | ----------------------------- | ----------------------------------------------- | | Ranking Logic | Semantic similarity only | Semantic + LLM-based criteria scoring | | Control Over Relevance | None | Fully customizable with weighted criteria | | Memory Reordering | Static based on similarity | Dynamically re-ranked by intent alignment | | Emotional Sensitivity | No tone or trait awareness | Incorporates emotion, tone, or custom behaviors | | Activation | Default (no criteria defined) | Enabled when criteria are defined in project | If no criteria are defined for a project, search behaves normally based on semantic similarity only. ## Best Practices * Choose 3-5 criteria that reflect your application's intent * Make descriptions clear and distinct; these are interpreted by an LLM * Use stronger weights to amplify the impact of important traits * Avoid redundant or ambiguous criteria (e.g., "positivity" and "joy") * Always handle empty result sets in your application logic ## How It Works 1. **Criteria Definition**: Define custom criteria with a name, description, and weight. These describe what matters in a memory (e.g., joy, urgency, empathy). 2. **Project Configuration**: Register these criteria using `project.update()`. They apply at the project level and automatically influence all searches. 3. **Memory Retrieval**: When you perform a search, Mem0 first retrieves relevant memories based on the query. 4. **Weighted Scoring**: Each retrieved memory is evaluated and scored against your defined criteria and weights. This lets you prioritize memories that align with your agent's goals and not just those that look similar to the query. Criteria retrieval is automatically enabled when criteria are defined in your project. Use `use_criteria=False` in search to temporarily disable it for a specific query. ## Summary * Define what "relevant" means using criteria * Apply them per project via `project.update()` * Criteria-aware search activates automatically when criteria are configured * Build agents that reason not just with relevance, but **contextual importance** *** Need help designing or tuning your criteria? # Custom Categories Source: https://docs.mem0.ai/platform/features/custom-categories Teach Mem0 the labels that matter to your team. # Custom Categories Mem0 automatically tags every memory, but the default labels (travel, sports, music, etc.) may not match the names your app uses. Custom categories let you replace that list so the tags line up with your own wording. **Use custom categories when…** * You need Mem0 to tag memories with names your product team already uses. * You want clean reports or automations that rely on those tags. * You’re moving from the open-source version and want the same labels here. Per-request overrides (`custom_categories=...` on `client.add`) are not supported on the managed API yet. Set categories at the project level, then ingest memories as usual. ## Configure access * Ensure `MEM0_API_KEY` is set in your environment or pass it to the SDK constructor. * If you scope work to a specific organization/project, initialize the client with those identifiers. ## How it works * **Default list** — Each project starts with 15 broad categories like `travel`, `sports`, and `music`. * **Project override** — When you call `project.update(custom_categories=[...])`, that list replaces the defaults for future memories. * **Automatic tags** — As new memories come in, Mem0 picks the closest matches from your list and saves them in the `categories` field. Default catalog: `personal_details`, `family`, `professional_details`, `sports`, `travel`, `food`, `music`, `health`, `technology`, `hobbies`, `fashion`, `entertainment`, `milestones`, `user_preferences`, `misc`. ## Configure it ### 1. Set custom categories at the project level ```python Code theme={null} import os from mem0 import MemoryClient os.environ["MEM0_API_KEY"] = "your-api-key" client = MemoryClient() # Update custom categories new_categories = [ {"lifestyle_management_concerns": "Tracks daily routines, habits, hobbies and interests including cooking, time management and work-life balance"}, {"seeking_structure": "Documents goals around creating routines, schedules, and organized systems in various life areas"}, {"personal_information": "Basic information about the user including name, preferences, and personality traits"} ] response = client.project.update(custom_categories=new_categories) print(response) ``` ```json Output theme={null} { "message": "Updated custom categories" } ``` ### 2. Confirm the active catalog ```python Code theme={null} # Get current custom categories categories = client.project.get(fields=["custom_categories"]) print(categories) ``` ```json Output theme={null} { "custom_categories": [ {"lifestyle_management_concerns": "Tracks daily routines, habits, hobbies and interests including cooking, time management and work-life balance"}, {"seeking_structure": "Documents goals around creating routines, schedules, and organized systems in various life areas"}, {"personal_information": "Basic information about the user including name, preferences, and personality traits"} ] } ``` ## See it in action ### Add a memory (uses the project catalog automatically) ```python Code theme={null} messages = [ {"role": "user", "content": "My name is Alice. I need help organizing my daily schedule better. I feel overwhelmed trying to balance work, exercise, and social life."}, {"role": "assistant", "content": "I understand how overwhelming that can feel. Let's break this down together. What specific areas of your schedule feel most challenging to manage?"}, {"role": "user", "content": "I want to be more productive at work, maintain a consistent workout routine, and still have energy for friends and hobbies."}, {"role": "assistant", "content": "Those are great goals for better time management. What's one small change you could make to start improving your daily routine?"}, ] # Add memories with project-level custom categories client.add(messages, user_id="alice", async_mode=False) ``` ### Retrieve memories and inspect categories ```python Code theme={null} memories = client.get_all(filters={"user_id": "alice"}) ``` ```json Output theme={null} ["lifestyle_management_concerns", "seeking_structure"] ``` **Sample memory payload** ```json theme={null} { "id": "33d2***", "memory": "Trying to balance work and workouts", "user_id": "alice", "metadata": null, "categories": ["wellness"], // ← matches the custom category we set "created_at": "2025-11-01T02:13:32.828364-07:00", "updated_at": "2025-11-01T02:13:32.830896-07:00", "expiration_date": null, "structured_attributes": { "day": 1, "hour": 9, "year": 2025, "month": 11, "minute": 13, "quarter": 4, "is_weekend": true, "day_of_week": "saturday", "day_of_year": 305, "week_of_year": 44 } } ``` Need ad-hoc labels for a single call? Store them in `metadata` until per-request overrides become available. ## Default categories (fallback) If you do nothing, memories are tagged with the built-in set below. ``` - personal_details - family - professional_details - sports - travel - food - music - health - technology - hobbies - fashion - entertainment - milestones - user_preferences - misc ``` ```python Code theme={null} import os from mem0 import MemoryClient os.environ["MEM0_API_KEY"] = "your-api-key" client = MemoryClient() messages = [ {"role": "user", "content": "Hi, my name is Alice."}, {"role": "assistant", "content": "Hi Alice, what sports do you like to play?"}, {"role": "user", "content": "I love playing badminton, football, and basketball. I'm quite athletic!"}, {"role": "assistant", "content": "That's great! Alice seems to enjoy both individual sports like badminton and team sports like football and basketball."}, {"role": "user", "content": "Sometimes, I also draw and sketch in my free time."}, {"role": "assistant", "content": "That's cool! I'm sure you're good at it."} ] # Add memories with default categories client.add(messages, user_id='alice', async_mode=False) ``` ```python Memories with categories theme={null} # Following categories will be created for the memories added Sometimes draws and sketches in free time (hobbies) Is quite athletic (sports) Loves playing badminton, football, and basketball (sports) Name is Alice (personal_details) ``` You can verify the defaults are active by checking: ```python Code theme={null} client.project.get(["custom_categories"]) ``` ```json Output theme={null} { "custom_categories": None } ``` ## Verify the feature is working * `client.project.get(["custom_categories"])` returns the category list you set. * `client.get_all(filters={"user_id": ...})` shows populated `categories` lists on new memories. * The Mem0 dashboard (Project → Memories) displays the custom labels in the Category column. ## Best practices * Keep category descriptions concise but specific; the classifier uses them to disambiguate. * Review memories with empty `categories` to see where you might extend or rename your list. * Stick with project-level overrides until per-request support is released; mixing approaches causes confusion. Explore other ingestion tunables like custom prompts and selective writes. See custom tagging drive personalization in a full agent workflow. # Custom Instructions Source: https://docs.mem0.ai/platform/features/custom-instructions Control how Mem0 extracts and stores memories using natural language guidelines ## What are Custom Instructions? Custom instructions are natural language guidelines that let you define exactly what Mem0 should include or exclude when creating memories from conversations. This gives you precise control over what information is extracted, acting as smart filters so your AI application only remembers what matters for your use case. ```python Python theme={null} # Simple example: Health app focusing on wellness prompt = """ Extract only health and wellness information: - Symptoms, medications, and treatments - Exercise routines and dietary habits - Doctor appointments and health goals Exclude: Personal identifiers, financial data """ client.project.update(custom_instructions=prompt) ``` ```javascript JavaScript theme={null} // Simple example: Health app focusing on wellness const prompt = ` Extract only health and wellness information: - Symptoms, medications, and treatments - Exercise routines and dietary habits - Doctor appointments and health goals Exclude: Personal identifiers, financial data `; await client.project.update({ custom_instructions: prompt }); ``` ## Why Use Custom Instructions? * **Focus on What Matters**: Only capture information relevant to your application * **Maintain Privacy**: Explicitly exclude sensitive data like passwords or personal identifiers * **Ensure Consistency**: All memories follow the same extraction rules across your project * **Improve Quality**: Filter out noise and irrelevant conversations ## How to Set Custom Instructions ### Basic Setup ```python Python theme={null} # Set instructions for your project client.project.update(custom_instructions="Your guidelines here...") # Retrieve current instructions response = client.project.get(fields=["custom_instructions"]) print(response["custom_instructions"]) ``` ```javascript JavaScript theme={null} // Set instructions for your project await client.project.update({ custom_instructions: "Your guidelines here..." }); // Retrieve current instructions const response = await client.project.get({ fields: ["custom_instructions"] }); console.log(response.custom_instructions); ``` ### Best Practice Template Structure your instructions using this proven template: ``` Your Task: [Brief description of what to extract] Information to Extract: 1. [Category 1]: - [Specific details] - [What to look for] 2. [Category 2]: - [Specific details] - [What to look for] Guidelines: - [Processing rules] - [Quality requirements] Exclude: - [Sensitive data to avoid] - [Irrelevant information] ``` ## Real-World Examples ```python Python theme={null} instructions = """ Extract customer service information for better support: 1. Product Issues: - Product names, SKUs, defects - Return/exchange requests - Quality complaints 2. Customer Preferences: - Preferred brands, sizes, colors - Shopping frequency and habits - Price sensitivity 3. Service Experience: - Satisfaction with support - Resolution time expectations - Communication preferences Exclude: Payment card numbers, passwords, personal identifiers. """ client.project.update(custom_instructions=instructions) ``` ```javascript JavaScript theme={null} const instructions = ` Extract customer service information for better support: 1. Product Issues: - Product names, SKUs, defects - Return/exchange requests - Quality complaints 2. Customer Preferences: - Preferred brands, sizes, colors - Shopping frequency and habits - Price sensitivity 3. Service Experience: - Satisfaction with support - Resolution time expectations - Communication preferences Exclude: Payment card numbers, passwords, personal identifiers. `; await client.project.update({ custom_instructions: instructions }); ``` ```python Python theme={null} education_prompt = """ Extract learning-related information for personalized education: 1. Learning Progress: - Course completions and current modules - Skills acquired and improvement areas - Learning goals and objectives 2. Student Preferences: - Learning styles (visual, audio, hands-on) - Time availability and scheduling - Subject interests and career goals 3. Performance Data: - Assignment feedback and patterns - Areas of struggle or strength - Study habits and engagement Exclude: Specific grades, personal identifiers, financial information. """ client.project.update(custom_instructions=education_prompt) ``` ```javascript JavaScript theme={null} const educationPrompt = ` Extract learning-related information for personalized education: 1. Learning Progress: - Course completions and current modules - Skills acquired and improvement areas - Learning goals and objectives 2. Student Preferences: - Learning styles (visual, audio, hands-on) - Time availability and scheduling - Subject interests and career goals 3. Performance Data: - Assignment feedback and patterns - Areas of struggle or strength - Study habits and engagement Exclude: Specific grades, personal identifiers, financial information. `; await client.project.update({ custom_instructions: educationPrompt }); ``` ```python Python theme={null} finance_prompt = """ Extract financial planning information for advisory services: 1. Financial Goals: - Retirement and investment objectives - Risk tolerance and preferences - Short-term and long-term goals 2. Life Events: - Career and income changes - Family changes (marriage, children) - Major planned purchases 3. Investment Interests: - Asset allocation preferences - ESG or ethical investment interests - Previous investment experience Exclude: Account numbers, SSNs, passwords, specific financial amounts. """ client.project.update(custom_instructions=finance_prompt) ``` ```javascript JavaScript theme={null} const financePrompt = ` Extract financial planning information for advisory services: 1. Financial Goals: - Retirement and investment objectives - Risk tolerance and preferences - Short-term and long-term goals 2. Life Events: - Career and income changes - Family changes (marriage, children) - Major planned purchases 3. Investment Interests: - Asset allocation preferences - ESG or ethical investment interests - Previous investment experience Exclude: Account numbers, SSNs, passwords, specific financial amounts. `; await client.project.update({ custom_instructions: financePrompt }); ``` ## Advanced Techniques ### Conditional Processing Handle different conversation types with conditional logic: ```python Python theme={null} advanced_prompt = """ Extract information based on conversation context: IF customer support conversation: - Issue type, severity, resolution status - Customer satisfaction indicators IF sales conversation: - Product interests, budget range - Decision timeline and influencers IF onboarding conversation: - User experience level - Feature interests and priorities Always exclude personal identifiers and maintain professional context. """ client.project.update(custom_instructions=advanced_prompt) ``` ### Testing Your Instructions Always test your custom instructions with real message examples: ```python Python theme={null} # Test with sample messages messages = [ {"role": "user", "content": "I'm having billing issues with my subscription"}, {"role": "assistant", "content": "I can help with that. What's the specific problem?"}, {"role": "user", "content": "I'm being charged twice each month"} ] # Add the messages and check extracted memories result = client.add(messages, user_id="test_user") memories = client.get_all(filters={"AND": [{"user_id": "test_user"}]}) # Review if the right information was extracted for memory in memories: print(f"Extracted: {memory['memory']}") ``` ## Best Practices ### ✅ Do * **Be specific** about what information to extract * **Use clear categories** to organize your instructions * **Test with real conversations** before deploying * **Explicitly state exclusions** for privacy and compliance * **Start simple** and iterate based on results ### ❌ Don't * Make instructions too long or complex * Create conflicting rules within your guidelines * Be overly restrictive (balance specificity with flexibility) * Forget to exclude sensitive information * Skip testing with diverse conversation examples ## Common Issues and Solutions | Issue | Solution | | ----------------------------- | ----------------------------------------------- | | **Instructions too long** | Break into focused categories, keep concise | | **Missing important data** | Add specific examples of what to capture | | **Capturing irrelevant info** | Strengthen exclusion rules and be more specific | | **Inconsistent results** | Clarify guidelines and test with more examples | # Direct Import Source: https://docs.mem0.ai/platform/features/direct-import Bypass the memory deduction phase and directly store pre-defined memories for efficient retrieval ## How to Use Direct Import The Direct Import feature allows users to skip the memory deduction phase and directly input pre-defined memories into the system for storage and retrieval. To enable this feature, set the `infer` parameter to `False` in the `add` method. ```python Python theme={null} messages = [ {"role": "user", "content": "Alice loves playing badminton"}, {"role": "assistant", "content": "That's great! Alice is a fitness freak"}, {"role": "user", "content": "Alice mostly cooks at home because of her gym plan"}, ] client.add(messages, user_id="alice", infer=False) ``` ```markdown Output theme={null} [] ``` You can see that the output of the add call is an empty list. Only messages with the role "user" will be used for storage. Messages with roles such as "assistant" or "system" will be ignored during the storage process. Direct import skips the inference pipeline, so it also skips duplicate detection. If you later send the same fact with `infer=True`, Mem0 will store a second copy. Pick one mode per memory source unless you truly want both versions. ## How to Retrieve Memories You can retrieve memories using the `search` method. ```python Python theme={null} client.search("What is Alice's favorite sport?", user_id="alice") ``` ```json Output theme={null} { "results": [ { "id": "19d6d7aa-2454-4e58-96fc-e74d9e9f8dd1", "memory": "Alice loves playing badminton", "user_id": "pc123", "metadata": null, "categories": null, "created_at": "2024-10-15T21:52:11.474901-07:00", "updated_at": "2024-10-15T21:52:11.474912-07:00" } ] } ``` ## How to Retrieve All Memories You can retrieve all memories using the `get_all` method. `get_all()` now requires filters to be specified. ```python Python theme={null} client.get_all(filters={"AND": [{"user_id": "alice"}]}) ``` ```json Output theme={null} { "results": [ { "id": "19d6d7aa-2454-4e58-96fc-e74d9e9f8dd1", "memory": "Alice loves playing badminton", "user_id": "pc123", "metadata": null, "categories": null, "created_at": "2024-10-15T21:52:11.474901-07:00", "updated_at": "2024-10-15T21:52:11.474912-07:00" }, { "id": "8557f05d-7b3c-47e5-b409-9886f9e314fc", "memory": "Alice mostly cooks at home because of her gym plan", "user_id": "pc123", "metadata": null, "categories": null, "created_at": "2024-10-15T21:52:11.474929-07:00", "updated_at": "2024-10-15T21:52:11.474932-07:00" } ] } ``` If you have any questions, please feel free to reach out to us using one of the following methods: # Entity-Scoped Memory Source: https://docs.mem0.ai/platform/features/entity-scoped-memory Scope conversations by user, agent, app, and session so memories land exactly where they belong. Mem0's Platform API lets you separate memories for different users, agents, and apps. By tagging each write and query with the right identifiers, you can prevent data from mixing between them, maintain clear audit trails, and control data retention. Want the long-form tutorial? The Partition Memories by Entity cookbook walks through multi-agent storage, debugging, and cleanup step by step. **You'll use this when…** * You run assistants for multiple customers who each need private memory spaces * Different agents (like a planner and a critic) need separate context for the same user * Sessions should expire on their own schedule, making debugging and data removal more precise ## Configure access ```python theme={null} from mem0 import MemoryClient client = MemoryClient(api_key="m0-...") ``` Call `client.project.get()` to verify your connection. It should return your project details including `org_id` and `project_id`. If you get a 401 error, generate a new API key in the Mem0 dashboard. ## Feature anatomy | Dimension | Field | When to use it | Example value | | ----------- | ---------- | ------------------------------------------------ | ------------------- | | User | `user_id` | Persistent persona or account | `"customer_6412"` | | Agent | `agent_id` | Distinct agent persona or tool | `"meal_planner"` | | Application | `app_id` | White-label app or product surface | `"ios_retail_demo"` | | Session | `run_id` | Short-lived flow, ticket, or conversation thread | `"ticket-9241"` | * **Writes** (`client.add`) accept any combination of these fields. Absent fields default to `null`. * **Reads** (`client.search`, `client.get_all`, exports, deletes) accept the same identifiers inside the `filters` JSON object. * **Implicit null scoping**: Passing only `{"user_id": "alice"}` automatically restricts results to records where `agent_id`, `app_id`, and `run_id` are `null`. Add wildcards (`"*"`), explicit lists, or additional filters when you need broader joins. **Common Pitfall**: If you create a memory with `user_id="alice"` but the other fields default to `null`, then search with `{"AND": [{"user_id": "alice"}, {"agent_id": "bot"}]}` will return nothing because you're looking for a memory where `agent_id="bot"`, not `null`. ## Choose the right identifier | Identifier | Purpose | Example Use Cases | | ---------- | ------------------------------------------------------------------------------------------ | ------------------------------------------------------ | | `user_id` | Store preferences, profile details, and historical actions that follow a person everywhere | Dietary restrictions, seat preferences, meeting habits | | `agent_id` | Keep an agent's personality, operating modes, or brand voice in one place | Travel agent vs concierge vs customer support personas | | `app_id` | Tag every write from a partner app or deployment for tenant separation | White-label deployments, partner integrations | | `run_id` | Isolate temporary flows that should reset or expire independently | Support tickets, chat sessions, experiments | For more detailed examples, see the Partition Memories by Entity cookbook. ## Configure it The example below adds memories with entity tags: ```python theme={null} messages = [ {"role": "user", "content": "I teach ninth-grade algebra."}, {"role": "assistant", "content": "I'll tailor study plans to algebra topics."} ] client.add( messages, user_id="teacher_872", agent_id="study_planner", app_id="district_dashboard", run_id="prep-period-2025-09-02" ) ``` The response will include one or more memory IDs. Check the dashboard → Memories to confirm the entry appears under the correct user, agent, app, and run. The HTTP equivalent uses `POST /v1/memories/` with the same identifiers in the JSON body. See the Add Memories API reference for REST details. ## See it in action **1. Store scoped memories** ```python theme={null} traveler_messages = [ {"role": "user", "content": "I prefer boutique hotels and avoid shellfish."}, {"role": "assistant", "content": "Logged your travel preferences for future itineraries."} ] client.add( traveler_messages, user_id="customer_6412", agent_id="travel_planner", app_id="concierge_portal", run_id="itinerary-2025-apr", metadata={"category": "preferences"} ) ``` **2. Retrieve by user scope** ```python theme={null} user_scope = { "AND": [ {"user_id": "customer_6412"}, {"app_id": "concierge_portal"}, {"run_id": "itinerary-2025-apr"} ] } user_results = client.search("Any dietary flags?", filters=user_scope) print(user_results) ``` **3. Retrieve by agent scope** ```python theme={null} agent_scope = { "AND": [ {"agent_id": "travel_planner"}, {"app_id": "concierge_portal"} ] } agent_results = client.search("Any dietary flags?", filters=agent_scope) print(agent_results) ``` Writes can include multiple identifiers, but searches resolve one entity space at a time. Query user scope *or* agent scope in a given call—combining both returns an empty list today. Want to experiment with AND/OR logic, nested operators, or wildcards? The Memory Filters v2 guide walks through every filter pattern with working examples. **4. Audit everything for an app** ```python theme={null} app_scope = { "AND": [ {"app_id": "concierge_portal"}, {"user_id": "*"}, {"agent_id": "*"} ] } page = client.get_all(filters=app_scope, page=1, page_size=20) ``` Wildcards (`"*"`) include only non-null values. Use them when you want "any agent" or "any user" without limiting results to null-only records. **5. Clean up a session** ```python theme={null} client.delete_all( user_id="customer_6412", run_id="itinerary-2025-apr" ) ``` A successful delete returns `{"message": "Memories deleted successfully!"}`. Run the previous `get_all` call again to confirm the session memories were removed. ## Verify the feature is working * Run `client.search` with your filters and confirm only expected memories appear. Mismatched identifiers usually mean a typo in your scoping. * Check the Mem0 dashboard filter pills. User, agent, app, and run should all show populated values for your memory entry. * Call `client.delete_all` with a unique `run_id` and confirm other sessions remain intact (the count in `get_all` should only drop for that run). ## Best practices * Use consistent identifier formats (like `team-alpha` or `app-ios-retail`) so you can query or delete entire groups later * When debugging, print your filters before each call to verify wildcards (`"*"`), lists, and run IDs are spelled correctly * Combine entity filters with metadata filters (categories, created\_at) for precise exports or audits * Use `run_id` for temporary sessions like support tickets or experiments, then schedule cleanup jobs to delete them For a complete walkthrough, see the Partition Memories by Entity cookbook. # Expiration Date Source: https://docs.mem0.ai/platform/features/expiration-date Set time-bound memories in Mem0 with automatic expiration dates to manage temporal information effectively. ## Benefits of Memory Expiration Setting expiration dates for memories offers several advantages: * **Time-Sensitive Information Management**: Handle information that is only relevant for a specific time period. * **Event-Based Memory**: Manage information related to upcoming events that becomes irrelevant after the event passes. These benefits enable more sophisticated memory management for applications where temporal context matters. ## Setting Memory Expiration Date You can set an expiration date for memories, after which they will no longer be retrieved in searches. This is useful for creating temporary memories or memories that are relevant only for a specific time period. ```python Python theme={null} import datetime from mem0 import MemoryClient client = MemoryClient(api_key="your-api-key") messages = [ { "role": "user", "content": "I'll be in San Francisco until the end of this month." } ] # Set an expiration date for this memory client.add(messages=messages, user_id="alex", expiration_date=str(datetime.datetime.now().date() + datetime.timedelta(days=30))) # You can also use an explicit date string client.add(messages=messages, user_id="alex", expiration_date="2023-08-31") ``` ```javascript JavaScript theme={null} import MemoryClient from 'mem0ai'; const client = new MemoryClient({ apiKey: 'your-api-key' }); const messages = [ { "role": "user", "content": "I'll be in San Francisco until the end of this month." } ]; // Set an expiration date 30 days from now const expirationDate = new Date(); expirationDate.setDate(expirationDate.getDate() + 30); client.add(messages, { user_id: "alex", expiration_date: expirationDate.toISOString().split('T')[0] }) .then(response => console.log(response)) .catch(error => console.error(error)); // You can also use an explicit date string client.add(messages, { user_id: "alex", expiration_date: "2023-08-31" }) .then(response => console.log(response)) .catch(error => console.error(error)); ``` ```bash cURL theme={null} curl -X POST "https://api.mem0.ai/v1/memories/" \ -H "Authorization: Token your-api-key" \ -H "Content-Type: application/json" \ -d '{ "messages": [ { "role": "user", "content": "I'll be in San Francisco until the end of this month." } ], "user_id": "alex", "expiration_date": "2023-08-31" }' ``` ```json Output theme={null} { "results": [ { "id": "a1b2c3d4-e5f6-4g7h-8i9j-k0l1m2n3o4p5", "data": { "memory": "In San Francisco until the end of this month" }, "event": "ADD" } ] } ``` Once a memory reaches its expiration date, it will not be included in search or get results, though the data remains stored in the system. If you have any questions, please feel free to reach out to us using one of the following methods: # Feedback Mechanism Source: https://docs.mem0.ai/platform/features/feedback-mechanism Mem0's Feedback Mechanism allows you to provide feedback on the memories generated by your application. This feedback is used to improve the accuracy of the memories and search results. ## How it works The feedback mechanism is a simple API that allows you to provide feedback on the memories generated by your application. The feedback is stored in the database and used to improve the accuracy of the memories and search results. Over time, Mem0 continuously learns from this feedback, refining its memory generation and search capabilities for better performance. ## Give Feedback You can give feedback on a memory by calling the `feedback` method on the Mem0 client. ```python Python theme={null} from mem0 import MemoryClient client = MemoryClient(api_key="your_api_key") client.feedback(memory_id="your-memory-id", feedback="NEGATIVE", feedback_reason="I don't like this memory because it is not relevant.") ``` ```javascript JavaScript theme={null} import MemoryClient from 'mem0ai'; const client = new MemoryClient({ apiKey: 'your-api-key'}); client.feedback({ memory_id: "your-memory-id", feedback: "NEGATIVE", feedback_reason: "I don't like this memory because it is not relevant." }) ``` ## Feedback Types The `feedback` parameter can be one of the following values: * `POSITIVE`: The memory is useful. * `NEGATIVE`: The memory is not useful. * `VERY_NEGATIVE`: The memory is not useful at all. ## Parameters The `feedback` method accepts these parameters: | Parameter | Type | Required | Description | | ----------------- | ------ | -------- | ------------------------------------------------------------ | | `memory_id` | string | Yes | The ID of the memory to give feedback on | | `feedback` | string | No | Type of feedback: `POSITIVE`, `NEGATIVE`, or `VERY_NEGATIVE` | | `feedback_reason` | string | No | Optional explanation for the feedback | Pass `None` or `null` to the `feedback` and `feedback_reason` parameters to remove existing feedback for a memory. ## Bulk Feedback Operations For applications with high volumes of feedback, you can provide feedback on multiple memories at once: ```python Python theme={null} from mem0 import MemoryClient client = MemoryClient(api_key="your_api_key") # Bulk feedback example feedback_data = [ { "memory_id": "memory-1", "feedback": "POSITIVE", "feedback_reason": "Accurately captured the user's preference" }, { "memory_id": "memory-2", "feedback": "NEGATIVE", "feedback_reason": "Contains outdated information" } ] for item in feedback_data: client.feedback(**item) ``` ```javascript JavaScript theme={null} import MemoryClient from 'mem0ai'; const client = new MemoryClient({ apiKey: 'your-api-key'}); // Bulk feedback example const feedbackData = [ { memory_id: "memory-1", feedback: "POSITIVE", feedback_reason: "Accurately captured the user's preference" }, { memory_id: "memory-2", feedback: "NEGATIVE", feedback_reason: "Contains outdated information" } ]; for (const item of feedbackData) { await client.feedback(item); } ``` ## Best Practices ### When to Provide Feedback * Immediately after memory retrieval when you can assess relevance * During user interactions when users explicitly indicate satisfaction or dissatisfaction * Through automated evaluation using your application's success metrics ### Effective Feedback Reasons Provide specific, actionable feedback reasons: **Good examples:** * "Contains outdated contact information" * "Accurately captured the user's dietary restrictions" * "Irrelevant to the current conversation context" **Avoid vague reasons:** * "Bad memory" * "Wrong" * "Not good" ### Feedback Strategy 1. Be consistent: Apply the same criteria across similar memories 2. Be specific: Detailed reasons help improve the system faster 3. Monitor patterns: Regular feedback analysis helps identify improvement areas ## Error Handling Handle potential errors when submitting feedback: ```python Python theme={null} from mem0 import MemoryClient from mem0.exceptions import MemoryNotFoundError, APIError client = MemoryClient(api_key="your_api_key") try: client.feedback( memory_id="memory-123", feedback="POSITIVE", feedback_reason="Helpful context for user query" ) print("Feedback submitted successfully") except MemoryNotFoundError: print("Memory not found") except APIError as e: print(f"API error: {e}") except Exception as e: print(f"Unexpected error: {e}") ``` ```javascript JavaScript theme={null} import MemoryClient from 'mem0ai'; const client = new MemoryClient({ apiKey: 'your-api-key'}); try { await client.feedback({ memory_id: "memory-123", feedback: "POSITIVE", feedback_reason: "Helpful context for user query" }); console.log("Feedback submitted successfully"); } catch (error) { if (error.status === 404) { console.log("Memory not found"); } else { console.log(`Error: ${error.message}`); } } ``` ## Feedback Analytics Track the impact of your feedback by monitoring memory performance over time. Consider implementing: * Feedback completion rates: What percentage of memories receive feedback * Feedback distribution: Balance of positive vs. negative feedback * Memory quality trends: How accuracy improves with feedback volume * User satisfaction metrics: Correlation between feedback and user experience # Graph Memory Source: https://docs.mem0.ai/platform/features/graph-memory Enable graph-based memory retrieval for more contextually relevant results ## Overview Graph Memory enhances the memory pipeline by creating relationships between entities in your data. It builds a network of interconnected information for more contextually relevant search results. This feature allows your AI applications to understand connections between entities, providing richer context for responses. It's ideal for applications needing relationship tracking and nuanced information retrieval across related memories. ## How Graph Memory Works The Graph Memory feature analyzes how each entity connects and relates to each other. When enabled: 1. Mem0 automatically builds a graph representation of entities 2. Vector search returns the top semantic matches (with any reranker you configure) 3. Graph relations are returned alongside those results to provide additional context—they do not reorder the vector hits ## Using Graph Memory To use Graph Memory, you need to enable it in your API calls by setting the `enable_graph=True` parameter. ### Adding Memories with Graph Memory When adding new memories, enable Graph Memory to automatically build relationships with existing memories: ```python Python theme={null} from mem0 import MemoryClient client = MemoryClient( api_key="your-api-key", org_id="your-org-id", project_id="your-project-id" ) messages = [ {"role": "user", "content": "My name is Joseph"}, {"role": "assistant", "content": "Hello Joseph, it's nice to meet you!"}, {"role": "user", "content": "I'm from Seattle and I work as a software engineer"} ] # Enable graph memory when adding client.add( messages, user_id="joseph", enable_graph=True ) ``` ```javascript JavaScript theme={null} import { MemoryClient } from "mem0"; const client = new MemoryClient({ apiKey: "your-api-key", org_id: "your-org-id", project_id: "your-project-id" }); const messages = [ { role: "user", content: "My name is Joseph" }, { role: "assistant", content: "Hello Joseph, it's nice to meet you!" }, { role: "user", content: "I'm from Seattle and I work as a software engineer" } ]; // Enable graph memory when adding await client.add({ messages, user_id: "joseph", enable_graph: true }); ``` ```json Output theme={null} { "results": [ { "memory": "Name is Joseph", "event": "ADD", "id": "4a5a417a-fa10-43b5-8c53-a77c45e80438" }, { "memory": "Is from Seattle", "event": "ADD", "id": "8d268d0f-5452-4714-b27d-ae46f676a49d" }, { "memory": "Is a software engineer", "event": "ADD", "id": "5f0a184e-ddea-4fe6-9b92-692d6a901df8" } ] } ``` The graph memory would look like this: Graph Memory Visualization showing relationships between entities Graph Memory creates a network of relationships between entities, enabling more contextual retrieval Response for the graph memory's `add` operation will not be available directly in the response. As adding graph memories is an asynchronous operation due to heavy processing, you can use the `get_all()` endpoint to retrieve the memory with the graph metadata. ### Searching with Graph Memory When searching memories, Graph Memory helps retrieve entities that are contextually important even if they're not direct semantic matches. ```python Python theme={null} # Search with graph memory enabled results = client.search( "what is my name?", user_id="joseph", enable_graph=True ) print(results) ``` ```javascript JavaScript theme={null} // Search with graph memory enabled const results = await client.search({ query: "what is my name?", user_id: "joseph", enable_graph: true }); console.log(results); ``` ```json Output theme={null} { "results": [ { "id": "4a5a417a-fa10-43b5-8c53-a77c45e80438", "memory": "Name is Joseph", "user_id": "joseph", "metadata": null, "categories": ["personal_details"], "immutable": false, "created_at": "2025-03-19T09:09:00.146390-07:00", "updated_at": "2025-03-19T09:09:00.146404-07:00", "score": 0.3621795393335552 }, { "id": "8d268d0f-5452-4714-b27d-ae46f676a49d", "memory": "Is from Seattle", "user_id": "joseph", "metadata": null, "categories": ["personal_details"], "immutable": false, "created_at": "2025-03-19T09:09:00.170680-07:00", "updated_at": "2025-03-19T09:09:00.170692-07:00", "score": 0.31212713194651254 } ], "relations": [ { "source": "joseph", "source_type": "person", "relationship": "name", "target": "joseph", "target_type": "person", "score": 0.39 } ] } ``` `results` always reflects the vector search order (optionally reranked). Graph Memory augments that response by adding related entities in the `relations` array; it does not re-rank the vector results automatically. ### Retrieving All Memories with Graph Memory When retrieving all memories, Graph Memory provides additional relationship context: `get_all()` now requires filters to be specified. ```python Python theme={null} # Get all memories with graph context memories = client.get_all( filters={"AND": [{"user_id": "joseph"}]}, enable_graph=True ) print(memories) ``` ```javascript JavaScript theme={null} // Get all memories with graph context const memories = await client.getAll({ filters: {"AND": [{"user_id": "joseph"}]}, enable_graph: true }); console.log(memories); ``` ```json Output theme={null} { "results": [ { "id": "5f0a184e-ddea-4fe6-9b92-692d6a901df8", "memory": "Is a software engineer", "user_id": "joseph", "metadata": null, "categories": ["professional_details"], "immutable": false, "created_at": "2025-03-19T09:09:00.194116-07:00", "updated_at": "2025-03-19T09:09:00.194128-07:00", }, { "id": "8d268d0f-5452-4714-b27d-ae46f676a49d", "memory": "Is from Seattle", "user_id": "joseph", "metadata": null, "categories": ["personal_details"], "immutable": false, "created_at": "2025-03-19T09:09:00.170680-07:00", "updated_at": "2025-03-19T09:09:00.170692-07:00", }, { "id": "4a5a417a-fa10-43b5-8c53-a77c45e80438", "memory": "Name is Joseph", "user_id": "joseph", "metadata": null, "categories": ["personal_details"], "immutable": false, "created_at": "2025-03-19T09:09:00.146390-07:00", "updated_at": "2025-03-19T09:09:00.146404-07:00", } ], "relations": [ { "source": "joseph", "source_type": "person", "relationship": "name", "target": "joseph", "target_type": "person" }, { "source": "joseph", "source_type": "person", "relationship": "city", "target": "seattle", "target_type": "city" }, { "source": "joseph", "source_type": "person", "relationship": "job", "target": "software engineer", "target_type": "job" } ] } ``` ### Setting Graph Memory at Project Level Instead of passing `enable_graph=True` to every add call, you can enable it once at the project level: ```python Python theme={null} from mem0 import MemoryClient client = MemoryClient( api_key="your-api-key", org_id="your-org-id", project_id="your-project-id" ) # Enable graph memory for all operations in this project client.project.update(enable_graph=True) # Now all add operations will use graph memory by default messages = [ {"role": "user", "content": "My name is Joseph"}, {"role": "assistant", "content": "Hello Joseph, it's nice to meet you!"}, {"role": "user", "content": "I'm from Seattle and I work as a software engineer"} ] client.add( messages, user_id="joseph" ) ``` ```javascript JavaScript theme={null} import { MemoryClient } from "mem0"; const client = new MemoryClient({ apiKey: "your-api-key", org_id: "your-org-id", project_id: "your-project-id" }); // Enable graph memory for all operations in this project await client.project.update({ enable_graph: true }); // Now all add operations will use graph memory by default const messages = [ { role: "user", content: "My name is Joseph" }, { role: "assistant", content: "Hello Joseph, it's nice to meet you!" }, { role: "user", content: "I'm from Seattle and I work as a software engineer" } ]; await client.add({ messages, user_id: "joseph" }); ``` ## Best Practices * Enable Graph Memory for applications where understanding context and relationships between memories is important. * Graph Memory works best with a rich history of related conversations. * Consider Graph Memory for long-running assistants that need to track evolving information. ## Performance Considerations Graph Memory requires additional processing and may increase response times slightly for very large memory stores. However, for most use cases, the improved retrieval quality outweighs the minimal performance impact. If you have any questions, please feel free to reach out to us using one of the following methods: # Configurable Graph Threshold Source: https://docs.mem0.ai/platform/features/graph-threshold ## Overview The graph store threshold parameter controls how strictly nodes are matched during graph data ingestion based on embedding similarity. This feature allows you to customize the matching behavior to prevent false matches or enable entity merging based on your specific use case. ## Configuration Add the `threshold` parameter to your graph store configuration: ```python theme={null} from mem0 import Memory config = { "graph_store": { "provider": "neo4j", # or memgraph, neptune, kuzu "config": { "url": "bolt://localhost:7687", "username": "neo4j", "password": "password" }, "threshold": 0.7 # Default value, range: 0.0 to 1.0 } } memory = Memory.from_config(config) ``` ## Parameters | Parameter | Type | Default | Range | Description | | ----------- | ----- | ------- | --------- | ------------------------------------------------------------------------------------------ | | `threshold` | float | 0.7 | 0.0 - 1.0 | Minimum embedding similarity score required to match existing nodes during graph ingestion | ## Use Cases ### Strict Matching (UUIDs, IDs) Use higher thresholds (0.95-0.99) when working with identifiers that should remain distinct: ```python theme={null} config = { "graph_store": { "provider": "neo4j", "config": {...}, "threshold": 0.95 # Strict matching } } ``` **Example:** Prevents UUID collisions like `MXxBUE18QVBQTElDQVRJT058MjM3MTM4NjI5` being matched with `MXxBUE18QVBQTElDQVRJT058MjA2OTYxMzM` ### Permissive Matching (Natural Language) Use lower thresholds (0.6-0.7) when entity variations should be merged: ```python theme={null} config = { "graph_store": { "threshold": 0.6 # Permissive matching } } ``` **Example:** Merges similar entities like "Bob" and "Robert" as the same person. ## Threshold Guidelines | Use Case | Recommended Threshold | Behavior | | ---------------- | --------------------- | ------------------------------------------------- | | UUIDs, IDs, Keys | 0.95 - 0.99 | Prevent false matches between similar identifiers | | Structured Data | 0.85 - 0.9 | Balanced precision and recall | | General Purpose | 0.7 - 0.8 | Default recommendation | | Natural Language | 0.6 - 0.7 | Allow entity variations to merge | ## Examples ### Example 1: Preventing Data Loss with UUIDs ```python theme={null} from mem0 import Memory config = { "graph_store": { "provider": "neo4j", "config": { "url": "bolt://localhost:7687", "username": "neo4j", "password": "password" }, "threshold": 0.98 # Very strict for UUIDs } } memory = Memory.from_config(config) # These UUIDs create separate nodes instead of being incorrectly merged memory.add( [{"role": "user", "content": "MXxBUE18QVBQTElDQVRJT058MjM3MTM4NjI5 relates to Project A"}], user_id="user1" ) memory.add( [{"role": "user", "content": "MXxBUE18QVBQTElDQVRJT058MjA2OTYxMzM relates to Project B"}], user_id="user1" ) ``` ### Example 2: Merging Entity Variations ```python theme={null} config = { "graph_store": { "provider": "neo4j", "config": {...}, "threshold": 0.6 # More permissive } } memory = Memory.from_config(config) # These will be merged as the same entity memory.add([{"role": "user", "content": "Bob works at Google"}], user_id="user1") memory.add([{"role": "user", "content": "Robert works at Google"}], user_id="user1") ``` ### Example 3: Different Thresholds for Different Clients ```python theme={null} # Client 1: Strict matching for transactional data memory_strict = Memory.from_config({ "graph_store": {"threshold": 0.95} }) # Client 2: Permissive matching for conversational data memory_permissive = Memory.from_config({ "graph_store": {"threshold": 0.6} }) ``` ## Supported Graph Providers The threshold parameter works with all graph store providers: * ✅ Neo4j * ✅ Memgraph * ✅ Kuzu * ✅ Neptune (both Analytics and DB) ## How It Works When adding a relation to the graph: 1. **Embedding Generation**: The system generates embeddings for source and destination entities 2. **Node Search**: Searches for existing nodes with similar embeddings 3. **Threshold Comparison**: Compares similarity scores against the configured threshold 4. **Decision**: * If similarity ≥ threshold: Uses the existing node * If similarity \< threshold: Creates a new node ```python theme={null} # Pseudocode if node_similarity >= threshold: use_existing_node() else: create_new_node() ``` ## Troubleshooting ### Issue: Duplicate nodes being created **Symptom**: Expected nodes to merge but they're created separately **Solution**: Lower the threshold ```python theme={null} config = {"graph_store": {"threshold": 0.6}} ``` ### Issue: Unrelated entities being merged **Symptom**: Different entities incorrectly matched as the same node **Solution**: Raise the threshold ```python theme={null} config = {"graph_store": {"threshold": 0.95}} ``` ### Issue: Validation error **Symptom**: `ValidationError: threshold must be between 0.0 and 1.0` **Solution**: Ensure threshold is in valid range ```python theme={null} config = {"graph_store": {"threshold": 0.7}} # Valid: 0.0 ≤ x ≤ 1.0 ``` ## Backward Compatibility * **Default Value**: 0.7 (maintains existing behavior) * **Optional Parameter**: Existing code works without any changes * **No Breaking Changes**: Graceful fallback if not specified ## Related * [Graph Memory](/platform/features/graph-memory) * [Issue #3590](https://github.com/mem0ai/mem0/issues/3590) # Group Chat Source: https://docs.mem0.ai/platform/features/group-chat Enable multi-participant conversations with automatic memory attribution to individual speakers ## Overview The Group Chat feature enables Mem0 to process conversations involving multiple participants and automatically attribute memories to individual speakers. This allows for precise tracking of each participant's preferences, characteristics, and contributions in collaborative discussions, team meetings, or multi-agent conversations. When you provide messages with participant names, Mem0 automatically: * Extracts memories from each participant's messages separately * Attributes each memory to the correct speaker using their name as the `user_id` or `agent_id` * Maintains individual memory profiles for each participant ## How Group Chat Works Mem0 automatically detects group chat scenarios when messages contain a `name` field: ```json theme={null} { "role": "user", "name": "Alice", "content": "Hey team, I think we should use React for the frontend" } ``` When names are present, Mem0: * Formats messages as `"Alice (user): content"` for processing * Extracts memories with proper attribution to each speaker * Stores memories with the speaker's name as the `user_id` (for users) or `agent_id` (for assistants/agents) ### Memory Attribution Rules * **User Messages**: The `name` field becomes the `user_id` in stored memories * **Assistant/Agent Messages**: The `name` field becomes the `agent_id` in stored memories * **Messages without names**: Fall back to standard processing using role as identifier ## Using Group Chat ### Basic Group Chat Add memories from a multi-participant conversation: ```python Python theme={null} from mem0 import MemoryClient client = MemoryClient(api_key="your-api-key") # Group chat with multiple users messages = [ {"role": "user", "name": "Alice", "content": "Hey team, I think we should use React for the frontend"}, {"role": "user", "name": "Bob", "content": "I disagree, Vue.js would be better for our use case"}, {"role": "user", "name": "Charlie", "content": "What about considering Angular? It has great enterprise support"}, {"role": "assistant", "content": "All three frameworks have their merits. Let me summarize the pros and cons of each."} ] response = client.add( messages, run_id="group_chat_1", infer=True ) print(response) ``` ```json Output theme={null} { "results": [ { "id": "4d82478a-8d50-47e6-9324-1f65efff5829", "event": "ADD", "memory": "prefers using React for the frontend" }, { "id": "1d8b8f39-7b17-4d18-8632-ab1c64fa35b9", "event": "ADD", "memory": "prefers Vue.js for our use case" }, { "id": "147559a8-c5f7-44d0-9418-91f53f7a89a4", "event": "ADD", "memory": "suggests considering Angular because it has great enterprise support" } ] } ``` ## Retrieving Group Chat Memories ### Get All Memories for a Session Retrieve all memories from a specific group chat session: ```python Python theme={null} # Get all memories for a specific run_id # Use wildcard "*" for user_id to match all participants filters = { "AND": [ {"user_id": "*"}, {"run_id": "group_chat_1"} ] } all_memories = client.get_all(filters=filters, page=1) print(all_memories) ``` ```json Output theme={null} [ { "id": "147559a8-c5f7-44d0-9418-91f53f7a89a4", "memory": "suggests considering Angular because it has great enterprise support", "user_id": "charlie", "run_id": "group_chat_1", "created_at": "2025-06-21T05:51:11.007223-07:00", "updated_at": "2025-06-21T05:51:11.626562-07:00" }, { "id": "1d8b8f39-7b17-4d18-8632-ab1c64fa35b9", "memory": "prefers Vue.js for our use case", "user_id": "bob", "run_id": "group_chat_1", "created_at": "2025-06-21T05:51:08.675301-07:00", "updated_at": "2025-06-21T05:51:09.319269-07:00", }, { "id": "4d82478a-8d50-47e6-9324-1f65efff5829", "memory": "prefers using React for the frontend", "user_id": "alice", "run_id": "group_chat_1", "created_at": "2025-06-21T05:51:05.943223-07:00", "updated_at": "2025-06-21T05:51:06.982539-07:00", } ] ``` ### Get Memories for a Specific Participant Retrieve memories from a specific participant in a group chat: ```python Python theme={null} # Get memories for a specific participant filters = { "AND": [ {"user_id": "charlie"}, {"run_id": "group_chat_1"} ] } charlie_memories = client.get_all(filters=filters, page=1) print(charlie_memories) ``` ```json Output theme={null} [ { "id": "147559a8-c5f7-44d0-9418-91f53f7a89a4", "memory": "suggests considering Angular because it has great enterprise support", "user_id": "charlie", "run_id": "group_chat_1", "created_at": "2025-06-21T05:51:11.007223-07:00", "updated_at": "2025-06-21T05:51:11.626562-07:00", } ] ``` ### Search Within Group Chat Context Search for specific information within a group chat session: ```python Python theme={null} # Search within group chat context filters = { "AND": [ {"user_id": "charlie"}, {"run_id": "group_chat_1"} ] } search_response = client.search( query="What are the tasks?", filters=filters ) print(search_response) ``` ```json Output theme={null} [ { "id": "147559a8-c5f7-44d0-9418-91f53f7a89a4", "memory": "suggests considering Angular because it has great enterprise support", "user_id": "charlie", "run_id": "group_chat_1", "created_at": "2025-06-21T05:51:11.007223-07:00", "updated_at": "2025-06-21T05:51:11.626562-07:00", } ] ``` ## Async Mode Support Group chat also supports async processing for improved performance: ```python Python theme={null} # Group chat with async mode response = client.add( messages, run_id="groupchat_async", infer=True, async_mode=True ) print(response) ``` ## Message Format Requirements ### Required Fields Each message in a group chat must include: * `role`: The participant's role (`"user"`, `"assistant"`, `"agent"`) * `content`: The message content * `name`: The participant's name (required for group chat detection) ### Example Message Structure ```json theme={null} { "role": "user", "name": "Alice", "content": "I think we should use React for the frontend" } ``` ### Supported Roles * **`user`**: Human participants (memories stored with `user_id`) * **`assistant`**: AI assistants (memories stored with `agent_id`) ## Best Practices 1. **Consistent Naming**: Use consistent names for participants across sessions to maintain proper memory attribution. 2. **Clear Role Assignment**: Ensure each participant has the correct role (`user`, `assistant`, or `agent`) for proper memory categorization. 3. **Session Management**: Use meaningful `run_id` values to organize group chat sessions and enable easy retrieval. 4. **Memory Filtering**: Use filters to retrieve memories from specific participants or sessions when needed. 5. **Async Processing**: Use `async_mode=True` for large group conversations to improve performance. 6. **Search Context**: Leverage the search functionality to find specific information within group chat contexts. ## Use Cases * **Team Meetings**: Track individual team member preferences and contributions * **Customer Support**: Maintain separate memory profiles for different customers * **Multi-Agent Systems**: Manage conversations with multiple AI assistants * **Collaborative Projects**: Track individual preferences and expertise areas * **Group Discussions**: Maintain context for each participant's viewpoints If you have any questions, please feel free to reach out to us using one of the following methods: # Memory Export Source: https://docs.mem0.ai/platform/features/memory-export Export memories in a structured format using customizable Pydantic schemas ## Overview The Memory Export feature allows you to create structured exports of memories using customizable Pydantic schemas. This process enables you to transform your stored memories into specific data formats that match your needs. You can apply various filters to narrow down which memories to export and define exactly how the data should be structured. ## Creating a Memory Export To create a memory export, you'll need to: 1. Define your schema structure 2. Submit an export job 3. Retrieve the exported data ### Define Schema Here's an example schema for extracting professional profile information: ```json theme={null} { "$defs": { "EducationLevel": { "enum": ["high_school", "bachelors", "masters"], "title": "EducationLevel", "type": "string" }, "EmploymentStatus": { "enum": ["full_time", "part_time", "student"], "title": "EmploymentStatus", "type": "string" } }, "properties": { "full_name": { "anyOf": [ { "maxLength": 100, "minLength": 2, "type": "string" }, { "type": "null" } ], "default": null, "description": "The professional's full name", "title": "Full Name" }, "current_role": { "anyOf": [ { "type": "string" }, { "type": "null" } ], "default": null, "description": "Current job title or role", "title": "Current Role" } }, "title": "ProfessionalProfile", "type": "object" } ``` ### Submit Export Job You can optionally provide additional instructions to guide how memories are processed and structured during export using the `export_instructions` parameter. ```python Python theme={null} # Basic export request filters = {"user_id": "alice"} response = client.create_memory_export( schema=json_schema, filters=filters ) # Export with custom instructions and additional filters export_instructions = """ 1. Create a comprehensive profile with detailed information in each category 2. Only mark fields as "None" when absolutely no relevant information exists 3. Base all information directly on the user's memories 4. When contradictions exist, prioritize the most recent information 5. Clearly distinguish between factual statements and inferences """ filters = { "AND": [ {"user_id": "alex"}, {"created_at": {"gte": "2024-01-01"}} ] } response = client.create_memory_export( schema=json_schema, filters=filters, export_instructions=export_instructions # Optional ) print(response) ``` ```javascript JavaScript theme={null} // Basic Export request const filters = {"user_id": "alice"}; const response = await client.createMemoryExport({ schema: json_schema, filters: filters }); // Export with custom instructions and additional filters const export_instructions = ` 1. Create a comprehensive profile with detailed information in each category 2. Only mark fields as "None" when absolutely no relevant information exists 3. Base all information directly on the user's memories 4. When contradictions exist, prioritize the most recent information 5. Clearly distinguish between factual statements and inferences `; // For create operation, using only user_id filter as requested const filters = { "AND": [ {"user_id": "alex"}, {"created_at": {"gte": "2024-01-01"}} ] } const responseWithInstructions = await client.createMemoryExport({ schema: json_schema, filters: filters, export_instructions: export_instructions }); console.log(responseWithInstructions); ``` ```bash cURL theme={null} curl -X POST "https://api.mem0.ai/v1/memories/export/" \ -H "Authorization: Token your-api-key" \ -H "Content-Type: application/json" \ -d '{ "schema": {json_schema}, "filters": {"user_id": "alice"}, "export_instructions": "1. Create a comprehensive profile with detailed information\n2. Only mark fields as \"None\" when absolutely no relevant information exists" }' ``` ```json Output theme={null} { "message": "Memory export request received. The export will be ready in a few seconds.", "id": "550e8400-e29b-41d4-a716-446655440000" } ``` ### Retrieve Export Once the export job is complete, you can retrieve the structured data in two ways: #### Using Export ID ```python Python theme={null} # Retrieve using export ID response = client.get_memory_export(memory_export_id="550e8400-e29b-41d4-a716-446655440000") print(response) ``` ```javascript JavaScript theme={null} // Retrieve using export ID const memory_export_id = "550e8400-e29b-41d4-a716-446655440000"; const response = await client.getMemoryExport({ memory_export_id: memory_export_id }); console.log(response); ``` ```json Output theme={null} { "full_name": "John Doe", "current_role": "Senior Software Engineer", "years_experience": 8, "employment_status": "full_time", "education_level": "masters", "skills": ["Python", "AWS", "Machine Learning"] } ``` #### Using Filters ```python Python theme={null} # Retrieve using filters filters = { "AND": [ {"created_at": {"gte": "2024-07-10", "lte": "2024-07-20"}}, {"user_id": "alex"} ] } response = client.get_memory_export(filters=filters) print(response) ``` ```javascript JavaScript theme={null} // Retrieve using filters const filters = { "AND": [ {"created_at": {"gte": "2024-07-10", "lte": "2024-07-20"}}, {"user_id": "alex"} ] } const response = await client.getMemoryExport({ filters: filters }); console.log(response); ``` ```json Output theme={null} { "full_name": "John Doe", "current_role": "Senior Software Engineer", "years_experience": 8, "employment_status": "full_time", "education_level": "masters", "skills": ["Python", "AWS", "Machine Learning"] } ``` ## Available Filters You can apply various filters to customize which memories are included in the export: * `user_id`: Filter memories by specific user * `agent_id`: Filter memories by specific agent * `run_id`: Filter memories by specific run * `session_id`: Filter memories by specific session * `created_at`: Filter memories by date The export process may take some time to complete, especially when dealing with a large number of memories or complex schemas. If you have any questions, please feel free to reach out to us using one of the following methods: # Multimodal Support Source: https://docs.mem0.ai/platform/features/multimodal-support Integrate images and documents into your interactions with Mem0 Mem0 extends its capabilities beyond text by supporting multimodal data, including images and documents. With this feature, users can seamlessly integrate visual and document content into their interactions, allowing Mem0 to extract relevant information from various media types and enrich the memory system. ## How It Works When a user submits an image or document, Mem0 processes it to extract textual information and other pertinent details. These details are then added to the user's memory, enhancing the system's ability to understand and recall multimodal inputs. ```python Python theme={null} import os from mem0 import MemoryClient os.environ["MEM0_API_KEY"] = "your-api-key" client = MemoryClient() messages = [ { "role": "user", "content": "Hi, my name is Alice." }, { "role": "assistant", "content": "Nice to meet you, Alice! What do you like to eat?" }, { "role": "user", "content": { "type": "image_url", "image_url": { "url": "https://www.superhealthykids.com/wp-content/uploads/2021/10/best-veggie-pizza-featured-image-square-2.jpg" } } }, ] # Calling the add method to ingest messages into the memory system client.add(messages, user_id="alice") ``` ```typescript TypeScript theme={null} import MemoryClient from "mem0ai"; const client = new MemoryClient(); const messages = [ { role: "user", content: "Hi, my name is Alice." }, { role: "assistant", content: "Nice to meet you, Alice! What do you like to eat?" }, { role: "user", content: { type: "image_url", image_url: { url: "https://www.superhealthykids.com/wp-content/uploads/2021/10/best-veggie-pizza-featured-image-square-2.jpg" } } }, ] await client.add(messages, { user_id: "alice" }) ``` ```json Output theme={null} { "results": [ { "memory": "Name is Alice", "event": "ADD", "id": "7ae113a3-3cb5-46e9-b6f7-486c36391847" }, { "memory": "Likes large pizza with toppings including cherry tomatoes, black olives, green spinach, yellow bell peppers, diced ham, and sliced mushrooms", "event": "ADD", "id": "56545065-7dee-4acf-8bf2-a5b2535aabb3" } ] } ``` ## Supported Media Types Mem0 currently supports the following media types: 1. **Images** - JPG, PNG, and other common image formats 2. **Documents** - MDX, TXT, and PDF files ## Integration Methods ### 1. Images #### Using an Image URL You can include an image by providing its direct URL. This method is simple and efficient for online images. ```python {2, 5-13} theme={null} # Define the image URL image_url = "https://www.superhealthykids.com/wp-content/uploads/2021/10/best-veggie-pizza-featured-image-square-2.jpg" # Create the message dictionary with the image URL image_message = { "role": "user", "content": { "type": "image_url", "image_url": { "url": image_url } } } client.add([image_message], user_id="alice") ``` #### Using Base64 Image Encoding for Local Files For local images or when embedding the image directly is preferable, you can use a Base64-encoded string. ```python Python theme={null} import base64 # Path to the image file image_path = "path/to/your/image.jpg" # Encode the image in Base64 with open(image_path, "rb") as image_file: base64_image = base64.b64encode(image_file.read()).decode("utf-8") # Create the message dictionary with the Base64-encoded image image_message = { "role": "user", "content": { "type": "image_url", "image_url": { "url": f"data:image/jpeg;base64,{base64_image}" } } } client.add([image_message], user_id="alice") ``` ```typescript TypeScript theme={null} import MemoryClient from "mem0ai"; import fs from 'fs'; const imagePath = 'path/to/your/image.jpg'; const base64Image = fs.readFileSync(imagePath, { encoding: 'base64' }); const imageMessage = { role: "user", content: { type: "image_url", image_url: { url: `data:image/jpeg;base64,${base64Image}` } } }; await client.add([imageMessage], { user_id: "alice" }) ``` ### 2. Text Documents (MDX/TXT) Mem0 supports both online and local text documents in MDX or TXT format. #### Using a Document URL ```python theme={null} # Define the document URL document_url = "https://www.w3.org/TR/2003/REC-PNG-20031110/iso_8859-1.txt" # Create the message dictionary with the document URL document_message = { "role": "user", "content": { "type": "mdx_url", "mdx_url": { "url": document_url } } } client.add([document_message], user_id="alice") ``` #### Using Base64 Encoding for Local Documents ```python theme={null} import base64 # Path to the document file document_path = "path/to/your/document.txt" # Function to convert file to Base64 def file_to_base64(file_path): with open(file_path, "rb") as file: return base64.b64encode(file.read()).decode('utf-8') # Encode the document in Base64 base64_document = file_to_base64(document_path) # Create the message dictionary with the Base64-encoded document document_message = { "role": "user", "content": { "type": "mdx_url", "mdx_url": { "url": base64_document } } } client.add([document_message], user_id="alice") ``` ### 3. PDF Documents Mem0 supports PDF documents via URL. ```python theme={null} # Define the PDF URL pdf_url = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf" # Create the message dictionary with the PDF URL pdf_message = { "role": "user", "content": { "type": "pdf_url", "pdf_url": { "url": pdf_url } } } client.add([pdf_message], user_id="alice") ``` ## Complete Example with Multiple File Types Here's a comprehensive example showing how to work with different file types: ```python theme={null} import base64 from mem0 import MemoryClient client = MemoryClient() def file_to_base64(file_path): with open(file_path, "rb") as file: return base64.b64encode(file.read()).decode('utf-8') # Example 1: Using an image URL image_message = { "role": "user", "content": { "type": "image_url", "image_url": { "url": "https://example.com/sample-image.jpg" } } } # Example 2: Using a text document URL text_message = { "role": "user", "content": { "type": "mdx_url", "mdx_url": { "url": "https://www.w3.org/TR/2003/REC-PNG-20031110/iso_8859-1.txt" } } } # Example 3: Using a PDF URL pdf_message = { "role": "user", "content": { "type": "pdf_url", "pdf_url": { "url": "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf" } } } # Add each message to the memory system client.add([image_message], user_id="alice") client.add([text_message], user_id="alice") client.add([pdf_message], user_id="alice") ``` Using these methods, you can seamlessly incorporate various media types into your interactions, further enhancing Mem0's multimodal capabilities. If you have any questions, please feel free to reach out to us using one of the following methods: # Overview Source: https://docs.mem0.ai/platform/features/platform-overview See how Mem0 Platform features evolve from baseline filters to graph-powered retrieval. Mem0 Platform features help managed deployments scale from basic filtering to graph-powered retrieval and data governance. Use this page to pick the right feature lane for your team. New to the platform? Start with the Platform quickstart, then dive into the journeys below. ## Choose your path Field-level filtering with async defaults. Non-blocking add/search requests for agents. Relationship-aware recall across entities. Metadata filters, rerankers, and toggles. Imports, exports, timestamps, and expirations. Webhooks, feedback loops, and multi-agent chat. Self-hosting instead? Jump to the{" "} OSS feature overview for equivalent capabilities. ## Keep going # Memory Timestamps Source: https://docs.mem0.ai/platform/features/timestamp Add timestamps to your memories to maintain chronological accuracy and historical context ## Overview The Memory Timestamps feature allows you to specify when a memory was created, regardless of when it's actually added to the system. This powerful capability enables you to: * Maintain accurate chronological ordering of memories * Import historical data with proper timestamps * Create memories that reflect when events actually occurred * Build timelines with precise temporal information By leveraging custom timestamps, you can ensure that your memory system maintains an accurate representation of when information was generated or events occurred. ## Benefits of Custom Timestamps Custom timestamps offer several important benefits: * **Historical Accuracy**: Preserve the exact timing of past events and information. * **Data Migration**: Seamlessly migrate existing data while maintaining original timestamps. * **Time-Sensitive Analysis**: Enable time-based analysis and pattern recognition across memories. * **Consistent Chronology**: Maintain proper ordering of memories for coherent storytelling. ## Using Custom Timestamps When adding new memories, you can specify a custom timestamp to indicate when the memory was created. This timestamp will be used instead of the current time. ### Adding Memories with Custom Timestamps ```python Python theme={null} import os import time from datetime import datetime, timedelta from mem0 import MemoryClient os.environ["MEM0_API_KEY"] = "your-api-key" client = MemoryClient() # Get the current time current_time = datetime.now() # Calculate 5 days ago five_days_ago = current_time - timedelta(days=5) # Convert to Unix timestamp (seconds since epoch) unix_timestamp = int(five_days_ago.timestamp()) # Add memory with custom timestamp messages = [ {"role": "user", "content": "I'm travelling to SF"} ] client.add(messages, user_id="user1", timestamp=unix_timestamp) ``` ```javascript JavaScript theme={null} import MemoryClient from 'mem0ai'; const client = new MemoryClient({ apiKey: 'your-api-key' }); // Get the current time const currentTime = new Date(); // Calculate 5 days ago const fiveDaysAgo = new Date(); fiveDaysAgo.setDate(currentTime.getDate() - 5); // Convert to Unix timestamp (seconds since epoch) const unixTimestamp = Math.floor(fiveDaysAgo.getTime() / 1000); // Add memory with custom timestamp const messages = [ {"role": "user", "content": "I'm travelling to SF"} ] client.add(messages, { user_id: "user1", timestamp: unixTimestamp }) .then(response => console.log(response)) .catch(error => console.error(error)); ``` ```bash cURL theme={null} curl -X POST "https://api.mem0.ai/v1/memories/" \ -H "Authorization: Token your-api-key" \ -H "Content-Type: application/json" \ -d '{ "messages": [{"role": "user", "content": "I'm travelling to SF"}], "user_id": "user1", "timestamp": 1721577600 }' ``` ```json Output theme={null} { "results": [ { "id": "a1b2c3d4-e5f6-4g7h-8i9j-k0l1m2n3o4p5", "data": {"memory": "Travelling to SF"}, "event": "ADD" } ] } ``` ### Timestamp Format When specifying a custom timestamp, you should provide a Unix timestamp (seconds since epoch). This is an integer representing the number of seconds that have elapsed since January 1, 1970 (UTC). For example, to create a memory with a timestamp of January 1, 2023: ```python Python theme={null} # January 1, 2023 timestamp january_2023_timestamp = 1672531200 # Unix timestamp for 2023-01-01 00:00:00 UTC messages = [ {"role": "user", "content": "I'm travelling to SF"} ] client.add(messages, user_id="user1", timestamp=january_2023_timestamp) ``` ```javascript JavaScript theme={null} // January 1, 2023 timestamp const january2023Timestamp = 1672531200; // Unix timestamp for 2023-01-01 00:00:00 UTC const messages = [ {"role": "user", "content": "I'm travelling to SF"} ] client.add(messages, { user_id: "user1", timestamp: january2023Timestamp }) .then(response => console.log(response)) .catch(error => console.error(error)); ``` If you have any questions, please feel free to reach out to us using one of the following methods: # Memory Filters Source: https://docs.mem0.ai/platform/features/v2-memory-filters Query and retrieve memories with powerful filtering capabilities. Filter by users, agents, content, time ranges, and more. > Memory filters provide a flexible way to query and retrieve specific memories from your memory store. You can filter by users, agents, content categories, time ranges, and combine multiple conditions using logical operators. ## When to use filters When working with large-scale memory stores, you need precise control over which memories to retrieve. Filters help you: * **Isolate user data**: Retrieve memories for specific users while maintaining privacy * **Debug and audit**: Export specific memory subsets for analysis * **Target content**: Find memories with specific categories or metadata * **Time-based queries**: Retrieve memories within specific date ranges * **Performance optimization**: Reduce query complexity by pre-filtering Filters were introduced in v1.0.0 to provide precise control over memory retrieval. ## Filter structure Filters use a nested JSON structure with logical operators at the root: ```python theme={null} # Basic structure { "AND": [ # or "OR", "NOT" { "field": "value" }, { "field": { "operator": "value" } } ] } ``` ## Available fields and operators ### Entity fields | Field | Operators | Example | | ---------- | -------------------- | -------------------------------------- | | `user_id` | `=`, `!=`, `in`, `*` | `{"user_id": "user_123"}` | | `agent_id` | `=`, `!=`, `in`, `*` | `{"agent_id": "*"}` | | `app_id` | `=`, `!=`, `in`, `*` | `{"app_id": {"in": ["app1", "app2"]}}` | | `run_id` | `=`, `!=`, `in`, `*` | `{"run_id": "*"}` | ### Time fields | Field | Operators | Example | | ------------ | ------------------------------- | --------------------------------------- | | `created_at` | `>`, `>=`, `<`, `<=`, `=`, `!=` | `{"created_at": {"gte": "2024-01-01"}}` | | `updated_at` | `>`, `>=`, `<`, `<=`, `=`, `!=` | `{"updated_at": {"lt": "2024-12-31"}}` | | `timestamp` | `>`, `>=`, `<`, `<=`, `=`, `!=` | `{"timestamp": {"gt": "2024-01-01"}}` | ### Content fields | Field | Operators | Example | | ------------ | --------------------------- | ---------------------------------------- | | `categories` | `=`, `!=`, `in`, `contains` | `{"categories": {"in": ["finance"]}}` | | `metadata` | `=`, `!=` | `{"metadata": {"key": "value"}}` | | `keywords` | `contains`, `icontains` | `{"keywords": {"icontains": "invoice"}}` | ### Special fields | Field | Operators | Example | | ------------ | --------- | -------------------------------- | | `memory_ids` | `in` | `{"memory_ids": ["id1", "id2"]}` | The `*` wildcard matches any non-null value. Records with null values for that field are excluded. ## Common filter patterns Use these ready-made filters to target typical retrieval scenarios without rebuilding logic from scratch. ```python theme={null} # Narrow to one user's memories filters = {"AND": [{"user_id": "user_123"}]} memories = client.get_all(filters=filters) ``` ```python theme={null} # Wildcard skips null user_id entries filters = {"AND": [{"user_id": "*"}]} memories = client.get_all(filters=filters) ``` ```python theme={null} # Pair a user filter with a run wildcard filters = { "AND": [ {"user_id": "user_123"}, {"run_id": "*"} ] } memories = client.get_all(filters=filters) ``` ### Content search Find memories containing specific text, categories, or metadata values. ```python theme={null} # Case-insensitive match filters = { "AND": [ {"user_id": "user_123"}, {"keywords": {"icontains": "pizza"}} ] } # Case-sensitive match filters = { "AND": [ {"user_id": "user_123"}, {"keywords": {"contains": "Invoice_2024"}} ] } ``` ```python theme={null} # Match against category list filters = { "AND": [ {"user_id": "user_123"}, {"categories": {"in": ["finance", "health"]}} ] } # Partial category match filters = { "AND": [ {"user_id": "user_123"}, {"categories": {"contains": "finance"}} ] } ``` ```python theme={null} # Pin to a metadata attribute filters = { "AND": [ {"user_id": "user_123"}, {"metadata": {"source": "email"}} ] } ``` ### Time-based filtering Retrieve memories within specific date ranges using time operators. ```python theme={null} # Created in January 2024 filters = { "AND": [ {"user_id": "user_123"}, {"created_at": {"gte": "2024-01-01T00:00:00Z"}}, {"created_at": {"lt": "2024-02-01T00:00:00Z"}} ] } # Updated recently filters = { "AND": [ {"user_id": "user_123"}, {"updated_at": {"gte": "2024-12-01T00:00:00Z"}} ] } ``` ### Multiple criteria Combine various filters for complex queries across different dimensions. ```python theme={null} # Expand scope to a short user list filters = { "AND": [ {"user_id": {"in": ["user_1", "user_2", "user_3"]}} ] } ``` ```python theme={null} # Return matches on either condition filters = { "OR": [ {"user_id": "user_123"}, {"run_id": "run_456"} ] } ``` ```python theme={null} # Wrap negative logic with NOT filters = { "AND": [ {"user_id": "user_123"}, {"NOT": { "categories": {"in": ["spam", "test"]} }} ] } ``` ```python theme={null} # Fetch a fixed set of memory IDs filters = { "AND": [ {"user_id": "user_123"}, {"memory_ids": ["mem_1", "mem_2", "mem_3"]} ] } ``` ```python theme={null} # Require every entity field to be non-null filters = { "AND": [ {"user_id": "*"}, {"agent_id": "*"}, {"run_id": "*"}, {"app_id": "*"} ] } ``` ## Advanced examples Level up foundational patterns with compound filters that coordinate entity scope, tighten time windows, and weave in exclusion rules for high-precision retrievals. ```python theme={null} # Invoice memories in Q1 2024 filters = { "AND": [ {"user_id": "user_123"}, {"keywords": {"icontains": "invoice"}}, {"categories": {"in": ["finance"]}}, {"created_at": {"gte": "2024-01-01T00:00:00Z"}}, {"created_at": {"lt": "2024-04-01T00:00:00Z"}} ] } ``` ```python theme={null} # From specific agent, all users filters = { "AND": [ {"agent_id": "finance_bot"}, {"user_id": "*"} ] } # Any entity populated filters = { "OR": [ {"user_id": "*"}, {"agent_id": "*"}, {"run_id": "*"}, {"app_id": "*"} ] } ``` ```python theme={null} # User memories from 2024, excluding spam and test filters = { "AND": [ {"user_id": "user_123"}, {"created_at": {"gte": "2024-01-01T00:00:00Z"}}, {"NOT": { "OR": [ {"categories": {"in": ["spam"]}}, {"categories": {"in": ["test"]}} ] }} ] } ``` ## Best practices The root must be `AND`, `OR`, or `NOT` with an array of conditions. Use `"*"` to match any non-null value for a field. When you specify `user_id` only, other entity fields default to null. Use wildcards to include them. ## Troubleshooting **Problem**: Filtered by `user_id` but don't see agent memories. **Solution**: Add a wildcard for `agent_id` so non-null entries return: ```python theme={null} {"AND": [{"user_id": "user_123"}, {"agent_id": "*"}]} ``` **Problem**: `ne` comparison pulls in records with null values. **Solution**: Pair `ne` with a wildcard guard: ```python theme={null} {"AND": [{"agent_id": "*"}, {"agent_id": {"ne": "old_agent"}}]} ``` **Solution**: Swap to `icontains` to normalize casing. **Solution**: Use `gte` for the start and `lt` for the end boundary: ```python theme={null} {"AND": [ {"created_at": {"gte": "2024-01-01"}}, {"created_at": {"lt": "2024-02-01"}} ]} ``` **Solution**: Match top-level metadata keys exactly: ```python theme={null} {"metadata": {"source": "email"}} ``` ## FAQ Yes. The root must be a logical operator with an array. Any non-null value. Nulls are excluded. Unspecified fields default to NULL. Use `"*"` to include non-null values. No. Equality is the default: `{"user_id": "u1"}` works. Only top-level keys are supported. Use `keywords` with `contains` (case-sensitive) or `icontains` (case-insensitive). ```python theme={null} { "AND": [ {"user_id": "user_123"}, {"OR": [ {"categories": "finance"}, {"categories": "health"} ]} ] } ``` # Webhooks Source: https://docs.mem0.ai/platform/features/webhooks Configure and manage webhooks to receive real-time notifications about memory events ## Overview Webhooks enable real-time notifications for memory events in your Mem0 project. Webhooks are configured at the project level, meaning each webhook is tied to a specific project and receives events solely from that project. You can configure webhooks to send HTTP POST requests to your specified URLs whenever memories are created, updated, or deleted. ## Managing Webhooks ### Create Webhook Create a webhook for your project. It will receive events only from that project: ```python Python theme={null} import os from mem0 import MemoryClient os.environ["MEM0_API_KEY"] = "your-api-key" client = MemoryClient() # Create webhook in a specific project webhook = client.create_webhook( url="https://your-app.com/webhook", name="Memory Logger", project_id="proj_123", event_types=["memory_add"] ) print(webhook) ``` ```javascript JavaScript theme={null} const { MemoryClient } = require('mem0ai'); const client = new MemoryClient({ apiKey: 'your-api-key'}); // Create webhook in a specific project const webhook = await client.createWebhook({ url: "https://your-app.com/webhook", name: "Memory Logger", projectId: "proj_123", eventTypes: ["memory_add"] }); console.log(webhook); ``` ```json Output theme={null} { "webhook_id": "wh_123", "name": "Memory Logger", "url": "https://your-app.com/webhook", "event_types": ["memory_add"], "project": "default-project", "is_active": true, "created_at": "2025-02-18T22:59:56.804993-08:00", "updated_at": "2025-02-18T23:06:41.479361-08:00" } ``` ### Get Webhooks Retrieve all webhooks for your project: ```python Python theme={null} # Get webhooks for a specific project webhooks = client.get_webhooks(project_id="proj_123") print(webhooks) ``` ```javascript JavaScript theme={null} // Get webhooks for a specific project const webhooks = await client.getWebhooks({projectId: "proj_123"}); console.log(webhooks); ``` ```json Output theme={null} [ { "webhook_id": "wh_123", "url": "https://mem0.ai", "name": "mem0", "owner": "john", "event_types": ["memory_add"], "project": "default-project", "is_active": true, "created_at": "2025-02-18T22:59:56.804993-08:00", "updated_at": "2025-02-18T23:06:41.479361-08:00" } ] ``` ### Update Webhook Update an existing webhook’s configuration by specifying its `webhook_id`: ```python Python theme={null} # Update webhook for a specific project updated_webhook = client.update_webhook( name="Updated Logger", url="https://your-app.com/new-webhook", event_types=["memory_update", "memory_add"], webhook_id="wh_123" ) print(updated_webhook) ``` ```javascript JavaScript theme={null} // Update webhook for a specific project const updatedWebhook = await client.updateWebhook({ name: "Updated Logger", url: "https://your-app.com/new-webhook", eventTypes: ["memory_update", "memory_add"], webhookId: "wh_123" }); console.log(updatedWebhook); ``` ```json Output theme={null} { "message": "Webhook updated successfully" } ``` ### Delete Webhook Delete a webhook by providing its `webhook_id`: ```python Python theme={null} # Delete webhook from a specific project response = client.delete_webhook(webhook_id="wh_123") print(response) ``` ```javascript JavaScript theme={null} // Delete webhook from a specific project const response = await client.deleteWebhook({webhookId: "wh_123"}); console.log(response); ``` ```json Output theme={null} { "message": "Webhook deleted successfully" } ``` ## Event Types Mem0 supports the following event types for webhooks: * `memory_add`: Triggered when a memory is added. * `memory_update`: Triggered when an existing memory is updated. * `memory_delete`: Triggered when a memory is deleted. ## Webhook Payload When a memory event occurs, Mem0 sends an HTTP POST request to your webhook URL with the following payload: ```json theme={null} { "event_details": { "id": "a1b2c3d4-e5f6-4g7h-8i9j-k0l1m2n3o4p5", "data": { "memory": "Name is Alex" }, "event": "ADD" } } ``` ## Best Practices 1. **Implement Retry Logic**: Ensure your webhook endpoint can handle temporary failures. 2. **Verify Webhook Source**: Implement security measures to verify that webhook requests originate from Mem0. 3. **Process Events Asynchronously**: Process webhook events asynchronously to avoid timeouts and ensure reliable handling. 4. **Monitor Webhook Health**: Regularly review your webhook logs to ensure functionality and promptly address delivery failures. If you have any questions, please feel free to reach out to us using one of the following methods: # Overview Source: https://docs.mem0.ai/platform/overview Managed memory layer for AI agents - production-ready in minutes # Mem0 Platform Overview Mem0 is the memory engine that keeps conversations contextual so users never repeat themselves and your agents respond with continuity. Mem0 Platform delivers that experience as a fully managed service—scaling, securing, and enriching memories without any infrastructure work on your side. Mem0 v1.0.0 shipped rerankers, async-by-default behavior, and Azure OpenAI support. Catch the full list of changes in the release notes. ## Why it matters * **Personalized replies**: Memories persist across users and agents, cutting prompt bloat and repeat questions. * **Hosted stack**: Mem0 runs the vector store, graph services, and rerankers—no provisioning, tuning, or maintenance. * **Enterprise controls**: SOC 2, audit logs, and workspace governance ship by default for production readiness. | Feature | Why it helps | | ----------------- | ----------------------------------------------------------------------------------------------------- | | Fast setup | Add a few lines of code and you’re production-ready—no vector database or LLM configuration required. | | Production scale | Automatic scaling, high availability, and managed infrastructure so you focus on product work. | | Advanced features | Graph memory, webhooks, multimodal support, and custom categories are ready to enable. | | Enterprise ready | SOC 2 Type II, GDPR compliance, and dedicated support keep security and governance covered. | Start with the Platform quickstart to provision your workspace, then pick the journey below that matches your next milestone. ## Choose your path Create project and ship first memory. User, agent, and session memory behavior. Add, search, update, and delete workflows. Graph memory, async clients, and rerankers. Metadata filters and per-request toggles. LangChain, CrewAI, Vercel AI SDK. Track activity and manage workspaces. Evaluating self-hosting instead? Jump to the Platform vs OSS comparison to see trade-offs before you commit. ## Keep going # Platform vs Open Source Source: https://docs.mem0.ai/platform/platform-vs-oss Choose the right Mem0 solution for your needs ## Which Mem0 is right for you? Mem0 offers two powerful ways to add memory to your AI applications. Choose based on your priorities: **Managed, hassle-free** Get started in 5 minutes with our hosted solution. Perfect for fast iteration and production apps. **Self-hosted, full control** Deploy on your infrastructure. Choose your vector DB, LLM, and configure everything. *** ## Feature Comparison | Feature | Platform | Open Source | | ------------------------- | ------------------------ | ------------------------------------ | | **Time to first memory** | 5 minutes | 15-30 minutes | | **Infrastructure needed** | None | Vector DB + Python/Node env | | **API key setup** | One environment variable | Configure LLM + embedder + vector DB | | **Maintenance** | Fully managed by Mem0 | Self-managed | | Feature | Platform | Open Source | | ------------------------- | ------------------ | ------------------ | | **User & agent memories** | ✅ | ✅ | | **Smart deduplication** | ✅ | ✅ | | **Semantic search** | ✅ | ✅ | | **Memory updates** | ✅ | ✅ | | **Multi-language SDKs** | Python, JavaScript | Python, JavaScript | | Feature | Platform | Open Source | | ---------------------- | ----------- | ------------------- | | **Graph Memory** | ✅ (Managed) | ✅ (Self-configured) | | **Multimodal support** | ✅ | ✅ | | **Custom categories** | ✅ | Limited | | **Advanced retrieval** | ✅ | ✅ | | **Memory filters v2** | ✅ | ⚠️ (via metadata) | | **Webhooks** | ✅ | ❌ | | **Memory export** | ✅ | ❌ | | Feature | Platform | Open Source | | --------------------- | ------------------- | --------------------------------------------- | | **Hosting** | Managed by Mem0 | Self-hosted | | **Auto-scaling** | ✅ | Manual | | **High availability** | ✅ Built-in | DIY setup | | **Vector DB choice** | Managed | Qdrant, Chroma, Pinecone, Milvus, +20 more | | **LLM choice** | Managed (optimized) | OpenAI, Anthropic, Ollama, Together, +10 more | | **Data residency** | US (expandable) | Your choice | | Aspect | Platform | Open Source | | ------------------------ | ------------------------------- | ------------------------------------ | | **License** | Usage-based pricing | Apache 2.0 (free) | | **Infrastructure costs** | Included in pricing | You pay for VectorDB + LLM + hosting | | **Support** | Included | Community + GitHub | | **Best for** | Fast iteration, production apps | Cost-sensitive, custom requirements | | Feature | Platform | Open Source | | -------------------------- | ---------------------------------- | -------------------- | | **REST API** | ✅ | ✅ (via feature flag) | | **Python SDK** | ✅ | ✅ | | **JavaScript SDK** | ✅ | ✅ | | **Framework integrations** | LangChain, CrewAI, LlamaIndex, +15 | Same | | **Dashboard** | ✅ Web-based | ❌ | | **Analytics** | ✅ Built-in | DIY | *** ## Decision Guide ### Choose **Platform** if you want: Get your AI app with memory live in hours, not weeks. No infrastructure setup needed. Auto-scaling, high availability, and managed infrastructure out of the box. Track memory usage, query patterns, and user engagement through our dashboard. Access to webhooks, memory export, custom categories, and priority support. ### Choose **Open Source** if you need: Host everything on your infrastructure. Complete data residency and privacy control. Choose your own vector DB, LLM provider, embedder, and deployment strategy. Modify the codebase, add custom features, and contribute back to the community. Use local LLMs (Ollama), self-hosted vector DBs, and optimize for your specific use case. *** ## Still not sure? Sign up and test the Platform with our free tier. No credit card required. Clone the repo and run locally to see how it works. Star us while you're there! # Quickstart Source: https://docs.mem0.ai/platform/quickstart Get started with Mem0 Platform in minutes Get started with Mem0 Platform's hosted API in under 5 minutes. This guide shows you how to authenticate and store your first memory. ## Prerequisites * Mem0 Platform account ([Sign up here](https://app.mem0.ai)) * API key ([Get one from dashboard](https://app.mem0.ai/settings/api-keys)) * Python 3.10+, Node.js 14+, or cURL ## Installation ```bash pip theme={null} pip install mem0ai ``` ```bash npm theme={null} npm install mem0ai ``` ```python Python theme={null} from mem0 import MemoryClient client = MemoryClient(api_key="your-api-key") ``` ```javascript JavaScript theme={null} import MemoryClient from 'mem0ai'; const client = new MemoryClient({ apiKey: 'your-api-key' }); ``` ```bash cURL theme={null} export MEM0_API_KEY="your-api-key" ``` ```python Python theme={null} messages = [ {"role": "user", "content": "I'm a vegetarian and allergic to nuts."}, {"role": "assistant", "content": "Got it! I'll remember your dietary preferences."} ] client.add(messages, user_id="user123") ``` ```javascript JavaScript theme={null} const messages = [ {"role": "user", "content": "I'm a vegetarian and allergic to nuts."}, {"role": "assistant", "content": "Got it! I'll remember your dietary preferences."} ]; await client.add(messages, { user_id: "user123" }); ``` ```bash cURL theme={null} curl -X POST https://api.mem0.ai/v1/memories/add \ -H "Authorization: Bearer $MEM0_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "messages": [ {"role": "user", "content": "Im a vegetarian and allergic to nuts."}, {"role": "assistant", "content": "Got it! Ill remember your dietary preferences."} ], "user_id": "user123" }' ``` ```python Python theme={null} results = client.search("What are my dietary restrictions?", filters={"user_id": "user123"}) print(results) ``` ```javascript JavaScript theme={null} const results = await client.search("What are my dietary restrictions?", { filters: { user_id: "user123" } }); console.log(results); ``` ```bash cURL theme={null} curl -X POST https://api.mem0.ai/v1/memories/search \ -H "Authorization: Bearer $MEM0_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "query": "What are my dietary restrictions?", "filters": {"user_id": "user123"} }' ``` **Output:** ```json theme={null} { "results": [ { "id": "14e1b28a-2014-40ad-ac42-69c9ef42193d", "memory": "Allergic to nuts", "user_id": "user123", "categories": ["health"], "created_at": "2025-10-22T04:40:22.864647-07:00", "score": 0.30 } ] } ``` ## What's Next? Learn how to search, update, and delete memories with complete CRUD operations Explore advanced features like metadata filtering, graph memory, and webhooks See complete API documentation and integration examples ## Additional Resources * **[Platform vs OSS](/platform/platform-vs-oss)** - Understand the differences between Platform and Open Source * **[Troubleshooting](/platform/faqs)** - Common issues and solutions * **[Integration Examples](/cookbooks/companions/quickstart-demo)** - See Mem0 in action # Overview Source: https://docs.mem0.ai/api-reference REST APIs for memory management, search, and entity operations ## Mem0 REST API Mem0 provides a comprehensive REST API for integrating advanced memory capabilities into your applications. Create, search, update, and manage memories across users, agents, and custom entities with simple HTTP requests. **Quick start:** Get your API key from the [Mem0 Dashboard](https://app.mem0.ai/dashboard/api-keys) and make your first memory operation in minutes. *** ## Quick Start Guide Get started with Mem0 API in three simple steps: 1. **[Add Memories](/api-reference/memory/add-memories)** - Store information and context from user conversations 2. **[Search Memories](/api-reference/memory/v2-search-memories)** - Retrieve relevant memories using semantic search 3. **[Get Memories](/api-reference/memory/v2-get-memories)** - Fetch all memories for a specific entity *** ## Core Operations Store new memories from conversations and interactions Find relevant memories using semantic search with filters Modify existing memory content and metadata Remove specific memories or batch delete operations *** ## API Categories Explore the full API organized by functionality: Core and advanced operations: CRUD, search, batch updates, history, and exports Track and monitor the status of asynchronous memory operations Manage users, agents, and their associated memory data Multi-tenant support, access control, and team collaboration Real-time notifications for memory events and updates **Building multi-tenant apps?** Learn about [Organizations & Projects](/api-reference/organizations-projects) for team isolation and access control. *** ## Authentication All API requests require authentication using Token-based authentication. Include your API key in the Authorization header: ```bash theme={null} Authorization: Token ``` Get your API key from the [Mem0 Dashboard](https://app.mem0.ai/dashboard/api-keys). **Keep your API key secure.** Never expose it in client-side code or public repositories. Use environment variables and server-side requests only. *** ## Next Steps Start storing memories via the REST API Learn advanced search and filtering techniques # Delete User Source: https://docs.mem0.ai/api-reference/entities/delete-user delete /v1/entities/{entity_type}/{entity_id}/ # Get Users Source: https://docs.mem0.ai/api-reference/entities/get-users get /v1/entities/ # Get Event Source: https://docs.mem0.ai/api-reference/events/get-event get /v1/event/{event_id}/ Retrieve details about a specific event by passing its `event_id`. This endpoint is particularly helpful for tracking the status, payload, and completion details of asynchronous memory operations. # Get Events Source: https://docs.mem0.ai/api-reference/events/get-events get /v1/events/ List recent events for your organization and project. ## Use Cases * **Dashboards**: Summarize adds/searches over time by paging through events. * **Alerting**: Poll for `FAILED` events and trigger follow-up workflows. * **Audit**: Store the returned payload/metadata for compliance logs. # Add Memories Source: https://docs.mem0.ai/api-reference/memory/add-memories post /v1/memories/ Add memories. Add new facts, messages, or metadata to a user’s memory store. The Add Memories endpoint accepts either raw text or conversational turns and commits them asynchronously so the memory is ready for later search, retrieval, and graph queries. ## Endpoint * **Method**: `POST` * **URL**: `/v1/memories/` * **Content-Type**: `application/json` Memories are processed asynchronously by default. The response contains queued events you can track while the platform finalizes enrichment. ## Required headers | Header | Required | Description | | -------------------------------------- | -------- | --------------------------------- | | `Authorization: Bearer ` | Yes | API key scoped to your workspace. | | `Accept: application/json` | Yes | Ensures a JSON response. | ## Request body Provide at least one message or direct memory string. Most callers supply `messages` so Mem0 can infer structured memories as part of ingestion. ```json Basic request theme={null} { "user_id": "alice", "messages": [ { "role": "user", "content": "I moved to Austin last month." } ], "metadata": { "source": "onboarding_form" } } ``` ### Common fields | Field | Type | Required | Description | | --------------- | ------------------------ | -------- | ---------------------------------------------------------------------------------------------------- | | `user_id` | string | No\* | Associates the memory with a user. Provide when you want the memory scoped to a specific identity. | | `messages` | array | No\* | Conversation turns for Mem0 to infer memories from. Each object should include `role` and `content`. | | `metadata` | object | Optional | Custom key/value metadata (e.g., `{"topic": "preferences"}`). | | `infer` | boolean (default `true`) | Optional | Set to `false` to skip inference and store the provided text as-is. | | `async_mode` | boolean (default `true`) | Optional | Controls asynchronous processing. Most clients leave this enabled. | | `output_format` | string (default `v1.1`) | Optional | Response format. `v1.1` wraps results in a `results` array. | > \* Provide at least one `messages` entry to describe what you are storing. For scoped memories, include `user_id`. You can also attach `agent_id`, `app_id`, `run_id`, `project_id`, or `org_id` to refine ownership. ## Response Successful requests return an array of events queued for processing. Each event includes the generated memory text and an identifier you can persist for auditing. ```json 200 response theme={null} [ { "id": "mem_01JF8ZS4Y0R0SPM13R5R6H32CJ", "event": "ADD", "data": { "memory": "The user moved to Austin in 2025." } } ] ``` ```json 400 response theme={null} { "error": "400 Bad Request", "details": { "message": "Invalid input data. Please refer to the memory creation documentation at https://docs.mem0.ai/platform/quickstart#4-1-create-memories for correct formatting and required fields." } } ``` ## Graph relationships Add Memories can enrich the knowledge graph on write. Set `enable_graph: true` to create entity nodes and relationships for the stored memory. Use this when you want downstream `get_all` or search calls to traverse connected entities. ```json Graph-aware request theme={null} { "user_id": "alice", "messages": [ { "role": "user", "content": "I met with Dr. Lee at General Hospital." } ], "enable_graph": true } ``` The response follows the same format, and related entities become available in [Graph Memory](/platform/features/graph-memory) queries. # Batch Delete Memories Source: https://docs.mem0.ai/api-reference/memory/batch-delete delete /v1/batch/ Batch delete multiple memories (up to 1000) in a single API call. # Batch Update Memories Source: https://docs.mem0.ai/api-reference/memory/batch-update put /v1/batch/ Batch update multiple memories (up to 1000) in a single API call. # Create Memory Export Source: https://docs.mem0.ai/api-reference/memory/create-memory-export post /v1/exports/ Create a structured export of memories based on a provided schema. Submit a job to create a structured export of memories using a customizable Pydantic schema. This process may take some time to complete, especially if you're exporting a large number of memories. You can tailor the export by applying various filters (e.g., `user_id`, `agent_id`, `run_id`, or `session_id`) and by modifying the Pydantic schema to ensure the final data matches your exact needs. # Delete Memories Source: https://docs.mem0.ai/api-reference/memory/delete-memories delete /v1/memories/ Delete memories. # Delete Memory Source: https://docs.mem0.ai/api-reference/memory/delete-memory delete /v1/memories/{memory_id}/ Get or Update or delete a memory. # Feedback Source: https://docs.mem0.ai/api-reference/memory/feedback post /v1/feedback/ Submit feedback for a memory. # Get Memories Source: https://docs.mem0.ai/api-reference/memory/get-memories post /v2/memories/ Get all memories. The v2 get memories API is powerful and flexible, allowing for more precise memory listing without the need for a search query. It supports complex logical operations (AND, OR, NOT) and comparison operators for advanced filtering capabilities. The comparison operators include: * `in`: Matches any of the values specified * `gte`: Greater than or equal to * `lte`: Less than or equal to * `gt`: Greater than * `lt`: Less than * `ne`: Not equal to * `icontains`: Case-insensitive containment check * `*`: Wildcard character that matches everything ```python Code theme={null} memories = client.get_all( filters={ "AND": [ { "user_id": "alex" }, { "created_at": {"gte": "2024-07-01", "lte": "2024-07-31"} } ] } ) ``` ```python Output theme={null} { "results": [ { "id": "f4cbdb08-7062-4f3e-8eb2-9f5c80dfe64c", "memory": "Alex is planning a trip to San Francisco from July 1st to July 10th", "created_at": "2024-07-01T12:00:00Z", "updated_at": "2024-07-01T12:00:00Z" }, { "id": "a2b8c3d4-5e6f-7g8h-9i0j-1k2l3m4n5o6p", "memory": "Alex prefers vegetarian restaurants", "created_at": "2024-07-05T15:30:00Z", "updated_at": "2024-07-05T15:30:00Z" } ], "total": 2 } ``` ## Graph Memory To retrieve graph memory relationships between entities, pass `output_format="v1.1"` in your request. This will return memories with entity and relationship information from the knowledge graph. ```python Code theme={null} memories = client.get_all( filters={ "user_id": "alex" }, output_format="v1.1" ) ``` ```python Output theme={null} { "results": [ { "id": "f4cbdb08-7062-4f3e-8eb2-9f5c80dfe64c", "memory": "Alex is planning a trip to San Francisco", "entities": [ { "id": "entity-1", "name": "Alex", "type": "person" }, { "id": "entity-2", "name": "San Francisco", "type": "location" } ], "relations": [ { "source": "entity-1", "target": "entity-2", "relationship": "traveling_to" } ] } ] } ``` # Get Memory Source: https://docs.mem0.ai/api-reference/memory/get-memory get /v1/memories/{memory_id}/ Get a memory. # Get Memory Export Source: https://docs.mem0.ai/api-reference/memory/get-memory-export post /v1/exports/get Get the latest memory export. Retrieve the latest structured memory export after submitting an export job. You can filter the export by `user_id`, `run_id`, `session_id`, or `app_id` to get the most recent export matching your filters. # Memory History Source: https://docs.mem0.ai/api-reference/memory/history-memory get /v1/memories/{memory_id}/history/ Retrieve the history of a memory. # Search Memories Source: https://docs.mem0.ai/api-reference/memory/search-memories post /v2/memories/search/ Search memories based on a query and filters. The v2 search API is powerful and flexible, allowing for more precise memory retrieval. It supports complex logical operations (AND, OR, NOT) and comparison operators for advanced filtering capabilities. The comparison operators include: * `in`: Matches any of the values specified * `gte`: Greater than or equal to * `lte`: Less than or equal to * `gt`: Greater than * `lt`: Less than * `ne`: Not equal to * `icontains`: Case-insensitive containment check * `*`: Wildcard character that matches everything ```python Platform API Example theme={null} related_memories = client.search( query="What are Alice's hobbies?", filters={ "OR": [ { "user_id": "alice" }, { "agent_id": {"in": ["travel-agent", "sports-agent"]} } ] }, ) ``` ```json Output theme={null} { "memories": [ { "id": "ea925981-272f-40dd-b576-be64e4871429", "memory": "Likes to play cricket and plays cricket on weekends.", "metadata": { "category": "hobbies" }, "score": 0.32116443111457704, "created_at": "2024-07-26T10:29:36.630547-07:00", "updated_at": null, "user_id": "alice", "agent_id": "sports-agent" } ], } ``` ```python Wildcard Example theme={null} # Using wildcard to match all run_ids for a specific user all_memories = client.search( query="What are Alice's hobbies?", filters={ "AND": [ { "user_id": "alice" }, { "run_id": "*" } ] }, ) ``` ```python Categories Filter Examples theme={null} # Example 1: Using 'contains' for partial matching finance_memories = client.search( query="What are my financial goals?", filters={ "AND": [ { "user_id": "alice" }, { "categories": { "contains": "finance" } } ] }, ) # Example 2: Using 'in' for exact matching personal_memories = client.search( query="What personal information do you have?", filters={ "AND": [ { "user_id": "alice" }, { "categories": { "in": ["personal_information"] } } ] }, ) ``` # Update Memory Source: https://docs.mem0.ai/api-reference/memory/update-memory put /v1/memories/{memory_id}/ Get or Update or delete a memory. # Add Member Source: https://docs.mem0.ai/api-reference/organization/add-org-member post /api/v1/orgs/organizations/{org_id}/members/ Add a new member to a specific organization. The API provides two roles for organization members: * `READER`: Allows viewing of organization resources. * `OWNER`: Grants full administrative access to manage the organization and its resources. # Create Organization Source: https://docs.mem0.ai/api-reference/organization/create-org post /api/v1/orgs/organizations/ Create a new organization. # Delete Organization Source: https://docs.mem0.ai/api-reference/organization/delete-org delete /api/v1/orgs/organizations/{org_id}/ Delete an organization by its ID. # Get Organization Source: https://docs.mem0.ai/api-reference/organization/get-org get /api/v1/orgs/organizations/{org_id}/ Get a organization. # Get Members Source: https://docs.mem0.ai/api-reference/organization/get-org-members get /api/v1/orgs/organizations/{org_id}/members/ Retrieve a list of members for a specific organization. # Get Organizations Source: https://docs.mem0.ai/api-reference/organization/get-orgs get /api/v1/orgs/organizations/ # Organizations & Projects Source: https://docs.mem0.ai/api-reference/organizations-projects Manage multi-tenant applications with organization and project APIs ## Overview Organizations and projects provide multi-tenant support, access control, and team collaboration capabilities for Mem0 Platform. Use these APIs to build applications that support multiple teams, customers, or isolated environments. Organizations and projects are **optional** features. You can use Mem0 without them for single-user or simple multi-user applications. ## Key Capabilities * **Multi-org/project Support**: Specify organization and project when initializing the Mem0 client to attribute API usage appropriately * **Member Management**: Control access to data through organization and project membership * **Access Control**: Only members can access memories and data within their organization/project scope * **Team Isolation**: Maintain data separation between different teams and projects for secure collaboration *** ## Using Organizations & Projects ### Initialize with Org/Project Context Example with the mem0 Python package: ```python theme={null} from mem0 import MemoryClient client = MemoryClient(org_id='YOUR_ORG_ID', project_id='YOUR_PROJECT_ID') ``` ```javascript theme={null} import { MemoryClient } from "mem0ai"; const client = new MemoryClient({ organizationId: "YOUR_ORG_ID", projectId: "YOUR_PROJECT_ID" }); ``` *** ## Project Management The Mem0 client provides comprehensive project management through the `client.project` interface: ### Get Project Details Retrieve information about the current project: ```python theme={null} # Get all project details project_info = client.project.get() # Get specific fields only project_info = client.project.get(fields=["name", "description", "custom_categories"]) ``` ### Create a New Project Create a new project within your organization: ```python theme={null} # Create a project with name and description new_project = client.project.create( name="My New Project", description="A project for managing customer support memories" ) ``` ### Update Project Settings Modify project configuration including custom instructions, categories, and graph settings: ```python theme={null} # Update project with custom categories client.project.update( custom_categories=[ {"customer_preferences": "Customer likes, dislikes, and preferences"}, {"support_history": "Previous support interactions and resolutions"} ] ) # Update project with custom instructions client.project.update( custom_instructions="..." ) # Enable graph memory for the project client.project.update(enable_graph=True) # Update multiple settings at once client.project.update( custom_instructions="...", custom_categories=[ {"personal_info": "User personal information and preferences"}, {"work_context": "Professional context and work-related information"} ], enable_graph=True ) ``` ### Delete Project This action will remove all memories, messages, and other related data in the project. **This operation is irreversible.** Remove a project and all its associated data: ```python theme={null} # Delete the current project (irreversible) result = client.project.delete() ``` *** ## Member Management Manage project members and their access levels: ```python theme={null} # Get all project members members = client.project.get_members() # Add a new member as a reader client.project.add_member( email="colleague@company.com", role="READER" # or "OWNER" ) # Update a member's role client.project.update_member( email="colleague@company.com", role="OWNER" ) # Remove a member from the project client.project.remove_member(email="colleague@company.com") ``` ### Member Roles | Role | Permissions | | ---------- | ----------------------------------------------------------------------------------------- | | **READER** | Can view and search memories, but cannot modify project settings or manage members | | **OWNER** | Full access including project modification, member management, and all reader permissions | *** ## Async Support All project methods are available in async mode: ```python theme={null} from mem0 import AsyncMemoryClient async def manage_project(): client = AsyncMemoryClient(org_id='YOUR_ORG_ID', project_id='YOUR_PROJECT_ID') # All methods support async/await project_info = await client.project.get() await client.project.update(enable_graph=True) members = await client.project.get_members() # To call the async function properly import asyncio asyncio.run(manage_project()) ``` *** ## API Reference For complete API specifications and additional endpoints, see: Create, get, and manage organizations Full project CRUD and member management endpoints # Create Project Source: https://docs.mem0.ai/api-reference/project/create-project post /api/v1/orgs/organizations/{org_id}/projects/ Create a new project within an organization. # Get Project Source: https://docs.mem0.ai/api-reference/project/get-project get /api/v1/orgs/organizations/{org_id}/projects/{project_id}/ Retrieve details of a specific project within an organization. # Get Projects Source: https://docs.mem0.ai/api-reference/project/get-projects get /api/v1/orgs/organizations/{org_id}/projects/ Retrieve a list of projects for a specific organization. # AWS Bedrock Source: https://docs.mem0.ai/components/embedders/models/aws_bedrock To use AWS Bedrock embedding models, you need to have the appropriate AWS credentials and permissions. The embeddings implementation relies on the `boto3` library. ### Setup * Ensure you have model access from the [AWS Bedrock Console](https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/modelaccess) * Authenticate the boto3 client using a method described in the [AWS documentation](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html) * Set up environment variables for authentication: ```bash theme={null} export AWS_REGION=us-east-1 export AWS_ACCESS_KEY_ID=your-access-key export AWS_SECRET_ACCESS_KEY=your-secret-key ``` ### Usage ```python Python theme={null} import os from mem0 import Memory # For LLM if needed os.environ["OPENAI_API_KEY"] = "your-openai-api-key" # AWS credentials os.environ["AWS_REGION"] = "us-west-2" os.environ["AWS_ACCESS_KEY_ID"] = "your-access-key" os.environ["AWS_SECRET_ACCESS_KEY"] = "your-secret-key" config = { "embedder": { "provider": "aws_bedrock", "config": { "model": "amazon.titan-embed-text-v2:0" } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice") ``` ### Config Here are the parameters available for configuring AWS Bedrock embedder: | Parameter | Description | Default Value | | --------- | -------------------------------------- | ---------------------------- | | `model` | The name of the embedding model to use | `amazon.titan-embed-text-v1` | # Google AI Source: https://docs.mem0.ai/components/embedders/models/google_AI To use Google AI embedding models, set the `GOOGLE_API_KEY` environment variables. You can obtain the Gemini API key from [here](https://aistudio.google.com/app/apikey). ### Usage ```python Python theme={null} import os from mem0 import Memory os.environ["GOOGLE_API_KEY"] = "key" os.environ["OPENAI_API_KEY"] = "your_api_key" # For LLM config = { "embedder": { "provider": "gemini", "config": { "model": "models/text-embedding-004", } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="john") ``` ```typescript TypeScript theme={null} import { Memory } from 'mem0ai/oss'; const config = { embedder: { provider: "google", config: { apiKey: process.env["GOOGLE_API_KEY"], model: "gemini-embedding-001", embeddingDims: 1536, }, }, }; const memory = new Memory(config); const messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] await memory.add(messages, { userId: "john" }); ``` ### Config Here are the parameters available for configuring Gemini embedder: | Parameter | Description | Default Value | | ---------------- | -------------------------------------- | --------------------------- | | `model` | The name of the embedding model to use | `models/text-embedding-004` | | `embedding_dims` | Dimensions of the embedding model | `1536` | | `api_key` | The Google API key | `None` | | Parameter | Description | Default Value | | --------------- | -------------------------------------- | ---------------------- | | `model` | The name of the embedding model to use | `gemini-embedding-001` | | `embeddingDims` | Dimensions of the embedding model | `1536` | | `apiKey` | Google API key | `None` | # LangChain Source: https://docs.mem0.ai/components/embedders/models/langchain Mem0 supports LangChain as a provider to access a wide range of embedding models. LangChain is a framework for developing applications powered by language models, making it easy to integrate various embedding providers through a consistent interface. For a complete list of available embedding models supported by LangChain, refer to the [LangChain Text Embedding documentation](https://python.langchain.com/docs/integrations/text_embedding/). ## Usage ```python Python theme={null} import os from mem0 import Memory from langchain_openai import OpenAIEmbeddings # Set necessary environment variables for your chosen LangChain provider os.environ["OPENAI_API_KEY"] = "your-api-key" # Initialize a LangChain embeddings model directly openai_embeddings = OpenAIEmbeddings( model="text-embedding-3-small", dimensions=1536 ) # Pass the initialized model to the config config = { "embedder": { "provider": "langchain", "config": { "model": openai_embeddings } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` ```typescript TypeScript theme={null} import { Memory } from 'mem0ai/oss'; import { OpenAIEmbeddings } from "@langchain/openai"; // Initialize a LangChain embeddings model directly const openaiEmbeddings = new OpenAIEmbeddings({ modelName: "text-embedding-3-small", dimensions: 1536, apiKey: process.env.OPENAI_API_KEY, }); const config = { embedder: { provider: 'langchain', config: { model: openaiEmbeddings, }, }, }; const memory = new Memory(config); const messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] await memory.add(messages, { userId: "alice", metadata: { category: "movies" } }); ``` ## Supported LangChain Embedding Providers LangChain supports a wide range of embedding providers, including: * OpenAI (`OpenAIEmbeddings`) * Cohere (`CohereEmbeddings`) * Google (`VertexAIEmbeddings`) * Hugging Face (`HuggingFaceEmbeddings`) * Sentence Transformers (`HuggingFaceEmbeddings`) * Azure OpenAI (`AzureOpenAIEmbeddings`) * Ollama (`OllamaEmbeddings`) * Together (`TogetherEmbeddings`) * And many more You can use any of these model instances directly in your configuration. For a complete and up-to-date list of available embedding providers, refer to the [LangChain Text Embedding documentation](https://python.langchain.com/docs/integrations/text_embedding/). ## Provider-Specific Configuration When using LangChain as an embedder provider, you'll need to: 1. Set the appropriate environment variables for your chosen embedding provider 2. Import and initialize the specific model class you want to use 3. Pass the initialized model instance to the config ### Examples with Different Providers #### HuggingFace Embeddings ```python Python theme={null} from langchain_huggingface import HuggingFaceEmbeddings # Initialize a HuggingFace embeddings model hf_embeddings = HuggingFaceEmbeddings( model_name="BAAI/bge-small-en-v1.5", encode_kwargs={"normalize_embeddings": True} ) config = { "embedder": { "provider": "langchain", "config": { "model": hf_embeddings } } } ``` ```typescript TypeScript theme={null} import { Memory } from 'mem0ai/oss'; import { HuggingFaceEmbeddings } from "@langchain/community/embeddings/hf"; // Initialize a HuggingFace embeddings model const hfEmbeddings = new HuggingFaceEmbeddings({ modelName: "BAAI/bge-small-en-v1.5", encode: { normalize_embeddings: true, }, }); const config = { embedder: { provider: 'langchain', config: { model: hfEmbeddings, }, }, }; ``` #### Ollama Embeddings ```python Python theme={null} from langchain_ollama import OllamaEmbeddings # Initialize an Ollama embeddings model ollama_embeddings = OllamaEmbeddings( model="nomic-embed-text" ) config = { "embedder": { "provider": "langchain", "config": { "model": ollama_embeddings } } } ``` ```typescript TypeScript theme={null} import { Memory } from 'mem0ai/oss'; import { OllamaEmbeddings } from "@langchain/community/embeddings/ollama"; // Initialize an Ollama embeddings model const ollamaEmbeddings = new OllamaEmbeddings({ model: "nomic-embed-text", baseUrl: "http://localhost:11434", // Ollama server URL }); const config = { embedder: { provider: 'langchain', config: { model: ollamaEmbeddings, }, }, }; ``` Make sure to install the necessary LangChain packages and any provider-specific dependencies. ## Config All available parameters for the `langchain` embedder config are present in [Master List of All Params in Config](../config). # null Source: https://docs.mem0.ai/components/embedders/models/lmstudio You can use embedding models from LM Studio to run Mem0 locally. ### Usage ```python theme={null} import os from mem0 import Memory os.environ["OPENAI_API_KEY"] = "your_api_key" # For LLM config = { "embedder": { "provider": "lmstudio", "config": { "model": "nomic-embed-text-v1.5-GGUF/nomic-embed-text-v1.5.f16.gguf" } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="john") ``` ### Config Here are the parameters available for configuring LM Studio embedder: | Parameter | Description | Default Value | | ------------------- | -------------------------------------- | ----------------------------------------------------------- | | `model` | The name of the LM Studio model to use | `nomic-embed-text-v1.5-GGUF/nomic-embed-text-v1.5.f16.gguf` | | `embedding_dims` | Dimensions of the embedding model | `1536` | | `lmstudio_base_url` | Base URL for LM Studio connection | `http://localhost:1234/v1` | # Together Source: https://docs.mem0.ai/components/embedders/models/together To use Together embedding models, set the `TOGETHER_API_KEY` environment variable. You can obtain the Together API key from the [Together Platform](https://api.together.xyz/settings/api-keys). ### Usage The `embedding_model_dims` parameter for `vector_store` should be set to `768` for Together embedder. ```python theme={null} import os from mem0 import Memory os.environ["TOGETHER_API_KEY"] = "your_api_key" os.environ["OPENAI_API_KEY"] = "your_api_key" # For LLM config = { "embedder": { "provider": "together", "config": { "model": "togethercomputer/m2-bert-80M-8k-retrieval" } } } m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="john") ``` ### Config Here are the parameters available for configuring Together embedder: | Parameter | Description | Default Value | | ---------------- | -------------------------------------- | ------------------------------------------- | | `model` | The name of the embedding model to use | `togethercomputer/m2-bert-80M-8k-retrieval` | | `embedding_dims` | Dimensions of the embedding model | `768` | | `api_key` | The Together API key | `None` | # Config Source: https://docs.mem0.ai/components/rerankers/config Configuration options for rerankers in Mem0 ## Common Configuration Parameters All rerankers share these common configuration parameters: | Parameter | Description | Type | Default | | ---------- | --------------------------------------------------- | ----- | -------- | | `provider` | Reranker provider name | `str` | Required | | `top_k` | Maximum number of results to return after reranking | `int` | `None` | | `api_key` | API key for the reranker service | `str` | `None` | ## Provider-Specific Configuration ### Zero Entropy | Parameter | Description | Type | Default | | --------- | -------------------------------------------- | ----- | ------------ | | `model` | Model to use: `zerank-1` or `zerank-1-small` | `str` | `"zerank-1"` | | `api_key` | Zero Entropy API key | `str` | `None` | ### Cohere | Parameter | Description | Type | Default | | -------------------- | -------------------------------------------- | ------ | ----------------------- | | `model` | Cohere rerank model | `str` | `"rerank-english-v3.0"` | | `api_key` | Cohere API key | `str` | `None` | | `return_documents` | Whether to return document texts in response | `bool` | `False` | | `max_chunks_per_doc` | Maximum chunks per document | `int` | `None` | ### Sentence Transformer | Parameter | Description | Type | Default | | ------------------- | -------------------------------------------- | ------ | ---------------------------------------- | | `model` | HuggingFace cross-encoder model name | `str` | `"cross-encoder/ms-marco-MiniLM-L-6-v2"` | | `device` | Device to run model on (`cpu`, `cuda`, etc.) | `str` | `None` | | `batch_size` | Batch size for processing | `int` | `32` | | `show_progress_bar` | Show progress during processing | `bool` | `False` | ### Hugging Face | Parameter | Description | Type | Default | | --------- | -------------------------------------------- | ----- | --------------------------- | | `model` | HuggingFace reranker model name | `str` | `"BAAI/bge-reranker-large"` | | `api_key` | HuggingFace API token | `str` | `None` | | `device` | Device to run model on (`cpu`, `cuda`, etc.) | `str` | `None` | ### LLM-based | Parameter | Description | Type | Default | | ---------------- | ------------------------------------------ | ------- | ---------------------- | | `model` | LLM model to use for scoring | `str` | `"gpt-4o-mini"` | | `provider` | LLM provider (`openai`, `anthropic`, etc.) | `str` | `"openai"` | | `api_key` | API key for LLM provider | `str` | `None` | | `temperature` | Temperature for LLM generation | `float` | `0.0` | | `max_tokens` | Maximum tokens for LLM response | `int` | `100` | | `scoring_prompt` | Custom prompt template for scoring | `str` | Default scoring prompt | ### LLM Reranker | Parameter | Description | Type | Default | | -------------- | --------------------------- | ------ | -------- | | `llm.provider` | LLM provider for reranking | `str` | Required | | `llm.config` | LLM configuration object | `dict` | Required | | `top_n` | Number of results to return | `int` | `None` | ## Environment Variables You can set API keys using environment variables: * `ZERO_ENTROPY_API_KEY` - Zero Entropy API key * `COHERE_API_KEY` - Cohere API key * `HUGGINGFACE_API_KEY` - HuggingFace API token * `OPENAI_API_KEY` - OpenAI API key (for LLM-based reranker) * `ANTHROPIC_API_KEY` - Anthropic API key (for LLM-based reranker) ## Basic Configuration Example ```python Python theme={null} config = { "vector_store": { "provider": "chroma", "config": { "collection_name": "my_memories", "path": "./chroma_db" } }, "llm": { "provider": "openai", "config": { "model": "gpt-4.1-nano-2025-04-14" } }, "reranker": { "provider": "zero_entropy", "config": { "model": "zerank-1", "top_k": 5 } } } ``` # Custom Prompts Source: https://docs.mem0.ai/components/rerankers/custom-prompts When using LLM rerankers, you can customize the prompts used for ranking to better suit your specific use case and domain. ## Default Prompt The default LLM reranker prompt is designed to be general-purpose: ``` Given a query and a list of memory entries, rank the memory entries based on their relevance to the query. Rate each memory on a scale of 1-10 where 10 is most relevant. Query: {query} Memory entries: {memories} Provide your ranking as a JSON array with scores for each memory. ``` ## Custom Prompt Configuration You can provide a custom prompt template when configuring the LLM reranker: ```python theme={null} from mem0 import Memory custom_prompt = """ You are an expert at ranking memories for a personal AI assistant. Given a user query and a list of memory entries, rank each memory based on: 1. Direct relevance to the query 2. Temporal relevance (recent memories may be more important) 3. Emotional significance 4. Actionability Query: {query} User Context: {user_context} Memory entries: {memories} Rate each memory from 1-10 and provide reasoning. Return as JSON: {{"rankings": [{{"index": 0, "score": 8, "reason": "..."}}]}} """ config = { "reranker": { "provider": "llm_reranker", "config": { "llm": { "provider": "openai", "config": { "model": "gpt-4.1-nano-2025-04-14", "api_key": "your-openai-key" } }, "custom_prompt": custom_prompt, "top_n": 5 } } } memory = Memory.from_config(config) ``` ## Prompt Variables Your custom prompt can use the following variables: | Variable | Description | | ---------------- | ------------------------------------- | | `{query}` | The search query | | `{memories}` | The list of memory entries to rank | | `{user_id}` | The user ID (if available) | | `{user_context}` | Additional user context (if provided) | ## Domain-Specific Examples ### Customer Support ```python theme={null} customer_support_prompt = """ You are ranking customer support conversation memories. Prioritize memories that: - Relate to the current customer issue - Show previous resolution patterns - Indicate customer preferences or constraints Query: {query} Customer Context: Previous interactions with this customer Memories: {memories} Rank each memory 1-10 based on support relevance. """ ``` ### Educational Content ```python theme={null} educational_prompt = """ Rank these learning memories for a student query. Consider: - Prerequisite knowledge requirements - Learning progression and difficulty - Relevance to current learning objectives Student Query: {query} Learning Context: {user_context} Available memories: {memories} Score each memory for educational value (1-10). """ ``` ### Personal Assistant ```python theme={null} personal_assistant_prompt = """ Rank personal memories for relevance to the user's query. Consider: - Recent vs. historical importance - Personal preferences and habits - Contextual relationships between memories Query: {query} Personal context: {user_context} Memories to rank: {memories} Provide relevance scores (1-10) with brief explanations. """ ``` ## Advanced Prompt Techniques ### Multi-Criteria Ranking ```python theme={null} multi_criteria_prompt = """ Evaluate memories using multiple criteria: 1. RELEVANCE (40%): How directly related to the query 2. RECENCY (20%): How recent the memory is 3. IMPORTANCE (25%): Personal or business significance 4. ACTIONABILITY (15%): How useful for next steps Query: {query} Context: {user_context} Memories: {memories} For each memory, provide: - Overall score (1-10) - Breakdown by criteria - Final ranking recommendation Format: JSON with detailed scoring """ ``` ### Contextual Ranking ```python theme={null} contextual_prompt = """ Consider the following context when ranking memories: - Current user situation: {user_context} - Time of day: {current_time} - Recent activities: {recent_activities} Query: {query} Rank these memories considering both direct relevance and contextual appropriateness: {memories} Provide contextually-aware relevance scores (1-10). """ ``` ## Best Practices 1. **Be Specific**: Clearly define what makes a memory relevant for your use case 2. **Use Examples**: Include examples in your prompt for better model understanding 3. **Structure Output**: Specify the exact JSON format you want returned 4. **Test Iteratively**: Refine your prompt based on actual ranking performance 5. **Consider Token Limits**: Keep prompts concise while being comprehensive ## Prompt Testing You can test different prompts by comparing ranking results: ```python theme={null} # Test multiple prompt variations prompts = [ default_prompt, custom_prompt_v1, custom_prompt_v2 ] for i, prompt in enumerate(prompts): config["reranker"]["config"]["custom_prompt"] = prompt memory = Memory.from_config(config) results = memory.search("test query", user_id="test_user") print(f"Prompt {i+1} results: {results}") ``` ## Common Issues * **Too Long**: Keep prompts under token limits for your chosen LLM * **Too Vague**: Be specific about ranking criteria * **Inconsistent Format**: Ensure JSON output format is clearly specified * **Missing Context**: Include relevant variables for your use case # Cohere Source: https://docs.mem0.ai/components/rerankers/models/cohere Reranking with Cohere Cohere provides enterprise-grade reranking models with excellent multilingual support and production-ready performance. ## Models Cohere offers several reranking models: * **`rerank-english-v3.0`**: Latest English reranker with best performance * **`rerank-multilingual-v3.0`**: Multilingual support for global applications * **`rerank-english-v2.0`**: Previous generation English reranker ## Installation ```bash theme={null} pip install cohere ``` ## Configuration ```python Python theme={null} from mem0 import Memory config = { "vector_store": { "provider": "chroma", "config": { "collection_name": "my_memories", "path": "./chroma_db" } }, "llm": { "provider": "openai", "config": { "model": "gpt-4.1-nano-2025-04-14" } }, "reranker": { "provider": "cohere", "config": { "model": "rerank-english-v3.0", "api_key": "your-cohere-api-key", # or set COHERE_API_KEY "top_k": 5, "return_documents": False, "max_chunks_per_doc": None } } } memory = Memory.from_config(config) ``` ## Environment Variables Set your API key as an environment variable: ```bash theme={null} export COHERE_API_KEY="your-api-key" ``` ## Usage Example ```python Python theme={null} import os from mem0 import Memory # Set API key os.environ["COHERE_API_KEY"] = "your-api-key" # Initialize memory with Cohere reranker config = { "vector_store": {"provider": "chroma"}, "llm": {"provider": "openai", "config": {"model": "gpt-4o-mini"}}, "rerank": { "provider": "cohere", "config": { "model": "rerank-english-v3.0", "top_k": 3 } } } memory = Memory.from_config(config) # Add memories messages = [ {"role": "user", "content": "I work as a data scientist at Microsoft"}, {"role": "user", "content": "I specialize in machine learning and NLP"}, {"role": "user", "content": "I enjoy playing tennis on weekends"} ] memory.add(messages, user_id="bob") # Search with reranking results = memory.search("What is the user's profession?", user_id="bob") for result in results['results']: print(f"Memory: {result['memory']}") print(f"Vector Score: {result['score']:.3f}") print(f"Rerank Score: {result['rerank_score']:.3f}") print() ``` ## Multilingual Support For multilingual applications, use the multilingual model: ```python Python theme={null} config = { "rerank": { "provider": "cohere", "config": { "model": "rerank-multilingual-v3.0", "top_k": 5 } } } ``` ## Configuration Parameters | Parameter | Description | Type | Default | | -------------------- | -------------------------------- | ------ | ----------------------- | | `model` | Cohere rerank model to use | `str` | `"rerank-english-v3.0"` | | `api_key` | Cohere API key | `str` | `None` | | `top_k` | Maximum documents to return | `int` | `None` | | `return_documents` | Whether to return document texts | `bool` | `False` | | `max_chunks_per_doc` | Maximum chunks per document | `int` | `None` | ## Features * **High Quality**: Enterprise-grade relevance scoring * **Multilingual**: Support for 100+ languages * **Scalable**: Production-ready with high throughput * **Reliable**: SLA-backed service with 99.9% uptime ## Best Practices 1. **Model Selection**: Use `rerank-english-v3.0` for English, `rerank-multilingual-v3.0` for other languages 2. **Batch Processing**: Process multiple queries efficiently 3. **Error Handling**: Implement retry logic for production systems 4. **Monitoring**: Track reranking performance and costs # Hugging Face Reranker Source: https://docs.mem0.ai/components/rerankers/models/huggingface Access thousands of reranking models from Hugging Face Hub ## Overview The Hugging Face reranker provider gives you access to thousands of reranking models available on the Hugging Face Hub. This includes popular models like BAAI's BGE rerankers and other state-of-the-art cross-encoder models. ## Configuration ### Basic Setup ```python theme={null} from mem0 import Memory config = { "reranker": { "provider": "huggingface", "config": { "model": "BAAI/bge-reranker-base", "device": "cpu" } } } m = Memory.from_config(config) ``` ### Configuration Parameters | Parameter | Type | Default | Description | | ------------------- | ---- | -------- | --------------------------------------------- | | `model` | str | Required | Hugging Face model identifier | | `device` | str | "cpu" | Device to run model on ("cpu", "cuda", "mps") | | `batch_size` | int | 32 | Batch size for processing | | `max_length` | int | 512 | Maximum input sequence length | | `trust_remote_code` | bool | False | Allow remote code execution | ### Advanced Configuration ```python theme={null} config = { "reranker": { "provider": "huggingface", "config": { "model": "BAAI/bge-reranker-large", "device": "cuda", "batch_size": 16, "max_length": 512, "trust_remote_code": False, "model_kwargs": { "torch_dtype": "float16" } } } } ``` ## Popular Models ### BGE Rerankers (Recommended) ```python theme={null} # Base model - good balance of speed and quality config = { "reranker": { "provider": "huggingface", "config": { "model": "BAAI/bge-reranker-base", "device": "cuda" } } } # Large model - better quality, slower config = { "reranker": { "provider": "huggingface", "config": { "model": "BAAI/bge-reranker-large", "device": "cuda" } } } # v2 models - latest improvements config = { "reranker": { "provider": "huggingface", "config": { "model": "BAAI/bge-reranker-v2-m3", "device": "cuda" } } } ``` ### Multilingual Models ```python theme={null} # Multilingual BGE reranker config = { "reranker": { "provider": "huggingface", "config": { "model": "BAAI/bge-reranker-v2-multilingual", "device": "cuda" } } } ``` ### Domain-Specific Models ```python theme={null} # For code search config = { "reranker": { "provider": "huggingface", "config": { "model": "microsoft/codebert-base", "device": "cuda" } } } # For biomedical content config = { "reranker": { "provider": "huggingface", "config": { "model": "dmis-lab/biobert-base-cased-v1.1", "device": "cuda" } } } ``` ## Usage Examples ### Basic Usage ```python theme={null} from mem0 import Memory m = Memory.from_config(config) # Add some memories m.add("I love hiking in the mountains", user_id="alice") m.add("Pizza is my favorite food", user_id="alice") m.add("I enjoy reading science fiction books", user_id="alice") # Search with reranking results = m.search( "What outdoor activities do I enjoy?", user_id="alice", rerank=True ) for result in results["results"]: print(f"Memory: {result['memory']}") print(f"Score: {result['score']:.3f}") ``` ### Batch Processing ```python theme={null} # Process multiple queries efficiently queries = [ "What are my hobbies?", "What food do I like?", "What books interest me?" ] results = [] for query in queries: result = m.search(query, user_id="alice", rerank=True) results.append(result) ``` ## Performance Optimization ### GPU Acceleration ```python theme={null} # Use GPU for better performance config = { "reranker": { "provider": "huggingface", "config": { "model": "BAAI/bge-reranker-base", "device": "cuda", "batch_size": 64, # Increase batch size for GPU } } } ``` ### Memory Optimization ```python theme={null} # For limited memory environments config = { "reranker": { "provider": "huggingface", "config": { "model": "BAAI/bge-reranker-base", "device": "cpu", "batch_size": 8, # Smaller batch size "max_length": 256, # Shorter sequences "model_kwargs": { "torch_dtype": "float16" # Half precision } } } } ``` ## Model Comparison | Model | Size | Quality | Speed | Memory | Best For | | ---------------------------- | ---- | ------- | ------ | ------ | ------------------- | | bge-reranker-base | 278M | Good | Fast | Low | General use | | bge-reranker-large | 560M | Better | Medium | Medium | High quality needs | | bge-reranker-v2-m3 | 568M | Best | Medium | Medium | Latest improvements | | bge-reranker-v2-multilingual | 568M | Good | Medium | Medium | Multiple languages | ## Error Handling ```python theme={null} try: results = m.search( "test query", user_id="alice", rerank=True ) except Exception as e: print(f"Reranking failed: {e}") # Fall back to vector search only results = m.search( "test query", user_id="alice", rerank=False ) ``` ## Custom Models ### Using Private Models ```python theme={null} # Use a private model from Hugging Face config = { "reranker": { "provider": "huggingface", "config": { "model": "your-org/custom-reranker", "device": "cuda", "use_auth_token": "your-hf-token" } } } ``` ### Local Model Path ```python theme={null} # Use a locally downloaded model config = { "reranker": { "provider": "huggingface", "config": { "model": "/path/to/local/model", "device": "cuda" } } } ``` ## Best Practices 1. **Choose the Right Model**: Balance quality vs speed based on your needs 2. **Use GPU**: Significantly faster than CPU for larger models 3. **Optimize Batch Size**: Tune based on your hardware capabilities 4. **Monitor Memory**: Watch GPU/CPU memory usage with large models 5. **Cache Models**: Download once and reuse to avoid repeated downloads ## Troubleshooting ### Common Issues **Out of Memory Error** ```python theme={null} # Reduce batch size and sequence length config = { "reranker": { "provider": "huggingface", "config": { "model": "BAAI/bge-reranker-base", "batch_size": 4, "max_length": 256 } } } ``` **Model Download Issues** ```python theme={null} # Set cache directory import os os.environ["TRANSFORMERS_CACHE"] = "/path/to/cache" # Or use offline mode config = { "reranker": { "provider": "huggingface", "config": { "model": "BAAI/bge-reranker-base", "local_files_only": True } } } ``` **CUDA Not Available** ```python theme={null} import torch config = { "reranker": { "provider": "huggingface", "config": { "model": "BAAI/bge-reranker-base", "device": "cuda" if torch.cuda.is_available() else "cpu" } } } ``` ## Next Steps Learn about reranking concepts Detailed configuration options # LLM Reranker Source: https://docs.mem0.ai/components/rerankers/models/llm_reranker Use any language model as a reranker with custom prompts ## Overview The LLM reranker allows you to use any supported language model as a reranker. This approach uses prompts to instruct the LLM to score and rank memories based on their relevance to the query. While slower than specialized rerankers, it offers maximum flexibility and can be fine-tuned with custom prompts. ## Configuration ### Basic Setup ```python theme={null} from mem0 import Memory config = { "reranker": { "provider": "llm_reranker", "config": { "llm": { "provider": "openai", "config": { "model": "gpt-4", "api_key": "your-openai-api-key" } } } } } m = Memory.from_config(config) ``` ### Configuration Parameters | Parameter | Type | Default | Description | | --------------- | ----- | -------- | ------------------------------- | | `llm` | dict | Required | LLM configuration object | | `top_k` | int | 10 | Number of results to rerank | | `temperature` | float | 0.0 | LLM temperature for consistency | | `custom_prompt` | str | None | Custom reranking prompt | | `score_range` | tuple | (0, 10) | Score range for relevance | ### Advanced Configuration ```python theme={null} config = { "reranker": { "provider": "llm_reranker", "config": { "llm": { "provider": "anthropic", "config": { "model": "claude-3-sonnet-20240229", "api_key": "your-anthropic-api-key" } }, "top_k": 15, "temperature": 0.0, "score_range": (1, 5), "custom_prompt": """ Rate the relevance of each memory to the query on a scale of 1-5. Consider semantic similarity, context, and practical utility. Only provide the numeric score. """ } } } ``` ## Supported LLM Providers ### OpenAI ```python theme={null} config = { "reranker": { "provider": "llm_reranker", "config": { "llm": { "provider": "openai", "config": { "model": "gpt-4", "api_key": "your-openai-api-key", "temperature": 0.0 } } } } } ``` ### Anthropic ```python theme={null} config = { "reranker": { "provider": "llm_reranker", "config": { "llm": { "provider": "anthropic", "config": { "model": "claude-3-sonnet-20240229", "api_key": "your-anthropic-api-key" } } } } } ``` ### Ollama (Local) ```python theme={null} config = { "reranker": { "provider": "llm_reranker", "config": { "llm": { "provider": "ollama", "config": { "model": "llama2", "ollama_base_url": "http://localhost:11434" } } } } } ``` ### Azure OpenAI ```python theme={null} config = { "reranker": { "provider": "llm_reranker", "config": { "llm": { "provider": "azure_openai", "config": { "model": "gpt-4", "api_key": "your-azure-api-key", "azure_endpoint": "https://your-resource.openai.azure.com/", "azure_deployment": "gpt-4-deployment" } } } } } ``` ## Custom Prompts ### Default Prompt Behavior The default prompt asks the LLM to score relevance on a 0-10 scale: ``` Given a query and a memory, rate how relevant the memory is to answering the query. Score from 0 (completely irrelevant) to 10 (perfectly relevant). Only provide the numeric score. Query: {query} Memory: {memory} Score: ``` ### Custom Prompt Examples #### Domain-Specific Scoring ```python theme={null} custom_prompt = """ You are a medical information specialist. Rate how relevant each memory is for answering the medical query. Consider clinical accuracy, specificity, and practical applicability. Rate from 1-10 where: - 1-3: Irrelevant or potentially harmful - 4-6: Somewhat relevant but incomplete - 7-8: Relevant and helpful - 9-10: Highly relevant and clinically useful Query: {query} Memory: {memory} Score: """ config = { "reranker": { "provider": "llm_reranker", "config": { "llm": { "provider": "openai", "config": { "model": "gpt-4", "api_key": "your-api-key" } }, "custom_prompt": custom_prompt } } } ``` #### Contextual Relevance ```python theme={null} contextual_prompt = """ Rate how well this memory answers the specific question asked. Consider: - Direct relevance to the question - Completeness of information - Recency and accuracy - Practical usefulness Rate 1-5: 1 = Not relevant 2 = Slightly relevant 3 = Moderately relevant 4 = Very relevant 5 = Perfectly answers the question Query: {query} Memory: {memory} Score: """ ``` #### Conversational Context ```python theme={null} conversation_prompt = """ You are helping evaluate which memories are most useful for a conversational AI assistant. Rate how helpful this memory would be for generating a relevant response. Consider: - Direct relevance to user's intent - Emotional appropriateness - Factual accuracy - Conversation flow Rate 0-10: Query: {query} Memory: {memory} Score: """ ``` ## Usage Examples ### Basic Usage ```python theme={null} from mem0 import Memory m = Memory.from_config(config) # Add memories m.add("I'm allergic to peanuts", user_id="alice") m.add("I love Italian food", user_id="alice") m.add("I'm vegetarian", user_id="alice") # Search with LLM reranking results = m.search( "What foods should I avoid?", user_id="alice", rerank=True ) for result in results["results"]: print(f"Memory: {result['memory']}") print(f"LLM Score: {result['score']:.2f}") ``` ### Batch Processing with Error Handling ```python theme={null} def safe_llm_rerank_search(query, user_id, max_retries=3): for attempt in range(max_retries): try: return m.search(query, user_id=user_id, rerank=True) except Exception as e: print(f"Attempt {attempt + 1} failed: {e}") if attempt == max_retries - 1: # Fall back to vector search return m.search(query, user_id=user_id, rerank=False) # Use the safe function results = safe_llm_rerank_search("What are my preferences?", "alice") ``` ## Performance Considerations ### Speed vs Quality Trade-offs | Model Type | Speed | Quality | Cost | Best For | | --------------- | -------- | --------- | ------ | ------------------------------ | | GPT-3.5 Turbo | Fast | Good | Low | High-volume applications | | GPT-4 | Medium | Excellent | Medium | Quality-critical applications | | Claude 3 Sonnet | Medium | Excellent | Medium | Balanced performance | | Ollama Local | Variable | Good | Free | Privacy-sensitive applications | ### Optimization Strategies ```python theme={null} # Fast configuration for high-volume use fast_config = { "reranker": { "provider": "llm_reranker", "config": { "llm": { "provider": "openai", "config": { "model": "gpt-3.5-turbo", "api_key": "your-api-key" } }, "top_k": 5, # Limit candidates "temperature": 0.0 } } } # High-quality configuration quality_config = { "reranker": { "provider": "llm_reranker", "config": { "llm": { "provider": "openai", "config": { "model": "gpt-4", "api_key": "your-api-key" } }, "top_k": 15, "temperature": 0.0 } } } ``` ## Advanced Use Cases ### Multi-Step Reasoning ```python theme={null} reasoning_prompt = """ Evaluate this memory's relevance using multi-step reasoning: 1. What is the main intent of the query? 2. What key information does the memory contain? 3. How directly does the memory address the query? 4. What additional context might be needed? Based on this analysis, rate relevance 1-10: Query: {query} Memory: {memory} Analysis: Step 1 (Intent): Step 2 (Information): Step 3 (Directness): Step 4 (Context): Final Score: """ ``` ### Comparative Ranking ```python theme={null} comparative_prompt = """ You will see a query and multiple memories. Rank them in order of relevance. Consider which memories best answer the question and would be most helpful. Query: {query} Memories to rank: {memories} Provide scores 1-10 for each memory, considering their relative usefulness. """ ``` ### Emotional Intelligence ```python theme={null} emotional_prompt = """ Consider both factual relevance and emotional appropriateness. Rate how suitable this memory is for responding to the user's query. Factors to consider: - Factual accuracy and relevance - Emotional tone and sensitivity - User's likely emotional state - Appropriateness of response Query: {query} Memory: {memory} Emotional Context: {context} Score (1-10): """ ``` ## Error Handling and Fallbacks ```python theme={null} class RobustLLMReranker: def __init__(self, primary_config, fallback_config=None): self.primary = Memory.from_config(primary_config) self.fallback = Memory.from_config(fallback_config) if fallback_config else None def search(self, query, user_id, max_retries=2): # Try primary LLM reranker for attempt in range(max_retries): try: return self.primary.search(query, user_id=user_id, rerank=True) except Exception as e: print(f"Primary reranker attempt {attempt + 1} failed: {e}") # Try fallback reranker if self.fallback: try: return self.fallback.search(query, user_id=user_id, rerank=True) except Exception as e: print(f"Fallback reranker failed: {e}") # Final fallback: vector search only return self.primary.search(query, user_id=user_id, rerank=False) # Usage primary_config = { "reranker": { "provider": "llm_reranker", "config": {"llm": {"provider": "openai", "config": {"model": "gpt-4"}}} } } fallback_config = { "reranker": { "provider": "llm_reranker", "config": {"llm": {"provider": "openai", "config": {"model": "gpt-3.5-turbo"}}} } } reranker = RobustLLMReranker(primary_config, fallback_config) results = reranker.search("What are my preferences?", "alice") ``` ## Best Practices 1. **Use Specific Prompts**: Tailor prompts to your domain and use case 2. **Set Temperature to 0**: Ensure consistent scoring across runs 3. **Limit Top-K**: Don't rerank too many candidates to control costs 4. **Implement Fallbacks**: Always have a backup plan for API failures 5. **Monitor Costs**: Track API usage, especially with expensive models 6. **Cache Results**: Consider caching reranking results for repeated queries 7. **Test Prompts**: Experiment with different prompts to find what works best ## Troubleshooting ### Common Issues **Inconsistent Scores** * Set temperature to 0.0 * Use more specific prompts * Consider using multiple calls and averaging **API Rate Limits** * Implement exponential backoff * Use cheaper models for high-volume scenarios * Add retry logic with delays **Poor Ranking Quality** * Refine your custom prompt * Try different LLM models * Add examples to your prompt ## Next Steps Learn to craft effective reranking prompts Optimize LLM reranker performance # Sentence Transformer Source: https://docs.mem0.ai/components/rerankers/models/sentence_transformer Local reranking with HuggingFace cross-encoder models Sentence Transformer reranker provides local reranking using HuggingFace cross-encoder models, perfect for privacy-focused deployments where you want to keep data on-premises. ## Models Any HuggingFace cross-encoder model can be used. Popular choices include: * **`cross-encoder/ms-marco-MiniLM-L-6-v2`**: Default, good balance of speed and accuracy * **`cross-encoder/ms-marco-TinyBERT-L-2-v2`**: Fastest, smaller model size * **`cross-encoder/ms-marco-electra-base`**: Higher accuracy, larger model * **`cross-encoder/stsb-distilroberta-base`**: Good for semantic similarity tasks ## Installation ```bash theme={null} pip install sentence-transformers ``` ## Configuration ```python Python theme={null} from mem0 import Memory config = { "vector_store": { "provider": "chroma", "config": { "collection_name": "my_memories", "path": "./chroma_db" } }, "llm": { "provider": "openai", "config": { "model": "gpt-4o-mini" } }, "rerank": { "provider": "sentence_transformer", "config": { "model": "cross-encoder/ms-marco-MiniLM-L-6-v2", "device": "cpu", # or "cuda" for GPU "batch_size": 32, "show_progress_bar": False, "top_k": 5 } } } memory = Memory.from_config(config) ``` ## GPU Acceleration For better performance, use GPU acceleration: ```python Python theme={null} config = { "rerank": { "provider": "sentence_transformer", "config": { "model": "cross-encoder/ms-marco-MiniLM-L-6-v2", "device": "cuda", # Use GPU "batch_size": 64 # high batch size for high memory GPUs } } } ``` ## Usage Example ```python Python theme={null} from mem0 import Memory # Initialize memory with local reranker config = { "vector_store": {"provider": "chroma"}, "llm": {"provider": "openai", "config": {"model": "gpt-4o-mini"}}, "rerank": { "provider": "sentence_transformer", "config": { "model": "cross-encoder/ms-marco-MiniLM-L-6-v2", "device": "cpu" } } } memory = Memory.from_config(config) # Add memories messages = [ {"role": "user", "content": "I love reading science fiction novels"}, {"role": "user", "content": "My favorite author is Isaac Asimov"}, {"role": "user", "content": "I also enjoy watching sci-fi movies"} ] memory.add(messages, user_id="charlie") # Search with local reranking results = memory.search("What books does the user like?", user_id="charlie") for result in results['results']: print(f"Memory: {result['memory']}") print(f"Vector Score: {result['score']:.3f}") print(f"Rerank Score: {result['rerank_score']:.3f}") print() ``` ## Custom Models You can use any HuggingFace cross-encoder model: ```python Python theme={null} # Using a different model config = { "rerank": { "provider": "sentence_transformer", "config": { "model": "cross-encoder/stsb-distilroberta-base", "device": "cpu" } } } ``` ## Configuration Parameters | Parameter | Description | Type | Default | | ------------------- | -------------------------------------------- | ------ | ---------------------------------------- | | `model` | HuggingFace cross-encoder model name | `str` | `"cross-encoder/ms-marco-MiniLM-L-6-v2"` | | `device` | Device to run model on (`cpu`, `cuda`, etc.) | `str` | `None` | | `batch_size` | Batch size for processing documents | `int` | `32` | | `show_progress_bar` | Show progress bar during processing | `bool` | `False` | | `top_k` | Maximum documents to return | `int` | `None` | ## Advantages * **Privacy**: Complete local processing, no external API calls * **Cost**: No per-token charges after initial model download * **Customization**: Use any HuggingFace cross-encoder model * **Offline**: Works without internet connection after model download ## Performance Considerations * **First Run**: Model download may take time initially * **Memory Usage**: Models require GPU/CPU memory * **Batch Size**: Optimize batch size based on available memory * **Device**: GPU acceleration significantly improves speed ## Best Practices 1. **Model Selection**: Choose model based on accuracy vs speed requirements 2. **Device Management**: Use GPU when available for better performance 3. **Batch Processing**: Process multiple documents together for efficiency 4. **Memory Monitoring**: Monitor system memory usage with larger models # Zero Entropy Source: https://docs.mem0.ai/components/rerankers/models/zero_entropy Neural reranking with Zero Entropy [Zero Entropy](https://www.zeroentropy.dev) provides neural reranking models that significantly improve search relevance with fast performance. ## Models Zero Entropy offers two reranking models: * **`zerank-1`**: Flagship state-of-the-art reranker (non-commercial license) * **`zerank-1-small`**: Open-source model (Apache 2.0 license) ## Installation ```bash theme={null} pip install zeroentropy ``` ## Configuration ```python Python theme={null} from mem0 import Memory config = { "vector_store": { "provider": "chroma", "config": { "collection_name": "my_memories", "path": "./chroma_db" } }, "llm": { "provider": "openai", "config": { "model": "gpt-4o-mini" } }, "rerank": { "provider": "zero_entropy", "config": { "model": "zerank-1", # or "zerank-1-small" "api_key": "your-zero-entropy-api-key", # or set ZERO_ENTROPY_API_KEY "top_k": 5 } } } memory = Memory.from_config(config) ``` ## Environment Variables Set your API key as an environment variable: ```bash theme={null} export ZERO_ENTROPY_API_KEY="your-api-key" ``` ## Usage Example ```python Python theme={null} import os from mem0 import Memory # Set API key os.environ["ZERO_ENTROPY_API_KEY"] = "your-api-key" # Initialize memory with Zero Entropy reranker config = { "vector_store": {"provider": "chroma"}, "llm": {"provider": "openai", "config": {"model": "gpt-4o-mini"}}, "rerank": {"provider": "zero_entropy", "config": {"model": "zerank-1"}} } memory = Memory.from_config(config) # Add memories messages = [ {"role": "user", "content": "I love Italian pasta, especially carbonara"}, {"role": "user", "content": "Japanese sushi is also amazing"}, {"role": "user", "content": "I enjoy cooking Mediterranean dishes"} ] memory.add(messages, user_id="alice") # Search with reranking results = memory.search("What Italian food does the user like?", user_id="alice") for result in results['results']: print(f"Memory: {result['memory']}") print(f"Vector Score: {result['score']:.3f}") print(f"Rerank Score: {result['rerank_score']:.3f}") print() ``` ## Configuration Parameters | Parameter | Description | Type | Default | | --------- | ------------------------------------------------ | ----- | ------------ | | `model` | Model to use: `"zerank-1"` or `"zerank-1-small"` | `str` | `"zerank-1"` | | `api_key` | Zero Entropy API key | `str` | `None` | | `top_k` | Maximum documents to return after reranking | `int` | `None` | ## Performance * **Fast**: Optimized neural architecture for low latency * **Accurate**: State-of-the-art relevance scoring * **Cost-effective**: \~\$0.025/1M tokens processed ## Best Practices 1. **Model Selection**: Use `zerank-1` for best quality, `zerank-1-small` for faster processing 2. **Batch Size**: Process multiple queries together when possible 3. **Top-k Limiting**: Set reasonable `top_k` values (5-20) for best performance 4. **API Key Management**: Use environment variables for secure key storage # Performance Optimization Source: https://docs.mem0.ai/components/rerankers/optimization Optimizing reranker performance is crucial for maintaining fast search response times while improving result quality. This guide covers best practices for different reranker types. ## General Optimization Principles ### Candidate Set Size The number of candidates sent to the reranker significantly impacts performance: ```python theme={null} # Optimal candidate sizes for different rerankers config_map = { "cohere": {"initial_candidates": 100, "top_n": 10}, "sentence_transformer": {"initial_candidates": 50, "top_n": 10}, "huggingface": {"initial_candidates": 30, "top_n": 5}, "llm_reranker": {"initial_candidates": 20, "top_n": 5} } ``` ### Batching Strategy Process multiple queries efficiently: ```python theme={null} # Configure for batch processing config = { "reranker": { "provider": "sentence_transformer", "config": { "model": "cross-encoder/ms-marco-MiniLM-L-6-v2", "batch_size": 16, # Process multiple candidates at once "top_n": 10 } } } ``` ## Provider-Specific Optimizations ### Cohere Optimization ```python theme={null} # Optimized Cohere configuration config = { "reranker": { "provider": "cohere", "config": { "model": "rerank-english-v3.0", "top_n": 10, "max_chunks_per_doc": 10, # Limit chunk processing "return_documents": False # Reduce response size } } } ``` **Best Practices:** * Use v3.0 models for better speed/accuracy balance * Limit candidates to 100 or fewer * Cache API responses when possible * Monitor API rate limits ### Sentence Transformer Optimization ```python theme={null} # Performance-optimized configuration config = { "reranker": { "provider": "sentence_transformer", "config": { "model": "cross-encoder/ms-marco-MiniLM-L-6-v2", "device": "cuda", # Use GPU when available "batch_size": 32, "top_n": 10, "max_length": 512 # Limit input length } } } ``` **Device Optimization:** ```python theme={null} import torch # Auto-detect best device device = "cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu" config = { "reranker": { "provider": "sentence_transformer", "config": { "device": device, "model": "cross-encoder/ms-marco-MiniLM-L-6-v2" } } } ``` ### Hugging Face Optimization ```python theme={null} # Optimized for Hugging Face models config = { "reranker": { "provider": "huggingface", "config": { "model": "BAAI/bge-reranker-base", "use_fp16": True, # Half precision for speed "max_length": 512, "batch_size": 8, "top_n": 10 } } } ``` ### LLM Reranker Optimization ```python theme={null} # Optimized LLM reranker configuration config = { "reranker": { "provider": "llm_reranker", "config": { "llm": { "provider": "openai", "config": { "model": "gpt-3.5-turbo", # Faster than gpt-4 "temperature": 0, # Deterministic results "max_tokens": 500 # Limit response length } }, "batch_ranking": True, # Rank multiple at once "top_n": 5, # Fewer results for faster processing "timeout": 10 # Request timeout } } } ``` ## Performance Monitoring ### Latency Tracking ```python theme={null} import time from mem0 import Memory def measure_reranker_performance(config, queries, user_id): memory = Memory.from_config(config) latencies = [] for query in queries: start_time = time.time() results = memory.search(query, user_id=user_id) latency = time.time() - start_time latencies.append(latency) return { "avg_latency": sum(latencies) / len(latencies), "max_latency": max(latencies), "min_latency": min(latencies) } ``` ### Memory Usage Monitoring ```python theme={null} import psutil import os def monitor_memory_usage(): process = psutil.Process(os.getpid()) return { "memory_mb": process.memory_info().rss / 1024 / 1024, "memory_percent": process.memory_percent() } ``` ## Caching Strategies ### Result Caching ```python theme={null} from functools import lru_cache import hashlib class CachedReranker: def __init__(self, config): self.memory = Memory.from_config(config) self.cache_size = 1000 @lru_cache(maxsize=1000) def search_cached(self, query_hash, user_id): return self.memory.search(query, user_id=user_id) def search(self, query, user_id): query_hash = hashlib.md5(f"{query}_{user_id}".encode()).hexdigest() return self.search_cached(query_hash, user_id) ``` ### Model Caching ```python theme={null} # Pre-load models to avoid initialization overhead config = { "reranker": { "provider": "sentence_transformer", "config": { "model": "cross-encoder/ms-marco-MiniLM-L-6-v2", "cache_folder": "/path/to/model/cache", "device": "cuda" } } } ``` ## Parallel Processing ### Async Configuration ```python theme={null} import asyncio from mem0 import Memory async def parallel_search(config, queries, user_id): memory = Memory.from_config(config) # Process multiple queries concurrently tasks = [ memory.search_async(query, user_id=user_id) for query in queries ] results = await asyncio.gather(*tasks) return results ``` ## Hardware Optimization ### GPU Configuration ```python theme={null} # Optimize for GPU usage import torch if torch.cuda.is_available(): torch.cuda.set_per_process_memory_fraction(0.8) # Reserve GPU memory config = { "reranker": { "provider": "sentence_transformer", "config": { "device": "cuda", "model": "cross-encoder/ms-marco-electra-base", "batch_size": 64, # Larger batch for GPU "fp16": True # Half precision } } } ``` ### CPU Optimization ```python theme={null} import torch # Optimize CPU threading torch.set_num_threads(4) # Adjust based on your CPU config = { "reranker": { "provider": "sentence_transformer", "config": { "device": "cpu", "model": "cross-encoder/ms-marco-MiniLM-L-6-v2", "num_workers": 4 # Parallel processing } } } ``` ## Benchmarking Different Configurations ```python theme={null} def benchmark_rerankers(): configs = [ {"provider": "cohere", "model": "rerank-english-v3.0"}, {"provider": "sentence_transformer", "model": "cross-encoder/ms-marco-MiniLM-L-6-v2"}, {"provider": "huggingface", "model": "BAAI/bge-reranker-base"} ] test_queries = ["sample query 1", "sample query 2", "sample query 3"] results = {} for config in configs: provider = config["provider"] performance = measure_reranker_performance( {"reranker": {"provider": provider, "config": config}}, test_queries, "test_user" ) results[provider] = performance return results ``` ## Production Best Practices 1. **Model Selection**: Choose the right balance of speed vs. accuracy 2. **Resource Allocation**: Monitor CPU/GPU usage and memory consumption 3. **Error Handling**: Implement fallbacks for reranker failures 4. **Load Balancing**: Distribute reranking load across multiple instances 5. **Monitoring**: Track latency, throughput, and error rates 6. **Caching**: Cache frequent queries and model predictions 7. **Batch Processing**: Group similar queries for efficient processing # Overview Source: https://docs.mem0.ai/components/rerankers/overview Pick the right reranker path to boost Mem0 search relevance. Mem0 rerankers rescore vector search hits so your agents surface the most relevant memories. Use this hub to decide when reranking helps, configure a provider, and fine-tune performance. Reranking trades extra latency for better precision. Start once you have baseline search working and measure before/after relevance. ## Picking the Right Reranker * **API-first** when you need top quality and can absorb request costs (Cohere, Zero Entropy). * **Self-hosted** for privacy-sensitive deployments that must stay on your hardware (Sentence Transformer, Hugging Face). * **LLM-driven** when you need bespoke scoring logic or complex prompts. * **Hybrid** by enabling reranking only on premium journeys to control spend. ## Implementation Checklist 1. Confirm baseline search KPIs so you can measure uplift. 2. Select a provider and add the `reranker` block to your config. 3. Test latency impact with production-like query batches. 4. Decide whether to enable reranking globally or per-search via the `rerank` flag. # Development Source: https://docs.mem0.ai/contributing/development # Development Contributions We strive to make contributions **easy, collaborative, and enjoyable**. Follow the steps below to ensure a smooth contribution process. ## Submitting Your Contribution through PR To contribute, follow these steps: 1. **Fork & Clone** the repository: [Mem0 on GitHub](https://github.com/mem0ai/mem0) 2. **Create a Feature Branch**: Use a dedicated branch for your changes, e.g., `feature/my-new-feature` 3. **Implement Changes**: If adding a feature or fixing a bug, ensure to: * Write necessary **tests** * Add **documentation, docstrings, and runnable examples** 4. **Code Quality Checks**: * Run **linting** to catch style issues * Ensure **all tests pass** 5. **Submit a Pull Request** For detailed guidance on pull requests, refer to [GitHub's documentation](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request). *** ## Dependency Management We use `hatch` as our package manager. Install it by following the [official instructions](https://hatch.pypa.io/latest/install/). **Do NOT use `pip` or `conda` for dependency management.** Instead, follow these steps in order: ```bash theme={null} # 1. Install base dependencies make install # 2. Activate virtual environment (this will install dependencies) hatch shell # For default environment hatch -e dev_py_3_11 shell # For dev_py_3_11 (differences are mentioned in pyproject.toml) # 3. Install all optional dependencies make install_all ``` *** ## Development Standards ### Pre-commit Hooks Ensure `pre-commit` is installed before contributing: ```bash theme={null} pre-commit install ``` ### Linting with `ruff` Run the linter and fix any reported issues before submitting your PR: ```bash theme={null} make lint ``` ### Code Formatting To maintain a consistent code style, format your code: ```bash theme={null} make format ``` ### Testing with `pytest` Run tests to verify functionality before submitting your PR: ```bash theme={null} make test ``` **Note:** Some dependencies have been removed from the main dependencies to reduce package size. Run `make install_all` to install necessary dependencies before running tests. *** ## Release Process Currently, releases are handled manually. We aim for frequent releases, typically when new features or bug fixes are introduced. *** Thank you for contributing to Mem0! # Documentation Source: https://docs.mem0.ai/contributing/documentation # Documentation Contributions ## Prerequisites Before getting started, ensure you have **Node.js (version 23.6.0 or higher)** installed on your system. *** ## Setting Up Mintlify ### Step 1: Install Mintlify Install Mintlify globally using your preferred package manager: ```bash npm theme={null} npm i -g mintlify ``` ```bash yarn theme={null} yarn global add mintlify ``` ### Step 2: Run the Documentation Server Navigate to the `docs/` directory (where `docs.json` is located) and start the development server: ```bash theme={null} mintlify dev ``` The documentation website will be available at: [http://localhost:3000](http://localhost:3000). *** ## Custom Ports By default, Mintlify runs on **port 3000**. To use a different port, add the `--port` flag: ```bash theme={null} mintlify dev --port 3333 ``` *** By following these steps, you can efficiently contribute to Mem0's documentation. # Personalized AI Tutor Source: https://docs.mem0.ai/cookbooks/companions/ai-tutor Keep student progress and preferences persistent across tutoring sessions. You can create a personalized AI Tutor using Mem0. This guide will walk you through the necessary steps and provide the complete code to get you started. ## Overview The Personalized AI Tutor leverages Mem0 to retain information across interactions, enabling a tailored learning experience. By integrating with OpenAI's GPT-4 model, the tutor can provide detailed and context-aware responses to user queries. ## Setup Before you begin, ensure you have the required dependencies installed. You can install the necessary packages using pip: ```bash theme={null} pip install openai mem0ai ``` ## Full Code Example Below is the complete code to create and interact with a Personalized AI Tutor using Mem0: ```python theme={null} import os from openai import OpenAI from mem0 import Memory # Set the OpenAI API key os.environ['OPENAI_API_KEY'] = 'sk-xxx' # Initialize the OpenAI client client = OpenAI() class PersonalAITutor: def __init__(self): """ Initialize the PersonalAITutor with memory configuration and OpenAI client. """ config = { "vector_store": { "provider": "qdrant", "config": { "host": "localhost", "port": 6333, } }, } self.memory = Memory.from_config(config) self.client = client self.app_id = "app-1" def ask(self, question, user_id=None): """ Ask a question to the AI and store the relevant facts in memory :param question: The question to ask the AI. :param user_id: Optional user ID to associate with the memory. """ # Start a streaming response request to the AI response = self.client.responses.create( model="gpt-4.1-nano-2025-04-14", instructions="You are a personal AI Tutor.", input=question, stream=True ) # Store the question in memory self.memory.add(question, user_id=user_id, metadata={"app_id": self.app_id}) # Print the response from the AI in real-time for event in response: if event.type == "response.output_text.delta": print(event.delta, end="") def get_memories(self, user_id=None): """ Retrieve all memories associated with the given user ID. :param user_id: Optional user ID to filter memories. :return: List of memories. """ return self.memory.get_all(user_id=user_id) # Instantiate the PersonalAITutor ai_tutor = PersonalAITutor() # Define a user ID user_id = "john_doe" # Ask a question ai_tutor.ask("I am learning introduction to CS. What is queue? Briefly explain.", user_id=user_id) ``` ### Fetching Memories You can fetch all the memories at any point in time using the following code: ```python theme={null} memories = ai_tutor.get_memories(user_id=user_id) for m in memories['results']: print(m['memory']) ``` ## Key Points * **Initialization**: The PersonalAITutor class is initialized with the necessary memory configuration and OpenAI client setup * **Asking Questions**: The ask method sends a question to the AI and stores the relevant information in memory * **Retrieving Memories**: The get\_memories method fetches all stored memories associated with a user ## Conclusion As the conversation progresses, Mem0's memory automatically updates based on the interactions, providing a continuously improving personalized learning experience. This setup ensures that the AI Tutor can offer contextually relevant and accurate responses, enhancing the overall educational process. *** Learn the foundations of memory-powered companions with production-ready patterns. Build a travel companion that remembers preferences and past conversations. # Self-Hosted AI Companion Source: https://docs.mem0.ai/cookbooks/companions/local-companion-ollama Run Mem0 end-to-end on your machine using Ollama-powered LLMs and embedders. Mem0 can be utilized entirely locally by leveraging Ollama for both the embedding model and the language model (LLM). This guide will walk you through the necessary steps and provide the complete code to get you started. ## Overview By using Ollama, you can run Mem0 locally, which allows for greater control over your data and models. This setup uses Ollama for both the embedding model and the language model, providing a fully local solution. ## Setup Before you begin, ensure you have Mem0 and Ollama installed and properly configured on your local machine. ## Full Code Example Below is the complete code to set up and use Mem0 locally with Ollama: ```python theme={null} from mem0 import Memory config = { "vector_store": { "provider": "qdrant", "config": { "collection_name": "test", "host": "localhost", "port": 6333, "embedding_model_dims": 768, # Change this according to your local model's dimensions }, }, "llm": { "provider": "ollama", "config": { "model": "llama3.1:latest", "temperature": 0, "max_tokens": 2000, "ollama_base_url": "http://localhost:11434", # Ensure this URL is correct }, }, "embedder": { "provider": "ollama", "config": { "model": "nomic-embed-text:latest", # Alternatively, you can use "snowflake-arctic-embed:latest" "ollama_base_url": "http://localhost:11434", }, }, } # Initialize Memory with the configuration m = Memory.from_config(config) # Add a memory m.add("I'm visiting Paris", user_id="john") # Retrieve memories memories = m.get_all(user_id="john") ``` ## Key Points * **Configuration**: The setup involves configuring the vector store, language model, and embedding model to use local resources * **Vector Store**: Qdrant is used as the vector store, running on localhost * **Language Model**: Ollama is used as the LLM provider, with the `llama3.1:latest` model * **Embedding Model**: Ollama is also used for embeddings, with the `nomic-embed-text:latest` model ## Conclusion This local setup of Mem0 using Ollama provides a fully self-contained solution for memory management and AI interactions. It allows for greater control over your data and models while still leveraging the powerful capabilities of Mem0. *** Explore advanced configuration options for vector stores, LLMs, and embedders. Learn core companion patterns that work with any LLM provider. # Build a Node.js Companion Source: https://docs.mem0.ai/cookbooks/companions/nodejs-companion Build a JavaScript fitness coach that remembers user goals run after run. You can create a personalized AI Companion using Mem0. This guide will walk you through the necessary steps and provide the complete code to get you started. ## Overview The Personalized AI Companion leverages Mem0 to retain information across interactions, enabling a tailored learning experience. It creates memories for each user interaction and integrates with OpenAI's GPT models to provide detailed and context-aware responses to user queries. ## Setup Before you begin, ensure you have Node.js installed and create a new project. Install the required dependencies using npm: ```bash theme={null} npm install openai mem0ai ``` ## Full Code Example Below is the complete code to create and interact with an AI Companion using Mem0: ```javascript theme={null} import { OpenAI } from 'openai'; import { Memory } from 'mem0ai/oss'; import * as readline from 'readline'; const openaiClient = new OpenAI(); const memory = new Memory(); async function chatWithMemories(message, userId = "default_user") { const relevantMemories = await memory.search(message, { userId: userId }); const memoriesStr = relevantMemories.results .map(entry => `- ${entry.memory}`) .join('\n'); const systemPrompt = `You are a helpful AI. Answer the question based on query and memories. User Memories: ${memoriesStr}`; const messages = [ { role: "system", content: systemPrompt }, { role: "user", content: message } ]; const response = await openaiClient.chat.completions.create({ model: "gpt-4.1-nano-2025-04-14", messages: messages }); const assistantResponse = response.choices[0].message.content || ""; messages.push({ role: "assistant", content: assistantResponse }); await memory.add(messages, { userId: userId }); return assistantResponse; } async function main() { const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); console.log("Chat with AI (type 'exit' to quit)"); const askQuestion = () => { return new Promise((resolve) => { rl.question("You: ", (input) => { resolve(input.trim()); }); }); }; try { while (true) { const userInput = await askQuestion(); if (userInput.toLowerCase() === 'exit') { console.log("Goodbye!"); rl.close(); break; } const response = await chatWithMemories(userInput, "sample_user"); console.log(`AI: ${response}`); } } catch (error) { console.error("An error occurred:", error); rl.close(); } } main().catch(console.error); ``` ### Key Components 1. **Initialization** * The code initializes both OpenAI and Mem0 Memory clients * Uses Node.js's built-in readline module for command-line interaction 2. **Memory Management (chatWithMemories function)** * Retrieves relevant memories using Mem0's search functionality * Constructs a system prompt that includes past memories * Makes API calls to OpenAI for generating responses * Stores new interactions in memory 3. **Interactive Chat Interface (main function)** * Creates a command-line interface for user interaction * Handles user input and displays AI responses * Includes graceful exit functionality ### Environment Setup Make sure to set up your environment variables: ```bash theme={null} export OPENAI_API_KEY=your_api_key ``` ### Conclusion This implementation demonstrates how to create an AI Companion that maintains context across conversations using Mem0's memory capabilities. The system automatically stores and retrieves relevant information, creating a more personalized and context-aware interaction experience. As users interact with the system, Mem0's memory system continuously learns and adapts, making future responses more relevant and personalized. This setup is ideal for creating long-term learning AI assistants that can maintain context and provide increasingly personalized responses over time. *** Separate user, agent, and session context to keep your companion consistent. Run the full showcase app to see memory-powered companions in action. # Interactive Memory Demo Source: https://docs.mem0.ai/cookbooks/companions/quickstart-demo Spin up the showcase companion app to see Mem0 memories in action. You can create a personalized AI Companion using Mem0. This guide will walk you through the necessary steps and provide the complete setup instructions to get you started.