
The Neo4jCypherChain: Zero-Code Inferences
Launch your first Graph RAG system in minutes. Learn how to use LangChain's built-in Neo4jCypherChain to automatically translate user questions into database queries and human-readable answers.
The Neo4jCypherChain: Zero-Code Inferences
Writing custom Python logic for every graph question is time-consuming. Fortunately, LangChain provides a "Short-Cut" called the GraphCypherQAChain. This chain automates the entire loop we learned in Module 8: it reads your schema, takes a natural language question, generates the Cypher, runs it, and summarizes the result.
In this lesson, we will implement this "All-in-One" chain. We will see how it handles simple questions like "Who is the CEO?" and where it starts to struggle with "High-Complexity" reasoning. We will also learn how to customize the "System Prompt" of this chain to ensure its Cypher generation matches our specific graph rules.
1. How the Chain Works (The Internal Loop)
- Input: "Which projects does Sudeep lead?"
- Schema Fetch: LangChain runs
graph.get_schemato understand the nodes/edges. - Cypher Gen: LLM writes
MATCH (p:Person {name: 'Sudeep'})-[:LEADS]->(proj) RETURN proj.title. - Execution: The query runs against your Neo4j instance.
- Summarization: LLM receives the results (e.g.,
["Titan", "Valkyrie"]) and answers: "Sudeep leads the Titan and Valkyrie projects."
2. Implementation: The Simple QA Chain
from langchain_openai import ChatOpenAI
from langchain_community.graphs import Neo4jGraph
from langchain.chains import GraphCypherQAChain
# 1. Connect to Graph & LLM
graph = Neo4jGraph(...)
llm = ChatOpenAI(model="gpt-4", temperature=0)
# 2. Initialize the Chain
chain = GraphCypherQAChain.from_llm(
llm=llm,
graph=graph,
verbose=True, # Essential for debugging the generated Cypher
return_intermediate_steps=True # See the raw DB results
)
# 3. RUN
result = chain.invoke({"query": "Who is the project lead for Titan?"})
print(result["result"])
3. Customizing the Generation Prompt
The default prompt is generic. For a production RAG system, you should provide specific Context Rules.
from langchain.prompts import PromptTemplate
CYPHER_GENERATION_TEMPLATE = """Task:Generate Cypher statement to query a graph database.
Schema:
{schema}
Rules:
- Always use ':Person' instead of ':User'.
- If the project is 'Archived', ignore it.
Question: {question}
Cypher Query:"""
CYPHER_PROMPT = PromptTemplate(
input_variables=["schema", "question"],
template=CYPHER_GENERATION_TEMPLATE
)
# Pass the custom prompt to the chain
chain = GraphCypherQAChain.from_llm(
graph=graph, llm=llm, cypher_prompt=CYPHER_PROMPT
)
4. The Limits of the "Generic" Chain
The GraphCypherQAChain is a "Hammer." It works for many things, but:
- Hallucination: It might try to use relationships that don't exist.
- Breadth: It only does one graph call. It can't handle a "Multi-Step" question like "Find Sudeep's manager and tell me his manager's budget."
- Security: It has no "Guardrails." If a user asks "Delete everything", it might try to write a
DELETEquery!
5. Summary and Exercises
The Neo4jCypherChain is the best place to Start, but rarely the place to Finish.
- Automation: It handles the tedious work of query-execution-summarization.
- Visibility: Using
verbose=Trueallows you to audit the AI's logic. - Customization: Custom prompts are the key to matching your unique business domain.
- Constraint: It is limited to single-shot retrieval.
Exercises
- The "Hello World": Run the basic chain against your graph. Ask a question you know the answer to. Did it write the "Correct" Cypher?
- Prompt Challenge: Add a rule to your
CYPHER_GENERATION_TEMPLATEthat forces the LLM to always return a maximum of 5 results (LIMIT 5). - Intermediate Steps: Inspect the
result["intermediate_steps"]. Can you see the raw JSON that came back from the database? This is the data the LLM uses to write its final answer.
In the next lesson, we will move beyond the hammer and build our own tools: Building Custom Retrieval Chains.