MCPcopy Create free account
hub / github.com/MemTensor/MemOS / example_shared_db

Function example_shared_db

examples/basic_modules/neo4j_example.py:293–382  ·  view source on GitHub ↗

Example: Single(Shared)-DB multi-tenant (logical isolation) Multiple users' data in the same Neo4j DB with user_name as a tag.

(db_name: str = "shared-traval-group")

Source from the content-addressed store, hash-verified

291
292
293def example_shared_db(db_name: str = "shared-traval-group"):
294 """
295 Example: Single(Shared)-DB multi-tenant (logical isolation)
296 Multiple users' data in the same Neo4j DB with user_name as a tag.
297 """
298 # users
299 user_list = ["travel_member_alice", "travel_member_bob"]
300
301 for user_name in user_list:
302 # Step 1: Build factory config
303 config = GraphDBConfigFactory(
304 backend="neo4j",
305 config={
306 "uri": NEO4J_URI,
307 "user": NEO4J_USER,
308 "password": NEO4J_PASSWORD,
309 "db_name": db_name,
310 "user_name": user_name,
311 "use_multi_db": False,
312 "auto_create": True,
313 "embedding_dimension": EMBEDDING_DIMENSION,
314 },
315 )
316 # Step 2: Instantiate graph store
317 graph = GraphStoreFactory.from_config(config)
318 print(f"\n[INFO] Working in shared DB: {db_name}, for user: {user_name}")
319 graph.clear()
320
321 # Step 3: Create topic node
322 topic = TextualMemoryItem(
323 memory=f"Travel notes for {user_name}",
324 metadata=TreeNodeTextualMemoryMetadata(
325 memory_type="LongTermMemory",
326 hierarchy_level="topic",
327 status="activated",
328 visibility="public",
329 embedding=embed_memory_item(f"Travel notes for {user_name}"),
330 ),
331 )
332
333 graph.add_node(
334 id=topic.id, memory=topic.memory, metadata=topic.metadata.model_dump(exclude_none=True)
335 )
336
337 # Step 4: Add a concept for each user
338 concept = TextualMemoryItem(
339 memory=f"Itinerary plan for {user_name}",
340 metadata=TreeNodeTextualMemoryMetadata(
341 memory_type="LongTermMemory",
342 hierarchy_level="concept",
343 status="activated",
344 visibility="public",
345 embedding=embed_memory_item(f"Itinerary plan for {user_name}"),
346 ),
347 )
348
349 graph.add_node(
350 id=concept.id,

Callers 1

neo4j_example.pyFile · 0.85

Calls 12

TextualMemoryItemClass · 0.90
embed_memory_itemFunction · 0.85
model_dumpMethod · 0.80
clearMethod · 0.65
from_configMethod · 0.45
add_nodeMethod · 0.45
add_edgeMethod · 0.45
export_graphMethod · 0.45
search_by_embeddingMethod · 0.45
get_nodeMethod · 0.45

Tested by

no test coverage detected