You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

0 D铆as
2 Hrs
21 Min
48 Seg

MultiQuery retriever

11/17
Resources

What is a MultiQuery Retriever?

A MultiQuery Retriever is an advanced technique used to improve the accuracy of retrieving relevant documents in a search system. It works by generating multiple queries from an initial one, allowing queries to be approached from different angles and capturing a broader context. This method is similar to the human process of rephrasing questions for better understanding and more accurate answers.

How does the MultiQuery Retriever work?

The process begins with the submission of an initial query, which is processed by a language model such as OpenAI. This model generates several new queries related to the original one. From these queries, the system searches a VectorStore for documents that best match each generated query. Finally, the relevant documents are combined into a complete set that provides a more detailed and contextualized answer to the user's original question.

Import and configuration

To implement a MultiQuery Retriever, you must import the corresponding module. You will also need a VectorStore and an embedding system, as well as a language model (LLM). Here's how to do it:

from langchain.retrievers.multiquery import MultiQueryRetriever
 # Setting up the VectorStore and language model modelmodel = chatOpenAI(temperature=0) # Adjusting the temperature to get consistent responsesretriever = MultiQueryRetriever(model, vectorStore).

Creating a Retriever

Once the environment is set up, a basic retriever is created using a converted VectorStore using the asRetriever method. The language model generates the questions and the retriever uses them to retrieve the relevant documents.

Customization of question generation

To customize the question generation process, LangChain offers the option to define an output schema using Pydantic. This is achieved by creating an output parser:

from pydantic import BaseModel
class lineList(BaseModel): lines: list[str] # List of generated questions
class lineListsOutputParser(PydanticOutputParser): def parse(self, text: str)-> lineList: lines = text.strip().split('\n') return lineList(lines=lines).

This definition allows structuring the data in such a way that it is iterable and easily manageable within the application.

Prompt construction

Once the parser is defined, a specific prompt is constructed to guide the generation of questions. This is encapsulated in an LLM string:

prompt = "Generate multiple queries from an initial query..."output_parser = lineListsOutputParser().

Executing and obtaining results

Finally, the functionality is tested by executing the configured action string. This method invokes the language model, generating multiple queries and consolidating the results for the user's query:

result = retriever.invoke({ " question": "How to create a retriever with LangChain?"})print(result).

When executed, the system returns a structured set of questions, resulting in more accurate and complete answers. This process is critical for applications where the accuracy and detail of the answer is crucial.

Practical recommendations when using MultiQuery Retriever

  1. Use low temperature: Set the language model to a low temperature to ensure consistency in the generated answers.

  2. Customize the prompt: Tailor the prompt to the specific needs of the use case to ensure that the generated questions cover all necessary angles.

  3. Check question consistency: Ensure that the generated questions are consistent and relevant to the context of the original query.

  4. Optimize response time: Although the process may be slower due to multiple queries, finding a balance between the quality of responses and processing time is vital.

Incorporating these techniques into document search and retrieval systems not only improves the accuracy of responses, but also optimizes the user experience by providing richer and more detailed context. Continue to explore these tools and hone your ability to deliver accurate and valuable responses.

Contributions 3

Questions 0

Sort by:

Want to see more contributions, questions and answers from the community?

siento que estoy entiendo el n煤cleo de la conversaci贸n humana con este curso jajajaja
Los notebooks de los recursos est谩n incompletos, solo aparece la mitad de lo que el profesor muestra en las clases
MultiQueryRetriever, es la implementaci贸n para preguntar, desde varios puntos de vista, la pregunta que nos env铆a el usuario. est谩s "preguntas con otra perspectiva" la genera un llm con la pregunta inicial.