{"id":99,"date":"2025-04-01T08:30:00","date_gmt":"2025-04-01T08:30:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/foundry\/?p=99"},"modified":"2025-04-11T08:59:35","modified_gmt":"2025-04-11T15:59:35","slug":"azure-ai-mem0-integration","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/foundry\/azure-ai-mem0-integration\/","title":{"rendered":"Building AI Applications with Memory: Mem0 and Azure AI Integration"},"content":{"rendered":"<h1>Building AI Applications with Memory: Mem0 and Azure AI Integration<\/h1>\n<h2>TL;DR<\/h2>\n<p>Learn how to integrate Mem0 with Azure AI Search and Azure OpenAI to create AI applications with persistent memory. This tutorial provides code examples for setting up a memory layer using Azure services and demonstrates how to build a travel planning assistant that remembers user preferences across conversations.<\/p>\n<h2>Introduction<\/h2>\n<p>One of the key limitations of most AI systems is their inability to maintain context beyond a single session. This lack of memory significantly impacts the quality of user interactions, often requiring users to repeat information they&#8217;ve already provided. Enter Mem0, a powerful memory layer designed specifically for AI applications.<\/p>\n<p>In this guide, we&#8217;ll explore how to integrate Mem0 with Azure AI services to create AI applications with persistent memory. We&#8217;ll cover:<\/p>\n<ol>\n<li>Setting up Mem0 with Azure AI Search and Azure OpenAI<\/li>\n<li>Basic memory operations (storing, retrieving, and searching memories)<\/li>\n<li>Building a practical travel planning assistant that remembers user preferences<\/li>\n<\/ol>\n<h2>Prerequisites<\/h2>\n<ul>\n<li>Azure OpenAI account with access to model deployments<\/li>\n<li>Azure AI Search service<\/li>\n<li>Python environment with required packages<\/li>\n<\/ul>\n<p>First, let&#8217;s install the necessary packages:<\/p>\n<pre><code class=\"language-bash\">pip install mem0ai python-dotenv<\/code><\/pre>\n<h2>Setting Up Your Azure Environment<\/h2>\n<p>To get started, you&#8217;ll need to configure your Azure environment variables:<\/p>\n<pre><code class=\"language-python\">import os\r\nfrom mem0 import Memory\r\nfrom openai import AzureOpenAI\r\n\r\n# Load Azure OpenAI configuration\r\nAZURE_OPENAI_ENDPOINT = os.getenv(\"AZURE_OPENAI_ENDPOINT\")\r\nAZURE_OPENAI_API_KEY = os.getenv(\"AZURE_OPENAI_API_KEY\")\r\nAZURE_OPENAI_CHAT_COMPLETION_DEPLOYED_MODEL_NAME = os.getenv(\"AZURE_OPENAI_CHAT_COMPLETION_DEPLOYED_MODEL_NAME\")\r\nAZURE_OPENAI_EMBEDDING_DEPLOYED_MODEL_NAME = os.getenv(\"AZURE_OPENAI_EMBEDDING_DEPLOYED_MODEL_NAME\")\r\n\r\n# Load Azure AI Search configuration\r\nSEARCH_SERVICE_ENDPOINT = os.getenv(\"AZURE_SEARCH_SERVICE_ENDPOINT\")\r\nSEARCH_SERVICE_API_KEY = os.getenv(\"AZURE_SEARCH_ADMIN_KEY\")\r\nSEARCH_SERVICE_NAME = \"your-search-service-name\"\r\n\r\n# Create Azure OpenAI client\r\nazure_openai_client = AzureOpenAI(\r\n    azure_endpoint=AZURE_OPENAI_ENDPOINT,\r\n    api_key=AZURE_OPENAI_API_KEY,\r\n    api_version=\"2024-10-21\"\r\n)<\/code><\/pre>\n<h2>Basic Memory Operations with Mem0 and Azure AI Search<\/h2>\n<p>Let&#8217;s start with a simple example demonstrating how to store and retrieve memories:<\/p>\n<pre><code class=\"language-python\"># Configure Mem0 with Azure AI Search\r\nmemory_config = {\r\n    \"vector_store\": {\r\n        \"provider\": \"azure_ai_search\",\r\n        \"config\": {\r\n            \"service_name\": SEARCH_SERVICE_NAME,\r\n            \"api_key\": SEARCH_SERVICE_API_KEY,\r\n            \"collection_name\": \"memories\",\r\n            \"embedding_model_dims\": 1536,\r\n        },\r\n    },\r\n    \"embedder\": {\r\n        \"provider\": \"azure_openai\",\r\n        \"config\": {\r\n            \"model\": AZURE_OPENAI_EMBEDDING_DEPLOYED_MODEL_NAME,\r\n            \"embedding_dims\": 1536,\r\n            \"azure_kwargs\": {\r\n                \"api_version\": \"2024-10-21\",\r\n                \"azure_deployment\": AZURE_OPENAI_EMBEDDING_DEPLOYED_MODEL_NAME,\r\n                \"azure_endpoint\": AZURE_OPENAI_ENDPOINT,\r\n                \"api_key\": AZURE_OPENAI_API_KEY,\r\n            },\r\n        },\r\n    },\r\n    \"llm\": {\r\n        \"provider\": \"azure_openai\",\r\n        \"config\": {\r\n            \"model\": AZURE_OPENAI_CHAT_COMPLETION_DEPLOYED_MODEL_NAME,\r\n            \"temperature\": 0.1,\r\n            \"max_tokens\": 2000,\r\n            \"azure_kwargs\": {\r\n                \"azure_deployment\": AZURE_OPENAI_CHAT_COMPLETION_DEPLOYED_MODEL_NAME,\r\n                \"api_version\": \"2024-10-21\",\r\n                \"azure_endpoint\": AZURE_OPENAI_ENDPOINT,\r\n                \"api_key\": AZURE_OPENAI_API_KEY,\r\n            },\r\n        },\r\n    },\r\n    \"version\": \"v1.1\",\r\n}\r\n\r\n# Initialize memory\r\nmemory = Memory.from_config(memory_config)\r\nprint(\"Mem0 initialized with Azure AI Search\")<\/code><\/pre>\n<p>The configuration above sets up:<\/p>\n<ol>\n<li><strong>Vector Store<\/strong>: Using Azure AI Search to store and retrieve vectors<\/li>\n<li><strong>Embedder<\/strong>: Using Azure OpenAI to generate embeddings for semantic search<\/li>\n<li><strong>LLM<\/strong>: Using Azure OpenAI for language model capabilities<\/li>\n<\/ol>\n<h3>Storing Memories<\/h3>\n<p>With Mem0, you can store three types of memories:<\/p>\n<ol>\n<li><strong>Simple statements<\/strong>:<\/li>\n<\/ol>\n<pre><code class=\"language-python\">memory.add(\r\n    \"I enjoy hiking in national parks and taking landscape photos.\",\r\n    user_id=\"demo_user\"\r\n)<\/code><\/pre>\n<ol start=\"2\">\n<li><strong>Conversations<\/strong>:<\/li>\n<\/ol>\n<pre><code class=\"language-python\">conversation = [\r\n    {\"role\": \"user\", \"content\": \"I'm planning a trip to Japan in the fall.\"},\r\n    {\"role\": \"assistant\", \"content\": \"That's a great time to visit Japan!\"},\r\n    {\"role\": \"user\", \"content\": \"I'd like to visit Tokyo and Kyoto, and see Mount Fuji.\"}\r\n]\r\nmemory.add(conversation, user_id=\"demo_user\")<\/code><\/pre>\n<ol start=\"3\">\n<li><strong>Memories with metadata<\/strong>:<\/li>\n<\/ol>\n<pre><code class=\"language-python\">memory.add(\r\n    \"I prefer window seats on long flights and usually bring my own headphones.\",\r\n    user_id=\"demo_user\",\r\n    metadata={\"category\": \"travel_preferences\", \"importance\": \"medium\"}\r\n)<\/code><\/pre>\n<h3>Searching Memories<\/h3>\n<p>When you need to retrieve relevant memories, you can use the <code>search<\/code> method:<\/p>\n<pre><code class=\"language-python\">search_results = memory.search(\r\n    \"What are this user's travel plans?\",\r\n    user_id=\"demo_user\",\r\n    limit=3\r\n)\r\n\r\nfor i, result in enumerate(search_results['results'], 1):\r\n    print(f\"{i}. {result['memory']} (Score: {result['score']:.4f})\")<\/code><\/pre>\n<p>This will return memories sorted by relevance to the query, along with their similarity scores.<\/p>\n<h3>Retrieving All Memories<\/h3>\n<p>You can also retrieve all memories for a user:<\/p>\n<pre><code class=\"language-python\">all_memories = memory.get_all(user_id=\"demo_user\")\r\nprint(f\"Total memories: {len(all_memories['results'])}\")<\/code><\/pre>\n<h2>Building a Travel Planning Assistant with Memory<\/h2>\n<p>Now, let&#8217;s create a more practical example: a London travel planning assistant that remembers user preferences across conversations.<\/p>\n<pre><code class=\"language-python\">class TravelAssistant:\r\n    def __init__(self, user_id):\r\n        \"\"\"Initialize travel assistant with memory for a specific user\"\"\"\r\n        self.user_id = user_id\r\n\r\n        # Configure memory for travel planning\r\n        memory_config = {\r\n            \"vector_store\": {\r\n                \"provider\": \"azure_ai_search\",\r\n                \"config\": {\r\n                    \"service_name\": SEARCH_SERVICE_NAME,\r\n                    \"api_key\": SEARCH_SERVICE_API_KEY,\r\n                    \"collection_name\": \"travel_memories\",\r\n                    \"embedding_model_dims\": 1536,\r\n                    \"compression_type\": \"binary\",\r\n                },\r\n            },\r\n            \"llm\": {\r\n                \"provider\": \"azure_openai\",\r\n                \"config\": {\r\n                    \"model\": AZURE_OPENAI_CHAT_COMPLETION_DEPLOYED_MODEL_NAME,\r\n                    \"temperature\": 0.7,\r\n                    \"max_tokens\": 800,\r\n                    \"azure_kwargs\": {\r\n                        \"azure_deployment\": AZURE_OPENAI_CHAT_COMPLETION_DEPLOYED_MODEL_NAME,\r\n                        \"api_version\": \"2024-10-21\",\r\n                        \"azure_endpoint\": AZURE_OPENAI_ENDPOINT,\r\n                        \"api_key\": AZURE_OPENAI_API_KEY,\r\n                    },\r\n                },\r\n            },\r\n            \"embedder\": {\r\n                \"provider\": \"azure_openai\",\r\n                \"config\": {\r\n                    \"model\": AZURE_OPENAI_EMBEDDING_DEPLOYED_MODEL_NAME,\r\n                    \"embedding_dims\": 1536,\r\n                    \"azure_kwargs\": {\r\n                        \"api_version\": \"2024-10-21\",\r\n                        \"azure_deployment\": AZURE_OPENAI_EMBEDDING_DEPLOYED_MODEL_NAME,\r\n                        \"azure_endpoint\": AZURE_OPENAI_ENDPOINT,\r\n                        \"api_key\": AZURE_OPENAI_API_KEY,\r\n                    },\r\n                },\r\n            },\r\n            \"version\": \"v1.1\",\r\n        }\r\n\r\n        self.memory = Memory.from_config(memory_config)\r\n        self.azure_client = azure_openai_client\r\n        print(f\"Travel Assistant initialized for user {user_id}\")\r\n\r\n    def get_response(self, query, memory_context=True):\r\n        \"\"\"Get response from Azure OpenAI with memory context\"\"\"\r\n        # Retrieve relevant memories if enabled\r\n        memory_text = \"\"\r\n        if memory_context:\r\n            memories = self.memory.search(query, user_id=self.user_id)\r\n            if 'results' in memories and memories['results']:\r\n                memory_text = \"\\n\\nRelevant information from previous conversations:\\n\"\r\n                for i, mem in enumerate(memories['results'][:5], 1):\r\n                    memory_text += f\"{i}. {mem['memory']}\\n\"\r\n                print(f\"Including {len(memories['results'][:5])} memories in context\")\r\n            else:\r\n                print(\"No relevant memories found\")\r\n\r\n        # Construct messages with system prompt and memory context\r\n        messages = [\r\n            {\r\n                \"role\": \"system\",\r\n                \"content\": \"You are a helpful travel assistant for London travel planning. \"\r\n                           \"Be concise, specific, and helpful. Refer to the user by name when appropriate. \"\r\n                           \"Recommend specific places when relevant.\"\r\n            },\r\n            {\r\n                \"role\": \"user\",\r\n                \"content\": f\"{query}\\n{memory_text}\"\r\n            }\r\n        ]\r\n\r\n        # Get response from Azure OpenAI\r\n        response = self.azure_client.chat.completions.create(\r\n            model=AZURE_OPENAI_CHAT_COMPLETION_DEPLOYED_MODEL_NAME,\r\n            messages=messages,\r\n            temperature=0.7,\r\n            max_tokens=800,\r\n        )\r\n\r\n        # Extract response content\r\n        response_content = response.choices[0].message.content\r\n\r\n        # Store the conversation in memory\r\n        conversation = [\r\n            {\"role\": \"user\", \"content\": query},\r\n            {\"role\": \"assistant\", \"content\": response_content}\r\n        ]\r\n        self.memory.add(conversation, user_id=self.user_id)\r\n\r\n        return response_content\r\n\r\n    def verify_memories(self):\r\n        \"\"\"Verify what memories have been stored\"\"\"\r\n        all_memories = self.memory.get_all(user_id=self.user_id)\r\n        print(f\"\\n===== STORED MEMORIES ({len(all_memories['results'])}) =====\")\r\n        for i, memory in enumerate(all_memories['results'], 1):\r\n            print(f\"{i}. {memory['memory']}\")\r\n        print(\"==============================\\n\")\r\n        return all_memories<\/code><\/pre>\n<h3>Using the Travel Assistant<\/h3>\n<p>Now, let&#8217;s put our travel assistant to work:<\/p>\n<pre><code class=\"language-python\"># Create travel assistant for a user\r\nassistant = TravelAssistant(user_id=\"farzad_london_2025\")\r\n\r\n# First interaction - Initial inquiry\r\nquery1 = \"Hi, my name is Farzad. I'm planning a business trip to London next month for about 5 days.\"\r\nprint(f\"User: {query1}\")\r\nresponse1 = assistant.get_response(query1, memory_context=False)  # No memories yet\r\nprint(f\"Assistant: {response1}\\n\")\r\n\r\n# Second interaction - Specific question about fish and chips\r\nquery2 = \"I need recommendations for fish and chips restaurants near London Bridge cause I love the taste!\"\r\nprint(f\"User: {query2}\")\r\nresponse2 = assistant.get_response(query2)  # Should use memory context\r\nprint(f\"Assistant: {response2}\\n\")\r\n\r\n# Verify what memories have been stored\r\nassistant.verify_memories()<\/code><\/pre>\n<p>This demonstration shows how the assistant:<\/p>\n<ol>\n<li>Stores Farzad&#8217;s name and travel plans from the first interaction<\/li>\n<li>Remembers these details in the second interaction<\/li>\n<li>Uses the memory context to provide a personalized response<\/li>\n<\/ol>\n<h3>Searching for Specific Preferences<\/h3>\n<p>You can also directly search for specific user preferences:<\/p>\n<pre><code class=\"language-python\">search_query = \"What are Farzad's preferences for food in London?\"\r\nsearch_results = assistant.memory.search(search_query, user_id=\"farzad_london_2025\")\r\nprint(f\"Found {len(search_results['results'])} relevant memories:\")\r\nfor i, result in enumerate(search_results['results'][:3], 1):\r\n    print(f\"{i}. {result['memory']} (Score: {result['score']:.4f})\")<\/code><\/pre>\n<p>This might return results like:<\/p>\n<pre><code>Found 3 relevant memories:\r\n1. Name is Farzad (Score: 0.6696)\r\n2. Needs recommendations for fish and chips restaurants near London Bridge (Score: 0.6564)\r\n3. Loves the taste of fish and chips (Score: 0.6324)<\/code><\/pre>\n<h2>The Power of Persistent Memory<\/h2>\n<p>The key advantage of this approach is that the assistant maintains context across multiple interactions. By leveraging Mem0 with Azure AI services, we&#8217;ve created a system that:<\/p>\n<ol>\n<li><strong>Remembers user details<\/strong>: The assistant stores information like names, preferences, and travel plans<\/li>\n<li><strong>Personalizes responses<\/strong>: By retrieving relevant memories, the assistant can refer to the user by name and tailor recommendations<\/li>\n<li><strong>Improves over time<\/strong>: As more interactions occur, the system builds a more comprehensive understanding of the user<\/li>\n<\/ol>\n<p>This persistent memory dramatically improves the user experience by eliminating the need to repeat information in every conversation.<\/p>\n<h2>Conclusion<\/h2>\n<p>Integrating Mem0 with Azure AI services opens up a world of possibilities for creating more personalized and context-aware AI applications. By maintaining user memories across interactions, we can build assistants that feel more intelligent and responsive to user needs.<\/p>\n<p>This tutorial has shown you how to:<\/p>\n<ul>\n<li>Configure Mem0 with Azure AI Search and Azure OpenAI<\/li>\n<li>Store and retrieve different types of memories<\/li>\n<li>Build a practical travel assistant that remembers user preferences<\/li>\n<\/ul>\n<p>As you implement this in your own applications, consider the different types of memories you might want to store and how they can be used to enhance user experiences. With Mem0&#8217;s flexible memory system, you can create AI applications that truly understand and adapt to individual users over time.<\/p>\n<h2>Next Steps<\/h2>\n<ul>\n<li>Explore using different types of metadata to categorize memories<\/li>\n<li>Implement memory expiration or importance scoring<\/li>\n<li>Combine memories with other Azure services like Azure AI Services for more advanced features<\/li>\n<\/ul>\n<p>For more information on Mem0 and its capabilities, visit the <a href=\"https:\/\/docs.mem0.ai\">Mem0 documentation<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to integrate Mem0 with Azure AI Search and Azure OpenAI to create AI applications with persistent memory capabilities across conversations.<\/p>\n","protected":false},"author":170596,"featured_media":190,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[16,6,13,12,14,15],"class_list":["post-99","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-microsoft-foundry","tag-ai-applications","tag-azure-ai","tag-azure-ai-search","tag-azure-openai","tag-mem0","tag-memory-layer"],"acf":[],"blog_post_summary":"<p>Learn how to integrate Mem0 with Azure AI Search and Azure OpenAI to create AI applications with persistent memory capabilities across conversations.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/foundry\/wp-json\/wp\/v2\/posts\/99","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/foundry\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/foundry\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/foundry\/wp-json\/wp\/v2\/users\/170596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/foundry\/wp-json\/wp\/v2\/comments?post=99"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/foundry\/wp-json\/wp\/v2\/posts\/99\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/foundry\/wp-json\/wp\/v2\/media\/190"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/foundry\/wp-json\/wp\/v2\/media?parent=99"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/foundry\/wp-json\/wp\/v2\/categories?post=99"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/foundry\/wp-json\/wp\/v2\/tags?post=99"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}