graphiti-core 0.18.8__py3-none-any.whl → 0.19.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of graphiti-core might be problematic. Click here for more details.
- graphiti_core/driver/driver.py +4 -0
- graphiti_core/driver/falkordb_driver.py +3 -14
- graphiti_core/driver/kuzu_driver.py +175 -0
- graphiti_core/driver/neptune_driver.py +301 -0
- graphiti_core/edges.py +155 -62
- graphiti_core/graph_queries.py +31 -2
- graphiti_core/graphiti.py +6 -1
- graphiti_core/helpers.py +8 -8
- graphiti_core/llm_client/config.py +1 -1
- graphiti_core/llm_client/openai_base_client.py +15 -5
- graphiti_core/llm_client/openai_client.py +16 -6
- graphiti_core/migrations/__init__.py +0 -0
- graphiti_core/migrations/neo4j_node_group_labels.py +114 -0
- graphiti_core/models/edges/edge_db_queries.py +205 -76
- graphiti_core/models/nodes/node_db_queries.py +253 -74
- graphiti_core/nodes.py +271 -98
- graphiti_core/prompts/extract_edges.py +1 -0
- graphiti_core/prompts/extract_nodes.py +1 -1
- graphiti_core/search/search.py +42 -12
- graphiti_core/search/search_config.py +4 -0
- graphiti_core/search/search_filters.py +35 -22
- graphiti_core/search/search_utils.py +1329 -392
- graphiti_core/utils/bulk_utils.py +50 -15
- graphiti_core/utils/datetime_utils.py +13 -0
- graphiti_core/utils/maintenance/community_operations.py +39 -32
- graphiti_core/utils/maintenance/edge_operations.py +47 -13
- graphiti_core/utils/maintenance/graph_data_operations.py +100 -15
- graphiti_core/utils/maintenance/node_operations.py +7 -3
- {graphiti_core-0.18.8.dist-info → graphiti_core-0.19.0.dist-info}/METADATA +87 -13
- {graphiti_core-0.18.8.dist-info → graphiti_core-0.19.0.dist-info}/RECORD +32 -28
- {graphiti_core-0.18.8.dist-info → graphiti_core-0.19.0.dist-info}/WHEEL +0 -0
- {graphiti_core-0.18.8.dist-info → graphiti_core-0.19.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -18,23 +18,104 @@ from typing import Any
|
|
|
18
18
|
|
|
19
19
|
from graphiti_core.driver.driver import GraphProvider
|
|
20
20
|
|
|
21
|
-
EPISODIC_NODE_SAVE = """
|
|
22
|
-
MERGE (n:Episodic {uuid: $uuid})
|
|
23
|
-
SET n = {uuid: $uuid, name: $name, group_id: $group_id, source_description: $source_description, source: $source, content: $content,
|
|
24
|
-
entity_edges: $entity_edges, created_at: $created_at, valid_at: $valid_at}
|
|
25
|
-
RETURN n.uuid AS uuid
|
|
26
|
-
"""
|
|
27
21
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
22
|
+
def get_episode_node_save_query(provider: GraphProvider) -> str:
|
|
23
|
+
match provider:
|
|
24
|
+
case GraphProvider.NEPTUNE:
|
|
25
|
+
return """
|
|
26
|
+
MERGE (n:Episodic {uuid: $uuid})
|
|
27
|
+
SET n = {uuid: $uuid, name: $name, group_id: $group_id, source_description: $source_description, source: $source, content: $content,
|
|
28
|
+
entity_edges: join([x IN coalesce($entity_edges, []) | toString(x) ], '|'), created_at: $created_at, valid_at: $valid_at}
|
|
29
|
+
RETURN n.uuid AS uuid
|
|
30
|
+
"""
|
|
31
|
+
case GraphProvider.KUZU:
|
|
32
|
+
return """
|
|
33
|
+
MERGE (n:Episodic {uuid: $uuid})
|
|
34
|
+
SET
|
|
35
|
+
n.name = $name,
|
|
36
|
+
n.group_id = $group_id,
|
|
37
|
+
n.created_at = $created_at,
|
|
38
|
+
n.source = $source,
|
|
39
|
+
n.source_description = $source_description,
|
|
40
|
+
n.content = $content,
|
|
41
|
+
n.valid_at = $valid_at,
|
|
42
|
+
n.entity_edges = $entity_edges
|
|
43
|
+
RETURN n.uuid AS uuid
|
|
44
|
+
"""
|
|
45
|
+
case GraphProvider.FALKORDB:
|
|
46
|
+
return """
|
|
47
|
+
MERGE (n:Episodic {uuid: $uuid})
|
|
48
|
+
SET n = {uuid: $uuid, name: $name, group_id: $group_id, source_description: $source_description, source: $source, content: $content,
|
|
49
|
+
entity_edges: $entity_edges, created_at: $created_at, valid_at: $valid_at}
|
|
50
|
+
RETURN n.uuid AS uuid
|
|
51
|
+
"""
|
|
52
|
+
case _: # Neo4j
|
|
53
|
+
return """
|
|
54
|
+
MERGE (n:Episodic {uuid: $uuid})
|
|
55
|
+
SET n:$($group_label)
|
|
56
|
+
SET n = {uuid: $uuid, name: $name, group_id: $group_id, source_description: $source_description, source: $source, content: $content,
|
|
57
|
+
entity_edges: $entity_edges, created_at: $created_at, valid_at: $valid_at}
|
|
58
|
+
RETURN n.uuid AS uuid
|
|
59
|
+
"""
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def get_episode_node_save_bulk_query(provider: GraphProvider) -> str:
|
|
63
|
+
match provider:
|
|
64
|
+
case GraphProvider.NEPTUNE:
|
|
65
|
+
return """
|
|
66
|
+
UNWIND $episodes AS episode
|
|
67
|
+
MERGE (n:Episodic {uuid: episode.uuid})
|
|
68
|
+
SET n = {uuid: episode.uuid, name: episode.name, group_id: episode.group_id, source_description: episode.source_description,
|
|
69
|
+
source: episode.source, content: episode.content,
|
|
70
|
+
entity_edges: join([x IN coalesce(episode.entity_edges, []) | toString(x) ], '|'), created_at: episode.created_at, valid_at: episode.valid_at}
|
|
71
|
+
RETURN n.uuid AS uuid
|
|
72
|
+
"""
|
|
73
|
+
case GraphProvider.KUZU:
|
|
74
|
+
return """
|
|
75
|
+
MERGE (n:Episodic {uuid: $uuid})
|
|
76
|
+
SET
|
|
77
|
+
n.name = $name,
|
|
78
|
+
n.group_id = $group_id,
|
|
79
|
+
n.created_at = $created_at,
|
|
80
|
+
n.source = $source,
|
|
81
|
+
n.source_description = $source_description,
|
|
82
|
+
n.content = $content,
|
|
83
|
+
n.valid_at = $valid_at,
|
|
84
|
+
n.entity_edges = $entity_edges
|
|
85
|
+
RETURN n.uuid AS uuid
|
|
86
|
+
"""
|
|
87
|
+
case GraphProvider.FALKORDB:
|
|
88
|
+
return """
|
|
89
|
+
UNWIND $episodes AS episode
|
|
90
|
+
MERGE (n:Episodic {uuid: episode.uuid})
|
|
91
|
+
SET n = {uuid: episode.uuid, name: episode.name, group_id: episode.group_id, source_description: episode.source_description, source: episode.source, content: episode.content,
|
|
92
|
+
entity_edges: episode.entity_edges, created_at: episode.created_at, valid_at: episode.valid_at}
|
|
93
|
+
RETURN n.uuid AS uuid
|
|
94
|
+
"""
|
|
95
|
+
case _: # Neo4j
|
|
96
|
+
return """
|
|
97
|
+
UNWIND $episodes AS episode
|
|
98
|
+
MERGE (n:Episodic {uuid: episode.uuid})
|
|
99
|
+
SET n:$(episode.group_label)
|
|
100
|
+
SET n = {uuid: episode.uuid, name: episode.name, group_id: episode.group_id, source_description: episode.source_description, source: episode.source, content: episode.content,
|
|
101
|
+
entity_edges: episode.entity_edges, created_at: episode.created_at, valid_at: episode.valid_at}
|
|
102
|
+
RETURN n.uuid AS uuid
|
|
103
|
+
"""
|
|
104
|
+
|
|
36
105
|
|
|
37
106
|
EPISODIC_NODE_RETURN = """
|
|
107
|
+
e.uuid AS uuid,
|
|
108
|
+
e.name AS name,
|
|
109
|
+
e.group_id AS group_id,
|
|
110
|
+
e.created_at AS created_at,
|
|
111
|
+
e.source AS source,
|
|
112
|
+
e.source_description AS source_description,
|
|
113
|
+
e.content AS content,
|
|
114
|
+
e.valid_at AS valid_at,
|
|
115
|
+
e.entity_edges AS entity_edges
|
|
116
|
+
"""
|
|
117
|
+
|
|
118
|
+
EPISODIC_NODE_RETURN_NEPTUNE = """
|
|
38
119
|
e.content AS content,
|
|
39
120
|
e.created_at AS created_at,
|
|
40
121
|
e.valid_at AS valid_at,
|
|
@@ -43,90 +124,188 @@ EPISODIC_NODE_RETURN = """
|
|
|
43
124
|
e.group_id AS group_id,
|
|
44
125
|
e.source_description AS source_description,
|
|
45
126
|
e.source AS source,
|
|
46
|
-
e.entity_edges AS entity_edges
|
|
127
|
+
split(e.entity_edges, ",") AS entity_edges
|
|
47
128
|
"""
|
|
48
129
|
|
|
49
130
|
|
|
50
131
|
def get_entity_node_save_query(provider: GraphProvider, labels: str) -> str:
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
132
|
+
match provider:
|
|
133
|
+
case GraphProvider.FALKORDB:
|
|
134
|
+
return f"""
|
|
135
|
+
MERGE (n:Entity {{uuid: $entity_data.uuid}})
|
|
136
|
+
SET n:{labels}
|
|
137
|
+
SET n = $entity_data
|
|
138
|
+
RETURN n.uuid AS uuid
|
|
139
|
+
"""
|
|
140
|
+
case GraphProvider.KUZU:
|
|
141
|
+
return """
|
|
142
|
+
MERGE (n:Entity {uuid: $uuid})
|
|
143
|
+
SET
|
|
144
|
+
n.name = $name,
|
|
145
|
+
n.group_id = $group_id,
|
|
146
|
+
n.labels = $labels,
|
|
147
|
+
n.created_at = $created_at,
|
|
148
|
+
n.name_embedding = $name_embedding,
|
|
149
|
+
n.summary = $summary,
|
|
150
|
+
n.attributes = $attributes
|
|
151
|
+
WITH n
|
|
152
|
+
RETURN n.uuid AS uuid
|
|
153
|
+
"""
|
|
154
|
+
case GraphProvider.NEPTUNE:
|
|
155
|
+
label_subquery = ''
|
|
156
|
+
for label in labels.split(':'):
|
|
157
|
+
label_subquery += f' SET n:{label}\n'
|
|
158
|
+
return f"""
|
|
159
|
+
MERGE (n:Entity {{uuid: $entity_data.uuid}})
|
|
160
|
+
{label_subquery}
|
|
161
|
+
SET n = removeKeyFromMap(removeKeyFromMap($entity_data, "labels"), "name_embedding")
|
|
162
|
+
SET n.name_embedding = join([x IN coalesce($entity_data.name_embedding, []) | toString(x) ], ",")
|
|
163
|
+
RETURN n.uuid AS uuid
|
|
164
|
+
"""
|
|
165
|
+
case _:
|
|
166
|
+
return f"""
|
|
167
|
+
MERGE (n:Entity {{uuid: $entity_data.uuid}})
|
|
168
|
+
SET n:{labels}
|
|
169
|
+
SET n = $entity_data
|
|
170
|
+
WITH n CALL db.create.setNodeVectorProperty(n, "name_embedding", $entity_data.name_embedding)
|
|
171
|
+
RETURN n.uuid AS uuid
|
|
172
|
+
"""
|
|
66
173
|
|
|
67
174
|
|
|
68
175
|
def get_entity_node_save_bulk_query(provider: GraphProvider, nodes: list[dict]) -> str | Any:
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
for
|
|
176
|
+
match provider:
|
|
177
|
+
case GraphProvider.FALKORDB:
|
|
178
|
+
queries = []
|
|
179
|
+
for node in nodes:
|
|
180
|
+
for label in node['labels']:
|
|
181
|
+
queries.append(
|
|
182
|
+
(
|
|
183
|
+
f"""
|
|
184
|
+
UNWIND $nodes AS node
|
|
185
|
+
MERGE (n:Entity {{uuid: node.uuid}})
|
|
186
|
+
SET n:{label}
|
|
187
|
+
SET n = node
|
|
188
|
+
WITH n, node
|
|
189
|
+
SET n.name_embedding = vecf32(node.name_embedding)
|
|
190
|
+
RETURN n.uuid AS uuid
|
|
191
|
+
""",
|
|
192
|
+
{'nodes': [node]},
|
|
193
|
+
)
|
|
194
|
+
)
|
|
195
|
+
return queries
|
|
196
|
+
case GraphProvider.NEPTUNE:
|
|
197
|
+
queries = []
|
|
198
|
+
for node in nodes:
|
|
199
|
+
labels = ''
|
|
200
|
+
for label in node['labels']:
|
|
201
|
+
labels += f' SET n:{label}\n'
|
|
73
202
|
queries.append(
|
|
74
|
-
|
|
75
|
-
f"""
|
|
203
|
+
f"""
|
|
76
204
|
UNWIND $nodes AS node
|
|
77
205
|
MERGE (n:Entity {{uuid: node.uuid}})
|
|
78
|
-
|
|
79
|
-
SET n = node
|
|
80
|
-
|
|
81
|
-
SET n.name_embedding = vecf32(node.name_embedding)
|
|
206
|
+
{labels}
|
|
207
|
+
SET n = removeKeyFromMap(removeKeyFromMap(node, "labels"), "name_embedding")
|
|
208
|
+
SET n.name_embedding = join([x IN coalesce(node.name_embedding, []) | toString(x) ], ",")
|
|
82
209
|
RETURN n.uuid AS uuid
|
|
83
|
-
|
|
84
|
-
{'nodes': [node]},
|
|
85
|
-
)
|
|
210
|
+
"""
|
|
86
211
|
)
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
212
|
+
return queries
|
|
213
|
+
case GraphProvider.KUZU:
|
|
214
|
+
return """
|
|
215
|
+
MERGE (n:Entity {uuid: $uuid})
|
|
216
|
+
SET
|
|
217
|
+
n.name = $name,
|
|
218
|
+
n.group_id = $group_id,
|
|
219
|
+
n.labels = $labels,
|
|
220
|
+
n.created_at = $created_at,
|
|
221
|
+
n.name_embedding = $name_embedding,
|
|
222
|
+
n.summary = $summary,
|
|
223
|
+
n.attributes = $attributes
|
|
224
|
+
RETURN n.uuid AS uuid
|
|
225
|
+
"""
|
|
226
|
+
case _: # Neo4j
|
|
227
|
+
return """
|
|
228
|
+
UNWIND $nodes AS node
|
|
229
|
+
MERGE (n:Entity {uuid: node.uuid})
|
|
230
|
+
SET n:$(node.labels)
|
|
231
|
+
SET n = node
|
|
232
|
+
WITH n, node CALL db.create.setNodeVectorProperty(n, "name_embedding", node.name_embedding)
|
|
233
|
+
RETURN n.uuid AS uuid
|
|
234
|
+
"""
|
|
97
235
|
|
|
98
236
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
n.group_id AS group_id,
|
|
103
|
-
n.created_at AS created_at,
|
|
104
|
-
n.summary AS summary,
|
|
105
|
-
labels(n) AS labels,
|
|
106
|
-
properties(n) AS attributes
|
|
107
|
-
"""
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
def get_community_node_save_query(provider: GraphProvider) -> str:
|
|
111
|
-
if provider == GraphProvider.FALKORDB:
|
|
237
|
+
def get_entity_node_return_query(provider: GraphProvider) -> str:
|
|
238
|
+
# `name_embedding` is not returned by default and must be loaded manually using `load_name_embedding()`.
|
|
239
|
+
if provider == GraphProvider.KUZU:
|
|
112
240
|
return """
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
241
|
+
n.uuid AS uuid,
|
|
242
|
+
n.name AS name,
|
|
243
|
+
n.group_id AS group_id,
|
|
244
|
+
n.labels AS labels,
|
|
245
|
+
n.created_at AS created_at,
|
|
246
|
+
n.summary AS summary,
|
|
247
|
+
n.attributes AS attributes
|
|
116
248
|
"""
|
|
117
249
|
|
|
118
250
|
return """
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
251
|
+
n.uuid AS uuid,
|
|
252
|
+
n.name AS name,
|
|
253
|
+
n.group_id AS group_id,
|
|
254
|
+
n.created_at AS created_at,
|
|
255
|
+
n.summary AS summary,
|
|
256
|
+
labels(n) AS labels,
|
|
257
|
+
properties(n) AS attributes
|
|
123
258
|
"""
|
|
124
259
|
|
|
125
260
|
|
|
261
|
+
def get_community_node_save_query(provider: GraphProvider) -> str:
|
|
262
|
+
match provider:
|
|
263
|
+
case GraphProvider.FALKORDB:
|
|
264
|
+
return """
|
|
265
|
+
MERGE (n:Community {uuid: $uuid})
|
|
266
|
+
SET n = {uuid: $uuid, name: $name, group_id: $group_id, summary: $summary, created_at: $created_at, name_embedding: vecf32($name_embedding)}
|
|
267
|
+
RETURN n.uuid AS uuid
|
|
268
|
+
"""
|
|
269
|
+
case GraphProvider.NEPTUNE:
|
|
270
|
+
return """
|
|
271
|
+
MERGE (n:Community {uuid: $uuid})
|
|
272
|
+
SET n = {uuid: $uuid, name: $name, group_id: $group_id, summary: $summary, created_at: $created_at}
|
|
273
|
+
SET n.name_embedding = join([x IN coalesce($name_embedding, []) | toString(x) ], ",")
|
|
274
|
+
RETURN n.uuid AS uuid
|
|
275
|
+
"""
|
|
276
|
+
case GraphProvider.KUZU:
|
|
277
|
+
return """
|
|
278
|
+
MERGE (n:Community {uuid: $uuid})
|
|
279
|
+
SET
|
|
280
|
+
n.name = $name,
|
|
281
|
+
n.group_id = $group_id,
|
|
282
|
+
n.created_at = $created_at,
|
|
283
|
+
n.name_embedding = $name_embedding,
|
|
284
|
+
n.summary = $summary
|
|
285
|
+
RETURN n.uuid AS uuid
|
|
286
|
+
"""
|
|
287
|
+
case _: # Neo4j
|
|
288
|
+
return """
|
|
289
|
+
MERGE (n:Community {uuid: $uuid})
|
|
290
|
+
SET n = {uuid: $uuid, name: $name, group_id: $group_id, summary: $summary, created_at: $created_at}
|
|
291
|
+
WITH n CALL db.create.setNodeVectorProperty(n, "name_embedding", $name_embedding)
|
|
292
|
+
RETURN n.uuid AS uuid
|
|
293
|
+
"""
|
|
294
|
+
|
|
295
|
+
|
|
126
296
|
COMMUNITY_NODE_RETURN = """
|
|
297
|
+
c.uuid AS uuid,
|
|
298
|
+
c.name AS name,
|
|
299
|
+
c.group_id AS group_id,
|
|
300
|
+
c.created_at AS created_at,
|
|
301
|
+
c.name_embedding AS name_embedding,
|
|
302
|
+
c.summary AS summary
|
|
303
|
+
"""
|
|
304
|
+
|
|
305
|
+
COMMUNITY_NODE_RETURN_NEPTUNE = """
|
|
127
306
|
n.uuid AS uuid,
|
|
128
307
|
n.name AS name,
|
|
129
|
-
n.name_embedding AS name_embedding,
|
|
308
|
+
[x IN split(n.name_embedding, ",") | toFloat(x)] AS name_embedding,
|
|
130
309
|
n.group_id AS group_id,
|
|
131
310
|
n.summary AS summary,
|
|
132
311
|
n.created_at AS created_at
|