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,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,21 +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.
106
+ Pronoun references such as he/she/they or this/that/those should be disambiguated to the names of the
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.
93
108
 
94
109
  1. **Speaker Extraction**: Always extract the speaker (the part before the colon `:` in each dialogue line) as the first entity node.
95
110
  - If the speaker is mentioned again in the message, treat both mentions as a **single entity**.
@@ -122,15 +137,16 @@ def extract_json(context: dict[str, Any]) -> list[Message]:
122
137
  Your primary task is to extract and classify relevant entities from JSON files"""
123
138
 
124
139
  user_prompt = f"""
140
+ <ENTITY TYPES>
141
+ {context['entity_types']}
142
+ </ENTITY TYPES>
143
+
125
144
  <SOURCE DESCRIPTION>:
126
145
  {context['source_description']}
127
146
  </SOURCE DESCRIPTION>
128
147
  <JSON>
129
148
  {context['episode_content']}
130
149
  </JSON>
131
- <ENTITY TYPES>
132
- {context['entity_types']}
133
- </ENTITY TYPES>
134
150
 
135
151
  {context['custom_prompt']}
136
152
 
@@ -139,8 +155,9 @@ For each entity extracted, also determine its entity type based on the provided
139
155
  Indicate the classified entity type by providing its entity_type_id.
140
156
 
141
157
  Guidelines:
142
- 1. Always try to extract an entities that the JSON represents. This will often be something like a "name" or "user field
143
- 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
144
161
  """
145
162
  return [
146
163
  Message(role='system', content=sys_prompt),
@@ -153,13 +170,14 @@ def extract_text(context: dict[str, Any]) -> list[Message]:
153
170
  Your primary task is to extract and classify the speaker and other significant entities mentioned in the provided text."""
154
171
 
155
172
  user_prompt = f"""
156
- <TEXT>
157
- {context['episode_content']}
158
- </TEXT>
159
173
  <ENTITY TYPES>
160
174
  {context['entity_types']}
161
175
  </ENTITY TYPES>
162
176
 
177
+ <TEXT>
178
+ {context['episode_content']}
179
+ </TEXT>
180
+
163
181
  Given the above text, extract entities from the TEXT that are explicitly or implicitly mentioned.
164
182
  For each entity extracted, also determine its entity type based on the provided ENTITY TYPES and their descriptions.
165
183
  Indicate the classified entity type by providing its entity_type_id.
@@ -183,7 +201,7 @@ def reflexion(context: dict[str, Any]) -> list[Message]:
183
201
 
184
202
  user_prompt = f"""
185
203
  <PREVIOUS MESSAGES>
186
- {json.dumps([ep for ep in context['previous_episodes']], indent=2)}
204
+ {to_prompt_json([ep for ep in context['previous_episodes']])}
187
205
  </PREVIOUS MESSAGES>
188
206
  <CURRENT MESSAGE>
189
207
  {context['episode_content']}
@@ -207,22 +225,22 @@ def classify_nodes(context: dict[str, Any]) -> list[Message]:
207
225
 
208
226
  user_prompt = f"""
209
227
  <PREVIOUS MESSAGES>
210
- {json.dumps([ep for ep in context['previous_episodes']], indent=2)}
228
+ {to_prompt_json([ep for ep in context['previous_episodes']])}
211
229
  </PREVIOUS MESSAGES>
212
230
  <CURRENT MESSAGE>
213
231
  {context['episode_content']}
214
232
  </CURRENT MESSAGE>
215
-
233
+
216
234
  <EXTRACTED ENTITIES>
217
235
  {context['extracted_entities']}
218
236
  </EXTRACTED ENTITIES>
219
-
237
+
220
238
  <ENTITY TYPES>
221
239
  {context['entity_types']}
222
240
  </ENTITY TYPES>
223
-
241
+
224
242
  Given the above conversation, extracted entities, and provided entity types and their descriptions, classify the extracted entities.
225
-
243
+
226
244
  Guidelines:
227
245
  1. Each entity must have exactly one type
228
246
  2. Only use the provided ENTITY TYPES as types, do not use additional types to classify entities.
@@ -243,21 +261,45 @@ def extract_attributes(context: dict[str, Any]) -> list[Message]:
243
261
  Message(
244
262
  role='user',
245
263
  content=f"""
246
-
247
- <MESSAGES>
248
- {json.dumps(context['previous_episodes'], indent=2)}
249
- {json.dumps(context['episode_content'], indent=2)}
250
- </MESSAGES>
251
-
252
- 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
253
265
  in MESSAGES. Use the provided attribute descriptions to better understand how each attribute should be determined.
254
266
 
255
267
  Guidelines:
256
268
  1. Do not hallucinate entity property values if they cannot be found in the current context.
257
269
  2. Only use the provided MESSAGES and ENTITY to set attribute values.
258
- 3. The summary attribute represents a summary of the ENTITY, and should be updated with new information about the Entity from the MESSAGES.
259
- Summaries must be no longer than 250 words.
260
-
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
+
261
303
  <ENTITY>
262
304
  {context['node']}
263
305
  </ENTITY>
@@ -271,6 +313,7 @@ versions: Versions = {
271
313
  'extract_json': extract_json,
272
314
  'extract_text': extract_text,
273
315
  'reflexion': reflexion,
316
+ 'extract_summary': extract_summary,
274
317
  'classify_nodes': classify_nodes,
275
318
  'extract_attributes': extract_attributes,
276
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
  ]