When implementing Retrieval Augmented Generation (RAG), you provide information as part of the prompt that will help the LLM generate an accurate response.
In this challenge, you must modify the initGenerateAnswerChain()
function in modules/agent/chains/answer-generation.chain.ts
to return a chain that instructs the LLM to generate an answer to a question based on the context provided.
The chain should not be concerned with where that information comes from.
The chain will accept the following input:
Unresolved directive in lesson.adoc - include::{repository-raw}/main/src/solutions/modules/agent/chains/answer-generation.chain.ts[tag=interface]
The chain will need to:
-
Format a prompt based on the input to the chain. The prompt template must include
{question}
and{context}
-
Pass the formatted prompt to the LLM
-
Convert the output to a string
answer-generation.chain.ts
→
Modifying Functions
Each challenge in this course will follow the same structure. The repository contains helper functions that will accept the appropriate arguments. You will complete the implementation using the arguments passed to the function.
Later in this course, as you build out the agent, you will use these functions to create instances of the chains and register them as tools.
Create a Prompt Template
Inside the initGenerateAnswerChain()
function, use the PromptTemplate.fromTemplate()
method to create a new prompt template.
Use the following prompt as the first parameter.
Unresolved directive in lesson.adoc - include::{repository-raw}/main/prompts/speculative-answer-generation.txt[]
Due to the nature of semantic search, the documents passed by the application to this prompt may not fully answer the question. Therefore. this prompt instructs the LLM to do its best to generate an answer based on the input or respond with "I don’t know" if it cannot answer the question.
Later in the course, you will build a chain to provide a definitive answer.
Your code should resemble the following:
Unresolved directive in lesson.adoc - include::{repository-raw}/main/src/solutions/modules/agent/chains/answer-generation.chain.ts[tag=prompt, indent=0]
Create the 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::{repository-raw}/main/src/solutions/modules/agent/chains/answer-generation.chain.ts[tag=sequence, indent=0]
Use the return
keyword to return the chain from the function.
Working Solution
Click here to reveal the fully-implemented answer-generation.chain.ts
Unresolved directive in lesson.adoc - include::{repository-raw}/main/src/solutions/modules/agent/chains/answer-generation.chain.ts[tag=function]
Using the Chain
You will be able to initialize and run the chain in your application with the following code:
Unresolved directive in lesson.adoc - include::{repository-raw}/main/src/solutions/modules/agent/chains/answer-generation.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 speculative-answer-generation.chain.test.ts
View Unit Test
Unresolved directive in ../../../../includes/test.adoc - include::{repository-raw}/main/src/modules/agent/chains/speculative-answer-generation.chain.test.ts[]
Summary
In this lesson, you implemented your first chain using LCEL. You will use this chain to convert raw data from Neo4j into a natural language response.
In the next lesson, you will learn how to validate the response generated by the LLM.