Class Introduction
Function
Sends an input query to an LLM for question rewriting, searches for topk similar documents based on each question, deduplicates questions n × topk documents, and sorts documents by document length. This class inherits mx_rag.retrievers.Retriever and calls the invoke method of the base class to enable retrieval. The length of the input query cannot exceed 1 million characters.
Prototype
from mx_rag.retrievers import MultiQueryRetriever # All parameters must be passed through keyword parameters. MultiQueryRetriever(llm, prompt, parser, llm_config)
Parameters
Parameter |
Data Type |
Required/Optional |
Description |
|---|---|---|---|
llm |
Text2TextLLM |
Required |
LLM object instance. For details, see Text2TextLLM. |
prompt |
langchain_core.prompts.PromptTemplate |
Optional |
question is a character string with a fixed length, indicating the entered question. It cannot be changed and must be contained in prompt.input_variables. prompt.template indicates a prompt, and its length falls within the range of (0, 1 × 1024 × 1024]. The query of an LLM request is a prompt combined with a question. The valid value depends on MindIE configurations. For details, see the description of maxSeqLen in "Core Concepts and Configurations" > "Configuration Parameters (Serving)" in MindIE LLM Development Guide. It is recommended that the language type of the prompt be the same as that of the question, or the language type of the LLM answer be specified. Otherwise, the answer effect will be affected. PromptTemplate(
input_variables=["question"],
template="""You are an AI language model assistant. Your task is to rewrite and generate three questions from different perspectives based on the original question of the user.
Give your answers in Chinese with number ID starting from 1. Separate each question by a newline character. The following is a rewriting example:
Example of a question:
Can you tell me about Einstein?
Example of three rewritten questions:
1. What are Einstein's life and major scientific achievements?
2. What are Einstein's important contributions to relativity and other physics fields?
3. What is Einstein's personal life and his impact on society?
Question to be rewritten: {question}"""
)
|
parser |
langchain_core.output_parsers.BaseOutputParser |
Optional |
Implementation class of the OutputParser class for processing the content returned by an LLM. The default value is DefaultOutputParser. It inherits langchain_core.output_parsers.BaseOutputParser and splits multiple questions returned by an LLM by number. |
llm_config |
LLMParameterConfig |
Optional |
Parameters for calling an LLM. For details, see LLMParameterConfig. |
Example
# For details about how to build a knowledge base, see Step 1 in section "Retriever". In this example, an offline knowledge base has been built by default.
from mx_rag.retrievers import MultiQueryRetriever
from mx_rag.llm import Text2TextLLM
from mx_rag.utils import ClientParam
# Initialize an LLM.
llm = Text2TextLLM(base_url="https://<ip>:<port>/v1/chat/completions",
model_name="Llama3-8B-Chinese-Chat",
client_param=ClientParam(ca_file="/path/to/ca.crt"))
# Initialize the retriever.
multi_text_retriever = MultiQueryRetriever(llm=llm,
vector_store=vector_store,
document_store=chunk_store,
embed_func=emb.embed_documents,
k=1,
score_threshold=0.2
)
res = multi_text_retriever.invoke("Describe the requirements of the composition test of the 2024 National College Entrance Examination.")
print(res)