Fine-tuning The Cypher Generation

Have you tried asking the bot Who directed The Matrix?

If you ask it, it should respond telling you that it cannot provide an answer. This is due to a peculiarity in the data.

Your challenge is to update the instructions used during the Cypher generation process.

The Problem

In the previous module, as part of defining the scope of the chatbot, we included an instruction not to answer the question if it was not provided in the context.

If you take a look at the database movies with a title starting with The have the The portion moved to the end in the .title property. For example, The 39 Steps is stored as 39 Steps, The.

To fix this or any other data-related discrepancies, we can fine-tune the Cypher generation process by modifying the instructions used by the LLM when generating the Cypher statement.

Original Prompts

You can view the the default prompts used to instruct the LLM for inspiration.

Make sure that your prompt includes two placeholders, {schema} and {question}.

The template you provide must be a string that includes these two placeholders.

The Solution

In tools/cypher.py, you must create a PromptTemplate. Start by importing the PromptTemplate class.

python
Imports
from langchain.prompts.prompt import PromptTemplate

Next, create a variable called CYPHER_GENERATION_TEMPLATE and set it to a string containing some instructions. Remember to include the {schema} and {question} placeholders.

python
Creating a template
CYPHER_GENERATION_TEMPLATE = """
You are an expert Neo4j Developer translating user questions into Cypher to answer questions about movies and provide recommendations.
Convert the user's question based on the schema.

Use only the provided relationship types and properties in the schema.
Do not use any other relationship types or properties that are not provided.

Fine Tuning:

For movie titles that begin with "The", move "the" to the end. For example "The 39 Steps" becomes "39 Steps, The" or "the matrix" becomes "Matrix, The".


Schema:
{schema}

Question:
{question}

Cypher Query:
"""

The PromptTemplate class has a static .from_template() method that allows you to pass a plain string. Use this method to create a variable called cypher_prompt.

python
Cypher Template
cypher_prompt = PromptTemplate.from_template(CYPHER_GENERATION_TEMPLATE)

Finally, append the cypher_prompt keyword argument to the initialize_agent() function call.

python
Append the Cypher Prompt argument
cypher_qa = GraphCypherQAChain.from_llm(
    llm,
    graph=graph,
    verbose=True,
    cypher_prompt=cypher_prompt
)

Testing the Prompt

Ask the question Who directed The Matrix? and keep an eye on the console. You should see that the Cypher statement is now generated with , The appended to the string.

Console output
> Entering new AgentExecutor chain...
```json
{
    "action": "Cypher QA",
    "action_input": "who directed the matrix?"
}
```
> Entering new GraphCypherQAChain chain...
Generated Cypher:
cypher
MATCH (m:Movie)<-[:DIRECTED]-(d:Director)
WHERE m.title = "Matrix, The"
RETURN d.name AS director
Full Context:
[{'director': 'Lana Wachowski'}, {'director': ' Lilly Wachowski'}]
> Finished chain.
Observation: {'query': 'who directed the matrix?', 'result': 'The Matrix was directed by Lana Wachowski and Lilly Wachowski.'}
Thought:```json
{
    "action": "Final Answer",
    "action_input": "The Matrix was directed by Lana Wachowski and Lilly Wachowski."
}
```
> Finished chain.
> Entering new AgentExecutor chain...
```json
{
    "action": "Final Answer",
    "action_input": "The Matrix was directed by Lana Wachowski and Lilly Wachowski."
}
```
> Finished chain.

Once you have completed the steps, click the button below to mark the lesson as completed.

Summary

In this lesson, you used fine-tuning to fix a problem with Cypher statement generation.

In the next lesson, you will learn how few-shot prompting can help improve the quality of the Cypher statements generated by the LLM.

Chatbot

Hi, I am an Educational Learning Assistant for Intelligent Network Exploration. You can call me E.L.A.I.N.E.

How can I help you today?