Previously we've seen @LangChainAI ParentDocumentRetriever that creates smaller chunks from a document and links them back to the initial documents during retrieval.
MultiVectorRetriever is a more customizable version of that. Let's see how to use it 🧵👇
@LangChainAI ParentDocumentRetriever automatically creates the small chunks and links their parent document id.
If we want to create some additional vectors for each documents, other than smaller chunks, we can do that and then retrieve those using MultiVectorRetriever.
We can customize how these additional vectors are created for each parent document. Here're some ways @LangChainAI mentioned in their documentation.
- smaller chunks
- store the summary vector of each document
- store the vectors of hypothetical questions for each documents
Now let's try to understand the example code from langchain documentation 👇
First we create the retriever itself.
Here we pass the
- vectorstore to store all the vectors for the documents
- docstore to store the documents themselves
- id_key is the key of the metadata field which will be used to store the document id for each vector
Also we create unique uuid for each of the documents.
We'll use these ids to store the documents in the docstore.
MultiVectorRetriever will use these ids to retrieve the documents from the vector similarity search.
Now let's implement the ParentDocumentRetriever using MultiVectorRetriever
- iterate over each document
- split the document to get the children chunks
- store each small chunk in the vectorstore, with the parent doc_id as metadata
As MultiVectorRetriever is more flexible and customizable, we need to manually add the additional vectors to the vectorstore and set the doc_id of the associated document as a metadata field.
Also we need to add the docs with their id to the docstore.
We can also create a summary for each document.
Oftentimes a summary may be able to capture more accurately what a chunk is about, leading to better retrieval.
Also as we'll be matching the vectors with user's query embedding vector, we might get better results if we create some hypothetical user queries of a particular document and store them in the vectorstore.
Based on the specific use case, we can create other vectors as well for each document.
For these vectors, we need to make sure to add the doc_id as the metadata. And MultiVectorRetriever will handle the rest to retrieve the initial documents from these vectors.
An open-source Discord bot that listens to your conversations, remembers them and answers your questions across a discord server, created using @llama_index (inspired by @seldo 's LlamaBot for Slack)
Previously we've seen how to improve retrieval by funetuning an embedding model.
@llama_index also supports finetuning an adapter on top of existing models, which lets us improve retrieval without updating our existing embeddings. 🚀
Let's see how it works 👇🧵
@llama_index For adapters, we pull apart every single layer of the transformer and add randomly initialized new weights.
Then, instead of finetuning all the weights, we freeze the weights of the pre-trained model, only finetune the newly added weights.
We apply similar technique here 👇
@llama_index Here we "freeze" the document embeddings, and then we train a transformation on the query embedding instead.
Thus we're not limited to only Sentence Transformer models.
We can apply this on top of any existing model without re-embedding existing data.
Extract tables from documents using @llama_index UnstructuredElementParser and then use RecursiveRetriever to enable hybrid tabular/semantic queries and also comparisons over multiple docs.
Let's see how to use this advanced RAG technique 🧵👇
@llama_index First we load the documents.
Then we create the new UnstructuredElementNodeParser from LLamaIndex.
@llama_index This parser:
- extracts tables from data
- converts those tables to Dataframe
- for each of those tables, it creates 2 nodes
- one Table Node that contains the Dataframe as string
- another IndexNode that stores the summary of that table and a reference to that Table Node