Cypher Generation

To improve the accuracy of the generated Cypher queries you can customize the generation prompt for your data requirements.

In this lesson, you will learn how to provide specific instructions and examples queries to improve Cypher query generation.

Prompt

You can provide a custom prompt to the GraphCypherQAChain. You can tailor the prompt to your use case to generate more accurate Cypher queries.

Update the cypher_qa.py program to include a custom prompt:

python
Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/solutions/cypher_qa_prompt.py[tag=import_prompt]

Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/solutions/cypher_qa_prompt.py[tag=cypher_template]

Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/solutions/cypher_qa_prompt.py[tag=cypher_prompt]

The prompt includes instructions for generating Cypher queries including parameters for the schema, and question. When invoked the GraphCypherQAChain will insert the schema and question parameters into the prompt.

Add the custom prompt to the GraphCypherQAChain:

python
Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/solutions/cypher_qa_prompt.py[tag=cypher_qa]

Specific instructions

To manage specific data or business rules, you can provide specific instructions to the LLM when generating the Cypher.

For example, movie titles that start with "The" are stored in the graph as "Matrix, The" instead of "The Matrix".

Asking the LLM to generate Cypher queries without this information will result in no data being returned.

[user]
Who acted in the movie The Matrix?
[assistant]
I don't know.

Update the cypher_template to include a specific instruction to the LLM to handle this case:

For movie titles that begin with "The", move "the" to the end,
for example "The 39 Steps" becomes "39 Steps, The".
Click to view the complete prompt
python
Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/solutions/cypher_qa_prompt.py[tag=cypher_template_instructions]

Update the code to ask the question, "Who acted in the movie The Matrix?, and review the results.

Examples

You can provide examples of questions and relevant Cypher queries to help the LLM generate more accurate Cypher queries.

Questions that relate to movies ratings often generate ambiguous or incorrect Cypher. This is because the rating is a property of the RATED relationship, and the Movie node also includes a imdbRating property.

Cypher examples should describe the query and the expected Cypher query, for example:

Question: Get user ratings?
Cypher: MATCH (u:User)-[r:RATED]->(m:Movie)
        WHERE u.name = "User name"
        RETURN r.rating AS userRating

Update the cypher_template to include the examples relating to movie ratings:

python
Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/solutions/cypher_qa_prompt.py[tag=cypher_template_examples]
Click to view the complete code
python
Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/solutions/cypher_qa_prompt.py[tags="**,!cypher_template,!cypher_template_instructions,!cypher_template_example_genre,!examples_the,!examples_rating,!examples_genre"]

Genres

The database contains data about movie genres.

When generating more complex Cypher queries, such as those that involve genres, the LLM may not generate the correct Cypher query.

These queries may require a specific example on how to retrieve genres from the graph

Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/solutions/cypher_qa_prompt.py[tag=examples_genre]

Your challenge is to provide an example Cypher query that demonstrates how to retrieve genres from the graph.

Click to view an example solution

There is no right or wrong solution. Here is an example solution that provides a Cypher query to retrieve genres:

python
Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/solutions/cypher_qa_prompt.py[tag=cypher_template_example_genre]

The example is generic enough to be used for any query that involves genres.

Continue

When you are ready, continue to the next lesson.

Lesson Summary

In this lesson, you learned how you can improve the quality of the generated Cypher queries by customizing the prompt and providing specific instructions to the LLM.

In the next lesson, you will learn how to restrict the schema used to generate Cypher queries.