graphiti-core 0.17.4__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 (58) hide show
  1. graphiti_core/cross_encoder/gemini_reranker_client.py +1 -1
  2. graphiti_core/cross_encoder/openai_reranker_client.py +1 -1
  3. graphiti_core/decorators.py +110 -0
  4. graphiti_core/driver/driver.py +62 -2
  5. graphiti_core/driver/falkordb_driver.py +215 -23
  6. graphiti_core/driver/graph_operations/graph_operations.py +191 -0
  7. graphiti_core/driver/kuzu_driver.py +182 -0
  8. graphiti_core/driver/neo4j_driver.py +61 -8
  9. graphiti_core/driver/neptune_driver.py +305 -0
  10. graphiti_core/driver/search_interface/search_interface.py +89 -0
  11. graphiti_core/edges.py +264 -132
  12. graphiti_core/embedder/azure_openai.py +10 -3
  13. graphiti_core/embedder/client.py +2 -1
  14. graphiti_core/graph_queries.py +114 -101
  15. graphiti_core/graphiti.py +582 -255
  16. graphiti_core/graphiti_types.py +2 -0
  17. graphiti_core/helpers.py +21 -14
  18. graphiti_core/llm_client/anthropic_client.py +142 -52
  19. graphiti_core/llm_client/azure_openai_client.py +57 -19
  20. graphiti_core/llm_client/client.py +83 -21
  21. graphiti_core/llm_client/config.py +1 -1
  22. graphiti_core/llm_client/gemini_client.py +75 -57
  23. graphiti_core/llm_client/openai_base_client.py +94 -50
  24. graphiti_core/llm_client/openai_client.py +28 -8
  25. graphiti_core/llm_client/openai_generic_client.py +91 -56
  26. graphiti_core/models/edges/edge_db_queries.py +259 -35
  27. graphiti_core/models/nodes/node_db_queries.py +311 -32
  28. graphiti_core/nodes.py +388 -164
  29. graphiti_core/prompts/dedupe_edges.py +42 -31
  30. graphiti_core/prompts/dedupe_nodes.py +56 -39
  31. graphiti_core/prompts/eval.py +4 -4
  32. graphiti_core/prompts/extract_edges.py +23 -14
  33. graphiti_core/prompts/extract_nodes.py +73 -32
  34. graphiti_core/prompts/prompt_helpers.py +39 -0
  35. graphiti_core/prompts/snippets.py +29 -0
  36. graphiti_core/prompts/summarize_nodes.py +23 -25
  37. graphiti_core/search/search.py +154 -74
  38. graphiti_core/search/search_config.py +39 -4
  39. graphiti_core/search/search_filters.py +109 -31
  40. graphiti_core/search/search_helpers.py +5 -6
  41. graphiti_core/search/search_utils.py +1360 -473
  42. graphiti_core/tracer.py +193 -0
  43. graphiti_core/utils/bulk_utils.py +216 -90
  44. graphiti_core/utils/datetime_utils.py +13 -0
  45. graphiti_core/utils/maintenance/community_operations.py +62 -38
  46. graphiti_core/utils/maintenance/dedup_helpers.py +262 -0
  47. graphiti_core/utils/maintenance/edge_operations.py +286 -126
  48. graphiti_core/utils/maintenance/graph_data_operations.py +44 -74
  49. graphiti_core/utils/maintenance/node_operations.py +320 -158
  50. graphiti_core/utils/maintenance/temporal_operations.py +11 -3
  51. graphiti_core/utils/ontology_utils/entity_types_utils.py +1 -1
  52. graphiti_core/utils/text_utils.py +53 -0
  53. {graphiti_core-0.17.4.dist-info → graphiti_core-0.24.3.dist-info}/METADATA +221 -87
  54. graphiti_core-0.24.3.dist-info/RECORD +86 -0
  55. {graphiti_core-0.17.4.dist-info → graphiti_core-0.24.3.dist-info}/WHEEL +1 -1
  56. graphiti_core-0.17.4.dist-info/RECORD +0 -77
  57. /graphiti_core/{utils/maintenance/utils.py → migrations/__init__.py} +0 -0
  58. {graphiti_core-0.17.4.dist-info → graphiti_core-0.24.3.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,305 @@
1
+ """
2
+ Copyright 2024, Zep Software, Inc.
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ """
16
+
17
+ import asyncio
18
+ import datetime
19
+ import logging
20
+ from collections.abc import Coroutine
21
+ from typing import Any
22
+
23
+ import boto3
24
+ from langchain_aws.graphs import NeptuneAnalyticsGraph, NeptuneGraph
25
+ from opensearchpy import OpenSearch, Urllib3AWSV4SignerAuth, Urllib3HttpConnection, helpers
26
+
27
+ from graphiti_core.driver.driver import GraphDriver, GraphDriverSession, GraphProvider
28
+
29
+ logger = logging.getLogger(__name__)
30
+ DEFAULT_SIZE = 10
31
+
32
+ aoss_indices = [
33
+ {
34
+ 'index_name': 'node_name_and_summary',
35
+ 'body': {
36
+ 'mappings': {
37
+ 'properties': {
38
+ 'uuid': {'type': 'keyword'},
39
+ 'name': {'type': 'text'},
40
+ 'summary': {'type': 'text'},
41
+ 'group_id': {'type': 'text'},
42
+ }
43
+ }
44
+ },
45
+ 'query': {
46
+ 'query': {'multi_match': {'query': '', 'fields': ['name', 'summary', 'group_id']}},
47
+ 'size': DEFAULT_SIZE,
48
+ },
49
+ },
50
+ {
51
+ 'index_name': 'community_name',
52
+ 'body': {
53
+ 'mappings': {
54
+ 'properties': {
55
+ 'uuid': {'type': 'keyword'},
56
+ 'name': {'type': 'text'},
57
+ 'group_id': {'type': 'text'},
58
+ }
59
+ }
60
+ },
61
+ 'query': {
62
+ 'query': {'multi_match': {'query': '', 'fields': ['name', 'group_id']}},
63
+ 'size': DEFAULT_SIZE,
64
+ },
65
+ },
66
+ {
67
+ 'index_name': 'episode_content',
68
+ 'body': {
69
+ 'mappings': {
70
+ 'properties': {
71
+ 'uuid': {'type': 'keyword'},
72
+ 'content': {'type': 'text'},
73
+ 'source': {'type': 'text'},
74
+ 'source_description': {'type': 'text'},
75
+ 'group_id': {'type': 'text'},
76
+ }
77
+ }
78
+ },
79
+ 'query': {
80
+ 'query': {
81
+ 'multi_match': {
82
+ 'query': '',
83
+ 'fields': ['content', 'source', 'source_description', 'group_id'],
84
+ }
85
+ },
86
+ 'size': DEFAULT_SIZE,
87
+ },
88
+ },
89
+ {
90
+ 'index_name': 'edge_name_and_fact',
91
+ 'body': {
92
+ 'mappings': {
93
+ 'properties': {
94
+ 'uuid': {'type': 'keyword'},
95
+ 'name': {'type': 'text'},
96
+ 'fact': {'type': 'text'},
97
+ 'group_id': {'type': 'text'},
98
+ }
99
+ }
100
+ },
101
+ 'query': {
102
+ 'query': {'multi_match': {'query': '', 'fields': ['name', 'fact', 'group_id']}},
103
+ 'size': DEFAULT_SIZE,
104
+ },
105
+ },
106
+ ]
107
+
108
+
109
+ class NeptuneDriver(GraphDriver):
110
+ provider: GraphProvider = GraphProvider.NEPTUNE
111
+
112
+ def __init__(self, host: str, aoss_host: str, port: int = 8182, aoss_port: int = 443):
113
+ """This initializes a NeptuneDriver for use with Neptune as a backend
114
+
115
+ Args:
116
+ host (str): The Neptune Database or Neptune Analytics host
117
+ aoss_host (str): The OpenSearch host value
118
+ port (int, optional): The Neptune Database port, ignored for Neptune Analytics. Defaults to 8182.
119
+ aoss_port (int, optional): The OpenSearch port. Defaults to 443.
120
+ """
121
+ if not host:
122
+ raise ValueError('You must provide an endpoint to create a NeptuneDriver')
123
+
124
+ if host.startswith('neptune-db://'):
125
+ # This is a Neptune Database Cluster
126
+ endpoint = host.replace('neptune-db://', '')
127
+ self.client = NeptuneGraph(endpoint, port)
128
+ logger.debug('Creating Neptune Database session for %s', host)
129
+ elif host.startswith('neptune-graph://'):
130
+ # This is a Neptune Analytics Graph
131
+ graphId = host.replace('neptune-graph://', '')
132
+ self.client = NeptuneAnalyticsGraph(graphId)
133
+ logger.debug('Creating Neptune Graph session for %s', host)
134
+ else:
135
+ raise ValueError(
136
+ 'You must provide an endpoint to create a NeptuneDriver as either neptune-db://<endpoint> or neptune-graph://<graphid>'
137
+ )
138
+
139
+ if not aoss_host:
140
+ raise ValueError('You must provide an AOSS endpoint to create an OpenSearch driver.')
141
+
142
+ session = boto3.Session()
143
+ self.aoss_client = OpenSearch(
144
+ hosts=[{'host': aoss_host, 'port': aoss_port}],
145
+ http_auth=Urllib3AWSV4SignerAuth(
146
+ session.get_credentials(), session.region_name, 'aoss'
147
+ ),
148
+ use_ssl=True,
149
+ verify_certs=True,
150
+ connection_class=Urllib3HttpConnection,
151
+ pool_maxsize=20,
152
+ )
153
+
154
+ def _sanitize_parameters(self, query, params: dict):
155
+ if isinstance(query, list):
156
+ queries = []
157
+ for q in query:
158
+ queries.append(self._sanitize_parameters(q, params))
159
+ return queries
160
+ else:
161
+ for k, v in params.items():
162
+ if isinstance(v, datetime.datetime):
163
+ params[k] = v.isoformat()
164
+ elif isinstance(v, list):
165
+ # Handle lists that might contain datetime objects
166
+ for i, item in enumerate(v):
167
+ if isinstance(item, datetime.datetime):
168
+ v[i] = item.isoformat()
169
+ query = str(query).replace(f'${k}', f'datetime(${k})')
170
+ if isinstance(item, dict):
171
+ query = self._sanitize_parameters(query, v[i])
172
+
173
+ # If the list contains datetime objects, we need to wrap each element with datetime()
174
+ if any(isinstance(item, str) and 'T' in item for item in v):
175
+ # Create a new list expression with datetime() wrapped around each element
176
+ datetime_list = (
177
+ '['
178
+ + ', '.join(
179
+ f'datetime("{item}")'
180
+ if isinstance(item, str) and 'T' in item
181
+ else repr(item)
182
+ for item in v
183
+ )
184
+ + ']'
185
+ )
186
+ query = str(query).replace(f'${k}', datetime_list)
187
+ elif isinstance(v, dict):
188
+ query = self._sanitize_parameters(query, v)
189
+ return query
190
+
191
+ async def execute_query(
192
+ self, cypher_query_, **kwargs: Any
193
+ ) -> tuple[dict[str, Any], None, None]:
194
+ params = dict(kwargs)
195
+ if isinstance(cypher_query_, list):
196
+ for q in cypher_query_:
197
+ result, _, _ = self._run_query(q[0], q[1])
198
+ return result, None, None
199
+ else:
200
+ return self._run_query(cypher_query_, params)
201
+
202
+ def _run_query(self, cypher_query_, params):
203
+ cypher_query_ = str(self._sanitize_parameters(cypher_query_, params))
204
+ try:
205
+ result = self.client.query(cypher_query_, params=params)
206
+ except Exception as e:
207
+ logger.error('Query: %s', cypher_query_)
208
+ logger.error('Parameters: %s', params)
209
+ logger.error('Error executing query: %s', e)
210
+ raise e
211
+
212
+ return result, None, None
213
+
214
+ def session(self, database: str | None = None) -> GraphDriverSession:
215
+ return NeptuneDriverSession(driver=self)
216
+
217
+ async def close(self) -> None:
218
+ return self.client.client.close()
219
+
220
+ async def _delete_all_data(self) -> Any:
221
+ return await self.execute_query('MATCH (n) DETACH DELETE n')
222
+
223
+ def delete_all_indexes(self) -> Coroutine[Any, Any, Any]:
224
+ return self.delete_all_indexes_impl()
225
+
226
+ async def delete_all_indexes_impl(self) -> Coroutine[Any, Any, Any]:
227
+ # No matter what happens above, always return True
228
+ return self.delete_aoss_indices()
229
+
230
+ async def create_aoss_indices(self):
231
+ for index in aoss_indices:
232
+ index_name = index['index_name']
233
+ client = self.aoss_client
234
+ if not client.indices.exists(index=index_name):
235
+ client.indices.create(index=index_name, body=index['body'])
236
+ # Sleep for 1 minute to let the index creation complete
237
+ await asyncio.sleep(60)
238
+
239
+ async def delete_aoss_indices(self):
240
+ for index in aoss_indices:
241
+ index_name = index['index_name']
242
+ client = self.aoss_client
243
+ if client.indices.exists(index=index_name):
244
+ client.indices.delete(index=index_name)
245
+
246
+ async def build_indices_and_constraints(self, delete_existing: bool = False):
247
+ # Neptune uses OpenSearch (AOSS) for indexing
248
+ if delete_existing:
249
+ await self.delete_aoss_indices()
250
+ await self.create_aoss_indices()
251
+
252
+ def run_aoss_query(self, name: str, query_text: str, limit: int = 10) -> dict[str, Any]:
253
+ for index in aoss_indices:
254
+ if name.lower() == index['index_name']:
255
+ index['query']['query']['multi_match']['query'] = query_text
256
+ query = {'size': limit, 'query': index['query']}
257
+ resp = self.aoss_client.search(body=query['query'], index=index['index_name'])
258
+ return resp
259
+ return {}
260
+
261
+ def save_to_aoss(self, name: str, data: list[dict]) -> int:
262
+ for index in aoss_indices:
263
+ if name.lower() == index['index_name']:
264
+ to_index = []
265
+ for d in data:
266
+ item = {'_index': name, '_id': d['uuid']}
267
+ for p in index['body']['mappings']['properties']:
268
+ if p in d:
269
+ item[p] = d[p]
270
+ to_index.append(item)
271
+ success, failed = helpers.bulk(self.aoss_client, to_index, stats_only=True)
272
+ return success
273
+
274
+ return 0
275
+
276
+
277
+ class NeptuneDriverSession(GraphDriverSession):
278
+ provider = GraphProvider.NEPTUNE
279
+
280
+ def __init__(self, driver: NeptuneDriver): # type: ignore[reportUnknownArgumentType]
281
+ self.driver = driver
282
+
283
+ async def __aenter__(self):
284
+ return self
285
+
286
+ async def __aexit__(self, exc_type, exc, tb):
287
+ # No cleanup needed for Neptune, but method must exist
288
+ pass
289
+
290
+ async def close(self):
291
+ # No explicit close needed for Neptune, but method must exist
292
+ pass
293
+
294
+ async def execute_write(self, func, *args, **kwargs):
295
+ # Directly await the provided async function with `self` as the transaction/session
296
+ return await func(self, *args, **kwargs)
297
+
298
+ async def run(self, query: str | list, **kwargs: Any) -> Any:
299
+ if isinstance(query, list):
300
+ res = None
301
+ for q in query:
302
+ res = await self.driver.execute_query(q, **kwargs)
303
+ return res
304
+ else:
305
+ return await self.driver.execute_query(str(query), **kwargs)
@@ -0,0 +1,89 @@
1
+ """
2
+ Copyright 2024, Zep Software, Inc.
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ """
16
+
17
+ from typing import Any
18
+
19
+ from pydantic import BaseModel
20
+
21
+
22
+ class SearchInterface(BaseModel):
23
+ """
24
+ This is an interface for implementing custom search logic
25
+ """
26
+
27
+ async def edge_fulltext_search(
28
+ self,
29
+ driver: Any,
30
+ query: str,
31
+ search_filter: Any,
32
+ group_ids: list[str] | None = None,
33
+ limit: int = 100,
34
+ ) -> list[Any]:
35
+ raise NotImplementedError
36
+
37
+ async def edge_similarity_search(
38
+ self,
39
+ driver: Any,
40
+ search_vector: list[float],
41
+ source_node_uuid: str | None,
42
+ target_node_uuid: str | None,
43
+ search_filter: Any,
44
+ group_ids: list[str] | None = None,
45
+ limit: int = 100,
46
+ min_score: float = 0.7,
47
+ ) -> list[Any]:
48
+ raise NotImplementedError
49
+
50
+ async def node_fulltext_search(
51
+ self,
52
+ driver: Any,
53
+ query: str,
54
+ search_filter: Any,
55
+ group_ids: list[str] | None = None,
56
+ limit: int = 100,
57
+ ) -> list[Any]:
58
+ raise NotImplementedError
59
+
60
+ async def node_similarity_search(
61
+ self,
62
+ driver: Any,
63
+ search_vector: list[float],
64
+ search_filter: Any,
65
+ group_ids: list[str] | None = None,
66
+ limit: int = 100,
67
+ min_score: float = 0.7,
68
+ ) -> list[Any]:
69
+ raise NotImplementedError
70
+
71
+ async def episode_fulltext_search(
72
+ self,
73
+ driver: Any,
74
+ query: str,
75
+ search_filter: Any, # kept for parity even if unused in your impl
76
+ group_ids: list[str] | None = None,
77
+ limit: int = 100,
78
+ ) -> list[Any]:
79
+ raise NotImplementedError
80
+
81
+ # ---------- SEARCH FILTERS (sync) ----------
82
+ def build_node_search_filters(self, search_filters: Any) -> Any:
83
+ raise NotImplementedError
84
+
85
+ def build_edge_search_filters(self, search_filters: Any) -> Any:
86
+ raise NotImplementedError
87
+
88
+ class Config:
89
+ arbitrary_types_allowed = True