Updated integration docs (#3392)
This commit is contained in:
@@ -16,7 +16,7 @@ In this guide, we'll create a Travel Agent AI that:
|
||||
Install necessary libraries:
|
||||
|
||||
```bash
|
||||
pip install langchain langchain_openai mem0ai
|
||||
pip install langchain langchain_openai mem0ai python-dotenv
|
||||
```
|
||||
|
||||
Import required modules and set up configurations:
|
||||
@@ -30,10 +30,13 @@ from langchain_openai import ChatOpenAI
|
||||
from langchain_core.messages import SystemMessage, HumanMessage, AIMessage
|
||||
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
||||
from mem0 import MemoryClient
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
# Configuration
|
||||
os.environ["OPENAI_API_KEY"] = "your-openai-api-key"
|
||||
os.environ["MEM0_API_KEY"] = "your-mem0-api-key"
|
||||
# os.environ["OPENAI_API_KEY"] = "your-openai-api-key"
|
||||
# os.environ["MEM0_API_KEY"] = "your-mem0-api-key"
|
||||
|
||||
# Initialize LangChain and Mem0
|
||||
llm = ChatOpenAI(model="gpt-4o-mini")
|
||||
@@ -61,19 +64,26 @@ Create functions to handle context retrieval, response generation, and addition
|
||||
```python
|
||||
def retrieve_context(query: str, user_id: str) -> List[Dict]:
|
||||
"""Retrieve relevant context from Mem0"""
|
||||
memories = mem0.search(query, user_id=user_id)
|
||||
serialized_memories = ' '.join([mem["memory"] for mem in memories.get('results', [])])
|
||||
context = [
|
||||
{
|
||||
"role": "system",
|
||||
"content": f"Relevant information: {serialized_memories}"
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": query
|
||||
}
|
||||
]
|
||||
return context
|
||||
try:
|
||||
memories = mem0.search(query, user_id=user_id, output_format='v1.1')
|
||||
memory_list = memories['results']
|
||||
|
||||
serialized_memories = ' '.join([mem["memory"] for mem in memory_list])
|
||||
context = [
|
||||
{
|
||||
"role": "system",
|
||||
"content": f"Relevant information: {serialized_memories}"
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": query
|
||||
}
|
||||
]
|
||||
return context
|
||||
except Exception as e:
|
||||
print(f"Error retrieving memories: {e}")
|
||||
# Return empty context if there's an error
|
||||
return [{"role": "user", "content": query}]
|
||||
|
||||
def generate_response(input: str, context: List[Dict]) -> str:
|
||||
"""Generate a response using the language model"""
|
||||
@@ -86,17 +96,21 @@ def generate_response(input: str, context: List[Dict]) -> str:
|
||||
|
||||
def save_interaction(user_id: str, user_input: str, assistant_response: str):
|
||||
"""Save the interaction to Mem0"""
|
||||
interaction = [
|
||||
{
|
||||
"role": "user",
|
||||
"content": user_input
|
||||
},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": assistant_response
|
||||
}
|
||||
]
|
||||
mem0.add(interaction, user_id=user_id)
|
||||
try:
|
||||
interaction = [
|
||||
{
|
||||
"role": "user",
|
||||
"content": user_input
|
||||
},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": assistant_response
|
||||
}
|
||||
]
|
||||
result = mem0.add(interaction, user_id=user_id, output_format='v1.1')
|
||||
print(f"Memory saved successfully: {len(result.get('results', []))} memories added")
|
||||
except Exception as e:
|
||||
print(f"Error saving interaction: {e}")
|
||||
```
|
||||
|
||||
## Create Chat Turn Function
|
||||
@@ -124,7 +138,7 @@ Set up the main program loop for user interaction:
|
||||
```python
|
||||
if __name__ == "__main__":
|
||||
print("Welcome to your personal Travel Agent Planner! How can I assist you with your travel plans today?")
|
||||
user_id = "john"
|
||||
user_id = "alice"
|
||||
|
||||
while True:
|
||||
user_input = input("You: ")
|
||||
|
||||
Reference in New Issue
Block a user