The first step in instructing an LLM to retrieve data from a Neo4j database is to generate a Cypher statement.
To complete this challenge, you must modify the initCypherGenerationChain()
function in modules/agent/tools/cypher/cypher-generation.chain.ts
to return a chain that:
-
Accepts the rephrased question as a
string
-
Format a prompt that instructs the LLM to use the schema provided to generate a Cypher statement to retrieve the data that answers the question
-
Pass the formatted prompt to an LLM
-
Parse the output as a string
cypher-generation.chain.ts
→
Prompt Template
In the initCypherGenerationChain()
function, use the PromptTemplate.fromTemplate()
method to create a new prompt template with the following prompt.
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-python/main/prompts/cypher-generation-with-instructions.txt[]
Remember to use backslashes (\
) to escape the back-ticks if you are using template strings.
Specific Instructions
This prompt includes specific instructions that the LLM should follow when writing the Cypher statement.
This technique is known as in-context learning, where an LLM uses instructions in the prompt to adapt its responses to new tasks or questions without needing prior training on specific tasks.
You can learn more in the Providing Specific Instructions lesson in Neo4j & LLM Fundamentals.
Your code should resemble the following:
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-python/main/src/solutions/modules/agent/tools/cypher/cypher-generation.chain.ts[tag=prompt, indent=0]
Returning Element IDs
You may have noticed the instruction to use the elementId()
function to return the Element ID of any nodes returned.
You will use this value to create :CONTEXT
relationships in the database.
Return a Runnable Sequence
Use the RunnableSequence.from()
method to create a new chain.
The chain should pass the prompt to the LLM passed as a parameter, then format the response as a string using a new instance of the StringOutputParser
.
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-python/main/src/solutions/modules/agent/tools/cypher/cypher-generation.chain.ts[tag=startsequence, indent=0]
// ...
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-python/main/src/solutions/modules/agent/tools/cypher/cypher-generation.chain.ts[tag=endsequence, indent=0]
Initial Inputs
Inside the array, add an object that sets the question
and schema
for the chain.
To assign the original input string to the question
key, create a new RunnablePassthrough
instance.
Use the graph.getSchema()
to assign a copy of the database schema to the schema
key.
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-python/main/src/solutions/modules/agent/tools/cypher/cypher-generation.chain.ts[tag=assign, indent=0]
Format Prompt and Process
Now that the prompt inputs are ready, these can be replaced in the prompt, passed to the LLM, and the output parsed as a string.
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-python/main/src/solutions/modules/agent/tools/cypher/cypher-generation.chain.ts[tags="sequence", indent=0]
Finished Sequence
If you have followed the steps correctly, your code should resemble the following:
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-python/main/src/solutions/modules/agent/tools/cypher/cypher-generation.chain.ts[tag=function, indent=0]
Testing your changes
If you have followed the instructions, you should be able to run the following unit test to verify the response using the npm run test
command.
npm run test cypher-generation.chain.test.ts
View Unit Test
Unresolved directive in ../../../../includes/test.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-python/main/src/modules/agent/tools/cypher/cypher-generation.chain.test.ts[]
It works!
If all the tests have passed, you will have a chain capable of generating Cypher statements based on a question using the database schema.
Hit the button below to mark the challenge as complete.
Summary
In this lesson, you built a chain that generates a Cypher statement based on user input.
In the next lesson, you will learn how LLMs can be used to evaluate the statement.