Home / File/ cohere_rerank.py — langchain Source File

cohere_rerank.py — langchain Source File

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

File python CoreAbstractions Serialization 10 imports 1 classes

Entity Profile

Dependency Diagram

graph LR
  0519b6e6_7be9_0c69_4045_09855e5f977e["cohere_rerank.py"]
  cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7["collections.abc"]
  0519b6e6_7be9_0c69_4045_09855e5f977e --> cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7
  e874d8a4_cef0_9d0b_d1ee_84999c07cc2c["copy"]
  0519b6e6_7be9_0c69_4045_09855e5f977e --> e874d8a4_cef0_9d0b_d1ee_84999c07cc2c
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  0519b6e6_7be9_0c69_4045_09855e5f977e --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  2d1edd36_20be_d887_f834_7b58c1b5a50f["langchain_core._api.deprecation"]
  0519b6e6_7be9_0c69_4045_09855e5f977e --> 2d1edd36_20be_d887_f834_7b58c1b5a50f
  f3bc7443_c889_119d_0744_aacc3620d8d2["langchain_core.callbacks"]
  0519b6e6_7be9_0c69_4045_09855e5f977e --> f3bc7443_c889_119d_0744_aacc3620d8d2
  c554676d_b731_47b2_a98f_c1c2d537c0aa["langchain_core.documents"]
  0519b6e6_7be9_0c69_4045_09855e5f977e --> c554676d_b731_47b2_a98f_c1c2d537c0aa
  f4d905c6_a2b2_eb8f_be9b_7808b72f6a16["langchain_core.utils"]
  0519b6e6_7be9_0c69_4045_09855e5f977e --> f4d905c6_a2b2_eb8f_be9b_7808b72f6a16
  6e58aaea_f08e_c099_3cc7_f9567bfb1ae7["pydantic"]
  0519b6e6_7be9_0c69_4045_09855e5f977e --> 6e58aaea_f08e_c099_3cc7_f9567bfb1ae7
  91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"]
  0519b6e6_7be9_0c69_4045_09855e5f977e --> 91721f45_4909_e489_8c1f_084f8bd87145
  f0139559_92ef_4345_9b24_e5f9e39905d5["cohere"]
  0519b6e6_7be9_0c69_4045_09855e5f977e --> f0139559_92ef_4345_9b24_e5f9e39905d5
  style 0519b6e6_7be9_0c69_4045_09855e5f977e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from __future__ import annotations

from collections.abc import Sequence
from copy import deepcopy
from typing import Any

from langchain_core._api.deprecation import deprecated
from langchain_core.callbacks import Callbacks
from langchain_core.documents import BaseDocumentCompressor, Document
from langchain_core.utils import get_from_dict_or_env
from pydantic import ConfigDict, model_validator
from typing_extensions import override


@deprecated(
    since="0.0.30",
    removal="1.0",
    alternative_import="langchain_cohere.CohereRerank",
)
class CohereRerank(BaseDocumentCompressor):
    """Document compressor that uses `Cohere Rerank API`."""

    client: Any = None
    """Cohere client to use for compressing documents."""
    top_n: int | None = 3
    """Number of documents to return."""
    model: str = "rerank-english-v2.0"
    """Model to use for reranking."""
    cohere_api_key: str | None = None
    """Cohere API key. Must be specified directly or via environment variable
        COHERE_API_KEY."""
    user_agent: str = "langchain"
    """Identifier for the application making the request."""

    model_config = ConfigDict(
        arbitrary_types_allowed=True,
        extra="forbid",
    )

    @model_validator(mode="before")
    @classmethod
    def validate_environment(cls, values: dict) -> Any:
        """Validate that api key and python package exists in environment."""
        if not values.get("client"):
            try:
                import cohere
            except ImportError as e:
                msg = (
                    "Could not import cohere python package. "
                    "Please install it with `pip install cohere`."
                )
                raise ImportError(msg) from e
            cohere_api_key = get_from_dict_or_env(
                values,
                "cohere_api_key",
                "COHERE_API_KEY",
            )
            client_name = values.get("user_agent", "langchain")
            values["client"] = cohere.Client(cohere_api_key, client_name=client_name)
        return values
// ... (65 more lines)

Subdomains

Classes

Dependencies

  • cohere
  • collections.abc
  • copy
  • langchain_core._api.deprecation
  • langchain_core.callbacks
  • langchain_core.documents
  • langchain_core.utils
  • pydantic
  • typing
  • typing_extensions

Frequently Asked Questions

What does cohere_rerank.py do?
cohere_rerank.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, Serialization subdomain.
What does cohere_rerank.py depend on?
cohere_rerank.py imports 10 module(s): cohere, collections.abc, copy, langchain_core._api.deprecation, langchain_core.callbacks, langchain_core.documents, langchain_core.utils, pydantic, and 2 more.
Where is cohere_rerank.py in the architecture?
cohere_rerank.py is located at libs/langchain/langchain_classic/retrievers/document_compressors/cohere_rerank.py (domain: CoreAbstractions, subdomain: Serialization, directory: libs/langchain/langchain_classic/retrievers/document_compressors).

Analyze Your Own Codebase

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

Try Supermodel Free