Home / File/ ensemble.py — langchain Source File

ensemble.py — langchain Source File

Architecture documentation for ensemble.py, a python file in the langchain codebase. 13 imports, 0 dependents.

File python LangChainCore Runnables 13 imports 1 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  7c5221fd_0453_d6db_1e3d_2c6252f0057b["ensemble.py"]
  e6310202_b39e_ec9f_8b57_921c9c39c97d["asyncio"]
  7c5221fd_0453_d6db_1e3d_2c6252f0057b --> e6310202_b39e_ec9f_8b57_921c9c39c97d
  9cec496e_5583_62d2_b4a1_469bbdaf601f["collections"]
  7c5221fd_0453_d6db_1e3d_2c6252f0057b --> 9cec496e_5583_62d2_b4a1_469bbdaf601f
  2bf6d401_816d_d011_3b05_a6114f55ff58["collections.abc"]
  7c5221fd_0453_d6db_1e3d_2c6252f0057b --> 2bf6d401_816d_d011_3b05_a6114f55ff58
  3741f36e_1d35_fcc3_9884_1c5e40d99797["itertools"]
  7c5221fd_0453_d6db_1e3d_2c6252f0057b --> 3741f36e_1d35_fcc3_9884_1c5e40d99797
  feec1ec4_6917_867b_d228_b134d0ff8099["typing"]
  7c5221fd_0453_d6db_1e3d_2c6252f0057b --> feec1ec4_6917_867b_d228_b134d0ff8099
  17a62cb3_fefd_6320_b757_b53bb4a1c661["langchain_core.callbacks"]
  7c5221fd_0453_d6db_1e3d_2c6252f0057b --> 17a62cb3_fefd_6320_b757_b53bb4a1c661
  6a98b0a5_5607_0043_2e22_a46a464c2d62["langchain_core.documents"]
  7c5221fd_0453_d6db_1e3d_2c6252f0057b --> 6a98b0a5_5607_0043_2e22_a46a464c2d62
  2b1aa4a8_5352_1757_010a_46ac9ef4b0b0["langchain_core.retrievers"]
  7c5221fd_0453_d6db_1e3d_2c6252f0057b --> 2b1aa4a8_5352_1757_010a_46ac9ef4b0b0
  31eab4ab_7281_1e6c_b17d_12e6ad9de07a["langchain_core.runnables"]
  7c5221fd_0453_d6db_1e3d_2c6252f0057b --> 31eab4ab_7281_1e6c_b17d_12e6ad9de07a
  a8ec7563_2814_99b3_c6da_61c599efc542["langchain_core.runnables.config"]
  7c5221fd_0453_d6db_1e3d_2c6252f0057b --> a8ec7563_2814_99b3_c6da_61c599efc542
  b4920617_3c91_2457_3f8c_84ba49f64322["langchain_core.runnables.utils"]
  7c5221fd_0453_d6db_1e3d_2c6252f0057b --> b4920617_3c91_2457_3f8c_84ba49f64322
  dd5e7909_a646_84f1_497b_cae69735550e["pydantic"]
  7c5221fd_0453_d6db_1e3d_2c6252f0057b --> dd5e7909_a646_84f1_497b_cae69735550e
  f85fae70_1011_eaec_151c_4083140ae9e5["typing_extensions"]
  7c5221fd_0453_d6db_1e3d_2c6252f0057b --> f85fae70_1011_eaec_151c_4083140ae9e5
  style 7c5221fd_0453_d6db_1e3d_2c6252f0057b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Ensemble Retriever.

Ensemble retriever that ensemble the results of
multiple retrievers by using weighted  Reciprocal Rank Fusion.
"""

import asyncio
from collections import defaultdict
from collections.abc import Callable, Hashable, Iterable, Iterator
from itertools import chain
from typing import (
    Any,
    TypeVar,
    cast,
)

from langchain_core.callbacks import (
    AsyncCallbackManagerForRetrieverRun,
    CallbackManagerForRetrieverRun,
)
from langchain_core.documents import Document
from langchain_core.retrievers import BaseRetriever, RetrieverLike
from langchain_core.runnables import RunnableConfig
from langchain_core.runnables.config import ensure_config, patch_config
from langchain_core.runnables.utils import (
    ConfigurableFieldSpec,
    get_unique_config_specs,
)
from pydantic import model_validator
from typing_extensions import override

T = TypeVar("T")
H = TypeVar("H", bound=Hashable)


def unique_by_key(iterable: Iterable[T], key: Callable[[T], H]) -> Iterator[T]:
    """Yield unique elements of an iterable based on a key function.

    Args:
        iterable: The iterable to filter.
        key: A function that returns a hashable key for each element.

    Yields:
        Unique elements of the iterable based on the key function.
    """
    seen = set()
    for e in iterable:
        if (k := key(e)) not in seen:
            seen.add(k)
            yield e


class EnsembleRetriever(BaseRetriever):
    """Retriever that ensembles the multiple retrievers.

    It uses a rank fusion.

    Args:
        retrievers: A list of retrievers to ensemble.
        weights: A list of weights corresponding to the retrievers. Defaults to equal
// ... (277 more lines)

Domain

Subdomains

Functions

Dependencies

  • asyncio
  • collections
  • collections.abc
  • itertools
  • langchain_core.callbacks
  • langchain_core.documents
  • langchain_core.retrievers
  • langchain_core.runnables
  • langchain_core.runnables.config
  • langchain_core.runnables.utils
  • pydantic
  • typing
  • typing_extensions

Frequently Asked Questions

What does ensemble.py do?
ensemble.py is a source file in the langchain codebase, written in python. It belongs to the LangChainCore domain, Runnables subdomain.
What functions are defined in ensemble.py?
ensemble.py defines 1 function(s): unique_by_key.
What does ensemble.py depend on?
ensemble.py imports 13 module(s): asyncio, collections, collections.abc, itertools, langchain_core.callbacks, langchain_core.documents, langchain_core.retrievers, langchain_core.runnables, and 5 more.
Where is ensemble.py in the architecture?
ensemble.py is located at libs/langchain/langchain_classic/retrievers/ensemble.py (domain: LangChainCore, subdomain: Runnables, directory: libs/langchain/langchain_classic/retrievers).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free