The Memory Layer for AI Agents - Drop-in memory infrastructure for AI agents and apps. Context that persists. Built for production.
Python agentic-memoryagentic-memory-systemagentsaiai-agentschatgptgenaillmlong-term-memorymemorymemory-managementpythonragstate-management
[Docs] Add example for building Personal AI Assistant using Mem0 (#1486)
Deshraj Yadav2a43aa69023f
diff --git a/.gitignore b/.gitignoreindex 77254bff2..02e61e395 100644--- a/.gitignore+++ b/.gitignore@@ -182,4 +182,5 @@ notebooks/*.yaml# local directories for testingeval/-qdrant_storage/\ No newline at end of file+qdrant_storage/+.crossnote\ No newline at end of filediff --git a/README.md b/README.mdindex 0eec5639b..db7dce343 100644--- a/README.md+++ b/README.md@@ -18,6 +18,7 @@Mem0 provides a smart, self-improving memory layer for Large Language Models, enabling personalized AI experiences across applications.+> Note: The Mem0 repository now also includes the Embedchain project. We continue to maintain and support Embedchain ❤️. You can find the Embedchain codebase in the [embedchain](https://github.com/mem0ai/mem0/tree/main/embedchain) directory.## 🚀 Quick Start### Installationdiff --git a/docs/examples/customer-support-agent.mdx b/docs/examples/customer-support-agent.mdxindex 58ecf33a4..16ff56d8a 100644--- a/docs/examples/customer-support-agent.mdx+++ b/docs/examples/customer-support-agent.mdx@@ -24,6 +24,9 @@ Below is the simplified code to create and interact with a Customer Support AI Afrom openai import OpenAIfrom mem0 import Memory+# Set the OpenAI API key+os.environ['OPENAI_API_KEY'] = 'sk-xxx'+class CustomerSupportAIAgent:def __init__(self):"""diff --git a/docs/examples/overview.mdx b/docs/examples/overview.mdxindex 934720d34..b4c0a5041 100644--- a/docs/examples/overview.mdx+++ b/docs/examples/overview.mdx@@ -17,12 +17,16 @@ Here are some examples of how Mem0 can be integrated into various applications:## Example Use Cases<CardGroup cols={1}>- <Card title="Personalized AI Tutor" icon="square-2" href="/examples/personal-ai-tutor">+ <Card title="Personal AI Tutor" icon="square-1" href="/examples/personal-ai-tutor"><img width="100%" src="/images/ai-tutor.png" />- Build a Personalized AI Tutor that adapts to student progress and learning preferences. This tutor can offer tailored lessons, remember past interactions, and provide a more effective and engaging educational experience.+ Create a Personalized AI Tutor that adapts to student progress and learning preferences.</Card>- <Card title="Customer Support Agent" icon="square-1" href="/examples/customer-support-agent">+ <Card title="Personal Travel Assistant" icon="square-2" href="/examples/personal-travel-assistant">+ <img src="/images/personal-travel-agent.png" />+ Build a Personalized AI Travel Assistant that understands your travel preferences and past itineraries.+ </Card>+ <Card title="Customer Support Agent" icon="square-3" href="/examples/customer-support-agent"><img width="100%" src="/images/customer-support-agent.png" />- Develop a Personal AI Assistant that can remember user preferences, past interactions, and context to provide personalized and efficient assistance. This assistant can manage tasks, provide reminders, and adapt to individual user needs, enhancing productivity and user experience.+ Develop a Personal AI Assistant that remembers user preferences, past interactions, and context to provide personalized and efficient assistance.</Card></CardGroup>\ No newline at end of filediff --git a/docs/examples/personal-ai-tutor.mdx b/docs/examples/personal-ai-tutor.mdxindex 3ccb4dab3..9df825ddf 100644--- a/docs/examples/personal-ai-tutor.mdx+++ b/docs/examples/personal-ai-tutor.mdx@@ -23,6 +23,9 @@ Below is the complete code to create and interact with a Personalized AI Tutor ufrom openai import OpenAIfrom mem0 import Memory+# Set the OpenAI API key+os.environ['OPENAI_API_KEY'] = 'sk-xxx'+# Initialize the OpenAI clientclient = OpenAI()diff --git a/docs/examples/personal-travel-assistant.mdx b/docs/examples/personal-travel-assistant.mdxnew file mode 100644index 000000000..0b59193f0--- /dev/null+++ b/docs/examples/personal-travel-assistant.mdx@@ -0,0 +1,101 @@+---+title: Personal AI Travel Assistant+---+Create a personalized AI Travel Assistant using Mem0. This guide provides step-by-step instructions and the complete code to get you started.++## Overview++The Personalized AI Travel Assistant uses Mem0 to store and retrieve information across interactions, enabling a tailored travel planning experience. It integrates with OpenAI's GPT-4 model to provide detailed and context-aware responses to user queries.++## Setup++Install the required dependencies using pip:++```bash+pip install openai mem0ai+```++## Full Code Example++Here's the complete code to create and interact with a Personalized AI Travel Assistant using Mem0:++```python+import os+from openai import OpenAI+from mem0 import Memory++# Set the OpenAI API key+os.environ['OPENAI_API_KEY'] = 'sk-xxx'++class PersonalTravelAssistant:+ def __init__(self):+ self.client = OpenAI()+ self.memory = Memory()+ self.messages = [{"role": "system", "content": "You are a personal AI Assistant."}]++ def ask_question(self, question, user_id):+ # Fetch previous related memories+ previous_memories = self.search_memories(question, user_id=user_id)+ prompt = question+ if previous_memories:+ prompt = f"User input: {question}\n Previous memories: {previous_memories}"+ self.messages.append({"role": "user", "content": prompt})++ # Generate response using GPT-4o+ response = self.client.chat.completions.create(+ model="gpt-4o",+ messages=self.messages+ )+ answer = response.choices[0].message.content+ self.messages.append({"role": "assistant", "content": answer})++ # Store the question in memory+ self.memory.add(question, user_id=user_id)+ return answer++ def get_memories(self, user_id):+ memories = self.memory.get_all(user_id=user_id)+ return [m['text'] for m in memories]++ def search_memories(self, query, user_id):+ memories = self.memory.search(query, user_id=user_id)+ return [m['text'] for m in memories]++# Usage example+user_id = "traveler_123"+ai_assistant = PersonalTravelAssistant()++def main():+ while True:+ question = input("Question: ")+ if question.lower() in ['q', 'exit']:+ print("Exiting...")+ break++ answer = ai_assistant.ask_question(question, user_id=user_id)+ print(f"Answer: {answer}")+ memories = ai_assistant.get_memories(user_id=user_id)+ print("Memories:")+ for memory in memories:+ print(f"- {memory}")+ print("-----")++if __name__ == "__main__":+ main()+```++## Key Components++- **Initialization**: The `PersonalTravelAssistant` class is initialized with the OpenAI client and Mem0 memory setup.+- **Asking Questions**: The `ask_question` method sends a question to the AI, incorporates previous memories, and stores new information.+- **Memory Management**: The `get_memories` and search_memories methods handle retrieval and searching of stored memories.++## Usage++1. Set your OpenAI API key in the environment variable.+2. Instantiate the `PersonalTravelAssistant`.+3. Use the `main()` function to interact with the assistant in a loop.++## Conclusion++This Personalized AI Travel Assistant leverages Mem0's memory capabilities to provide context-aware responses. As you interact with it, the assistant learns and improves, offering increasingly personalized travel advice and information.\ No newline at end of filediff --git a/docs/images/personal-travel-agent.png b/docs/images/personal-travel-agent.pngnew file mode 100644index 000000000..290d205f2Binary files /dev/null and b/docs/images/personal-travel-agent.png differdiff --git a/docs/llms.mdx b/docs/llms.mdxindex 03367baf9..0d0a75e26 100644--- a/docs/llms.mdx+++ b/docs/llms.mdx@@ -1,5 +1,5 @@----title: 🤖 Large language models (LLMs)+title: 🤖 LLMs---## Overviewdiff --git a/docs/mint.json b/docs/mint.jsonindex 03ca08c17..5aa3f08ff 100644--- a/docs/mint.json+++ b/docs/mint.json@@ -54,7 +54,7 @@]},{- "group": "LLMs",+ "group": "Integrations","pages": ["llms"]@@ -64,7 +64,8 @@"pages": ["examples/overview","examples/personal-ai-tutor",- "examples/customer-support-agent"+ "examples/customer-support-agent",+ "examples/personal-travel-assistant"]}],