In this lesson, you will use the GraphCypherQAChain
to add a text to Cypher retriever to the LangChain agent.
Text to Cypher
Open the genai_integration_langchain/cypher_retriever.py
file and review the code:
Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/cypher_retriever.py[tag=**]
You will need to:
-
Create a
GraphCypherQAChain
instance. -
Update the
retriever
function to use theGraphCypherQAChain
to convert natural language queries into Cypher queries.
Create a new GraphCypherQAChain
instance:
Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/solutions/cypher_retriever.py[tag=import_cypher_qa]
Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/solutions/cypher_retriever.py[tag=cypher_qa]
Return Direct
The return_direct
parameter is set to True
to return the result of the Cypher query instead of an answer.
This is useful when you want to pass the raw data to the agent for further processing or analysis.
Update the retrieve
function to invoke the GraphCypherQAChain
:
Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/solutions/cypher_retriever.py[tag=retrieve]
Experiment with the retriever by running the program and asking questions about data in the graph:
Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/solutions/cypher_retriever.py[tag=examples]
Verbose
Set the verbose
parameter of the GraphCypherQAChain
to True
to see the generated Cypher queries and the full context used to generate the answers.
Click to view the complete code
Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/solutions/cypher_retriever.py[tags="**;!examples"]
Improve the retriever
Your challenge is to improve the retriever using the techniques you learned in the previous lessons, which could include:
-
Providing a custom prompt and specific instructions.
-
Including example questions and Cypher queries.
-
Using a different LLM model for Cypher generation.
-
Restricting the schema to provide more focused results.
Here are some examples of more complex questions you can try:
Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/solutions/cypher_retriever_enhanced.py[tag=examples]
There is no right or wrong solution. You should experiment with different approaches to see how they affect the accuracy and relevance of the generated Cypher queries.
Verbose
When adapting the retriever, you may want to set the verbose
parameter of the GraphCypherQAChain
to True
to see the generated Cypher queries and the full context used to generate the answers.
Click here to see an example solution
This example uses some of the techniques you learned in the previous lessons to improve the retriever:
Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/solutions/cypher_retriever_enhanced.py[tags="**; !examples"]
Congratulations!
When you are ready, click continue to complete the course.
Lesson Summary
In this lesson, you learned how to create a text to Cypher retriever and customized it to improve the quality of the generated Cypher queries.
Congratulations on completing the course!