Skip to content

Commit 2a43aa6

Browse files
authored
[Docs] Add example for building Personal AI Assistant using Mem0 (mem0ai#1486)
1 parent b620f8f commit 2a43aa6

9 files changed

+122
-8
lines changed

Diff for: .gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,5 @@ notebooks/*.yaml
182182

183183
# local directories for testing
184184
eval/
185-
qdrant_storage/
185+
qdrant_storage/
186+
.crossnote

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
Mem0 provides a smart, self-improving memory layer for Large Language Models, enabling personalized AI experiences across applications.
2020

21+
> 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.
2122
## 🚀 Quick Start
2223

2324
### Installation

Diff for: docs/examples/customer-support-agent.mdx

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ Below is the simplified code to create and interact with a Customer Support AI A
2424
from openai import OpenAI
2525
from mem0 import Memory
2626

27+
# Set the OpenAI API key
28+
os.environ['OPENAI_API_KEY'] = 'sk-xxx'
29+
2730
class CustomerSupportAIAgent:
2831
def __init__(self):
2932
"""

Diff for: docs/examples/overview.mdx

+8-4
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@ Here are some examples of how Mem0 can be integrated into various applications:
1717
## Example Use Cases
1818

1919
<CardGroup cols={1}>
20-
<Card title="Personalized AI Tutor" icon="square-2" href="/examples/personal-ai-tutor">
20+
<Card title="Personal AI Tutor" icon="square-1" href="/examples/personal-ai-tutor">
2121
<img width="100%" src="/images/ai-tutor.png" />
22-
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.
22+
Create a Personalized AI Tutor that adapts to student progress and learning preferences.
2323
</Card>
24-
<Card title="Customer Support Agent" icon="square-1" href="/examples/customer-support-agent">
24+
<Card title="Personal Travel Assistant" icon="square-2" href="/examples/personal-travel-assistant">
25+
<img src="/images/personal-travel-agent.png" />
26+
Build a Personalized AI Travel Assistant that understands your travel preferences and past itineraries.
27+
</Card>
28+
<Card title="Customer Support Agent" icon="square-3" href="/examples/customer-support-agent">
2529
<img width="100%" src="/images/customer-support-agent.png" />
26-
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.
30+
Develop a Personal AI Assistant that remembers user preferences, past interactions, and context to provide personalized and efficient assistance.
2731
</Card>
2832
</CardGroup>

Diff for: docs/examples/personal-ai-tutor.mdx

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ Below is the complete code to create and interact with a Personalized AI Tutor u
2323
from openai import OpenAI
2424
from mem0 import Memory
2525

26+
# Set the OpenAI API key
27+
os.environ['OPENAI_API_KEY'] = 'sk-xxx'
28+
2629
# Initialize the OpenAI client
2730
client = OpenAI()
2831

Diff for: docs/examples/personal-travel-assistant.mdx

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
title: Personal AI Travel Assistant
3+
---
4+
Create a personalized AI Travel Assistant using Mem0. This guide provides step-by-step instructions and the complete code to get you started.
5+
6+
## Overview
7+
8+
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.
9+
10+
## Setup
11+
12+
Install the required dependencies using pip:
13+
14+
```bash
15+
pip install openai mem0ai
16+
```
17+
18+
## Full Code Example
19+
20+
Here's the complete code to create and interact with a Personalized AI Travel Assistant using Mem0:
21+
22+
```python
23+
import os
24+
from openai import OpenAI
25+
from mem0 import Memory
26+
27+
# Set the OpenAI API key
28+
os.environ['OPENAI_API_KEY'] = 'sk-xxx'
29+
30+
class PersonalTravelAssistant:
31+
def __init__(self):
32+
self.client = OpenAI()
33+
self.memory = Memory()
34+
self.messages = [{"role": "system", "content": "You are a personal AI Assistant."}]
35+
36+
def ask_question(self, question, user_id):
37+
# Fetch previous related memories
38+
previous_memories = self.search_memories(question, user_id=user_id)
39+
prompt = question
40+
if previous_memories:
41+
prompt = f"User input: {question}\n Previous memories: {previous_memories}"
42+
self.messages.append({"role": "user", "content": prompt})
43+
44+
# Generate response using GPT-4o
45+
response = self.client.chat.completions.create(
46+
model="gpt-4o",
47+
messages=self.messages
48+
)
49+
answer = response.choices[0].message.content
50+
self.messages.append({"role": "assistant", "content": answer})
51+
52+
# Store the question in memory
53+
self.memory.add(question, user_id=user_id)
54+
return answer
55+
56+
def get_memories(self, user_id):
57+
memories = self.memory.get_all(user_id=user_id)
58+
return [m['text'] for m in memories]
59+
60+
def search_memories(self, query, user_id):
61+
memories = self.memory.search(query, user_id=user_id)
62+
return [m['text'] for m in memories]
63+
64+
# Usage example
65+
user_id = "traveler_123"
66+
ai_assistant = PersonalTravelAssistant()
67+
68+
def main():
69+
while True:
70+
question = input("Question: ")
71+
if question.lower() in ['q', 'exit']:
72+
print("Exiting...")
73+
break
74+
75+
answer = ai_assistant.ask_question(question, user_id=user_id)
76+
print(f"Answer: {answer}")
77+
memories = ai_assistant.get_memories(user_id=user_id)
78+
print("Memories:")
79+
for memory in memories:
80+
print(f"- {memory}")
81+
print("-----")
82+
83+
if __name__ == "__main__":
84+
main()
85+
```
86+
87+
## Key Components
88+
89+
- **Initialization**: The `PersonalTravelAssistant` class is initialized with the OpenAI client and Mem0 memory setup.
90+
- **Asking Questions**: The `ask_question` method sends a question to the AI, incorporates previous memories, and stores new information.
91+
- **Memory Management**: The `get_memories` and search_memories methods handle retrieval and searching of stored memories.
92+
93+
## Usage
94+
95+
1. Set your OpenAI API key in the environment variable.
96+
2. Instantiate the `PersonalTravelAssistant`.
97+
3. Use the `main()` function to interact with the assistant in a loop.
98+
99+
## Conclusion
100+
101+
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.

Diff for: docs/images/personal-travel-agent.png

3.9 MB
Loading

Diff for: docs/llms.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: 🤖 Large language models (LLMs)
2+
title: 🤖 LLMs
33
---
44

55
## Overview

Diff for: docs/mint.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
]
5555
},
5656
{
57-
"group": "LLMs",
57+
"group": "Integrations",
5858
"pages": [
5959
"llms"
6060
]
@@ -64,7 +64,8 @@
6464
"pages": [
6565
"examples/overview",
6666
"examples/personal-ai-tutor",
67-
"examples/customer-support-agent"
67+
"examples/customer-support-agent",
68+
"examples/personal-travel-assistant"
6869
]
6970
}
7071
],

0 commit comments

Comments
 (0)