langchain.retrievers.multi_query.MultiQueryRetriever¶

class langchain.retrievers.multi_query.MultiQueryRetriever(*, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, retriever: BaseRetriever, llm_chain: LLMChain, verbose: bool = True, parser_key: str = 'lines')[source]¶

Bases: BaseRetriever

Given a user query, use an LLM to write a set of queries. Retrieve docs for each query. Rake the unique union of all retrieved docs.

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.

param llm_chain: langchain.chains.llm.LLMChain [Required]¶
param metadata: Optional[Dict[str, Any]] = None¶

Optional metadata associated with the retriever. Defaults to None This metadata will be associated with each call to this retriever, and passed as arguments to the handlers defined in callbacks. You can use these to eg identify a specific instance of a retriever with its use case.

param parser_key: str = 'lines'¶
param retriever: langchain.schema.retriever.BaseRetriever [Required]¶
param tags: Optional[List[str]] = None¶

Optional list of tags associated with the retriever. Defaults to None These tags will be associated with each call to this retriever, and passed as arguments to the handlers defined in callbacks. You can use these to eg identify a specific instance of a retriever with its use case.

param verbose: bool = True¶
async aget_relevant_documents(query: str, *, callbacks: Callbacks = None, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, **kwargs: Any) List[Document]¶

Asynchronously get documents relevant to a query. :param query: string to find relevant documents for :param callbacks: Callback manager or list of callbacks :param tags: Optional list of tags associated with the retriever. Defaults to None

These tags will be associated with each call to this retriever, and passed as arguments to the handlers defined in callbacks.

Parameters

metadata – Optional metadata associated with the retriever. Defaults to None This metadata will be associated with each call to this retriever, and passed as arguments to the handlers defined in callbacks.

Returns

List of relevant documents

async ainvoke(input: str, config: Optional[RunnableConfig] = None) List[Document]¶
classmethod from_llm(retriever: BaseRetriever, llm: BaseLLM, prompt: PromptTemplate = PromptTemplate(input_variables=['question'], output_parser=None, partial_variables={}, template='You are an AI language model assistant. Your task is \n    to generate 3 different versions of the given user \n    question to retrieve relevant documents from a vector  database. \n    By generating multiple perspectives on the user question, \n    your goal is to help the user overcome some of the limitations \n    of distance-based similarity search. Provide these alternative \n    questions separated by newlines. Original question: {question}', template_format='f-string', validate_template=True), parser_key: str = 'lines') MultiQueryRetriever[source]¶

Initialize from llm using default template.

Parameters
  • retriever – retriever to query documents from

  • llm – llm for query generation using DEFAULT_QUERY_PROMPT

Returns

MultiQueryRetriever

generate_queries(question: str, run_manager: CallbackManagerForRetrieverRun) List[str][source]¶

Generate queries based upon user input.

Parameters

question – user query

Returns

List of LLM generated queries that are similar to the user input

get_relevant_documents(query: str, *, callbacks: Callbacks = None, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, **kwargs: Any) List[Document]¶

Retrieve documents relevant to a query. :param query: string to find relevant documents for :param callbacks: Callback manager or list of callbacks :param tags: Optional list of tags associated with the retriever. Defaults to None

These tags will be associated with each call to this retriever, and passed as arguments to the handlers defined in callbacks.

Parameters

metadata – Optional metadata associated with the retriever. Defaults to None This metadata will be associated with each call to this retriever, and passed as arguments to the handlers defined in callbacks.

Returns

List of relevant documents

invoke(input: str, config: Optional[RunnableConfig] = None) List[Document]¶
retrieve_documents(queries: List[str], run_manager: CallbackManagerForRetrieverRun) List[Document][source]¶

Run all LLM generated queries.

Parameters

queries – query list

Returns

List of retrieved Documents

to_json() Union[SerializedConstructor, SerializedNotImplemented]¶
to_json_not_implemented() SerializedNotImplemented¶
unique_union(documents: List[Document]) List[Document][source]¶

Get unique Documents.

Parameters

documents – List of retrieved Documents

Returns

List of unique retrieved Documents

property lc_attributes: Dict¶

Return a list of attribute names that should be included in the serialized kwargs. These attributes must be accepted by the constructor.

property lc_namespace: List[str]¶

Return the namespace of the langchain object. eg. [“langchain”, “llms”, “openai”]

property lc_secrets: Dict[str, str]¶

Return a map of constructor argument names to secret ids. eg. {“openai_api_key”: “OPENAI_API_KEY”}

property lc_serializable: bool¶

Return whether or not the class is serializable.

model Config¶

Bases: object

Configuration for this pydantic object.

arbitrary_types_allowed = True¶

Examples using MultiQueryRetriever¶