In this challenge, you must modify the initGenerateAnswerChain()
function in modules/agent/chains/rephrase-question.chain.ts
to add a chain that rephrases an input into a standalone question.
The chain will accept the following input:
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-python/main/src/solutions/modules/agent/chains/rephrase-question.chain.ts[tag=interface]
The output of the chain will be a string
.
To convert the message history from an array of objects to a string in the following format:
Human: {input}
AI: {output}
You will need to update the initRephraseChain
method to:
-
Pass the history and input to a
PromptTemplate
containing the prompt inprompts/rephrase-question.txt
. -
Pass the formatted prompt to the LLM
-
Parse the output to a string
rephrase-question.chain.ts
→
Create a Prompt Template
Use the PromptTemplate.fromTemplate()
static method to create a new prompt template containing the following prompt.
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-python/main/prompts/rephrase-question.txt[]
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/chains/rephrase-question.chain.ts[tag=prompt]
Runnable Sequence
Next, use the RunnableSequence.from()
static method to create a new chain that takes the RephraseQuestionInput
and outputs a string
.
The RunnableSequence
will need to:
-
Convert message history to a string
-
Use the input and formatted history to format the prompt
-
Pass the formatted prompt to the LLM
-
Coerce the output into a string
Use the return
keyword to return the sequence from the function.
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-python/main/src/solutions/modules/agent/chains/rephrase-question.chain.ts[tag=sequence,indent=0]
Convert Conversation History to a string
The RunnablePassthrough.assign()
static method is another method for modifying individual keys in a chain.
Here, the messages
input is an array of (:Response)
nodes from the database. Prompt templates expect placeholders to be a string, so you must convert the array into a string.
In the following code, the .map()
method uses the input
and output
properties on each response to a format the LLM will understand before the .join()
method joins them into a single string.
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-python/main/src/solutions/modules/agent/chains/rephrase-question.chain.ts[tag=assign,indent=0]
Using the Chain
Later in the course, you will update the application to use the chain. You could initialize and run the chain with the following code:
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-python/main/src/solutions/modules/agent/chains/rephrase-question.chain.ts[tag=usage]
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 rephrase-question.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/chains/rephrase-question.chain.test.ts[]
It works!
Once you have received a rephrased question from the LLM, click the button below to mark the challenge as completed.
Summary
In this lesson, you built a chain that will take this history to rephrase the user’s input into a standalone question.
In the next module, you will build a chain that uses a retriever to query a vector store for documents that are similar to an input.