Azure AI Search - Mem0

Azure AI Search

Azure AI 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

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": "<your-azure-ai-search-service-name>",
            "api_key": "<your-api-key>",
            "collection_name": "mem0",
            "embedding_model_dims": 1536
        }
    }
}

m = Memory.from_config(config)
m.add(messages, user_id="alice", metadata={"category": "movies"})

Using binary compression for large vector collections

config = {
    "vector_store": {
        "provider": "azure_ai_search",
        "config": {
            "service_name": "<your-azure-ai-search-service-name>",
            "api_key": "<your-api-key>",
            "collection_name": "mem0",
            "embedding_model_dims": 1536,
            "compression_type": "binary",
            "use_float16": True  # Use half precision for storage efficiency
        }
    }
}

Using hybrid search

config = {
    "vector_store": {
        "provider": "azure_ai_search",
        "config": {
            "service_name": "<your-azure-ai-search-service-name>",
            "api_key": "<your-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.”
  4. Go to Access Control (IAM): In the Azure Portal, select your Search service.
  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: Assign to a User, Group, Service Principal, or Managed Identity.
  8. Complete the Assignment: Click Review + Assign.

If you are using Azure Identity, do not set the api_key in the configuration.

config = {
    "vector_store": {
        "provider": "azure_ai_search",
        "config": {
            "service_name": "<your-azure-ai-search-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

Developer Logins for Azure Identity Credential

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 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