Skip to content

Elsai Graph Query RAG

Package: elsai-graph-query-rag  v0.1.0

Answers natural language questions by combining knowledge graph traversal with vector similarity search — finding both semantically related and structurally connected information.

Installation

bash
pip install --extra-index-url https://core-packages.elsai.ai/root/elsai-graph-query-rag/ elsai-graph-query-rag==0.1.0

Requirements: Python >= 3.10, populated Neo4j database (see Graph Storage)


Components

Neo4jRetriever

Translates natural language questions into Cypher queries using an LLM.

python
from elsai_graph_query_rag import Neo4jRetriever

retriever = Neo4jRetriever(connector, llm, verbose=True)

# Generate and execute a Cypher query
_, _, cypher = retriever.generate_cypher_query("Who works at Microsoft?")
_, _, results = retriever.execute_cypher_query(cypher)

# Look up an entity directly
_, _, entity = retriever.get_entity_by_name("Microsoft")

NeighborhoodExpander

Discovers entities connected to a starting node through relationship hops.

python
from elsai_graph_query_rag import NeighborhoodExpander

expander = NeighborhoodExpander(connector, verbose=True)

# All entities within 2 hops of "Microsoft"
_, _, neighborhood = expander.expand_neighborhood(
    "Microsoft", max_hops=2, max_nodes=30
)

# Subgraph connecting multiple entities
_, _, subgraph = expander.get_subgraph(["Alice", "Microsoft", "Seattle"])

HybridGraphRetriever

Combines vector similarity search with graph-based retrieval for maximum recall.

python
from elsai_graph_query_rag import HybridGraphRetriever

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

_, _, results = hybrid.hybrid_retrieve(
    "Who is the CEO of Microsoft?",
    top_k_vector=5,
    include_neighbors=True,
)

How it works:


GraphRAGEngine

High-level interface that coordinates retrieval and generates a natural language answer.

python
from elsai_graph_query_rag import GraphRAGEngine

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

# Simple answer
_, _, answer = engine.ask("How is Alice connected to Microsoft?")
print(answer)

# Detailed answer with source context
_, _, result = engine.ask_with_explanation("What is Microsoft's location?")
print(result["answer"])
print(result["context"])
print(result["cypher_used"])

Full pipeline

python
from elsai_graph_query_rag import Neo4jRetriever, HybridGraphRetriever, GraphRAGEngine
from elsai_graph_generator import Neo4jConnector, EmbeddingManager

# Setup (assumes graph is already stored in Neo4j)
connector = Neo4jConnector(uri="bolt://localhost:7687", user="neo4j", password="pw")
connector.connect()

embedding_manager = EmbeddingManager(connector, embedding_model)

neo4j_retriever = Neo4jRetriever(connector, llm)
hybrid_retriever = HybridGraphRetriever(connector, embedding_manager, neo4j_retriever)
engine = GraphRAGEngine(neo4j_retriever, hybrid_retriever, llm)

questions = [
    "Who works at Acme Corp?",
    "What technologies does Acme Corp use?",
    "Where is Acme Corp located?",
]

for q in questions:
    _, _, answer = engine.ask(q)
    print(f"Q: {q}\nA: {answer}\n")

Copyright © 2026 Elsai Foundry.