Searching the Graph
How to retrieve information from your Graphiti graph
The examples below demonstrate two search approaches in the Graphiti library:
-
Hybrid Search:
Combines semantic similarity and BM25 retrieval, reranked using Reciprocal Rank Fusion.
Example: Does a broad retrieval of facts related to Allbirds Wool Runners and Jane’s purchase.
-
Node Distance Reranking:
Extends Hybrid Search above by prioritizing results based on proximity to a specified node in the graph.
Example: Focuses on Jane-specific information, highlighting her wool allergy.
Node Distance Reranking is particularly useful for entity-specific queries, providing more contextually relevant results. It weights facts by their closeness to the focal node, emphasizing information directly related to the entity of interest.
This dual approach allows for both broad exploration and targeted, entity-specific information retrieval from the knowledge graph.
Configurable Search Strategies
Graphiti also provides a low-level search method that is more configurable than the out-of-the-box search.
This search method can be called using graphiti._search()
and passing in an additional config parameter of type SearchConfig
.
SearchConfig
contains 4 fields: one for the limit, and three more configs for each of edges, nodes, and communities.
The graphiti._search()
method returns a SearchResults
object containing a list of nodes, edges, and communities.
The graphiti._search()
method is quite configurable and can be complicated to work with at first.
As such, we also have a search_config_recipes.py
file that contains a few prebuilt SearchConfig
recipes for common use cases.
The 15 recipes are the following:
Supported Reranking Approaches
Reciprocal Rank Fusion (RRF) enhances search by combining results from different algorithms, like BM25 and semantic search. Each algorithm’s results are ranked, converted to reciprocal scores (1/rank), and summed. This aggregated score determines the final ranking, leveraging the strengths of each method for more accurate retrieval.
Maximal Marginal Relevance (MMR) is a search strategy that balances relevance and diversity in results. It selects results that are both relevant to the query and diverse from already chosen ones, reducing redundancy and covering different query aspects. MMR ensures comprehensive and varied search results by iteratively choosing results that maximize relevance while minimizing similarity to previously selected results.
A Cross-Encoder is a model that jointly encodes a query and a result, scoring their relevance by considering their combined context. This approach often yields more accurate results compared to methods that encode query and a text separately.
Graphiti supports two cross encoders:
OpenAIRerankerClient
(the default) andBGERerankerClient
. Rather than use a cross-encoder model, theOpenAIRerankerClient
uses an OpenAI model to classify relevance and the resultinglogprobs
are used to rerank results.- The
BGERerankerClient
uses theBAAI/bge-reranker-v2-m3
model and requiressentence_transformers
be installed.