langchain.retrievers.ensemble.EnsembleRetriever¶

class langchain.retrievers.ensemble.EnsembleRetriever(*, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, retrievers: List[BaseRetriever], weights: List[float], c: int = 60)[source]¶

Bases: BaseRetriever

This class ensemble the results of multiple retrievers by using rank fusion.

Parameters
  • retrievers – A list of retrievers to ensemble.

  • weights – A list of weights corresponding to the retrievers. Defaults to equal weighting for all retrievers.

  • c – A constant added to the rank, controlling the balance between the importance of high-ranked items and the consideration given to lower-ranked items. Default is 60.

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 c: int = 60¶
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 retrievers: List[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 weights: List[float] [Required]¶
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]¶
async arank_fusion(query: str, run_manager: AsyncCallbackManagerForRetrieverRun) List[Document][source]¶

Asynchronously retrieve the results of the retrievers and use rank_fusion_func to get the final result.

Parameters

query – The query to search for.

Returns

A list of reranked documents.

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]¶
rank_fusion(query: str, run_manager: CallbackManagerForRetrieverRun) List[Document][source]¶

Retrieve the results of the retrievers and use rank_fusion_func to get the final result.

Parameters

query – The query to search for.

Returns

A list of reranked documents.

validator set_weights  »  all fields[source]¶
to_json() Union[SerializedConstructor, SerializedNotImplemented]¶
to_json_not_implemented() SerializedNotImplemented¶
weighted_reciprocal_rank(doc_lists: List[List[Document]]) List[Document][source]¶

Perform weighted Reciprocal Rank Fusion on multiple rank lists. You can find more details about RRF here: https://plg.uwaterloo.ca/~gvcormac/cormacksigir09-rrf.pdf

Parameters

doc_lists – A list of rank lists, where each rank list contains unique items.

Returns

The final aggregated list of items sorted by their weighted RRF

scores in descending order.

Return type

list

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 EnsembleRetriever¶