-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_usage.py
More file actions
457 lines (387 loc) · 16.5 KB
/
example_usage.py
File metadata and controls
457 lines (387 loc) · 16.5 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
"""
Example usage of PostgreSQL driver for Graphiti
"""
import asyncio
import uuid
from datetime import datetime
from graphiti_postgres import PostgresDriver
async def example_basic_operations():
"""Example: Basic node and edge operations"""
# Initialize driver (adjust credentials for your setup)
driver = PostgresDriver(
host='localhost',
port=5432,
user='postgres',
password='postgres', # Change to your password
database='postgres',
group_id='example_app'
)
try:
# Wait for pool initialization
await asyncio.sleep(0.5)
# Health check
is_healthy = await driver.health_check()
print(f"Database connection healthy: {is_healthy}")
# Build indices (run once during setup)
await driver.build_indices_and_constraints(delete_existing=False)
print("Indices and constraints created")
# Create nodes
person_id = str(uuid.uuid4())
person = await driver.create_node(
uuid=person_id,
name="Alice",
node_type="entity",
properties={
"type": "person",
"age": 30,
"occupation": "Engineer"
},
summary="Alice is a software engineer who loves Python",
valid_at=datetime.now()
)
print(f"Created person node: {person['name']}")
company_id = str(uuid.uuid4())
company = await driver.create_node(
uuid=company_id,
name="TechCorp",
node_type="entity",
properties={
"type": "company",
"industry": "Technology",
"size": "Large"
},
summary="TechCorp is a leading technology company",
valid_at=datetime.now()
)
print(f"Created company node: {company['name']}")
# Create edge (relationship)
edge_id = str(uuid.uuid4())
edge = await driver.create_edge(
uuid=edge_id,
source_uuid=person_id,
target_uuid=company_id,
relation_type="WORKS_AT",
properties={
"since": "2020",
"position": "Senior Engineer"
},
fact="Alice works at TechCorp as a Senior Engineer"
)
print(f"Created edge: {person['name']} -> WORKS_AT -> {company['name']}")
# Retrieve node
retrieved_person = await driver.get_node(person_id)
print(f"\nRetrieved node: {retrieved_person['name']}")
print(f"Properties: {retrieved_person['properties']}")
# Search nodes using fulltext search
# Note: Requires pg_trgm extension and may need similarity threshold adjustment
# The search uses trigram similarity on the summary field
search_results = await driver.search_nodes(
search_term="software engineer", # More specific term for better matching
node_type="entity",
limit=5
)
print(f"\nSearch results for 'software engineer': {len(search_results)} nodes found")
if len(search_results) > 0:
for result in search_results:
print(f" - {result['name']} (similarity: {result.get('similarity', 'N/A')})")
else:
print(" (Note: pg_trgm similarity threshold may be too high. See README for details.)")
finally:
# Close connection
await driver.close()
print("\nConnection closed")
async def example_graphiti_integration():
"""Example: Using with Graphiti (pseudo-code)"""
# This is how you'd integrate with actual Graphiti library
# (assuming graphiti_core is installed)
try:
from graphiti_core import Graphiti
from graphiti_core.llm_client import OpenAIClient
from graphiti_core.embedder import OpenAIEmbedder
# Initialize PostgreSQL driver (set your credentials)
driver = PostgresDriver(
host='localhost',
port=5432,
user='postgres',
password='postgres', # Change to your password
database='postgres',
group_id='my_app'
)
# Initialize Graphiti with PostgreSQL driver
# Set your OpenAI API key in environment variable OPENAI_API_KEY
# or pass it directly: api_key="sk-..."
graphiti = Graphiti(
driver=driver,
llm_client=OpenAIClient(), # Uses OPENAI_API_KEY env var
embedder=OpenAIEmbedder() # Uses OPENAI_API_KEY env var
)
# Add episodes (events/facts)
await graphiti.add_episode(
name="meeting_notes",
episode_body="Alice met with the CEO to discuss the new product launch.",
source_description="Meeting notes from 2024-01-15"
)
# Search the knowledge graph
results = await graphiti.search(
query="What did Alice discuss with the CEO?",
num_results=5
)
print("Search results:", results)
await driver.close()
except ImportError:
print("graphiti-core not installed. Install with: pip install graphiti-core")
async def example_multi_tenancy():
"""Example: Multi-tenancy support using group_id"""
# Create driver for tenant 1 (adjust credentials for your setup)
tenant1_driver = PostgresDriver(
host='localhost',
port=5432,
user='postgres',
password='postgres', # Change to your password
database='postgres',
group_id='tenant_1'
)
# Create driver for tenant 2 (cloning)
tenant2_driver = tenant1_driver.clone(group_id='tenant_2')
try:
await asyncio.sleep(0.5) # Wait for pool init
# Create node for tenant 1
node1 = await tenant1_driver.create_node(
uuid=str(uuid.uuid4()),
name="Tenant 1 Data",
node_type="entity",
summary="This data belongs to tenant 1"
)
print(f"Created node for tenant 1: {node1['name']}")
# Create node for tenant 2
node2 = await tenant2_driver.create_node(
uuid=str(uuid.uuid4()),
name="Tenant 2 Data",
node_type="entity",
summary="This data belongs to tenant 2"
)
print(f"Created node for tenant 2: {node2['name']}")
# Each tenant's data is isolated by group_id
print("\nData isolation: Each tenant only sees their own data")
finally:
await tenant1_driver.close()
# tenant2_driver shares the pool, so no need to close separately
async def example_graph_traversal():
"""
Example: Using helper functions for graph traversal with deeper graphs
Graph structure:
Main Chain (10 nodes):
Alice -> Bob -> Charlie -> Diana -> Eve -> Frank -> Grace -> Henry -> Ivy -> Jack
| | |
v (mentors) v (manages) v (teaches)
Charlie Karen Mike
| |
v (supervises) v (mentors)
Leo Nina
|
v (advises)
Oscar
Disconnected Component (unreachable from Alice):
Patricia -> Quinn -> Rachel -> Sam -> Tina
Additional cross-connections:
- Diana -> Alice (collaborates_with)
- Eve -> Bob (reports_to)
"""
driver = PostgresDriver(
host='localhost',
port=5432,
user='postgres',
password='postgres', # Change to your password
database='postgres',
group_id='traversal_demo'
)
try:
await asyncio.sleep(0.5)
# Create a complex knowledge graph with multiple components
print("Creating a complex graph with 20 nodes...")
# Main chain (10 people)
main_chain = ["Alice", "Bob", "Charlie", "Diana", "Eve", "Frank", "Grace", "Henry", "Ivy", "Jack"]
# Branch off Diana (will be reachable)
diana_branch = ["Karen", "Leo"]
# Branch off Henry (requires deeper traversal)
henry_branch = ["Mike", "Nina", "Oscar"]
# Disconnected component (unreachable from Alice)
disconnected = ["Patricia", "Quinn", "Rachel", "Sam", "Tina"]
all_people = main_chain + diana_branch + henry_branch + disconnected
person_ids = {}
# Create all nodes
for person in all_people:
person_id = str(uuid.uuid4())
person_ids[person] = person_id
await driver.create_node(
uuid=person_id,
name=person,
node_type="entity",
properties={"type": "person"}
)
# Create main chain of KNOWS relationships
for i in range(len(main_chain) - 1):
await driver.create_edge(
uuid=str(uuid.uuid4()),
source_uuid=person_ids[main_chain[i]],
target_uuid=person_ids[main_chain[i + 1]],
relation_type="KNOWS"
)
# Create Diana's branch (depth 4 from Alice)
await driver.create_edge(
uuid=str(uuid.uuid4()),
source_uuid=person_ids["Diana"],
target_uuid=person_ids["Karen"],
relation_type="MANAGES"
)
await driver.create_edge(
uuid=str(uuid.uuid4()),
source_uuid=person_ids["Karen"],
target_uuid=person_ids["Leo"],
relation_type="SUPERVISES"
)
# Create Henry's branch (depth 8 from Alice, requires deep traversal)
await driver.create_edge(
uuid=str(uuid.uuid4()),
source_uuid=person_ids["Henry"],
target_uuid=person_ids["Mike"],
relation_type="TEACHES"
)
await driver.create_edge(
uuid=str(uuid.uuid4()),
source_uuid=person_ids["Mike"],
target_uuid=person_ids["Nina"],
relation_type="MENTORS"
)
await driver.create_edge(
uuid=str(uuid.uuid4()),
source_uuid=person_ids["Nina"],
target_uuid=person_ids["Oscar"],
relation_type="ADVISES"
)
# Add cross-connections in main chain
await driver.create_edge(
uuid=str(uuid.uuid4()),
source_uuid=person_ids["Alice"],
target_uuid=person_ids["Charlie"],
relation_type="MENTORS"
)
await driver.create_edge(
uuid=str(uuid.uuid4()),
source_uuid=person_ids["Diana"],
target_uuid=person_ids["Alice"],
relation_type="COLLABORATES_WITH"
)
await driver.create_edge(
uuid=str(uuid.uuid4()),
source_uuid=person_ids["Eve"],
target_uuid=person_ids["Bob"],
relation_type="REPORTS_TO"
)
# Create disconnected component (completely separate from Alice's network)
for i in range(len(disconnected) - 1):
await driver.create_edge(
uuid=str(uuid.uuid4()),
source_uuid=person_ids[disconnected[i]],
target_uuid=person_ids[disconnected[i + 1]],
relation_type="KNOWS"
)
total_edges = (len(main_chain) - 1) + 2 + 3 + 3 + (len(disconnected) - 1)
print(f"Created graph with {len(all_people)} nodes and {total_edges} edges")
print(f" - Main chain: {len(main_chain)} nodes")
print(f" - Diana's branch: {len(diana_branch)} nodes")
print(f" - Henry's branch: {len(henry_branch)} nodes")
print(f" - Disconnected component: {len(disconnected)} nodes (unreachable from Alice)")
print()
# Use SQL functions for traversal
async with driver.pool.acquire() as conn:
# Get Alice's direct neighbors
neighbors = await conn.fetch(
"SELECT * FROM get_node_neighbors($1, 'both')",
person_ids["Alice"]
)
print(f"Alice's direct neighbors: {len(neighbors)}")
for neighbor in neighbors:
print(f" - {neighbor['relation_type']} ({neighbor['direction']})")
# Shallow traversal (depth 2)
traversal_shallow = await conn.fetch(
"SELECT * FROM traverse_graph($1, $2)",
person_ids["Alice"],
2 # max depth
)
print(f"\nShallow traversal from Alice (depth 2): {len(traversal_shallow)} nodes reached")
depth_counts_shallow = {}
for node in traversal_shallow:
depth = node['depth']
depth_counts_shallow[depth] = depth_counts_shallow.get(depth, 0) + 1
for depth in sorted(depth_counts_shallow.keys()):
print(f" - Depth {depth}: {depth_counts_shallow[depth]} nodes")
print(f" → Only reaches main chain + immediate branches")
# Medium traversal (depth 5)
traversal_medium = await conn.fetch(
"SELECT * FROM traverse_graph($1, $2)",
person_ids["Alice"],
5 # max depth - reaches Diana's branch
)
print(f"\nMedium traversal from Alice (depth 5): {len(traversal_medium)} nodes reached")
depth_counts_medium = {}
for node in traversal_medium:
depth = node['depth']
depth_counts_medium[depth] = depth_counts_medium.get(depth, 0) + 1
for depth in sorted(depth_counts_medium.keys()):
print(f" - Depth {depth}: {depth_counts_medium[depth]} nodes")
print(f" → Reaches Diana's branch (Karen, Leo) but not Henry's branch yet")
# Deep traversal (depth 8)
traversal_deep = await conn.fetch(
"SELECT * FROM traverse_graph($1, $2)",
person_ids["Alice"],
8 # max depth - should reach most of the connected graph
)
print(f"\nDeep traversal from Alice (depth 8): {len(traversal_deep)} nodes reached")
depth_counts_deep = {}
for node in traversal_deep:
depth = node['depth']
depth_counts_deep[depth] = depth_counts_deep.get(depth, 0) + 1
for depth in sorted(depth_counts_deep.keys()):
print(f" - Depth {depth}: {depth_counts_deep[depth]} nodes")
print(f" → Reaches most connected nodes including Henry's branch")
# Very deep traversal (depth 12)
traversal_very_deep = await conn.fetch(
"SELECT * FROM traverse_graph($1, $2)",
person_ids["Alice"],
12 # max depth - reaches everything connected to Alice
)
print(f"\nVery deep traversal from Alice (depth 12): {len(traversal_very_deep)} nodes reached")
depth_counts_very_deep = {}
for node in traversal_very_deep:
depth = node['depth']
depth_counts_very_deep[depth] = depth_counts_very_deep.get(depth, 0) + 1
for depth in sorted(depth_counts_very_deep.keys()):
print(f" - Depth {depth}: {depth_counts_very_deep[depth]} nodes")
unreached = len(all_people) - len(traversal_very_deep)
print(f" → Reaches all connected nodes ({len(traversal_very_deep)}/{len(all_people)})")
print(f" → {unreached} nodes remain unreachable (disconnected component)")
print("\n✓ Demonstrates how traversal depth affects reach in the graph")
print("✓ Shows that disconnected nodes are never reached, regardless of depth")
finally:
await driver.close()
if __name__ == "__main__":
print("=" * 60)
print("PostgreSQL Driver for Graphiti - Examples")
print("=" * 60)
print("\n[1] Basic Operations")
print("-" * 60)
asyncio.run(example_basic_operations())
print("\n\n[2] Multi-Tenancy")
print("-" * 60)
asyncio.run(example_multi_tenancy())
print("\n\n[3] Graph Traversal")
print("-" * 60)
asyncio.run(example_graph_traversal())
print("\n\n[4] Graphiti Integration")
print("-" * 60)
print("See example_graphiti_integration() for integration code")
# asyncio.run(example_graphiti_integration())
print("\n" + "=" * 60)
print("Examples completed!")
print("=" * 60)