Question Rephrasing

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:

typescript
Chain 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:

  1. Pass the history and input to a PromptTemplate containing the prompt in prompts/rephrase-question.txt.

  2. Pass the formatted prompt to the LLM

  3. Parse the output to a string

Open rephrase-question.chain.ts

Create a Prompt Template

Use the PromptTemplate.fromTemplate() static method to create a new prompt template containing the following prompt.

Rephrase Question 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:

typescript
Prompt Template
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:

  1. Convert message history to a string

  2. Use the input and formatted history to format the prompt

  3. Pass the formatted prompt to the LLM

  4. Coerce the output into a string

Use the return keyword to return the sequence from the function.

typescript
Full Sequence
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.

typescript
Reformatting Messages
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:

typescript
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.

sh
Running the Test
npm run test rephrase-question.chain.test.ts
View Unit Test
typescript
rephrase-question.chain.test.ts
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.

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?