Introduction.
Continuing with this post from yesterday, I summarized the construction of a GraphRAG system using hybrid search.
I had imagined connecting pipes “|” like LangChain’s LCEL, but it was not what I had imagined. Please check it out below.
Information Sources
- Hybrid Retrieval for GraphRAG Applications Using the Neo4j GraphRAG Package for Python I tried the code in this article.
Execution results
Connecting to the neo4j database and setting the OpenAI API key as an environment variable is the same as in this post.
Preparing HybridRetriever
Replace VectorRetriever in this post with HybridRetriever as follows.
# Hybrid Retriever
from neo4j import GraphDatabase
from neo4j_graphrag.embeddings.openai import OpenAIEmbeddings
from neo4j_graphrag.retrievers import HybridRetriever
embedder = OpenAIEmbeddings(model="text-embedding-ada-002")
retriever = HybridRetriever(
driver,
vector_index_name="moviePlotsEmbedding",
fulltext_index_name="movieFulltext",
embedder=embedder,
return_properties=["title", "plot"],
)
Building a pipeline with GraphRAG
The pipeline section is the same as this post, except for the query.
# GraphRAG
from neo4j_graphrag.llm import OpenAILLM
from neo4j_graphrag.generation import GraphRAG
# LLM
# Note: the OPENAI_API_KEY must be in the env vars
llm = OpenAILLM(model_name="gpt-4o-mini", model_params={"temperature": 0})
# Initialize the RAG pipline
rag = GraphRAG(retriever=retriever, llm=llm)
# Query the graph
query_text = "What is the name of the movie set in 1375 in Imperial China?"
response = rag.search(query_text=query_text, retriever_config={"top_k": 3})
print(response.answer)
Execution Result
The name of the movie set in 1375 in Imperial China is "Musa the Warrior (Musa)."
Summary
Since the HybridRetriever class is provided in the neo4j_graphrag package, it can be easily realized by calling the GraphRAG class with that object as the retriever.