The final link in the chain
With the text-to-cypher chain from Chapter 1 and the vector retriever from earlier in this Chapter, you now have the components to create a chain that provides multiple ways—both vectors and nodes and relationships—to access the information in your knowledge graph.
The text_to_cypher_chain
and line_retriever
(the Neo4j vector retriever) variables are available for you to use, along with a question-answering prompt for hybrid retrieval, graphrag_qa_prompt
.
This exercise is part of the course
Graph RAG with LangChain and Neo4j
Exercise instructions
- Pass the
"question"
key to the vector retriever (line_retriever
) to get relevant results using vector search. - Extract the query from the
graph
object to execute the Cypher statement generated by thetext_to_cypher_chain
. - Execute
graphrag_qa_chain
on the input provided.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
graphrag_qa_chain = RunnablePassthrough.assign(
# Pass the "question" key to the retriever
vectors = RunnableLambda(lambda x: line_retriever.invoke(____)),
# Execute the generated Cypher statement
records = text_to_cypher_chain | graph.____
) | graphrag_qa_prompt | llm | StrOutputParser()
# Execute the hybrid chain
output = graphrag_qa_chain.____({"question": "Does Sampson bite his thumb at Gregory?"})
print(output)