Repository navigation
Expand file tree
/
Copy pathmemory_io.py
More file actions
76 lines (62 loc) · 2.59 KB
/
Copy pathmemory_io.py
File metadata and controls
76 lines (62 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""Add messages to the adopted graph and verify MENTIONS link to existing nodes.
Run after ``adopt.py``. The script writes a couple of messages that name
people and movies from the seed graph, then queries Neo4j to confirm
that the resulting ``MENTIONS`` edges point at the pre-existing domain
nodes — not at duplicates.
uv run examples/existing-graph/memory_io.py
"""
from __future__ import annotations
import asyncio
import os
import sys
# Allow running as a standalone script (uv run python examples/existing-graph/memory_io.py).
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from memory_settings import build_settings
from neo4j_agent_memory import MemoryClient
SESSION_ID = "existing-graph-demo"
async def write_and_verify() -> None:
settings = build_settings()
async with MemoryClient(settings) as client:
# A couple of messages that mention people/movies the domain graph
# already knows about.
await client.short_term.add_message(
SESSION_ID,
"user",
"Have you seen Inception? Bob Singh directed it.",
)
await client.short_term.add_message(
SESSION_ID,
"assistant",
"Yes, and Carol Reyes plays a brilliant linguist in Arrival.",
)
# If adoption worked, there should still be exactly one node per
# name across the entire graph — even though the messages above
# mentioned them and would have triggered a MERGE on
# (:Entity {name, type}).
rows = await client.graph.execute_read(
"""
UNWIND ['Bob Singh', 'Carol Reyes', 'Inception', 'Arrival'] AS target
MATCH (n {name: target})
WHERE n:Person OR n:Movie
RETURN target, count(n) AS count
ORDER BY target
"""
)
print("Per-name node count after writes (1 means adoption worked):")
for row in rows:
print(f" {row['target']:<14} -> {row['count']}")
# And the MENTIONS edges should connect messages to the
# pre-existing domain nodes.
rows = await client.graph.execute_read(
"""
MATCH (m:Message)-[:MENTIONS]->(e:Entity)
WHERE e.name IN ['Bob Singh', 'Carol Reyes', 'Inception', 'Arrival']
RETURN e.name AS name, labels(e) AS labels
ORDER BY name
"""
)
print("\nMENTIONS edges produced by add_message():")
for row in rows:
print(f" {row['name']:<14} -> labels={row['labels']}")
if __name__ == "__main__":
asyncio.run(write_and_verify())