MultiQueryRetriever
Author: hong-seongmin
Peer Review:
Proofread : Juni Lee
This is a part of LangChain OpenTutorial
Overview
MultiQueryRetriever offers a thoughtful approach to improving distance-based vector database retrieval by generating diverse queries with the help of an LLM.
This method simplifies the retrieval process, minimizes the need for manual prompt adjustments, and aims to provide more nuanced and comprehensive results.
Understanding Distance-Based Vector Search Distance-based vector search is a technique that identifies documents with embeddings similar to a query embedding based on their 'distance' in a high-dimensional space. However, subtle variations in query details or embedding representations can occasionally make it challenging to fully capture the intended meaning, which might affect the search results.
Streamlined Prompt Tuning
MultiQueryRetrieverreduces the complexity of prompt tuning by utilizing an LLM to automatically generate multiple queries from different perspectives for a single input. This helps minimize the effort required for manual adjustments or prompt engineering.Broader Document Retrieval Each generated query is used to perform a search, and the unique documents retrieved from all queries are combined. This approach helps uncover a wider range of potentially relevant documents, increasing the chances of retrieving valuable information.
Improved Search Robustness By exploring a question from multiple perspectives through diverse queries,
MultiQueryRetrieveraddresses some of the limitations of distance-based searches. This approach can better account for nuanced differences and deeper meanings in the data, leading to more contextually relevant and well-rounded results.
Table of Contents
References
Environment Setup
Set up the environment. You may refer to Environment Setup for more details.
[Note]
langchain-opentutorialis a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.You can checkout the
langchain-opentutorialfor more details.
Alternatively, environment variables can also be set using a .env file.
[Note]
This is not necessary if you've already set the environment variables in the previous step.
Building a Vector Database
Vector databases enable efficient retrieval of relevant documents by embedding text data into a high-dimensional vector space.
This example demonstrates creating a simple vector database using LangChain, which involves loading and splitting a document, generating embeddings with OpenAI, and performing a search query to retrieve contextually relevant information.
Usage
Simply specify the LLM to be used in MultiQueryRetriever and pass the query, and the retriever will handle the rest.
Below is code that you can run to debug the intermediate process of generating multiple queries.
First, we retrieve the langchain.retrievers.multi_query logger.
This is done using the logging.getLogger method. Then, we set the logger's log level to INFO, so that only log messages at the INFO level or above are printed.
This code uses the invoke method of the retriever_from_llm object to search for documents relevant to the given question.
The retrieved documents are stored in the variable relevant_docs, and checking the length of this variable lets you see how many relevant documents were found.
Through this process, you can effectively locate information related to the user's question and assess how much of it is available.
How to Use the LCEL Chain
Define a custom prompt, then create a
Chainwith that prompt.When the
Chainreceives a user question (in the following example), it generates 5 questions, and returns the 5 generated questions separated by '\n'.
You can pass the previously created Chain to the MultiQueryRetriever to perform retrieval.
Use the MultiQueryRetriever to search documents and check the results.
Last updated