* add episode scope to search * bump version * linter * Update graphiti_core/search/search_helpers.py Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com> * mypy --------- Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>
223 lines
7.3 KiB
Python
223 lines
7.3 KiB
Python
"""
|
|
Copyright 2024, Zep Software, Inc.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
"""
|
|
|
|
from graphiti_core.search.search_config import (
|
|
CommunityReranker,
|
|
CommunitySearchConfig,
|
|
CommunitySearchMethod,
|
|
EdgeReranker,
|
|
EdgeSearchConfig,
|
|
EdgeSearchMethod,
|
|
EpisodeReranker,
|
|
EpisodeSearchConfig,
|
|
EpisodeSearchMethod,
|
|
NodeReranker,
|
|
NodeSearchConfig,
|
|
NodeSearchMethod,
|
|
SearchConfig,
|
|
)
|
|
|
|
# Performs a hybrid search with rrf reranking over edges, nodes, and communities
|
|
COMBINED_HYBRID_SEARCH_RRF = SearchConfig(
|
|
edge_config=EdgeSearchConfig(
|
|
search_methods=[EdgeSearchMethod.bm25, EdgeSearchMethod.cosine_similarity],
|
|
reranker=EdgeReranker.rrf,
|
|
),
|
|
node_config=NodeSearchConfig(
|
|
search_methods=[NodeSearchMethod.bm25, NodeSearchMethod.cosine_similarity],
|
|
reranker=NodeReranker.rrf,
|
|
),
|
|
episode_config=EpisodeSearchConfig(
|
|
search_methods=[
|
|
EpisodeSearchMethod.bm25,
|
|
],
|
|
reranker=EpisodeReranker.rrf,
|
|
),
|
|
community_config=CommunitySearchConfig(
|
|
search_methods=[CommunitySearchMethod.bm25, CommunitySearchMethod.cosine_similarity],
|
|
reranker=CommunityReranker.rrf,
|
|
),
|
|
)
|
|
|
|
# Performs a hybrid search with mmr reranking over edges, nodes, and communities
|
|
COMBINED_HYBRID_SEARCH_MMR = SearchConfig(
|
|
edge_config=EdgeSearchConfig(
|
|
search_methods=[EdgeSearchMethod.bm25, EdgeSearchMethod.cosine_similarity],
|
|
reranker=EdgeReranker.mmr,
|
|
mmr_lambda=1,
|
|
),
|
|
node_config=NodeSearchConfig(
|
|
search_methods=[NodeSearchMethod.bm25, NodeSearchMethod.cosine_similarity],
|
|
reranker=NodeReranker.mmr,
|
|
mmr_lambda=1,
|
|
),
|
|
episode_config=EpisodeSearchConfig(
|
|
search_methods=[
|
|
EpisodeSearchMethod.bm25,
|
|
],
|
|
reranker=EpisodeReranker.rrf,
|
|
),
|
|
community_config=CommunitySearchConfig(
|
|
search_methods=[CommunitySearchMethod.bm25, CommunitySearchMethod.cosine_similarity],
|
|
reranker=CommunityReranker.mmr,
|
|
mmr_lambda=1,
|
|
),
|
|
)
|
|
|
|
# Performs a full-text search, similarity search, and bfs with cross_encoder reranking over edges, nodes, and communities
|
|
COMBINED_HYBRID_SEARCH_CROSS_ENCODER = SearchConfig(
|
|
edge_config=EdgeSearchConfig(
|
|
search_methods=[
|
|
EdgeSearchMethod.bm25,
|
|
EdgeSearchMethod.cosine_similarity,
|
|
EdgeSearchMethod.bfs,
|
|
],
|
|
reranker=EdgeReranker.cross_encoder,
|
|
),
|
|
node_config=NodeSearchConfig(
|
|
search_methods=[
|
|
NodeSearchMethod.bm25,
|
|
NodeSearchMethod.cosine_similarity,
|
|
NodeSearchMethod.bfs,
|
|
],
|
|
reranker=NodeReranker.cross_encoder,
|
|
),
|
|
episode_config=EpisodeSearchConfig(
|
|
search_methods=[
|
|
EpisodeSearchMethod.bm25,
|
|
],
|
|
reranker=EpisodeReranker.cross_encoder,
|
|
),
|
|
community_config=CommunitySearchConfig(
|
|
search_methods=[CommunitySearchMethod.bm25, CommunitySearchMethod.cosine_similarity],
|
|
reranker=CommunityReranker.cross_encoder,
|
|
),
|
|
)
|
|
|
|
# performs a hybrid search over edges with rrf reranking
|
|
EDGE_HYBRID_SEARCH_RRF = SearchConfig(
|
|
edge_config=EdgeSearchConfig(
|
|
search_methods=[EdgeSearchMethod.bm25, EdgeSearchMethod.cosine_similarity],
|
|
reranker=EdgeReranker.rrf,
|
|
)
|
|
)
|
|
|
|
# performs a hybrid search over edges with mmr reranking
|
|
EDGE_HYBRID_SEARCH_MMR = SearchConfig(
|
|
edge_config=EdgeSearchConfig(
|
|
search_methods=[EdgeSearchMethod.bm25, EdgeSearchMethod.cosine_similarity],
|
|
reranker=EdgeReranker.mmr,
|
|
)
|
|
)
|
|
|
|
# performs a hybrid search over edges with node distance reranking
|
|
EDGE_HYBRID_SEARCH_NODE_DISTANCE = SearchConfig(
|
|
edge_config=EdgeSearchConfig(
|
|
search_methods=[EdgeSearchMethod.bm25, EdgeSearchMethod.cosine_similarity],
|
|
reranker=EdgeReranker.node_distance,
|
|
),
|
|
)
|
|
|
|
# performs a hybrid search over edges with episode mention reranking
|
|
EDGE_HYBRID_SEARCH_EPISODE_MENTIONS = SearchConfig(
|
|
edge_config=EdgeSearchConfig(
|
|
search_methods=[EdgeSearchMethod.bm25, EdgeSearchMethod.cosine_similarity],
|
|
reranker=EdgeReranker.episode_mentions,
|
|
)
|
|
)
|
|
|
|
# performs a hybrid search over edges with cross encoder reranking
|
|
EDGE_HYBRID_SEARCH_CROSS_ENCODER = SearchConfig(
|
|
edge_config=EdgeSearchConfig(
|
|
search_methods=[
|
|
EdgeSearchMethod.bm25,
|
|
EdgeSearchMethod.cosine_similarity,
|
|
EdgeSearchMethod.bfs,
|
|
],
|
|
reranker=EdgeReranker.cross_encoder,
|
|
),
|
|
limit=10,
|
|
)
|
|
|
|
# performs a hybrid search over nodes with rrf reranking
|
|
NODE_HYBRID_SEARCH_RRF = SearchConfig(
|
|
node_config=NodeSearchConfig(
|
|
search_methods=[NodeSearchMethod.bm25, NodeSearchMethod.cosine_similarity],
|
|
reranker=NodeReranker.rrf,
|
|
)
|
|
)
|
|
|
|
# performs a hybrid search over nodes with mmr reranking
|
|
NODE_HYBRID_SEARCH_MMR = SearchConfig(
|
|
node_config=NodeSearchConfig(
|
|
search_methods=[NodeSearchMethod.bm25, NodeSearchMethod.cosine_similarity],
|
|
reranker=NodeReranker.mmr,
|
|
)
|
|
)
|
|
|
|
# performs a hybrid search over nodes with node distance reranking
|
|
NODE_HYBRID_SEARCH_NODE_DISTANCE = SearchConfig(
|
|
node_config=NodeSearchConfig(
|
|
search_methods=[NodeSearchMethod.bm25, NodeSearchMethod.cosine_similarity],
|
|
reranker=NodeReranker.node_distance,
|
|
)
|
|
)
|
|
|
|
# performs a hybrid search over nodes with episode mentions reranking
|
|
NODE_HYBRID_SEARCH_EPISODE_MENTIONS = SearchConfig(
|
|
node_config=NodeSearchConfig(
|
|
search_methods=[NodeSearchMethod.bm25, NodeSearchMethod.cosine_similarity],
|
|
reranker=NodeReranker.episode_mentions,
|
|
)
|
|
)
|
|
|
|
# performs a hybrid search over nodes with episode mentions reranking
|
|
NODE_HYBRID_SEARCH_CROSS_ENCODER = SearchConfig(
|
|
node_config=NodeSearchConfig(
|
|
search_methods=[
|
|
NodeSearchMethod.bm25,
|
|
NodeSearchMethod.cosine_similarity,
|
|
NodeSearchMethod.bfs,
|
|
],
|
|
reranker=NodeReranker.cross_encoder,
|
|
),
|
|
limit=10,
|
|
)
|
|
|
|
# performs a hybrid search over communities with rrf reranking
|
|
COMMUNITY_HYBRID_SEARCH_RRF = SearchConfig(
|
|
community_config=CommunitySearchConfig(
|
|
search_methods=[CommunitySearchMethod.bm25, CommunitySearchMethod.cosine_similarity],
|
|
reranker=CommunityReranker.rrf,
|
|
)
|
|
)
|
|
|
|
# performs a hybrid search over communities with mmr reranking
|
|
COMMUNITY_HYBRID_SEARCH_MMR = SearchConfig(
|
|
community_config=CommunitySearchConfig(
|
|
search_methods=[CommunitySearchMethod.bm25, CommunitySearchMethod.cosine_similarity],
|
|
reranker=CommunityReranker.mmr,
|
|
)
|
|
)
|
|
|
|
# performs a hybrid search over communities with mmr reranking
|
|
COMMUNITY_HYBRID_SEARCH_CROSS_ENCODER = SearchConfig(
|
|
community_config=CommunitySearchConfig(
|
|
search_methods=[CommunitySearchMethod.bm25, CommunitySearchMethod.cosine_similarity],
|
|
reranker=CommunityReranker.cross_encoder,
|
|
),
|
|
limit=3,
|
|
)
|