Elsai Graph Query RAG#

The Elsai Graph Query RAG package enables advanced question-answering capabilities by combining knowledge graph traversal with vector similarity search. It allows users to ask natural language questions and receive accurate, context-aware answers derived from a Neo4j knowledge graph.

Prerequisites#

  • Python >= 3.10

  • Neo4j database instance populated with data (see elsai-graph-generator)

  • LLM API access (Azure OpenAI, Amazon Bedrock, or Gemini)

  • .env file with Neo4j and LLM credentials

Installation#

To install the elsai-graph-query-rag package:

pip install --extra-index-url https://elsai-core-package.optisolbusiness.com/root/elsai-graph-query-rag/ elsai-graph-query-rag==0.1.0

Core Components#

1. Neo4jRetriever#

The Neo4jRetriever translates natural language questions into Cypher queries using an LLM. It can retrieve specific entities, explore relationships, and execute custom database queries.

from elsai_graph_query_rag import Neo4jRetriever

# Initialize with a Neo4j connector and an LLM adapter
neo4j_retriever = Neo4jRetriever(connector, llm, verbose=True)

# Generate and execute a Cypher query from natural language
success, message, cypher_query = neo4j_retriever.generate_cypher_query("Who works at Microsoft?")
success, message, results = neo4j_retriever.execute_cypher_query(cypher_query)

# Get a specific entity by name
success, message, entity = neo4j_retriever.get_entity_by_name("Microsoft")

2. NeighborhoodExpander#

The NeighborhoodExpander is used to discover entities connected to a starting point. It allows for “hopping” through relationship levels to find related information across the graph.

from elsai_graph_query_rag import NeighborhoodExpander

neighborhood_expander = NeighborhoodExpander(connector, verbose=True)

# Find all entities connected to Microsoft within 2 hops
success, message, neighborhood = neighborhood_expander.expand_neighborhood(
    "Microsoft",
    max_hops=2,
    max_nodes=30
)

# Find paths/subgraphs between a group of entities
success, message, subgraph = neighborhood_expander.get_subgraph(["John", "Microsoft", "Seattle"])

3. HybridGraphRetriever#

The HybridGraphRetriever combines vector similarity search with graph-based retrieval. This dual approach ensures that the system finds both semantically similar entities and those with explicit structural relationships.

from elsai_graph_query_rag import HybridGraphRetriever

hybrid_retriever = HybridGraphRetriever(
    connector,
    embedding_manager,
    neo4j_retriever,
    verbose=True
)

# Perform a hybrid search
success, message, results = hybrid_retriever.hybrid_retrieve(
    "Who is the CEO of Microsoft?",
    top_k_vector=5,
    include_neighbors=True
)

4. GraphRAGEngine#

The GraphRAGEngine is the high-level interface for the RAG system. It coordinates the retrieval process and uses an LLM to generate a natural language answer based on the retrieved context.

from elsai_graph_query_rag import GraphRAGEngine

rag_engine = GraphRAGEngine(neo4j_retriever, hybrid_retriever, llm, verbose=True)

# Ask a question and get a plain answer
success, message, answer = rag_engine.ask("How is John connected to Microsoft?")

# Get an answer along with the source context and metadata
success, message, detailed_result = rag_engine.ask_with_explanation("What is Microsoft's location?")

Full Pipeline Example#

Below is a complete example demonstrating the standard workflow for answering questions from a knowledge graph.

from elsai_graph_query_rag import Neo4jRetriever, HybridGraphRetriever, GraphRAGEngine

# 1. Setup retrievers
neo4j_retriever = Neo4jRetriever(connector, llm)
hybrid_retriever = HybridGraphRetriever(connector, embedding_manager, neo4j_retriever)

# 2. Initialize the RAG Engine
rag_engine = GraphRAGEngine(neo4j_retriever, hybrid_retriever, llm)

# 3. Ask a question (Hybrid mode by default)
# This combines vector similarity and graph structural data
success, message, answer = rag_engine.ask("Who founded Microsoft?")

# 4. View results
if success:
    print(f"Answer: {answer}")