graphiti-core 0.12.0rc1__py3-none-any.whl → 0.24.3__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.
Files changed (68) hide show
  1. graphiti_core/cross_encoder/bge_reranker_client.py +12 -2
  2. graphiti_core/cross_encoder/gemini_reranker_client.py +161 -0
  3. graphiti_core/cross_encoder/openai_reranker_client.py +7 -5
  4. graphiti_core/decorators.py +110 -0
  5. graphiti_core/driver/__init__.py +19 -0
  6. graphiti_core/driver/driver.py +124 -0
  7. graphiti_core/driver/falkordb_driver.py +362 -0
  8. graphiti_core/driver/graph_operations/graph_operations.py +191 -0
  9. graphiti_core/driver/kuzu_driver.py +182 -0
  10. graphiti_core/driver/neo4j_driver.py +117 -0
  11. graphiti_core/driver/neptune_driver.py +305 -0
  12. graphiti_core/driver/search_interface/search_interface.py +89 -0
  13. graphiti_core/edges.py +287 -172
  14. graphiti_core/embedder/azure_openai.py +71 -0
  15. graphiti_core/embedder/client.py +2 -1
  16. graphiti_core/embedder/gemini.py +116 -22
  17. graphiti_core/embedder/voyage.py +13 -2
  18. graphiti_core/errors.py +8 -0
  19. graphiti_core/graph_queries.py +162 -0
  20. graphiti_core/graphiti.py +705 -193
  21. graphiti_core/graphiti_types.py +4 -2
  22. graphiti_core/helpers.py +87 -10
  23. graphiti_core/llm_client/__init__.py +16 -0
  24. graphiti_core/llm_client/anthropic_client.py +159 -56
  25. graphiti_core/llm_client/azure_openai_client.py +115 -0
  26. graphiti_core/llm_client/client.py +98 -21
  27. graphiti_core/llm_client/config.py +1 -1
  28. graphiti_core/llm_client/gemini_client.py +290 -41
  29. graphiti_core/llm_client/groq_client.py +14 -3
  30. graphiti_core/llm_client/openai_base_client.py +261 -0
  31. graphiti_core/llm_client/openai_client.py +56 -132
  32. graphiti_core/llm_client/openai_generic_client.py +91 -56
  33. graphiti_core/models/edges/edge_db_queries.py +259 -35
  34. graphiti_core/models/nodes/node_db_queries.py +311 -32
  35. graphiti_core/nodes.py +420 -205
  36. graphiti_core/prompts/dedupe_edges.py +46 -32
  37. graphiti_core/prompts/dedupe_nodes.py +67 -42
  38. graphiti_core/prompts/eval.py +4 -4
  39. graphiti_core/prompts/extract_edges.py +27 -16
  40. graphiti_core/prompts/extract_nodes.py +74 -31
  41. graphiti_core/prompts/prompt_helpers.py +39 -0
  42. graphiti_core/prompts/snippets.py +29 -0
  43. graphiti_core/prompts/summarize_nodes.py +23 -25
  44. graphiti_core/search/search.py +158 -82
  45. graphiti_core/search/search_config.py +39 -4
  46. graphiti_core/search/search_filters.py +126 -35
  47. graphiti_core/search/search_helpers.py +5 -6
  48. graphiti_core/search/search_utils.py +1405 -485
  49. graphiti_core/telemetry/__init__.py +9 -0
  50. graphiti_core/telemetry/telemetry.py +117 -0
  51. graphiti_core/tracer.py +193 -0
  52. graphiti_core/utils/bulk_utils.py +364 -285
  53. graphiti_core/utils/datetime_utils.py +13 -0
  54. graphiti_core/utils/maintenance/community_operations.py +67 -49
  55. graphiti_core/utils/maintenance/dedup_helpers.py +262 -0
  56. graphiti_core/utils/maintenance/edge_operations.py +339 -197
  57. graphiti_core/utils/maintenance/graph_data_operations.py +50 -114
  58. graphiti_core/utils/maintenance/node_operations.py +319 -238
  59. graphiti_core/utils/maintenance/temporal_operations.py +11 -3
  60. graphiti_core/utils/ontology_utils/entity_types_utils.py +1 -1
  61. graphiti_core/utils/text_utils.py +53 -0
  62. graphiti_core-0.24.3.dist-info/METADATA +726 -0
  63. graphiti_core-0.24.3.dist-info/RECORD +86 -0
  64. {graphiti_core-0.12.0rc1.dist-info → graphiti_core-0.24.3.dist-info}/WHEEL +1 -1
  65. graphiti_core-0.12.0rc1.dist-info/METADATA +0 -350
  66. graphiti_core-0.12.0rc1.dist-info/RECORD +0 -66
  67. /graphiti_core/{utils/maintenance/utils.py → migrations/__init__.py} +0 -0
  68. {graphiti_core-0.12.0rc1.dist-info → graphiti_core-0.24.3.dist-info/licenses}/LICENSE +0 -0
@@ -14,39 +14,318 @@ See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  """
16
16
 
17
- EPISODIC_NODE_SAVE = """
18
- MERGE (n:Episodic {uuid: $uuid})
19
- SET n = {uuid: $uuid, name: $name, group_id: $group_id, source_description: $source_description, source: $source, content: $content,
20
- entity_edges: $entity_edges, created_at: $created_at, valid_at: $valid_at}
21
- RETURN n.uuid AS uuid"""
22
-
23
- EPISODIC_NODE_SAVE_BULK = """
24
- UNWIND $episodes AS episode
25
- MERGE (n:Episodic {uuid: episode.uuid})
26
- SET n = {uuid: episode.uuid, name: episode.name, group_id: episode.group_id, source_description: episode.source_description,
27
- source: episode.source, content: episode.content,
28
- entity_edges: episode.entity_edges, created_at: episode.created_at, valid_at: episode.valid_at}
29
- RETURN n.uuid AS uuid
17
+ from typing import Any
18
+
19
+ from graphiti_core.driver.driver import GraphProvider
20
+
21
+
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 = {uuid: $uuid, name: $name, group_id: $group_id, source_description: $source_description, source: $source, content: $content,
56
+ entity_edges: $entity_edges, created_at: $created_at, valid_at: $valid_at}
57
+ RETURN n.uuid AS uuid
58
+ """
59
+
60
+
61
+ def get_episode_node_save_bulk_query(provider: GraphProvider) -> str:
62
+ match provider:
63
+ case GraphProvider.NEPTUNE:
64
+ return """
65
+ UNWIND $episodes AS episode
66
+ MERGE (n:Episodic {uuid: episode.uuid})
67
+ SET n = {uuid: episode.uuid, name: episode.name, group_id: episode.group_id, source_description: episode.source_description,
68
+ source: episode.source, content: episode.content,
69
+ entity_edges: join([x IN coalesce(episode.entity_edges, []) | toString(x) ], '|'), created_at: episode.created_at, valid_at: episode.valid_at}
70
+ RETURN n.uuid AS uuid
71
+ """
72
+ case GraphProvider.KUZU:
73
+ return """
74
+ MERGE (n:Episodic {uuid: $uuid})
75
+ SET
76
+ n.name = $name,
77
+ n.group_id = $group_id,
78
+ n.created_at = $created_at,
79
+ n.source = $source,
80
+ n.source_description = $source_description,
81
+ n.content = $content,
82
+ n.valid_at = $valid_at,
83
+ n.entity_edges = $entity_edges
84
+ RETURN n.uuid AS uuid
85
+ """
86
+ case GraphProvider.FALKORDB:
87
+ return """
88
+ UNWIND $episodes AS episode
89
+ MERGE (n:Episodic {uuid: episode.uuid})
90
+ SET n = {uuid: episode.uuid, name: episode.name, group_id: episode.group_id, source_description: episode.source_description, source: episode.source, content: episode.content,
91
+ entity_edges: episode.entity_edges, created_at: episode.created_at, valid_at: episode.valid_at}
92
+ RETURN n.uuid AS uuid
93
+ """
94
+ case _: # Neo4j
95
+ return """
96
+ UNWIND $episodes AS episode
97
+ MERGE (n:Episodic {uuid: episode.uuid})
98
+ SET n = {uuid: episode.uuid, name: episode.name, group_id: episode.group_id, source_description: episode.source_description, source: episode.source, content: episode.content,
99
+ entity_edges: episode.entity_edges, created_at: episode.created_at, valid_at: episode.valid_at}
100
+ RETURN n.uuid AS uuid
101
+ """
102
+
103
+
104
+ EPISODIC_NODE_RETURN = """
105
+ e.uuid AS uuid,
106
+ e.name AS name,
107
+ e.group_id AS group_id,
108
+ e.created_at AS created_at,
109
+ e.source AS source,
110
+ e.source_description AS source_description,
111
+ e.content AS content,
112
+ e.valid_at AS valid_at,
113
+ e.entity_edges AS entity_edges
30
114
  """
31
115
 
32
- ENTITY_NODE_SAVE = """
33
- MERGE (n:Entity {uuid: $entity_data.uuid})
34
- SET n:$($labels)
35
- SET n = $entity_data
36
- WITH n CALL db.create.setNodeVectorProperty(n, "name_embedding", $entity_data.name_embedding)
37
- RETURN n.uuid AS uuid"""
38
-
39
- ENTITY_NODE_SAVE_BULK = """
40
- UNWIND $nodes AS node
41
- MERGE (n:Entity {uuid: node.uuid})
42
- SET n:$(node.labels)
43
- SET n = node
44
- WITH n, node CALL db.create.setNodeVectorProperty(n, "name_embedding", node.name_embedding)
45
- RETURN n.uuid AS uuid
116
+ EPISODIC_NODE_RETURN_NEPTUNE = """
117
+ e.content AS content,
118
+ e.created_at AS created_at,
119
+ e.valid_at AS valid_at,
120
+ e.uuid AS uuid,
121
+ e.name AS name,
122
+ e.group_id AS group_id,
123
+ e.source_description AS source_description,
124
+ e.source AS source,
125
+ split(e.entity_edges, ",") AS entity_edges
126
+ """
127
+
128
+
129
+ def get_entity_node_save_query(provider: GraphProvider, labels: str, has_aoss: bool = False) -> str:
130
+ match provider:
131
+ case GraphProvider.FALKORDB:
132
+ return f"""
133
+ MERGE (n:Entity {{uuid: $entity_data.uuid}})
134
+ SET n:{labels}
135
+ SET n = $entity_data
136
+ SET n.name_embedding = vecf32($entity_data.name_embedding)
137
+ RETURN n.uuid AS uuid
138
+ """
139
+ case GraphProvider.KUZU:
140
+ return """
141
+ MERGE (n:Entity {uuid: $uuid})
142
+ SET
143
+ n.name = $name,
144
+ n.group_id = $group_id,
145
+ n.labels = $labels,
146
+ n.created_at = $created_at,
147
+ n.name_embedding = $name_embedding,
148
+ n.summary = $summary,
149
+ n.attributes = $attributes
150
+ WITH n
151
+ RETURN n.uuid AS uuid
152
+ """
153
+ case GraphProvider.NEPTUNE:
154
+ label_subquery = ''
155
+ for label in labels.split(':'):
156
+ label_subquery += f' SET n:{label}\n'
157
+ return f"""
158
+ MERGE (n:Entity {{uuid: $entity_data.uuid}})
159
+ {label_subquery}
160
+ SET n = removeKeyFromMap(removeKeyFromMap($entity_data, "labels"), "name_embedding")
161
+ SET n.name_embedding = join([x IN coalesce($entity_data.name_embedding, []) | toString(x) ], ",")
162
+ RETURN n.uuid AS uuid
163
+ """
164
+ case _:
165
+ save_embedding_query = (
166
+ 'WITH n CALL db.create.setNodeVectorProperty(n, "name_embedding", $entity_data.name_embedding)'
167
+ if not has_aoss
168
+ else ''
169
+ )
170
+ return (
171
+ f"""
172
+ MERGE (n:Entity {{uuid: $entity_data.uuid}})
173
+ SET n:{labels}
174
+ SET n = $entity_data
175
+ """
176
+ + save_embedding_query
177
+ + """
178
+ RETURN n.uuid AS uuid
179
+ """
180
+ )
181
+
182
+
183
+ def get_entity_node_save_bulk_query(
184
+ provider: GraphProvider, nodes: list[dict], has_aoss: bool = False
185
+ ) -> str | Any:
186
+ match provider:
187
+ case GraphProvider.FALKORDB:
188
+ queries = []
189
+ for node in nodes:
190
+ for label in node['labels']:
191
+ queries.append(
192
+ (
193
+ f"""
194
+ UNWIND $nodes AS node
195
+ MERGE (n:Entity {{uuid: node.uuid}})
196
+ SET n:{label}
197
+ SET n = node
198
+ WITH n, node
199
+ SET n.name_embedding = vecf32(node.name_embedding)
200
+ RETURN n.uuid AS uuid
201
+ """,
202
+ {'nodes': [node]},
203
+ )
204
+ )
205
+ return queries
206
+ case GraphProvider.NEPTUNE:
207
+ queries = []
208
+ for node in nodes:
209
+ labels = ''
210
+ for label in node['labels']:
211
+ labels += f' SET n:{label}\n'
212
+ queries.append(
213
+ f"""
214
+ UNWIND $nodes AS node
215
+ MERGE (n:Entity {{uuid: node.uuid}})
216
+ {labels}
217
+ SET n = removeKeyFromMap(removeKeyFromMap(node, "labels"), "name_embedding")
218
+ SET n.name_embedding = join([x IN coalesce(node.name_embedding, []) | toString(x) ], ",")
219
+ RETURN n.uuid AS uuid
220
+ """
221
+ )
222
+ return queries
223
+ case GraphProvider.KUZU:
224
+ return """
225
+ MERGE (n:Entity {uuid: $uuid})
226
+ SET
227
+ n.name = $name,
228
+ n.group_id = $group_id,
229
+ n.labels = $labels,
230
+ n.created_at = $created_at,
231
+ n.name_embedding = $name_embedding,
232
+ n.summary = $summary,
233
+ n.attributes = $attributes
234
+ RETURN n.uuid AS uuid
235
+ """
236
+ case _: # Neo4j
237
+ save_embedding_query = (
238
+ 'WITH n, node CALL db.create.setNodeVectorProperty(n, "name_embedding", node.name_embedding)'
239
+ if not has_aoss
240
+ else ''
241
+ )
242
+ return (
243
+ """
244
+ UNWIND $nodes AS node
245
+ MERGE (n:Entity {uuid: node.uuid})
246
+ SET n:$(node.labels)
247
+ SET n = node
248
+ """
249
+ + save_embedding_query
250
+ + """
251
+ RETURN n.uuid AS uuid
252
+ """
253
+ )
254
+
255
+
256
+ def get_entity_node_return_query(provider: GraphProvider) -> str:
257
+ # `name_embedding` is not returned by default and must be loaded manually using `load_name_embedding()`.
258
+ if provider == GraphProvider.KUZU:
259
+ return """
260
+ n.uuid AS uuid,
261
+ n.name AS name,
262
+ n.group_id AS group_id,
263
+ n.labels AS labels,
264
+ n.created_at AS created_at,
265
+ n.summary AS summary,
266
+ n.attributes AS attributes
267
+ """
268
+
269
+ return """
270
+ n.uuid AS uuid,
271
+ n.name AS name,
272
+ n.group_id AS group_id,
273
+ n.created_at AS created_at,
274
+ n.summary AS summary,
275
+ labels(n) AS labels,
276
+ properties(n) AS attributes
277
+ """
278
+
279
+
280
+ def get_community_node_save_query(provider: GraphProvider) -> str:
281
+ match provider:
282
+ case GraphProvider.FALKORDB:
283
+ return """
284
+ MERGE (n:Community {uuid: $uuid})
285
+ SET n = {uuid: $uuid, name: $name, group_id: $group_id, summary: $summary, created_at: $created_at, name_embedding: vecf32($name_embedding)}
286
+ RETURN n.uuid AS uuid
287
+ """
288
+ case GraphProvider.NEPTUNE:
289
+ return """
290
+ MERGE (n:Community {uuid: $uuid})
291
+ SET n = {uuid: $uuid, name: $name, group_id: $group_id, summary: $summary, created_at: $created_at}
292
+ SET n.name_embedding = join([x IN coalesce($name_embedding, []) | toString(x) ], ",")
293
+ RETURN n.uuid AS uuid
294
+ """
295
+ case GraphProvider.KUZU:
296
+ return """
297
+ MERGE (n:Community {uuid: $uuid})
298
+ SET
299
+ n.name = $name,
300
+ n.group_id = $group_id,
301
+ n.created_at = $created_at,
302
+ n.name_embedding = $name_embedding,
303
+ n.summary = $summary
304
+ RETURN n.uuid AS uuid
305
+ """
306
+ case _: # Neo4j
307
+ return """
308
+ MERGE (n:Community {uuid: $uuid})
309
+ SET n = {uuid: $uuid, name: $name, group_id: $group_id, summary: $summary, created_at: $created_at}
310
+ WITH n CALL db.create.setNodeVectorProperty(n, "name_embedding", $name_embedding)
311
+ RETURN n.uuid AS uuid
312
+ """
313
+
314
+
315
+ COMMUNITY_NODE_RETURN = """
316
+ c.uuid AS uuid,
317
+ c.name AS name,
318
+ c.group_id AS group_id,
319
+ c.created_at AS created_at,
320
+ c.name_embedding AS name_embedding,
321
+ c.summary AS summary
46
322
  """
47
323
 
48
- COMMUNITY_NODE_SAVE = """
49
- MERGE (n:Community {uuid: $uuid})
50
- SET n = {uuid: $uuid, name: $name, group_id: $group_id, summary: $summary, created_at: $created_at}
51
- WITH n CALL db.create.setNodeVectorProperty(n, "name_embedding", $name_embedding)
52
- RETURN n.uuid AS uuid"""
324
+ COMMUNITY_NODE_RETURN_NEPTUNE = """
325
+ n.uuid AS uuid,
326
+ n.name AS name,
327
+ [x IN split(n.name_embedding, ",") | toFloat(x)] AS name_embedding,
328
+ n.group_id AS group_id,
329
+ n.summary AS summary,
330
+ n.created_at AS created_at
331
+ """