Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace UUID with indexes to reduce hallucinations #2004

Merged
merged 1 commit into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions mem0/configs/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def get_update_memory_messages(retrieved_old_memory_dict, response_content):
- Old Memory:
[
{{
"id" : "7f165f7e-b411-4afe-b7e5-35789b72c4a5",
"id" : "0",
"text" : "User is a software engineer"
}}
]
Expand All @@ -88,12 +88,12 @@ def get_update_memory_messages(retrieved_old_memory_dict, response_content):
{{
"memory" : [
{{
"id" : "7f165f7e-b411-4afe-b7e5-35789b72c4a5",
"id" : "0",
"text" : "User is a software engineer",
"event" : "NONE"
}},
{{
"id" : "5b265f7e-b412-4bce-c6e3-12349b72c4a5",
"id" : "1",
"text" : "Name is John",
"event" : "ADD"
}}
Expand All @@ -112,15 +112,15 @@ def get_update_memory_messages(retrieved_old_memory_dict, response_content):
- Old Memory:
[
{{
"id" : "f38b689d-6b24-45b7-bced-17fbb4d8bac7",
"id" : "0",
"text" : "I really like cheese pizza"
}},
{{
"id" : "0a14d8f0-e364-4f5c-b305-10da1f0d0878",
"id" : "1",
"text" : "User is a software engineer"
}},
{{
"id" : "b4229775-d860-4ccb-983f-0f628ca112f5",
"id" : "2",
"text" : "User likes to play cricket"
}}
]
Expand All @@ -129,20 +129,20 @@ def get_update_memory_messages(retrieved_old_memory_dict, response_content):
{{
"memory" : [
{{
"id" : "f38b689d-6b24-45b7-bced-17fbb4d8bac7",
"id" : "0",
"text" : "Loves cheese and chicken pizza",
"event" : "UPDATE",
"old_memory" : "I really like cheese pizza"
}},
{{
"id" : "0a14d8f0-e364-4f5c-b305-10da1f0d0878",
"id" : "1",
"text" : "User is a software engineer",
"event" : "NONE"
}},
{{
"id" : "b4229775-d860-4ccb-983f-0f628ca112f5",
"id" : "2",
"text" : "Loves to play cricket with friends",
"event" : "UPDATE"
"event" : "UPDATE",
"old_memory" : "User likes to play cricket"
}}
]
Expand All @@ -155,11 +155,11 @@ def get_update_memory_messages(retrieved_old_memory_dict, response_content):
- Old Memory:
[
{{
"id" : "df1aca24-76cf-4b92-9f58-d03857efcb64",
"id" : "0",
"text" : "Name is John"
}},
{{
"id" : "b4229775-d860-4ccb-983f-0f628ca112f5",
"id" : "1",
"text" : "Loves cheese pizza"
}}
]
Expand All @@ -168,12 +168,12 @@ def get_update_memory_messages(retrieved_old_memory_dict, response_content):
{{
"memory" : [
{{
"id" : "df1aca24-76cf-4b92-9f58-d03857efcb64",
"id" : "0",
"text" : "Name is John",
"event" : "NONE"
}},
{{
"id" : "b4229775-d860-4ccb-983f-0f628ca112f5",
"id" : "1",
"text" : "Loves cheese pizza",
"event" : "DELETE"
}}
Expand All @@ -185,11 +185,11 @@ def get_update_memory_messages(retrieved_old_memory_dict, response_content):
- Old Memory:
[
{{
"id" : "06d8df63-7bd2-4fad-9acb-60871bcecee0",
"id" : "0",
"text" : "Name is John"
}},
{{
"id" : "c190ab1a-a2f1-4f6f-914a-495e9a16b76e",
"id" : "1",
"text" : "Loves cheese pizza"
}}
]
Expand All @@ -198,12 +198,12 @@ def get_update_memory_messages(retrieved_old_memory_dict, response_content):
{{
"memory" : [
{{
"id" : "06d8df63-7bd2-4fad-9acb-60871bcecee0",
"id" : "0",
"text" : "Name is John",
"event" : "NONE"
}},
{{
"id" : "c190ab1a-a2f1-4f6f-914a-495e9a16b76e",
"id" : "1",
"text" : "Loves cheese pizza",
"event" : "NONE"
}}
Expand Down
15 changes: 11 additions & 4 deletions mem0/memory/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,14 @@ def _add_to_vector_store(self, messages, metadata, filters):

logging.info(f"Total existing memories: {len(retrieved_old_memory)}")

# mapping UUIDs with integers for handling UUID hallucinations
temp_uuid_mapping = {}
for idx, item in enumerate(retrieved_old_memory):
temp_uuid_mapping[str(idx)] = item["id"]
retrieved_old_memory[idx]["id"] = str(idx)

function_calling_prompt = get_update_memory_messages(retrieved_old_memory, new_retrieved_facts)

new_memories_with_actions = self.llm.generate_response(
messages=[{"role": "user", "content": function_calling_prompt}],
response_format={"type": "json_object"},
Expand All @@ -197,24 +204,24 @@ def _add_to_vector_store(self, messages, metadata, filters):
)
elif resp["event"] == "UPDATE":
self._update_memory(
memory_id=resp["id"],
memory_id=temp_uuid_mapping[resp["id"]],
data=resp["text"],
existing_embeddings=new_message_embeddings,
metadata=metadata,
)
returned_memories.append(
{
"id": resp["id"],
"id": temp_uuid_mapping[resp["id"]],
"memory": resp["text"],
"event": resp["event"],
"previous_memory": resp["old_memory"],
}
)
elif resp["event"] == "DELETE":
self._delete_memory(memory_id=resp["id"])
self._delete_memory(memory_id=temp_uuid_mapping[resp["id"]])
returned_memories.append(
{
"id": resp["id"],
"id": temp_uuid_mapping[resp["id"]],
"memory": resp["text"],
"event": resp["event"],
}
Expand Down
Loading