[Docs] Add example for building Personal AI Assistant using Mem0 (#1486)

This commit is contained in:
Deshraj Yadav
2024-07-16 15:20:03 -07:00
committed by GitHub
parent b620f8fae3
commit 2a43aa6902
9 changed files with 122 additions and 8 deletions

3
.gitignore vendored
View File

@@ -182,4 +182,5 @@ notebooks/*.yaml
# local directories for testing # local directories for testing
eval/ eval/
qdrant_storage/ qdrant_storage/
.crossnote

View File

@@ -18,6 +18,7 @@
Mem0 provides a smart, self-improving memory layer for Large Language Models, enabling personalized AI experiences across applications. 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 ## 🚀 Quick Start
### Installation ### Installation

View File

@@ -24,6 +24,9 @@ Below is the simplified code to create and interact with a Customer Support AI A
from openai import OpenAI from openai import OpenAI
from mem0 import Memory from mem0 import Memory
# Set the OpenAI API key
os.environ['OPENAI_API_KEY'] = 'sk-xxx'
class CustomerSupportAIAgent: class CustomerSupportAIAgent:
def __init__(self): def __init__(self):
""" """

View File

@@ -17,12 +17,16 @@ Here are some examples of how Mem0 can be integrated into various applications:
## Example Use Cases ## Example Use Cases
<CardGroup cols={1}> <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" /> <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>
<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" /> <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> </Card>
</CardGroup> </CardGroup>

View File

@@ -23,6 +23,9 @@ Below is the complete code to create and interact with a Personalized AI Tutor u
from openai import OpenAI from openai import OpenAI
from mem0 import Memory from mem0 import Memory
# Set the OpenAI API key
os.environ['OPENAI_API_KEY'] = 'sk-xxx'
# Initialize the OpenAI client # Initialize the OpenAI client
client = OpenAI() client = OpenAI()

View File

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 MiB

View File

@@ -1,5 +1,5 @@
--- ---
title: 🤖 Large language models (LLMs) title: 🤖 LLMs
--- ---
## Overview ## Overview

View File

@@ -54,7 +54,7 @@
] ]
}, },
{ {
"group": "LLMs", "group": "Integrations",
"pages": [ "pages": [
"llms" "llms"
] ]
@@ -64,7 +64,8 @@
"pages": [ "pages": [
"examples/overview", "examples/overview",
"examples/personal-ai-tutor", "examples/personal-ai-tutor",
"examples/customer-support-agent" "examples/customer-support-agent",
"examples/personal-travel-assistant"
] ]
} }
], ],