[Mem0] Fix issues and update docs (#1477)

This commit is contained in:
Deshraj Yadav
2024-07-14 22:21:07 -07:00
committed by GitHub
parent f842a92e25
commit 4ec51f2dd6
21 changed files with 1257 additions and 1010 deletions

View File

@@ -1,5 +1,5 @@
import importlib.metadata
__version__ = importlib.metadata.version(__package__ or __name__)
__version__ = importlib.metadata.version("mem0ai")
from mem0.memory.main import Memory # noqa

View File

@@ -1,5 +1,5 @@
UPDATE_MEMORY_PROMPT = """
You are an expert at merging, updating, and organizing user memories. When provided with existing memories and new information, your task is to merge and update the memory list to reflect the most accurate and current information. You are also provided with the matching score for each existing memory to the new information. Make sure to leverage this information to make informed decisions about which memories to update or merge.
You are an expert at merging, updating, and organizing memories. When provided with existing memories and new information, your task is to merge and update the memory list to reflect the most accurate and current information. You are also provided with the matching score for each existing memory to the new information. Make sure to leverage this information to make informed decisions about which memories to update or merge.
Guidelines:
- Eliminate duplicate memories and merge related memories to ensure a concise and updated list.
@@ -15,3 +15,17 @@ Here are the details of the task:
- New Memory: {memory}
"""
MEMORY_DEDUCTION_PROMPT = """
Deduce the facts, preferences, and memories from the provided text.
Just return the facts, preferences, and memories in bullet points:
Natural language text: {user_input}
User/Agent details: {metadata}
Constraint for deducing facts, preferences, and memories:
- The facts, preferences, and memories should be concise and informative.
- Don't start by "The person likes Pizza". Instead, start with "Likes Pizza".
- Don't remember the user/agent details provided. Only remember the facts, preferences, and memories.
Deduced facts, preferences, and memories:
"""

View File

@@ -38,4 +38,3 @@ class OpenAILLM(LLMBase):
response = self.client.chat.completions.create(**params)
return response
# return response.choices[0].message["content"]

View File

@@ -14,6 +14,7 @@ from mem0.llms.utils.tools import (
DELETE_MEMORY_TOOL,
UPDATE_MEMORY_TOOL,
)
from mem0.configs.prompts import MEMORY_DEDUCTION_PROMPT
from mem0.memory.base import MemoryBase
from mem0.memory.setup import mem0_dir, setup_config
from mem0.memory.storage import SQLiteManager
@@ -100,6 +101,7 @@ class Memory(MemoryBase):
run_id=None,
metadata=None,
filters=None,
prompt=None,
):
"""
Create a new memory.
@@ -116,7 +118,6 @@ class Memory(MemoryBase):
str: ID of the created memory.
"""
if metadata is None:
logging.warn("Metadata not provided. Using empty metadata.")
metadata = {}
embeddings = self.embedding_model.embed(data)
@@ -128,6 +129,18 @@ class Memory(MemoryBase):
if run_id:
filters["run_id"] = metadata["run_id"] = run_id
if not prompt:
prompt = MEMORY_DEDUCTION_PROMPT.format(user_input=data, metadata=metadata)
extracted_memories = self.llm.generate_response(
messages=[
{
"role": "system",
"content": "You are an expert at deducing facts, preferences and memories from unstructured text.",
},
{"role": "user", "content": prompt},
]
)
extracted_memories = extracted_memories.choices[0].message.content
existing_memories = self.vector_store.search(
name=self.collection_name,
query=embeddings,
@@ -148,7 +161,9 @@ class Memory(MemoryBase):
for item in existing_memories
]
logging.info(f"Total existing memories: {len(existing_memories)}")
messages = get_update_memory_messages(serialized_existing_memories, data)
messages = get_update_memory_messages(
serialized_existing_memories, extracted_memories
)
# Add tools for noop, add, update, delete memory.
tools = [ADD_MEMORY_TOOL, UPDATE_MEMORY_TOOL, DELETE_MEMORY_TOOL]
response = self.llm.generate_response(messages=messages, tools=tools)