-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path21_temporal_graph.py
More file actions
137 lines (111 loc) · 4.26 KB
/
21_temporal_graph.py
File metadata and controls
137 lines (111 loc) · 4.26 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python3
"""
SochDB Python SDK - Example: Temporal Graph Operations (PREVIEW)
NOTE: This feature requires SochDB Server 0.3.5+
The temporal graph RPC methods are defined in proto but may not be
fully implemented in earlier server versions.
Temporal graphs allow you to query "What did the system know at time T?"
This is essential for agent memory systems that need to reason about
state changes over time.
Requires: SochDB gRPC server running on localhost:50051
Start with: cargo run -p sochdb-grpc --release
"""
import time
from sochdb import SochDBClient
def main():
print("=" * 60)
print("SochDB - Temporal Graph Example")
print("=" * 60)
# Connect to server
client = SochDBClient("localhost:50051")
namespace = "smart_home"
# Current time in milliseconds
now = int(time.time() * 1000)
one_hour = 60 * 60 * 1000
print("\n--- Creating Temporal Edges ---")
# Record: Door was opened at 10:00, closed at 11:00
client.add_temporal_edge(
namespace=namespace,
from_id="door_front",
edge_type="STATE",
to_id="open",
valid_from=now - (2 * one_hour), # 2 hours ago
valid_until=now - one_hour, # 1 hour ago
properties={"sensor": "motion_1"}
)
print("✓ Added: door_front was OPEN from 2 hours ago to 1 hour ago")
# Record: Door was closed at 11:00 (still closed)
client.add_temporal_edge(
namespace=namespace,
from_id="door_front",
edge_type="STATE",
to_id="closed",
valid_from=now - one_hour, # 1 hour ago
valid_until=0, # No expiry (still valid)
properties={"sensor": "motion_1"}
)
print("✓ Added: door_front is CLOSED since 1 hour ago")
# Record: Light was on from 10:30 to 11:30
client.add_temporal_edge(
namespace=namespace,
from_id="light_living",
edge_type="STATE",
to_id="on",
valid_from=now - int(1.5 * one_hour),
valid_until=now - int(0.5 * one_hour),
properties={"brightness": "100"}
)
print("✓ Added: light_living was ON from 1.5 hours ago to 0.5 hours ago")
print("\n--- Querying: POINT_IN_TIME ---")
print('Query: "Was the door open 1.5 hours ago?"')
# Query door state 1.5 hours ago
edges = client.query_temporal_graph(
namespace=namespace,
node_id="door_front",
mode="POINT_IN_TIME",
timestamp=now - int(1.5 * one_hour)
)
for edge in edges:
print(f" → {edge.from_id} --[{edge.edge_type}]--> {edge.to_id}")
if any(e.to_id == "open" for e in edges):
print(" Answer: Yes, the door was OPEN")
else:
print(" Answer: No, the door was CLOSED")
print("\n--- Querying: CURRENT ---")
print('Query: "What is the current door state?"')
edges = client.query_temporal_graph(
namespace=namespace,
node_id="door_front",
mode="CURRENT"
)
for edge in edges:
print(f" → {edge.from_id} --[{edge.edge_type}]--> {edge.to_id}")
print("\n--- Querying: RANGE ---")
print('Query: "What changed in the last 3 hours?"')
edges = client.query_temporal_graph(
namespace=namespace,
node_id="door_front",
mode="RANGE",
start_time=now - (3 * one_hour),
end_time=now
)
print(f" Found {len(edges)} state changes:")
for edge in edges:
valid_from_str = time.strftime('%H:%M', time.localtime(edge.valid_from / 1000))
valid_until_str = "now" if edge.valid_until == 0 else time.strftime('%H:%M', time.localtime(edge.valid_until / 1000))
print(f" → {edge.to_id.upper()} from {valid_from_str} to {valid_until_str}")
print("\n--- Use Case: Agent Time-Travel Memory ---")
print("""
Temporal graphs enable agents to answer questions like:
- "Was the door open when the alarm went off?"
- "Who was in the room between 2pm and 3pm?"
- "What sensors triggered before the incident?"
This is critical for:
- Debugging agent behavior
- Auditing decisions
- Explaining reasoning at a specific point in time
""")
client.close()
print("✅ Temporal graph example complete!")
if __name__ == "__main__":
main()