graphiti-core 0.17.4__py3-none-any.whl → 0.25.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 (59) 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 +70 -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 +635 -260
  16. graphiti_core/graphiti_types.py +2 -0
  17. graphiti_core/helpers.py +37 -15
  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 +92 -48
  24. graphiti_core/llm_client/openai_client.py +39 -9
  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 +24 -15
  33. graphiti_core/prompts/extract_nodes.py +76 -35
  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 +110 -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/content_chunking.py +702 -0
  45. graphiti_core/utils/datetime_utils.py +13 -0
  46. graphiti_core/utils/maintenance/community_operations.py +62 -38
  47. graphiti_core/utils/maintenance/dedup_helpers.py +262 -0
  48. graphiti_core/utils/maintenance/edge_operations.py +306 -156
  49. graphiti_core/utils/maintenance/graph_data_operations.py +44 -74
  50. graphiti_core/utils/maintenance/node_operations.py +466 -206
  51. graphiti_core/utils/maintenance/temporal_operations.py +11 -3
  52. graphiti_core/utils/ontology_utils/entity_types_utils.py +1 -1
  53. graphiti_core/utils/text_utils.py +53 -0
  54. {graphiti_core-0.17.4.dist-info → graphiti_core-0.25.3.dist-info}/METADATA +221 -87
  55. graphiti_core-0.25.3.dist-info/RECORD +87 -0
  56. {graphiti_core-0.17.4.dist-info → graphiti_core-0.25.3.dist-info}/WHEEL +1 -1
  57. graphiti_core-0.17.4.dist-info/RECORD +0 -77
  58. /graphiti_core/{utils/maintenance/utils.py → migrations/__init__.py} +0 -0
  59. {graphiti_core-0.17.4.dist-info → graphiti_core-0.25.3.dist-info}/licenses/LICENSE +0 -0
@@ -14,12 +14,15 @@ See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  """
16
16
 
17
- import json
18
17
  from typing import Any, Protocol, TypedDict
19
18
 
20
19
  from pydantic import BaseModel, Field
21
20
 
21
+ from graphiti_core.utils.text_utils import MAX_SUMMARY_CHARS
22
+
22
23
  from .models import Message, PromptFunction, PromptVersion
24
+ from .prompt_helpers import to_prompt_json
25
+ from .snippets import summary_instructions
23
26
 
24
27
 
25
28
  class ExtractedEntity(BaseModel):
@@ -42,7 +45,8 @@ class EntityClassificationTriple(BaseModel):
42
45
  uuid: str = Field(description='UUID of the entity')
43
46
  name: str = Field(description='Name of the entity')
44
47
  entity_type: str | None = Field(
45
- default=None, description='Type of the entity. Must be one of the provided types or None'
48
+ default=None,
49
+ description='Type of the entity. Must be one of the provided types or None',
46
50
  )
47
51
 
48
52
 
@@ -52,6 +56,13 @@ class EntityClassification(BaseModel):
52
56
  )
53
57
 
54
58
 
59
+ class EntitySummary(BaseModel):
60
+ summary: str = Field(
61
+ ...,
62
+ description=f'Summary containing the important information about the entity. Under {MAX_SUMMARY_CHARS} characters.',
63
+ )
64
+
65
+
55
66
  class Prompt(Protocol):
56
67
  extract_message: PromptVersion
57
68
  extract_json: PromptVersion
@@ -59,6 +70,7 @@ class Prompt(Protocol):
59
70
  reflexion: PromptVersion
60
71
  classify_nodes: PromptVersion
61
72
  extract_attributes: PromptVersion
73
+ extract_summary: PromptVersion
62
74
 
63
75
 
64
76
  class Versions(TypedDict):
@@ -68,6 +80,7 @@ class Versions(TypedDict):
68
80
  reflexion: PromptFunction
69
81
  classify_nodes: PromptFunction
70
82
  extract_attributes: PromptFunction
83
+ extract_summary: PromptFunction
71
84
 
72
85
 
73
86
  def extract_message(context: dict[str, Any]) -> list[Message]:
@@ -75,23 +88,23 @@ def extract_message(context: dict[str, Any]) -> list[Message]:
75
88
  Your primary task is to extract and classify the speaker and other significant entities mentioned in the conversation."""
76
89
 
77
90
  user_prompt = f"""
91
+ <ENTITY TYPES>
92
+ {context['entity_types']}
93
+ </ENTITY TYPES>
94
+
78
95
  <PREVIOUS MESSAGES>
79
- {json.dumps([ep for ep in context['previous_episodes']], indent=2)}
96
+ {to_prompt_json([ep for ep in context['previous_episodes']])}
80
97
  </PREVIOUS MESSAGES>
81
98
 
82
99
  <CURRENT MESSAGE>
83
100
  {context['episode_content']}
84
101
  </CURRENT MESSAGE>
85
102
 
86
- <ENTITY TYPES>
87
- {context['entity_types']}
88
- </ENTITY TYPES>
89
-
90
103
  Instructions:
91
104
 
92
105
  You are given a conversation context and a CURRENT MESSAGE. Your task is to extract **entity nodes** mentioned **explicitly or implicitly** in the CURRENT MESSAGE.
93
106
  Pronoun references such as he/she/they or this/that/those should be disambiguated to the names of the
94
- reference entities.
107
+ reference entities. Only extract distinct entities from the CURRENT MESSAGE. Don't extract pronouns like you, me, he/she/they, we/us as entities.
95
108
 
96
109
  1. **Speaker Extraction**: Always extract the speaker (the part before the colon `:` in each dialogue line) as the first entity node.
97
110
  - If the speaker is mentioned again in the message, treat both mentions as a **single entity**.
@@ -111,7 +124,7 @@ reference entities.
111
124
  5. **Formatting**:
112
125
  - Be **explicit and unambiguous** in naming entities (e.g., use full names when available).
113
126
 
114
- {context['custom_prompt']}
127
+ {context['custom_extraction_instructions']}
115
128
  """
116
129
  return [
117
130
  Message(role='system', content=sys_prompt),
@@ -124,25 +137,27 @@ def extract_json(context: dict[str, Any]) -> list[Message]:
124
137
  Your primary task is to extract and classify relevant entities from JSON files"""
125
138
 
126
139
  user_prompt = f"""
140
+ <ENTITY TYPES>
141
+ {context['entity_types']}
142
+ </ENTITY TYPES>
143
+
127
144
  <SOURCE DESCRIPTION>:
128
145
  {context['source_description']}
129
146
  </SOURCE DESCRIPTION>
130
147
  <JSON>
131
148
  {context['episode_content']}
132
149
  </JSON>
133
- <ENTITY TYPES>
134
- {context['entity_types']}
135
- </ENTITY TYPES>
136
150
 
137
- {context['custom_prompt']}
151
+ {context['custom_extraction_instructions']}
138
152
 
139
153
  Given the above source description and JSON, extract relevant entities from the provided JSON.
140
154
  For each entity extracted, also determine its entity type based on the provided ENTITY TYPES and their descriptions.
141
155
  Indicate the classified entity type by providing its entity_type_id.
142
156
 
143
157
  Guidelines:
144
- 1. Always try to extract an entities that the JSON represents. This will often be something like a "name" or "user field
145
- 2. Do NOT extract any properties that contain dates
158
+ 1. Extract all entities that the JSON represents. This will often be something like a "name" or "user" field
159
+ 2. Extract all entities mentioned in all other properties throughout the JSON structure
160
+ 3. Do NOT extract any properties that contain dates
146
161
  """
147
162
  return [
148
163
  Message(role='system', content=sys_prompt),
@@ -155,18 +170,19 @@ def extract_text(context: dict[str, Any]) -> list[Message]:
155
170
  Your primary task is to extract and classify the speaker and other significant entities mentioned in the provided text."""
156
171
 
157
172
  user_prompt = f"""
158
- <TEXT>
159
- {context['episode_content']}
160
- </TEXT>
161
173
  <ENTITY TYPES>
162
174
  {context['entity_types']}
163
175
  </ENTITY TYPES>
164
176
 
177
+ <TEXT>
178
+ {context['episode_content']}
179
+ </TEXT>
180
+
165
181
  Given the above text, extract entities from the TEXT that are explicitly or implicitly mentioned.
166
182
  For each entity extracted, also determine its entity type based on the provided ENTITY TYPES and their descriptions.
167
183
  Indicate the classified entity type by providing its entity_type_id.
168
184
 
169
- {context['custom_prompt']}
185
+ {context['custom_extraction_instructions']}
170
186
 
171
187
  Guidelines:
172
188
  1. Extract significant entities, concepts, or actors mentioned in the conversation.
@@ -185,7 +201,7 @@ def reflexion(context: dict[str, Any]) -> list[Message]:
185
201
 
186
202
  user_prompt = f"""
187
203
  <PREVIOUS MESSAGES>
188
- {json.dumps([ep for ep in context['previous_episodes']], indent=2)}
204
+ {to_prompt_json([ep for ep in context['previous_episodes']])}
189
205
  </PREVIOUS MESSAGES>
190
206
  <CURRENT MESSAGE>
191
207
  {context['episode_content']}
@@ -209,22 +225,22 @@ def classify_nodes(context: dict[str, Any]) -> list[Message]:
209
225
 
210
226
  user_prompt = f"""
211
227
  <PREVIOUS MESSAGES>
212
- {json.dumps([ep for ep in context['previous_episodes']], indent=2)}
228
+ {to_prompt_json([ep for ep in context['previous_episodes']])}
213
229
  </PREVIOUS MESSAGES>
214
230
  <CURRENT MESSAGE>
215
231
  {context['episode_content']}
216
232
  </CURRENT MESSAGE>
217
-
233
+
218
234
  <EXTRACTED ENTITIES>
219
235
  {context['extracted_entities']}
220
236
  </EXTRACTED ENTITIES>
221
-
237
+
222
238
  <ENTITY TYPES>
223
239
  {context['entity_types']}
224
240
  </ENTITY TYPES>
225
-
241
+
226
242
  Given the above conversation, extracted entities, and provided entity types and their descriptions, classify the extracted entities.
227
-
243
+
228
244
  Guidelines:
229
245
  1. Each entity must have exactly one type
230
246
  2. Only use the provided ENTITY TYPES as types, do not use additional types to classify entities.
@@ -245,21 +261,45 @@ def extract_attributes(context: dict[str, Any]) -> list[Message]:
245
261
  Message(
246
262
  role='user',
247
263
  content=f"""
248
-
249
- <MESSAGES>
250
- {json.dumps(context['previous_episodes'], indent=2)}
251
- {json.dumps(context['episode_content'], indent=2)}
252
- </MESSAGES>
253
-
254
- Given the above MESSAGES and the following ENTITY, update any of its attributes based on the information provided
264
+ Given the MESSAGES and the following ENTITY, update any of its attributes based on the information provided
255
265
  in MESSAGES. Use the provided attribute descriptions to better understand how each attribute should be determined.
256
266
 
257
267
  Guidelines:
258
268
  1. Do not hallucinate entity property values if they cannot be found in the current context.
259
269
  2. Only use the provided MESSAGES and ENTITY to set attribute values.
260
- 3. The summary attribute represents a summary of the ENTITY, and should be updated with new information about the Entity from the MESSAGES.
261
- Summaries must be no longer than 250 words.
262
-
270
+
271
+ <MESSAGES>
272
+ {to_prompt_json(context['previous_episodes'])}
273
+ {to_prompt_json(context['episode_content'])}
274
+ </MESSAGES>
275
+
276
+ <ENTITY>
277
+ {context['node']}
278
+ </ENTITY>
279
+ """,
280
+ ),
281
+ ]
282
+
283
+
284
+ def extract_summary(context: dict[str, Any]) -> list[Message]:
285
+ return [
286
+ Message(
287
+ role='system',
288
+ content='You are a helpful assistant that extracts entity summaries from the provided text.',
289
+ ),
290
+ Message(
291
+ role='user',
292
+ content=f"""
293
+ Given the MESSAGES and the ENTITY, update the summary that combines relevant information about the entity
294
+ from the messages and relevant information from the existing summary.
295
+
296
+ {summary_instructions}
297
+
298
+ <MESSAGES>
299
+ {to_prompt_json(context['previous_episodes'])}
300
+ {to_prompt_json(context['episode_content'])}
301
+ </MESSAGES>
302
+
263
303
  <ENTITY>
264
304
  {context['node']}
265
305
  </ENTITY>
@@ -273,6 +313,7 @@ versions: Versions = {
273
313
  'extract_json': extract_json,
274
314
  'extract_text': extract_text,
275
315
  'reflexion': reflexion,
316
+ 'extract_summary': extract_summary,
276
317
  'classify_nodes': classify_nodes,
277
318
  'extract_attributes': extract_attributes,
278
319
  }
@@ -1 +1,40 @@
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 json
18
+ from typing import Any
19
+
1
20
  DO_NOT_ESCAPE_UNICODE = '\nDo not escape unicode characters.\n'
21
+
22
+
23
+ def to_prompt_json(data: Any, ensure_ascii: bool = False, indent: int | None = None) -> str:
24
+ """
25
+ Serialize data to JSON for use in prompts.
26
+
27
+ Args:
28
+ data: The data to serialize
29
+ ensure_ascii: If True, escape non-ASCII characters. If False (default), preserve them.
30
+ indent: Number of spaces for indentation. Defaults to None (minified).
31
+
32
+ Returns:
33
+ JSON string representation of the data
34
+
35
+ Notes:
36
+ By default (ensure_ascii=False), non-ASCII characters (e.g., Korean, Japanese, Chinese)
37
+ are preserved in their original form in the prompt, making them readable
38
+ in LLM logs and improving model understanding.
39
+ """
40
+ return json.dumps(data, ensure_ascii=ensure_ascii, indent=indent)
@@ -0,0 +1,29 @@
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
+ summary_instructions = """Guidelines:
18
+ 1. Output only factual content. Never explain what you're doing, why, or mention limitations/constraints.
19
+ 2. Only use the provided messages, entity, and entity context to set attribute values.
20
+ 3. Keep the summary concise and to the point. STATE FACTS DIRECTLY IN UNDER 250 CHARACTERS.
21
+
22
+ Example summaries:
23
+ BAD: "This is the only activity in the context. The user listened to this song. No other details were provided to include in this summary."
24
+ GOOD: "User played 'Blue Monday' by New Order (electronic genre) on 2024-12-03 at 14:22 UTC."
25
+ BAD: "Based on the messages provided, the user attended a meeting. This summary focuses on that event as it was the main topic discussed."
26
+ GOOD: "User attended Q3 planning meeting with sales team on March 15."
27
+ BAD: "The context shows John ordered pizza. Due to length constraints, other details are omitted from this summary."
28
+ GOOD: "John ordered pepperoni pizza from Mario's at 7:30 PM, delivered to office."
29
+ """
@@ -14,18 +14,19 @@ See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  """
16
16
 
17
- import json
18
17
  from typing import Any, Protocol, TypedDict
19
18
 
20
19
  from pydantic import BaseModel, Field
21
20
 
22
21
  from .models import Message, PromptFunction, PromptVersion
22
+ from .prompt_helpers import to_prompt_json
23
+ from .snippets import summary_instructions
23
24
 
24
25
 
25
26
  class Summary(BaseModel):
26
27
  summary: str = Field(
27
28
  ...,
28
- description='Summary containing the important information about the entity. Under 250 words',
29
+ description='Summary containing the important information about the entity. Under 250 characters',
29
30
  )
30
31
 
31
32
 
@@ -55,11 +56,11 @@ def summarize_pair(context: dict[str, Any]) -> list[Message]:
55
56
  role='user',
56
57
  content=f"""
57
58
  Synthesize the information from the following two summaries into a single succinct summary.
58
-
59
- Summaries must be under 250 words.
59
+
60
+ IMPORTANT: Keep the summary concise and to the point. SUMMARIES MUST BE LESS THAN 250 CHARACTERS.
60
61
 
61
62
  Summaries:
62
- {json.dumps(context['node_summaries'], indent=2)}
63
+ {to_prompt_json(context['node_summaries'])}
63
64
  """,
64
65
  ),
65
66
  ]
@@ -69,38 +70,35 @@ def summarize_context(context: dict[str, Any]) -> list[Message]:
69
70
  return [
70
71
  Message(
71
72
  role='system',
72
- content='You are a helpful assistant that extracts entity properties from the provided text.',
73
+ content='You are a helpful assistant that generates a summary and attributes from provided text.',
73
74
  ),
74
75
  Message(
75
76
  role='user',
76
77
  content=f"""
77
-
78
- <MESSAGES>
79
- {json.dumps(context['previous_episodes'], indent=2)}
80
- {json.dumps(context['episode_content'], indent=2)}
81
- </MESSAGES>
82
-
83
- Given the above MESSAGES and the following ENTITY name, create a summary for the ENTITY. Your summary must only use
78
+ Given the MESSAGES and the ENTITY name, create a summary for the ENTITY. Your summary must only use
84
79
  information from the provided MESSAGES. Your summary should also only contain information relevant to the
85
- provided ENTITY. Summaries must be under 250 words.
86
-
80
+ provided ENTITY.
81
+
87
82
  In addition, extract any values for the provided entity properties based on their descriptions.
88
83
  If the value of the entity property cannot be found in the current context, set the value of the property to the Python value None.
89
-
90
- Guidelines:
91
- 1. Do not hallucinate entity property values if they cannot be found in the current context.
92
- 2. Only use the provided messages, entity, and entity context to set attribute values.
93
-
84
+
85
+ {summary_instructions}
86
+
87
+ <MESSAGES>
88
+ {to_prompt_json(context['previous_episodes'])}
89
+ {to_prompt_json(context['episode_content'])}
90
+ </MESSAGES>
91
+
94
92
  <ENTITY>
95
93
  {context['node_name']}
96
94
  </ENTITY>
97
-
95
+
98
96
  <ENTITY CONTEXT>
99
97
  {context['node_summary']}
100
98
  </ENTITY CONTEXT>
101
-
99
+
102
100
  <ATTRIBUTES>
103
- {json.dumps(context['attributes'], indent=2)}
101
+ {to_prompt_json(context['attributes'])}
104
102
  </ATTRIBUTES>
105
103
  """,
106
104
  ),
@@ -117,10 +115,10 @@ def summary_description(context: dict[str, Any]) -> list[Message]:
117
115
  role='user',
118
116
  content=f"""
119
117
  Create a short one sentence description of the summary that explains what kind of information is summarized.
120
- Summaries must be under 250 words.
118
+ Summaries must be under 250 characters.
121
119
 
122
120
  Summary:
123
- {json.dumps(context['summary'], indent=2)}
121
+ {to_prompt_json(context['summary'])}
124
122
  """,
125
123
  ),
126
124
  ]