refactor: v2 search and update examples (#3508)
This commit is contained in:
@@ -330,9 +330,7 @@
|
||||
"pages": [
|
||||
"api-reference/memory/add-memories",
|
||||
"api-reference/memory/v2-search-memories",
|
||||
"api-reference/memory/v1-search-memories",
|
||||
"api-reference/memory/v2-get-memories",
|
||||
"api-reference/memory/v1-get-memories",
|
||||
"api-reference/memory/history-memory",
|
||||
"api-reference/memory/get-memory",
|
||||
"api-reference/memory/update-memory",
|
||||
|
||||
@@ -112,7 +112,6 @@ class EmailProcessor:
|
||||
query=query,
|
||||
user_id=user_id,
|
||||
categories=["email"],
|
||||
output_format="v1.1",
|
||||
version="v2"
|
||||
)
|
||||
|
||||
@@ -136,8 +135,7 @@ class EmailProcessor:
|
||||
|
||||
thread = self.client.get_all(
|
||||
version="v2",
|
||||
filters=filters,
|
||||
output_format="v1.1"
|
||||
filters=filters
|
||||
)
|
||||
|
||||
return thread
|
||||
|
||||
@@ -112,7 +112,7 @@ async def search_memory(
|
||||
query: The search query.
|
||||
"""
|
||||
user_id = context.context.user_id or "default_user"
|
||||
memories = await client.search(query, user_id=user_id, output_format="v1.1")
|
||||
memories = await client.search(query, user_id=user_id)
|
||||
results = '\n'.join([result["memory"] for result in memories["results"]])
|
||||
return str(results)
|
||||
```
|
||||
@@ -126,7 +126,7 @@ async def get_all_memory(
|
||||
) -> str:
|
||||
"""Retrieve all memories from Mem0"""
|
||||
user_id = context.context.user_id or "default_user"
|
||||
memories = await client.get_all(user_id=user_id, output_format="v1.1")
|
||||
memories = await client.get_all(user_id=user_id)
|
||||
results = '\n'.join([result["memory"] for result in memories["results"]])
|
||||
return str(results)
|
||||
```
|
||||
|
||||
@@ -80,8 +80,7 @@ def retrieve_patient_info(query: str) -> dict:
|
||||
query,
|
||||
user_id=USER_ID,
|
||||
limit=5,
|
||||
threshold=0.7, # Higher threshold for more relevant results
|
||||
output_format="v1.1"
|
||||
threshold=0.7 # Higher threshold for more relevant results
|
||||
)
|
||||
|
||||
# Format and return the results
|
||||
|
||||
@@ -76,7 +76,7 @@ def setup_user_history(user_id):
|
||||
]
|
||||
|
||||
for conversation in conversations:
|
||||
mem0_client.add(conversation, user_id=user_id, output_format="v1.1")
|
||||
mem0_client.add(conversation, user_id=user_id)
|
||||
```
|
||||
This gives the agent a baseline understanding of the user’s lifestyle and needs.
|
||||
|
||||
|
||||
@@ -125,7 +125,7 @@ def chat_user(
|
||||
|
||||
if user_input:
|
||||
# Search for relevant memories
|
||||
memories = client.search(user_input, user_id=user_id, output_format='v1.1')
|
||||
memories = client.search(user_input, user_id=user_id)
|
||||
memory_context = "\n".join(f"- {m['memory']}" for m in memories['results'])
|
||||
|
||||
# Construct the prompt
|
||||
|
||||
@@ -62,7 +62,7 @@ conversation = [
|
||||
{"role": "assistant", "content": "Thank you for the information. Let's troubleshoot this issue..."}
|
||||
]
|
||||
|
||||
memory_client.add(messages=conversation, user_id=USER_ID, output_format="v1.1")
|
||||
memory_client.add(messages=conversation, user_id=USER_ID)
|
||||
print("Conversation added to memory.")
|
||||
```
|
||||
|
||||
@@ -72,7 +72,7 @@ Create a function to get context-aware responses based on user's question and pr
|
||||
|
||||
```python
|
||||
def get_context_aware_response(question):
|
||||
relevant_memories = memory_client.search(question, user_id=USER_ID, output_format='v1.1')
|
||||
relevant_memories = memory_client.search(question, user_id=USER_ID)
|
||||
context = "\n".join([m["memory"] for m in relevant_memories.get('results', [])])
|
||||
|
||||
prompt = f"""Answer the user question considering the previous interactions:
|
||||
@@ -104,7 +104,7 @@ manager = ConversableAgent(
|
||||
)
|
||||
|
||||
def escalate_to_manager(question):
|
||||
relevant_memories = memory_client.search(question, user_id=USER_ID, output_format='v1.1')
|
||||
relevant_memories = memory_client.search(question, user_id=USER_ID)
|
||||
context = "\n".join([m["memory"] for m in relevant_memories.get('results', [])])
|
||||
|
||||
prompt = f"""
|
||||
|
||||
@@ -50,7 +50,7 @@ mem0 = MemoryClient()
|
||||
# Define memory function tools
|
||||
def search_memory(query: str, user_id: str) -> dict:
|
||||
"""Search through past conversations and memories"""
|
||||
memories = mem0.search(query, user_id=user_id, output_format='v1.1')
|
||||
memories = mem0.search(query, user_id=user_id)
|
||||
if memories.get('results', []):
|
||||
memory_list = memories['results']
|
||||
memory_context = "\n".join([f"- {mem['memory']}" for mem in memory_list])
|
||||
@@ -60,7 +60,7 @@ def search_memory(query: str, user_id: str) -> dict:
|
||||
def save_memory(content: str, user_id: str) -> dict:
|
||||
"""Save important information to memory"""
|
||||
try:
|
||||
result = mem0.add([{"role": "user", "content": content}], user_id=user_id, output_format='v1.1')
|
||||
result = mem0.add([{"role": "user", "content": content}], user_id=user_id)
|
||||
return {"status": "success", "message": "Information saved to memory", "result": result}
|
||||
except Exception as e:
|
||||
return {"status": "error", "message": f"Failed to save memory: {str(e)}"}
|
||||
|
||||
@@ -65,7 +65,7 @@ Create functions to handle context retrieval, response generation, and addition
|
||||
def retrieve_context(query: str, user_id: str) -> List[Dict]:
|
||||
"""Retrieve relevant context from Mem0"""
|
||||
try:
|
||||
memories = mem0.search(query, user_id=user_id, output_format='v1.1')
|
||||
memories = mem0.search(query, user_id=user_id)
|
||||
memory_list = memories['results']
|
||||
|
||||
serialized_memories = ' '.join([mem["memory"] for mem in memory_list])
|
||||
@@ -107,7 +107,7 @@ def save_interaction(user_id: str, user_input: str, assistant_response: str):
|
||||
"content": assistant_response
|
||||
}
|
||||
]
|
||||
result = mem0.add(interaction, user_id=user_id, output_format='v1.1')
|
||||
result = mem0.add(interaction, user_id=user_id)
|
||||
print(f"Memory saved successfully: {len(result.get('results', []))} memories added")
|
||||
except Exception as e:
|
||||
print(f"Error saving interaction: {e}")
|
||||
|
||||
@@ -67,7 +67,7 @@ def chatbot(state: State):
|
||||
|
||||
try:
|
||||
# Retrieve relevant memories
|
||||
memories = mem0.search(messages[-1].content, user_id=user_id, output_format='v1.1')
|
||||
memories = mem0.search(messages[-1].content, user_id=user_id)
|
||||
|
||||
# Handle dict response format
|
||||
memory_list = memories['results']
|
||||
@@ -94,7 +94,7 @@ def chatbot(state: State):
|
||||
"content": response.content
|
||||
}
|
||||
]
|
||||
result = mem0.add(interaction, user_id=user_id, output_format='v1.1')
|
||||
result = mem0.add(interaction, user_id=user_id)
|
||||
print(f"Memory saved: {len(result.get('results', []))} memories added")
|
||||
except Exception as e:
|
||||
print(f"Error saving memory: {e}")
|
||||
|
||||
@@ -41,7 +41,7 @@ You can retrieve memories using the `search` method.
|
||||
<CodeGroup>
|
||||
|
||||
```python Python
|
||||
client.search("What is Alice's favorite sport?", user_id="alice", output_format="v1.1")
|
||||
client.search("What is Alice's favorite sport?", user_id="alice")
|
||||
```
|
||||
|
||||
```json Output
|
||||
|
||||
@@ -70,7 +70,7 @@ const retrieveMemories = (memories: any) => {
|
||||
export async function POST(req: Request) {
|
||||
const { messages, system, tools, userId } = await req.json();
|
||||
|
||||
const memories = await getMemories(messages, { user_id: userId, rerank: true, threshold: 0.1, output_format: "v1.0" });
|
||||
const memories = await getMemories(messages, { user_id: userId, rerank: true, threshold: 0.1 });
|
||||
const mem0Instructions = retrieveMemories(memories);
|
||||
|
||||
const result = streamText({
|
||||
|
||||
@@ -27,7 +27,7 @@ agent = Agent(
|
||||
# Store user preferences as memory
|
||||
def store_user_preferences(conversation: list, user_id: str = USER_ID):
|
||||
"""Store user preferences from conversation history"""
|
||||
memory_client.add(conversation, user_id=user_id, output_format="v1.1")
|
||||
memory_client.add(conversation, user_id=user_id)
|
||||
|
||||
|
||||
# Memory-aware assistant function
|
||||
|
||||
@@ -99,7 +99,6 @@ Provide actionable insights in your area of expertise."""
|
||||
user_id=project_id, # Project-level memory
|
||||
agent_id=specialist, # Agent-specific memory
|
||||
metadata={"contributor": specialist, "task_type": "research", "model_used": spec_info["model"]},
|
||||
output_format="v1.1",
|
||||
)
|
||||
|
||||
return result
|
||||
|
||||
@@ -44,7 +44,7 @@ def chat_user(user_input: str = None, user_id: str = "user_123", image_path: str
|
||||
}
|
||||
|
||||
# Send both as separate message objects
|
||||
client.add([text_msg, image_msg], user_id=user_id, output_format="v1.1")
|
||||
client.add([text_msg, image_msg], user_id=user_id)
|
||||
print("✅ Image uploaded and stored in memory.")
|
||||
|
||||
if user_input:
|
||||
|
||||
@@ -66,7 +66,7 @@ def setup_user_history(user_id):
|
||||
|
||||
logger.info(f"Setting up user history for {user_id}")
|
||||
for conversation in conversations:
|
||||
mem0_client.add(conversation, user_id=user_id, output_format="v1.1")
|
||||
mem0_client.add(conversation, user_id=user_id)
|
||||
|
||||
|
||||
def get_user_context(user_id, query):
|
||||
@@ -209,7 +209,7 @@ def store_search_interaction(user_id, original_query, agent_response):
|
||||
{"role": "assistant", "content": f"Provided personalized results based on user preferences: {agent_response}"}
|
||||
]
|
||||
|
||||
mem0_client.add(messages=interaction, user_id=user_id, output_format="v1.1")
|
||||
mem0_client.add(messages=interaction, user_id=user_id)
|
||||
|
||||
logger.info(f"Stored search interaction for user {user_id}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user