Get startedGet started for free

Text-to-Cypher retrieval chain

You now have a chain capable of converting natural language into a Cypher statement. Let's use it in a larger chain to execute this LLM-generated Cypher and answer user questions with the retrieved information.

This exercise is part of the course

Graph RAG with LangChain and Neo4j

View Course

Exercise instructions

  • Update the code to execute the Cypher statement generated by the text_to_cypher_chain and assign it to the "context" variable used in QA_PROMPT.
  • Pass through the input to the "question" variable unchanged.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

qa_prompt = ChatPromptTemplate.from_messages([
    SystemMessagePromptTemplate.from_template(QA_PROMPT),
    HumanMessagePromptTemplate.from_template("Question: {question}")
])

qa_chain = {
  	# Use output of text_to_cypher to get database results
    "context": text_to_cypher_chain | ____(lambda cypher: graph.____(cypher)),
    # Pass the user input through to the "question" variable
    "question": ____
} | qa_prompt | llm | StrOutputParser()

res = qa_chain.invoke({"question": "What companies is Harrison Chase connected to?"})
print(res)
Edit and Run Code