Now that you have a vector store, you can use it to retrieve chunks of text that are semantically similar to a user’s question.
In this challenge, you will create a chain that will use the vector search index to find movies with similar plots.
You must first:
-
Use the
initVectorStore()
function implemented in the previous lesson to create a vector store and retriever -
Create an instance of the Answer Generation chain.
Then, create a chain that will:
-
Takes the
string
input and assigns it theinput
variable -
Uses the input to retrieve similar movie plots
-
Uses the answer generation chain to generate an answer
-
Use the
saveHistory()
function to save the response and context to the database -
Returns the output as a
string
.
Existing function
The modules/agent/tools/vector-retrieval.chain.ts
file contains the following placeholder functions for saving and retrieving history.
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-vectors-unstructured/main/src/modules/agent/tools/vector-retrieval.chain.ts[tag=function, indent=0]
vector-retrieval.chain.ts
→Instantiate Tools
Inside the initVectorRetrievalChain()
function, replace the // TODO
comments to create an instance of the vector store using the initVectorStore()
function from the previous lesson.
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-vectors-unstructured/main/src/solutions/modules/agent/tools/vector-retrieval.chain.ts[tag=vectorstore, indent=0]
Next, call the .asRetriever()
method on the vectorStore
object to create a new VectorStoreRetriever
instance.
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-vectors-unstructured/main/src/solutions/modules/agent/tools/vector-retrieval.chain.ts[tag=retriever, indent=0]
Finally, create an answer generation chain using the initGenerateAnswerChain()
function.
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-vectors-unstructured/main/src/solutions/modules/agent/tools/vector-retrieval.chain.ts[tag=answerchain, indent=0]
Building the Chain
As this chain will be called by an agent, it will receive a structured input containing an input
and rephrasedQuestion
.
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-vectors-unstructured/main/src/modules/agent/agent.types.ts[tag=agenttoolinput,indent=0]
Because the chain will receive an object as the input, you can use RunnablePassthrough.assign()
to modify the input directly rather than the RunnableSequence.from()
method used in the previous lessons.
This should be used to collect relevant context using the retriever.
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-vectors-unstructured/main/src/solutions/modules/agent/tools/vector-retrieval.chain.ts[tag=getcontext, indent=0]
Next, the elementIds
of the document must be extracted from the to create the :CONTEXT
relationship between the (:Response)
and (:Movie)
nodes.
At the same time, the context needs to be converted to a string so it can be used in the Answer Generation Chain.
Helper Functions
These functions in vector-retrieval.chain.ts
are used to extract information to modify the context.
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-vectors-unstructured/main/src/modules/agent/tools/vector-retrieval.chain.ts[tag=extractDocumentIds, indent=0]
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-vectors-unstructured/main/src/modules/agent/tools/vector-retrieval.chain.ts[tag=docsToJson, indent=0]
The RunnablePassthrough
is a fluent interface, so the .assign()
method can be called to chain the steps together.
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-vectors-unstructured/main/src/solutions/modules/agent/tools/vector-retrieval.chain.ts[tags="getcontext,mutatecontext", indent=0]
The rephrased question and context can then be passed to the answerChain
to generate an output.
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-vectors-unstructured/main/src/solutions/modules/agent/tools/vector-retrieval.chain.ts[tag=answer, indent=0]
Then, the input, rephrased question and output can be saved to the database using the saveHistory()
function created in Conversation Memory module.
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-vectors-unstructured/main/src/solutions/modules/agent/tools/vector-retrieval.chain.ts[tag=save, indent=0]
Before, finally picking the output
as a string.
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-vectors-unstructured/main/src/solutions/modules/agent/tools/vector-retrieval.chain.ts[tag=save, indent=0]
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-vectors-unstructured/main/src/solutions/modules/agent/tools/vector-retrieval.chain.ts[tag=output, indent=0]
If you have followed the instructions correctly, your code should resemble the following:
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-vectors-unstructured/main/src/solutions/modules/agent/tools/vector-retrieval.chain.ts[tag=function]
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 vector-retrieval.chain.test.ts
View Unit Test
Unresolved directive in ../../../../includes/test.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-vectors-unstructured/main/src/modules/agent/tools/vector-retrieval.chain.test.ts[]
Verifying the Test
If every test in the test suite has passed, a new (:Session)
node with a .id
property of vector-retriever-1
will have been created in your database.
The session should have atleast one (:Response)
node, linked with a :CONTEXT
relationship to at least one movie.
Click the Check Database button below to verify the tests have succeeded.
Hint
You can compare your code with the solution in src/solutions/modules/agent/tools/vector-retrieval.chain.ts
and double-check that the conditions have been met in the test suite.
Solution
You can compare your code with the solution in src/solutions/modules/agent/tools/vector-retrieval.chain.ts
and double-check that the conditions have been met in the test suite.
You can also run the following Cypher statement to double-check that the index has been created in your database.
MATCH (s:Session {id: 'vector-retriever-1'})
RETURN s,
[ (s)-[:HAS_RESPONSE]->(r)
| [ r,
[ (r)-[:CONTEXT]->(c) | c ]
]
]
Once you have verified your code and re-ran the tests, click Try again…* to complete the challenge.
Summary
In this lesson, you built a chain that takes the components built in the course so far to build a chain that retrieves documents from the vector search index and uses them to answer a question.
The chain then saves the response to the database.
In the next lesson, you will see how the response can be used to filter out documents that have been used to provide unhelpful responses in the past.