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
|
@@ -0,0 +1,74 @@
|
|
|
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, Protocol, TypedDict
|
|
18
|
+
|
|
19
|
+
from .models import Message, PromptFunction, PromptVersion
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class Prompt(Protocol):
|
|
23
|
+
v1: PromptVersion
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class Versions(TypedDict):
|
|
27
|
+
v1: PromptFunction
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def v1(context: dict[str, Any]) -> list[Message]:
|
|
31
|
+
return [
|
|
32
|
+
Message(
|
|
33
|
+
role='system',
|
|
34
|
+
content='You are an AI assistant that helps determine which relationships in a knowledge graph should be invalidated based solely on explicit contradictions in newer information.',
|
|
35
|
+
),
|
|
36
|
+
Message(
|
|
37
|
+
role='user',
|
|
38
|
+
content=f"""
|
|
39
|
+
Based on the provided existing edges and new edges with their timestamps, determine which relationships, if any, should be marked as expired due to contradictions or updates in the newer edges.
|
|
40
|
+
Use the start and end dates of the edges to determine which edges are to be marked expired.
|
|
41
|
+
Only mark a relationship as invalid if there is clear evidence from other edges that the relationship is no longer true.
|
|
42
|
+
Do not invalidate relationships merely because they weren't mentioned in the episodes. You may use the current episode and previous episodes as well as the facts of each edge to understand the context of the relationships.
|
|
43
|
+
|
|
44
|
+
Previous Episodes:
|
|
45
|
+
{context['previous_episodes']}
|
|
46
|
+
|
|
47
|
+
Current Episode:
|
|
48
|
+
{context['current_episode']}
|
|
49
|
+
|
|
50
|
+
Existing Edges (sorted by timestamp, newest first):
|
|
51
|
+
{context['existing_edges']}
|
|
52
|
+
|
|
53
|
+
New Edges:
|
|
54
|
+
{context['new_edges']}
|
|
55
|
+
|
|
56
|
+
Each edge is formatted as: "UUID | SOURCE_NODE - EDGE_NAME - TARGET_NODE (fact: EDGE_FACT), START_DATE (END_DATE, optional))"
|
|
57
|
+
|
|
58
|
+
For each existing edge that should be invalidated, respond with a JSON object in the following format:
|
|
59
|
+
{{
|
|
60
|
+
"invalidated_edges": [
|
|
61
|
+
{{
|
|
62
|
+
"edge_uuid": "The UUID of the edge to be invalidated (the part before the | character)",
|
|
63
|
+
"fact": "Updated fact of the edge"
|
|
64
|
+
}}
|
|
65
|
+
]
|
|
66
|
+
}}
|
|
67
|
+
|
|
68
|
+
If no relationships need to be invalidated based on these strict criteria, return an empty list for "invalidated_edges".
|
|
69
|
+
""",
|
|
70
|
+
),
|
|
71
|
+
]
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
versions: Versions = {'v1': v1}
|
|
@@ -0,0 +1,122 @@
|
|
|
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, Protocol, TypedDict
|
|
18
|
+
|
|
19
|
+
from .dedupe_edges import (
|
|
20
|
+
Prompt as DedupeEdgesPrompt,
|
|
21
|
+
)
|
|
22
|
+
from .dedupe_edges import (
|
|
23
|
+
Versions as DedupeEdgesVersions,
|
|
24
|
+
)
|
|
25
|
+
from .dedupe_edges import (
|
|
26
|
+
versions as dedupe_edges_versions,
|
|
27
|
+
)
|
|
28
|
+
from .dedupe_nodes import (
|
|
29
|
+
Prompt as DedupeNodesPrompt,
|
|
30
|
+
)
|
|
31
|
+
from .dedupe_nodes import (
|
|
32
|
+
Versions as DedupeNodesVersions,
|
|
33
|
+
)
|
|
34
|
+
from .dedupe_nodes import (
|
|
35
|
+
versions as dedupe_nodes_versions,
|
|
36
|
+
)
|
|
37
|
+
from .extract_edge_dates import (
|
|
38
|
+
Prompt as ExtractEdgeDatesPrompt,
|
|
39
|
+
)
|
|
40
|
+
from .extract_edge_dates import (
|
|
41
|
+
Versions as ExtractEdgeDatesVersions,
|
|
42
|
+
)
|
|
43
|
+
from .extract_edge_dates import (
|
|
44
|
+
versions as extract_edge_dates_versions,
|
|
45
|
+
)
|
|
46
|
+
from .extract_edges import (
|
|
47
|
+
Prompt as ExtractEdgesPrompt,
|
|
48
|
+
)
|
|
49
|
+
from .extract_edges import (
|
|
50
|
+
Versions as ExtractEdgesVersions,
|
|
51
|
+
)
|
|
52
|
+
from .extract_edges import (
|
|
53
|
+
versions as extract_edges_versions,
|
|
54
|
+
)
|
|
55
|
+
from .extract_nodes import (
|
|
56
|
+
Prompt as ExtractNodesPrompt,
|
|
57
|
+
)
|
|
58
|
+
from .extract_nodes import (
|
|
59
|
+
Versions as ExtractNodesVersions,
|
|
60
|
+
)
|
|
61
|
+
from .extract_nodes import (
|
|
62
|
+
versions as extract_nodes_versions,
|
|
63
|
+
)
|
|
64
|
+
from .invalidate_edges import (
|
|
65
|
+
Prompt as InvalidateEdgesPrompt,
|
|
66
|
+
)
|
|
67
|
+
from .invalidate_edges import (
|
|
68
|
+
Versions as InvalidateEdgesVersions,
|
|
69
|
+
)
|
|
70
|
+
from .invalidate_edges import (
|
|
71
|
+
versions as invalidate_edges_versions,
|
|
72
|
+
)
|
|
73
|
+
from .models import Message, PromptFunction
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
class PromptLibrary(Protocol):
|
|
77
|
+
extract_nodes: ExtractNodesPrompt
|
|
78
|
+
dedupe_nodes: DedupeNodesPrompt
|
|
79
|
+
extract_edges: ExtractEdgesPrompt
|
|
80
|
+
dedupe_edges: DedupeEdgesPrompt
|
|
81
|
+
invalidate_edges: InvalidateEdgesPrompt
|
|
82
|
+
extract_edge_dates: ExtractEdgeDatesPrompt
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
class PromptLibraryImpl(TypedDict):
|
|
86
|
+
extract_nodes: ExtractNodesVersions
|
|
87
|
+
dedupe_nodes: DedupeNodesVersions
|
|
88
|
+
extract_edges: ExtractEdgesVersions
|
|
89
|
+
dedupe_edges: DedupeEdgesVersions
|
|
90
|
+
invalidate_edges: InvalidateEdgesVersions
|
|
91
|
+
extract_edge_dates: ExtractEdgeDatesVersions
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
class VersionWrapper:
|
|
95
|
+
def __init__(self, func: PromptFunction):
|
|
96
|
+
self.func = func
|
|
97
|
+
|
|
98
|
+
def __call__(self, context: dict[str, Any]) -> list[Message]:
|
|
99
|
+
return self.func(context)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
class PromptTypeWrapper:
|
|
103
|
+
def __init__(self, versions: dict[str, PromptFunction]):
|
|
104
|
+
for version, func in versions.items():
|
|
105
|
+
setattr(self, version, VersionWrapper(func))
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
class PromptLibraryWrapper:
|
|
109
|
+
def __init__(self, library: PromptLibraryImpl):
|
|
110
|
+
for prompt_type, versions in library.items():
|
|
111
|
+
setattr(self, prompt_type, PromptTypeWrapper(versions)) # type: ignore[arg-type]
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
PROMPT_LIBRARY_IMPL: PromptLibraryImpl = {
|
|
115
|
+
'extract_nodes': extract_nodes_versions,
|
|
116
|
+
'dedupe_nodes': dedupe_nodes_versions,
|
|
117
|
+
'extract_edges': extract_edges_versions,
|
|
118
|
+
'dedupe_edges': dedupe_edges_versions,
|
|
119
|
+
'invalidate_edges': invalidate_edges_versions,
|
|
120
|
+
'extract_edge_dates': extract_edge_dates_versions,
|
|
121
|
+
}
|
|
122
|
+
prompt_library: PromptLibrary = PromptLibraryWrapper(PROMPT_LIBRARY_IMPL) # type: ignore[assignment]
|
|
@@ -0,0 +1,31 @@
|
|
|
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, Callable, Protocol
|
|
18
|
+
|
|
19
|
+
from pydantic import BaseModel
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class Message(BaseModel):
|
|
23
|
+
role: str
|
|
24
|
+
content: str
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class PromptVersion(Protocol):
|
|
28
|
+
def __call__(self, context: dict[str, Any]) -> list[Message]: ...
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
PromptFunction = Callable[[dict[str, Any]], list[Message]]
|
|
File without changes
|
|
@@ -0,0 +1,142 @@
|
|
|
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 datetime import datetime
|
|
19
|
+
from enum import Enum
|
|
20
|
+
from time import time
|
|
21
|
+
|
|
22
|
+
from neo4j import AsyncDriver
|
|
23
|
+
from pydantic import BaseModel, Field
|
|
24
|
+
|
|
25
|
+
from graphiti_core.edges import EntityEdge
|
|
26
|
+
from graphiti_core.llm_client.config import EMBEDDING_DIM
|
|
27
|
+
from graphiti_core.nodes import EntityNode, EpisodicNode
|
|
28
|
+
from graphiti_core.search.search_utils import (
|
|
29
|
+
edge_fulltext_search,
|
|
30
|
+
edge_similarity_search,
|
|
31
|
+
get_mentioned_nodes,
|
|
32
|
+
node_distance_reranker,
|
|
33
|
+
rrf,
|
|
34
|
+
)
|
|
35
|
+
from graphiti_core.utils import retrieve_episodes
|
|
36
|
+
from graphiti_core.utils.maintenance.graph_data_operations import EPISODE_WINDOW_LEN
|
|
37
|
+
|
|
38
|
+
logger = logging.getLogger(__name__)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class SearchMethod(Enum):
|
|
42
|
+
cosine_similarity = 'cosine_similarity'
|
|
43
|
+
bm25 = 'bm25'
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class Reranker(Enum):
|
|
47
|
+
rrf = 'reciprocal_rank_fusion'
|
|
48
|
+
node_distance = 'node_distance'
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class SearchConfig(BaseModel):
|
|
52
|
+
num_edges: int = Field(default=10)
|
|
53
|
+
num_nodes: int = Field(default=10)
|
|
54
|
+
num_episodes: int = EPISODE_WINDOW_LEN
|
|
55
|
+
search_methods: list[SearchMethod]
|
|
56
|
+
reranker: Reranker | None
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class SearchResults(BaseModel):
|
|
60
|
+
episodes: list[EpisodicNode]
|
|
61
|
+
nodes: list[EntityNode]
|
|
62
|
+
edges: list[EntityEdge]
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
async def hybrid_search(
|
|
66
|
+
driver: AsyncDriver,
|
|
67
|
+
embedder,
|
|
68
|
+
query: str,
|
|
69
|
+
timestamp: datetime,
|
|
70
|
+
config: SearchConfig,
|
|
71
|
+
center_node_uuid: str | None = None,
|
|
72
|
+
) -> SearchResults:
|
|
73
|
+
start = time()
|
|
74
|
+
|
|
75
|
+
episodes = []
|
|
76
|
+
nodes = []
|
|
77
|
+
edges = []
|
|
78
|
+
|
|
79
|
+
search_results = []
|
|
80
|
+
|
|
81
|
+
if config.num_episodes > 0:
|
|
82
|
+
episodes.extend(await retrieve_episodes(driver, timestamp, config.num_episodes))
|
|
83
|
+
nodes.extend(await get_mentioned_nodes(driver, episodes))
|
|
84
|
+
|
|
85
|
+
if SearchMethod.bm25 in config.search_methods:
|
|
86
|
+
text_search = await edge_fulltext_search(query, driver, 2 * config.num_edges)
|
|
87
|
+
search_results.append(text_search)
|
|
88
|
+
|
|
89
|
+
if SearchMethod.cosine_similarity in config.search_methods:
|
|
90
|
+
query_text = query.replace('\n', ' ')
|
|
91
|
+
search_vector = (
|
|
92
|
+
(await embedder.create(input=[query_text], model='text-embedding-3-small'))
|
|
93
|
+
.data[0]
|
|
94
|
+
.embedding[:EMBEDDING_DIM]
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
similarity_search = await edge_similarity_search(
|
|
98
|
+
search_vector, driver, 2 * config.num_edges
|
|
99
|
+
)
|
|
100
|
+
search_results.append(similarity_search)
|
|
101
|
+
|
|
102
|
+
if len(search_results) > 1 and config.reranker is None:
|
|
103
|
+
logger.exception('Multiple searches enabled without a reranker')
|
|
104
|
+
raise Exception('Multiple searches enabled without a reranker')
|
|
105
|
+
|
|
106
|
+
else:
|
|
107
|
+
edge_uuid_map = {}
|
|
108
|
+
search_result_uuids = []
|
|
109
|
+
|
|
110
|
+
for result in search_results:
|
|
111
|
+
result_uuids = []
|
|
112
|
+
for edge in result:
|
|
113
|
+
result_uuids.append(edge.uuid)
|
|
114
|
+
edge_uuid_map[edge.uuid] = edge
|
|
115
|
+
|
|
116
|
+
search_result_uuids.append(result_uuids)
|
|
117
|
+
|
|
118
|
+
search_result_uuids = [[edge.uuid for edge in result] for result in search_results]
|
|
119
|
+
|
|
120
|
+
reranked_uuids: list[str] = []
|
|
121
|
+
if config.reranker == Reranker.rrf:
|
|
122
|
+
reranked_uuids = rrf(search_result_uuids)
|
|
123
|
+
elif config.reranker == Reranker.node_distance:
|
|
124
|
+
if center_node_uuid is None:
|
|
125
|
+
logger.exception('No center node provided for Node Distance reranker')
|
|
126
|
+
raise Exception('No center node provided for Node Distance reranker')
|
|
127
|
+
reranked_uuids = await node_distance_reranker(
|
|
128
|
+
driver, search_result_uuids, center_node_uuid
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
reranked_edges = [edge_uuid_map[uuid] for uuid in reranked_uuids]
|
|
132
|
+
edges.extend(reranked_edges)
|
|
133
|
+
|
|
134
|
+
context = SearchResults(
|
|
135
|
+
episodes=episodes, nodes=nodes[: config.num_nodes], edges=edges[: config.num_edges]
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
end = time()
|
|
139
|
+
|
|
140
|
+
logger.info(f'search returned context for query {query} in {(end - start) * 1000} ms')
|
|
141
|
+
|
|
142
|
+
return context
|