06. MultiQueryRetriever
MultiQueryRetriever
Distance-based vector database searches are a way to find documents with similar embedding based on query embedding (expression) and'distance' in high-dimensional space. But query Search results may vary if detailed China embedding does not properly capture the meaning of the data There is. In addition, prompt engineering or tuning to manually adjust this can be cumbersome.
To solve this problem, MultiQueryRetriever Automates the prompt tuning process by utilizing the Language Learning Model (LLM), which automatically generates multiple queries from various perspectives for a given user input query.
This method searches for a set of related documents for each query, extracts a sum of unique documents that encompass all queries, and allows you to get a larger set of potentially related documents.
By creating the same question from multiple perspectives, MultiQueryRetriever Can overcome the limitations of distance-based searches in some part and provide richer search results.
Copy
# API A configuration file for managing keys as environment variables.
from dotenv import load_dotenv
# API Load key information
load_dotenv()Copy
TrueCopy
# LangSmith set up tracking. https://smith.langchain.com
# !pip install langchain-teddynote
from langchain_teddynote import logging
# Enter a project name.
logging.langsmith("CH11-Retriever")Copy
Copy
Copy
Copy
Output the contents of 1 document of the searched results.
Copy
Copy
Usage
MultiQueryRetriever When specifying the LLM to use for and using it for the creation of the vagina, retriever takes care of the rest of the work.
Copy
Below is the code you run to debug the intermediate process that creates multiple queries.
first "langchain.retrievers.multi_query" Get the logger.
This is logging.getLogger() This is done using functions. Then, the logarithm level of this logger INFO By setting it to, INFO Only log messages above the level can be output.
Copy
This code retriever_from_llm Object invoke Given using methods question Search for related documents.
Documents retrieved unique_docs The larvae are stored in variables, and by checking the length of these variables, you can see the total number of related documents retrieved. This course allows you to effectively find relevant information about your questions and determine the amount.
Copy
Copy
Copy
How to utilize LCEL Chain
Define custom prompts, and create a Chain with a defined prompt.
When Chain receives a user's question (in the example below), it creates 5 questions,
"\n"Returns 5 questions created separated by delimiters.
Copy
Copy
Chain previously created MultiQueryRetriever You can retrieve by passing it to.
Copy
MultiQueryRetriever Use to search for documents and check results.
Copy
Copy
Copy
Last updated