Filtering by user_id in graph memory search results does not work
🐛 Describe the bug
The vector memory search result (memories) is filtered by user_id, but the graph memory search result (entities) is not filtered by user_id, is this correct behavior? I expected graph memory search results to be filtered by user_id as well as vector memory ones.
For example, I executed the following code using vector memory and graph memory.
from mem0 import Memory
import os
neo4j_username = os.environ.get("NEO4J_USERNAME")
neo4j_password = os.environ.get("NEO4J_PASSWORD")
neo4j_url = os.environ.get("NEO4J_URL")
qdrant_url = os.environ.get("QDRANT_URL")
mem0_config = {
"vector_store": {
"provider": "qdrant",
"config": {
"collection_name": "knowledge",
"url": qdrant_url,
}
},
"graph_store": {
"provider": "neo4j",
"config": {
"url": neo4j_url,
"username": neo4j_username,
"password": neo4j_password
}
},
"version": "v1.1"
}
m = Memory.from_config(config_dict=mem0_config)
m.add("Charlie likes apples.", user_id="alice")
m.add("Charlie likes baseball.", user_id="bob")
memories_alice = m.search("What dose Charlie like?", user_id="alice")
memories_bob = m.search("What dose Charlie like?", user_id="bob")
print(f"Alice: {memories_alice}\n")
print(f"Bob: {memories_bob}\n")
The execution result is as follows.
Alice: {'memories': [{'id': '308f199f-60c8-451e-a7a3-ae7113d47c06', 'memory': 'Likes apples', 'hash': '7bd8bc77c25090d92299e49beb3ef9cc', 'metadata': None, 'score': 0.25869718, 'created_at': '2024-09-03T21:00:42.388677-07:00', 'updated_at': '2024-09-03T21:03:58.727110-07:00', 'user_id': 'alice'}], 'entities': [{'source': 'charlie', 'relation': 'likes', 'destination': 'apples'}, {'source': 'charlie', 'relation': 'likes', 'destination': 'baseball'}]}
Bob: {'memories': [{'id': '85d68144-cfe6-4e4a-8121-54c0694ce2ab', 'memory': 'Likes baseball', 'hash': 'fdde4363cbbeee916d9c25e63a49e8b9', 'metadata': None, 'score': 0.29662642, 'created_at': '2024-09-03T21:00:37.055906-07:00', 'updated_at': '2024-09-03T21:04:06.556679-07:00', 'user_id': 'bob'}], 'entities': [{'source': 'charlie', 'relation': 'likes', 'destination': 'apples'}, {'source': 'charlie', 'relation': 'likes', 'destination': 'baseball'}]}
Here are the results I expect.
Alice: {'memories': [{'id': '308f199f-60c8-451e-a7a3-ae7113d47c06', 'memory': 'Likes apples', 'hash': '7bd8bc77c25090d92299e49beb3ef9cc', 'metadata': None, 'score': 0.25869718, 'created_at': '2024-09-03T21:00:42.388677-07:00', 'updated_at': '2024-09-03T21:03:58.727110-07:00', 'user_id': 'alice'}], 'entities': [{'source': 'charlie', 'relation': 'likes', 'destination': 'apples'}]}
Bob: {'memories': [{'id': '85d68144-cfe6-4e4a-8121-54c0694ce2ab', 'memory': 'Likes baseball', 'hash': 'fdde4363cbbeee916d9c25e63a49e8b9', 'metadata': None, 'score': 0.29662642, 'created_at': '2024-09-03T21:00:37.055906-07:00', 'updated_at': '2024-09-03T21:04:06.556679-07:00', 'user_id': 'bob'}], 'entities': [{'source': 'charlie', 'relation': 'likes', 'destination': 'baseball'}]}
Hi @nibankougen we knew about this issue and released a patch last week. Could you please upgrade your mem0 package? Let me know if you are still having this problem.
There is another problem with graph memory sometimes not registering, but the filtering by user_id was working.
Thank you very much.
Alice: {'memories': [{'id': 'aed86cb9-a214-44a8-8571-b11d69616e05', 'memory': 'Charlie likes apples', 'hash': '81ccc605a4858b2a7c9c541b015d2998', 'metadata': None, 'score': 0.5901568, 'created_at': '2024-09-04T19:19:24.997233-07:00', 'updated_at': None, 'user_id': 'alice'}], 'entities': [{'source': 'charlie', 'relation': 'likes', 'destination': 'apples'}]}
Bob: {'memories': [{'id': '2ad0d497-159c-4515-aa75-66cf2aaed456', 'memory': 'Charlie likes baseball', 'hash': 'c2f687de030c5f2ba7d3412cb5e99529', 'metadata': None, 'score': 0.59092736, 'created_at': '2024-09-04T19:19:29.395515-07:00', 'updated_at': None, 'user_id': 'bob'}], 'entities': []}
by viewing the source code,
class MemoryGraph:
def __init__(self, config):
self.config = config
self.graph = Neo4jGraph(self.config.graph_store.config.url, self.config.graph_store.config.username, self.config.graph_store.config.password)
self.embedding_model = EmbedderFactory.create(
self.config.embedder.provider, self.config.embedder.config
)
if self.config.llm.provider:
llm_provider = self.config.llm.provider
if self.config.graph_store.llm:
llm_provider = self.config.graph_store.llm.provider
else:
llm_provider = "openai_structured"
self.llm = LlmFactory.create(llm_provider, self.config.llm.config)
self.user_id = None
self.threshold = 0.7
You can see self.user_id = None When you invoke m.add and set user_id param self.user_id will be change to your set user_id, But m.search will not reset user_id
@chenm1xuexi I understand your concern. However, the user_id in the add call is used to create the source node. Why do you think resetting "user_id" matters for search?