Replace UUID with indexes to reduce hallucinations (#2004)

This commit is contained in:
Dev Khant
2024-11-02 12:40:55 +05:30
committed by GitHub
parent e7cc8b9552
commit a6ac4a6698
2 changed files with 29 additions and 22 deletions

View File

@@ -79,7 +79,7 @@ def get_update_memory_messages(retrieved_old_memory_dict, response_content):
- Old Memory: - Old Memory:
[ [
{{ {{
"id" : "7f165f7e-b411-4afe-b7e5-35789b72c4a5", "id" : "0",
"text" : "User is a software engineer" "text" : "User is a software engineer"
}} }}
] ]
@@ -88,12 +88,12 @@ def get_update_memory_messages(retrieved_old_memory_dict, response_content):
{{ {{
"memory" : [ "memory" : [
{{ {{
"id" : "7f165f7e-b411-4afe-b7e5-35789b72c4a5", "id" : "0",
"text" : "User is a software engineer", "text" : "User is a software engineer",
"event" : "NONE" "event" : "NONE"
}}, }},
{{ {{
"id" : "5b265f7e-b412-4bce-c6e3-12349b72c4a5", "id" : "1",
"text" : "Name is John", "text" : "Name is John",
"event" : "ADD" "event" : "ADD"
}} }}
@@ -112,15 +112,15 @@ def get_update_memory_messages(retrieved_old_memory_dict, response_content):
- Old Memory: - Old Memory:
[ [
{{ {{
"id" : "f38b689d-6b24-45b7-bced-17fbb4d8bac7", "id" : "0",
"text" : "I really like cheese pizza" "text" : "I really like cheese pizza"
}}, }},
{{ {{
"id" : "0a14d8f0-e364-4f5c-b305-10da1f0d0878", "id" : "1",
"text" : "User is a software engineer" "text" : "User is a software engineer"
}}, }},
{{ {{
"id" : "b4229775-d860-4ccb-983f-0f628ca112f5", "id" : "2",
"text" : "User likes to play cricket" "text" : "User likes to play cricket"
}} }}
] ]
@@ -129,20 +129,20 @@ def get_update_memory_messages(retrieved_old_memory_dict, response_content):
{{ {{
"memory" : [ "memory" : [
{{ {{
"id" : "f38b689d-6b24-45b7-bced-17fbb4d8bac7", "id" : "0",
"text" : "Loves cheese and chicken pizza", "text" : "Loves cheese and chicken pizza",
"event" : "UPDATE", "event" : "UPDATE",
"old_memory" : "I really like cheese pizza" "old_memory" : "I really like cheese pizza"
}}, }},
{{ {{
"id" : "0a14d8f0-e364-4f5c-b305-10da1f0d0878", "id" : "1",
"text" : "User is a software engineer", "text" : "User is a software engineer",
"event" : "NONE" "event" : "NONE"
}}, }},
{{ {{
"id" : "b4229775-d860-4ccb-983f-0f628ca112f5", "id" : "2",
"text" : "Loves to play cricket with friends", "text" : "Loves to play cricket with friends",
"event" : "UPDATE" "event" : "UPDATE",
"old_memory" : "User likes to play cricket" "old_memory" : "User likes to play cricket"
}} }}
] ]
@@ -155,11 +155,11 @@ def get_update_memory_messages(retrieved_old_memory_dict, response_content):
- Old Memory: - Old Memory:
[ [
{{ {{
"id" : "df1aca24-76cf-4b92-9f58-d03857efcb64", "id" : "0",
"text" : "Name is John" "text" : "Name is John"
}}, }},
{{ {{
"id" : "b4229775-d860-4ccb-983f-0f628ca112f5", "id" : "1",
"text" : "Loves cheese pizza" "text" : "Loves cheese pizza"
}} }}
] ]
@@ -168,12 +168,12 @@ def get_update_memory_messages(retrieved_old_memory_dict, response_content):
{{ {{
"memory" : [ "memory" : [
{{ {{
"id" : "df1aca24-76cf-4b92-9f58-d03857efcb64", "id" : "0",
"text" : "Name is John", "text" : "Name is John",
"event" : "NONE" "event" : "NONE"
}}, }},
{{ {{
"id" : "b4229775-d860-4ccb-983f-0f628ca112f5", "id" : "1",
"text" : "Loves cheese pizza", "text" : "Loves cheese pizza",
"event" : "DELETE" "event" : "DELETE"
}} }}
@@ -185,11 +185,11 @@ def get_update_memory_messages(retrieved_old_memory_dict, response_content):
- Old Memory: - Old Memory:
[ [
{{ {{
"id" : "06d8df63-7bd2-4fad-9acb-60871bcecee0", "id" : "0",
"text" : "Name is John" "text" : "Name is John"
}}, }},
{{ {{
"id" : "c190ab1a-a2f1-4f6f-914a-495e9a16b76e", "id" : "1",
"text" : "Loves cheese pizza" "text" : "Loves cheese pizza"
}} }}
] ]
@@ -198,12 +198,12 @@ def get_update_memory_messages(retrieved_old_memory_dict, response_content):
{{ {{
"memory" : [ "memory" : [
{{ {{
"id" : "06d8df63-7bd2-4fad-9acb-60871bcecee0", "id" : "0",
"text" : "Name is John", "text" : "Name is John",
"event" : "NONE" "event" : "NONE"
}}, }},
{{ {{
"id" : "c190ab1a-a2f1-4f6f-914a-495e9a16b76e", "id" : "1",
"text" : "Loves cheese pizza", "text" : "Loves cheese pizza",
"event" : "NONE" "event" : "NONE"
}} }}

View File

@@ -172,7 +172,14 @@ class Memory(MemoryBase):
logging.info(f"Total existing memories: {len(retrieved_old_memory)}") 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) function_calling_prompt = get_update_memory_messages(retrieved_old_memory, new_retrieved_facts)
new_memories_with_actions = self.llm.generate_response( new_memories_with_actions = self.llm.generate_response(
messages=[{"role": "user", "content": function_calling_prompt}], messages=[{"role": "user", "content": function_calling_prompt}],
response_format={"type": "json_object"}, response_format={"type": "json_object"},
@@ -197,24 +204,24 @@ class Memory(MemoryBase):
) )
elif resp["event"] == "UPDATE": elif resp["event"] == "UPDATE":
self._update_memory( self._update_memory(
memory_id=resp["id"], memory_id=temp_uuid_mapping[resp["id"]],
data=resp["text"], data=resp["text"],
existing_embeddings=new_message_embeddings, existing_embeddings=new_message_embeddings,
metadata=metadata, metadata=metadata,
) )
returned_memories.append( returned_memories.append(
{ {
"id": resp["id"], "id": temp_uuid_mapping[resp["id"]],
"memory": resp["text"], "memory": resp["text"],
"event": resp["event"], "event": resp["event"],
"previous_memory": resp["old_memory"], "previous_memory": resp["old_memory"],
} }
) )
elif resp["event"] == "DELETE": elif resp["event"] == "DELETE":
self._delete_memory(memory_id=resp["id"]) self._delete_memory(memory_id=temp_uuid_mapping[resp["id"]])
returned_memories.append( returned_memories.append(
{ {
"id": resp["id"], "id": temp_uuid_mapping[resp["id"]],
"memory": resp["text"], "memory": resp["text"],
"event": resp["event"], "event": resp["event"],
} }