graphiti-core 0.1.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/__init__.py +3 -0
- graphiti_core/edges.py +232 -0
- graphiti_core/graphiti.py +618 -0
- graphiti_core/helpers.py +7 -0
- graphiti_core/llm_client/__init__.py +5 -0
- graphiti_core/llm_client/anthropic_client.py +63 -0
- graphiti_core/llm_client/client.py +96 -0
- graphiti_core/llm_client/config.py +58 -0
- graphiti_core/llm_client/groq_client.py +64 -0
- graphiti_core/llm_client/openai_client.py +65 -0
- graphiti_core/llm_client/utils.py +22 -0
- graphiti_core/nodes.py +250 -0
- graphiti_core/prompts/__init__.py +4 -0
- graphiti_core/prompts/dedupe_edges.py +154 -0
- graphiti_core/prompts/dedupe_nodes.py +151 -0
- graphiti_core/prompts/extract_edge_dates.py +60 -0
- graphiti_core/prompts/extract_edges.py +138 -0
- graphiti_core/prompts/extract_nodes.py +145 -0
- graphiti_core/prompts/invalidate_edges.py +74 -0
- graphiti_core/prompts/lib.py +122 -0
- graphiti_core/prompts/models.py +31 -0
- graphiti_core/search/__init__.py +0 -0
- graphiti_core/search/search.py +142 -0
- graphiti_core/search/search_utils.py +454 -0
- graphiti_core/utils/__init__.py +15 -0
- graphiti_core/utils/bulk_utils.py +227 -0
- graphiti_core/utils/maintenance/__init__.py +16 -0
- graphiti_core/utils/maintenance/edge_operations.py +170 -0
- graphiti_core/utils/maintenance/graph_data_operations.py +133 -0
- graphiti_core/utils/maintenance/node_operations.py +199 -0
- graphiti_core/utils/maintenance/temporal_operations.py +184 -0
- graphiti_core/utils/maintenance/utils.py +0 -0
- graphiti_core/utils/utils.py +39 -0
- graphiti_core-0.1.0.dist-info/LICENSE +201 -0
- graphiti_core-0.1.0.dist-info/METADATA +199 -0
- graphiti_core-0.1.0.dist-info/RECORD +37 -0
- graphiti_core-0.1.0.dist-info/WHEEL +4 -0
graphiti_core/edges.py
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
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 logging
|
|
18
|
+
from abc import ABC, abstractmethod
|
|
19
|
+
from datetime import datetime
|
|
20
|
+
from time import time
|
|
21
|
+
from uuid import uuid4
|
|
22
|
+
|
|
23
|
+
from neo4j import AsyncDriver
|
|
24
|
+
from pydantic import BaseModel, Field
|
|
25
|
+
|
|
26
|
+
from graphiti_core.helpers import parse_db_date
|
|
27
|
+
from graphiti_core.llm_client.config import EMBEDDING_DIM
|
|
28
|
+
from graphiti_core.nodes import Node
|
|
29
|
+
|
|
30
|
+
logger = logging.getLogger(__name__)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class Edge(BaseModel, ABC):
|
|
34
|
+
uuid: str = Field(default_factory=lambda: uuid4().hex)
|
|
35
|
+
source_node_uuid: str
|
|
36
|
+
target_node_uuid: str
|
|
37
|
+
created_at: datetime
|
|
38
|
+
|
|
39
|
+
@abstractmethod
|
|
40
|
+
async def save(self, driver: AsyncDriver): ...
|
|
41
|
+
|
|
42
|
+
@abstractmethod
|
|
43
|
+
async def delete(self, driver: AsyncDriver): ...
|
|
44
|
+
|
|
45
|
+
def __hash__(self):
|
|
46
|
+
return hash(self.uuid)
|
|
47
|
+
|
|
48
|
+
def __eq__(self, other):
|
|
49
|
+
if isinstance(other, Node):
|
|
50
|
+
return self.uuid == other.uuid
|
|
51
|
+
return False
|
|
52
|
+
|
|
53
|
+
@classmethod
|
|
54
|
+
async def get_by_uuid(cls, driver: AsyncDriver, uuid: str): ...
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class EpisodicEdge(Edge):
|
|
58
|
+
async def save(self, driver: AsyncDriver):
|
|
59
|
+
result = await driver.execute_query(
|
|
60
|
+
"""
|
|
61
|
+
MATCH (episode:Episodic {uuid: $episode_uuid})
|
|
62
|
+
MATCH (node:Entity {uuid: $entity_uuid})
|
|
63
|
+
MERGE (episode)-[r:MENTIONS {uuid: $uuid}]->(node)
|
|
64
|
+
SET r = {uuid: $uuid, created_at: $created_at}
|
|
65
|
+
RETURN r.uuid AS uuid""",
|
|
66
|
+
episode_uuid=self.source_node_uuid,
|
|
67
|
+
entity_uuid=self.target_node_uuid,
|
|
68
|
+
uuid=self.uuid,
|
|
69
|
+
created_at=self.created_at,
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
logger.info(f'Saved edge to neo4j: {self.uuid}')
|
|
73
|
+
|
|
74
|
+
return result
|
|
75
|
+
|
|
76
|
+
async def delete(self, driver: AsyncDriver):
|
|
77
|
+
result = await driver.execute_query(
|
|
78
|
+
"""
|
|
79
|
+
MATCH (n:Episodic)-[e:MENTIONS {uuid: $uuid}]->(m:Entity)
|
|
80
|
+
DELETE e
|
|
81
|
+
""",
|
|
82
|
+
uuid=self.uuid,
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
logger.info(f'Deleted Edge: {self.uuid}')
|
|
86
|
+
|
|
87
|
+
return result
|
|
88
|
+
|
|
89
|
+
@classmethod
|
|
90
|
+
async def get_by_uuid(cls, driver: AsyncDriver, uuid: str):
|
|
91
|
+
records, _, _ = await driver.execute_query(
|
|
92
|
+
"""
|
|
93
|
+
MATCH (n:Episodic)-[e:MENTIONS {uuid: $uuid}]->(m:Entity)
|
|
94
|
+
RETURN
|
|
95
|
+
e.uuid As uuid,
|
|
96
|
+
n.uuid AS source_node_uuid,
|
|
97
|
+
m.uuid AS target_node_uuid,
|
|
98
|
+
e.created_at AS created_at
|
|
99
|
+
""",
|
|
100
|
+
uuid=uuid,
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
edges: list[EpisodicEdge] = []
|
|
104
|
+
|
|
105
|
+
for record in records:
|
|
106
|
+
edges.append(
|
|
107
|
+
EpisodicEdge(
|
|
108
|
+
uuid=record['uuid'],
|
|
109
|
+
source_node_uuid=record['source_node_uuid'],
|
|
110
|
+
target_node_uuid=record['target_node_uuid'],
|
|
111
|
+
created_at=record['created_at'].to_native(),
|
|
112
|
+
)
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
logger.info(f'Found Edge: {uuid}')
|
|
116
|
+
|
|
117
|
+
return edges[0]
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
class EntityEdge(Edge):
|
|
121
|
+
name: str = Field(description='name of the edge, relation name')
|
|
122
|
+
fact: str = Field(description='fact representing the edge and nodes that it connects')
|
|
123
|
+
fact_embedding: list[float] | None = Field(default=None, description='embedding of the fact')
|
|
124
|
+
episodes: list[str] | None = Field(
|
|
125
|
+
default=None,
|
|
126
|
+
description='list of episode ids that reference these entity edges',
|
|
127
|
+
)
|
|
128
|
+
expired_at: datetime | None = Field(
|
|
129
|
+
default=None, description='datetime of when the node was invalidated'
|
|
130
|
+
)
|
|
131
|
+
valid_at: datetime | None = Field(
|
|
132
|
+
default=None, description='datetime of when the fact became true'
|
|
133
|
+
)
|
|
134
|
+
invalid_at: datetime | None = Field(
|
|
135
|
+
default=None, description='datetime of when the fact stopped being true'
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
async def generate_embedding(self, embedder, model='text-embedding-3-small'):
|
|
139
|
+
start = time()
|
|
140
|
+
|
|
141
|
+
text = self.fact.replace('\n', ' ')
|
|
142
|
+
embedding = (await embedder.create(input=[text], model=model)).data[0].embedding
|
|
143
|
+
self.fact_embedding = embedding[:EMBEDDING_DIM]
|
|
144
|
+
|
|
145
|
+
end = time()
|
|
146
|
+
logger.info(f'embedded {text} in {end - start} ms')
|
|
147
|
+
|
|
148
|
+
return embedding
|
|
149
|
+
|
|
150
|
+
async def save(self, driver: AsyncDriver):
|
|
151
|
+
result = await driver.execute_query(
|
|
152
|
+
"""
|
|
153
|
+
MATCH (source:Entity {uuid: $source_uuid})
|
|
154
|
+
MATCH (target:Entity {uuid: $target_uuid})
|
|
155
|
+
MERGE (source)-[r:RELATES_TO {uuid: $uuid}]->(target)
|
|
156
|
+
SET r = {uuid: $uuid, name: $name, fact: $fact, fact_embedding: $fact_embedding,
|
|
157
|
+
episodes: $episodes, created_at: $created_at, expired_at: $expired_at,
|
|
158
|
+
valid_at: $valid_at, invalid_at: $invalid_at}
|
|
159
|
+
RETURN r.uuid AS uuid""",
|
|
160
|
+
source_uuid=self.source_node_uuid,
|
|
161
|
+
target_uuid=self.target_node_uuid,
|
|
162
|
+
uuid=self.uuid,
|
|
163
|
+
name=self.name,
|
|
164
|
+
fact=self.fact,
|
|
165
|
+
fact_embedding=self.fact_embedding,
|
|
166
|
+
episodes=self.episodes,
|
|
167
|
+
created_at=self.created_at,
|
|
168
|
+
expired_at=self.expired_at,
|
|
169
|
+
valid_at=self.valid_at,
|
|
170
|
+
invalid_at=self.invalid_at,
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
logger.info(f'Saved edge to neo4j: {self.uuid}')
|
|
174
|
+
|
|
175
|
+
return result
|
|
176
|
+
|
|
177
|
+
async def delete(self, driver: AsyncDriver):
|
|
178
|
+
result = await driver.execute_query(
|
|
179
|
+
"""
|
|
180
|
+
MATCH (n:Entity)-[e:RELATES_TO {uuid: $uuid}]->(m:Entity)
|
|
181
|
+
DELETE e
|
|
182
|
+
""",
|
|
183
|
+
uuid=self.uuid,
|
|
184
|
+
)
|
|
185
|
+
|
|
186
|
+
logger.info(f'Deleted Edge: {self.uuid}')
|
|
187
|
+
|
|
188
|
+
return result
|
|
189
|
+
|
|
190
|
+
@classmethod
|
|
191
|
+
async def get_by_uuid(cls, driver: AsyncDriver, uuid: str):
|
|
192
|
+
records, _, _ = await driver.execute_query(
|
|
193
|
+
"""
|
|
194
|
+
MATCH (n:Entity)-[e:RELATES_TO {uuid: $uuid}]->(m:Entity)
|
|
195
|
+
RETURN
|
|
196
|
+
e.uuid AS uuid,
|
|
197
|
+
n.uuid AS source_node_uuid,
|
|
198
|
+
m.uuid AS target_node_uuid,
|
|
199
|
+
e.created_at AS created_at,
|
|
200
|
+
e.name AS name,
|
|
201
|
+
e.fact AS fact,
|
|
202
|
+
e.fact_embedding AS fact_embedding,
|
|
203
|
+
e.episodes AS episodes,
|
|
204
|
+
e.expired_at AS expired_at,
|
|
205
|
+
e.valid_at AS valid_at,
|
|
206
|
+
e.invalid_at AS invalid_at
|
|
207
|
+
""",
|
|
208
|
+
uuid=uuid,
|
|
209
|
+
)
|
|
210
|
+
|
|
211
|
+
edges: list[EntityEdge] = []
|
|
212
|
+
|
|
213
|
+
for record in records:
|
|
214
|
+
edges.append(
|
|
215
|
+
EntityEdge(
|
|
216
|
+
uuid=record['uuid'],
|
|
217
|
+
source_node_uuid=record['source_node_uuid'],
|
|
218
|
+
target_node_uuid=record['target_node_uuid'],
|
|
219
|
+
fact=record['fact'],
|
|
220
|
+
name=record['name'],
|
|
221
|
+
episodes=record['episodes'],
|
|
222
|
+
fact_embedding=record['fact_embedding'],
|
|
223
|
+
created_at=record['created_at'].to_native(),
|
|
224
|
+
expired_at=parse_db_date(record['expired_at']),
|
|
225
|
+
valid_at=parse_db_date(record['valid_at']),
|
|
226
|
+
invalid_at=parse_db_date(record['invalid_at']),
|
|
227
|
+
)
|
|
228
|
+
)
|
|
229
|
+
|
|
230
|
+
logger.info(f'Found Edge: {uuid}')
|
|
231
|
+
|
|
232
|
+
return edges[0]
|