With the LangChain library, you can conveniently generate Cypher queries, enabling an efficient retrieval of information from Neo4j.

A month ago, I spent a week researching and implementing a solution allowing anyone to retrieve information from .
A colleague of mine showed me a LangChain feature request, where the user requested that my work of having the option to retrieve information from the Neo4j database would be added as a module directly to the LangChain library so that no additional code or external modules would be needed to integrate Neo4j into LangChain applications. Since I was already familiar with LangChain internals, I decided to try and implement Cypher searching capabilities myself. I spent a weekend researching and coding the solution and ensuring it would conform to the contribution standards for it to be added to the library. Luckily, the maintainers of LangChain are very responsive and open to new ideas, and the Cypher Search has been added in the latest release of the LangChain library. Thanks to .
What is a knowledge graph
LangChain has already integrations with Vector and SQL databases, so why do we need an integration with a Graph Database like Neo4j?

Let’s start with a simple test.
chain.run("""
Which intermediary is connected to most entites?
""")Results

We can observe the generated Cypher statement and the retrieved information from Neo4j used to form the answer. That is as easy a setup as it gets. Let’s move on to the next example.
chain.run("""
Who are the officers of ZZZ-MILI COMPANY LTD.?
""")Results

Unfortunately, I wasn’t aware of this behavior during the PR. The Cypher statement was correctly generated, and the data was retrieved from the database. However, the answer-generating prompt is set up in a way where it needs explicit confirmation of the answer in the provided context. However, with the Cypher Search, we might only get relevant results, not the whole context. Not to worry, we can adjust the answer-generating prompt to let it know that the provided context always contains an answer.
from langchain.prompts.prompt import PromptTemplate
prompt_template = """
You are an assistant that helps to form nice and human understandable answers.
The information part contains the provided information that you can use to construct an answer.
The provided information is authorative, you must never doubt it or try to use your internal knowledge to correct it.
Make it sound like the information are coming from an AI assistant, but don't add any information.
Information:
{context}
Question: {question}
Helpful Answer:"""
QA_PROMPT = PromptTemplate(
template=prompt_template, input_variables=["context", "question"]
)
chain_improved = GraphCypherQAChain.from_llm(
ChatOpenAI(temperature=0), graph=graph, verbose=True,
qa_prompt=QA_PROMPT
)
This is the prompt I used in my other projects. Let’s rerun the question using the new answer-generating prompt.
chain_improved.run("""
Who are the officers of ZZZ-MILI COMPANY LTD.?
""")Results

Using the new prompt, the Cypher Search module returned a valid answer.
I have already .
on Medium, where people are continuing the conversation by highlighting and responding to this story.
SOCIAL SHARE CARD GENERATOR