CohereRerank Class — langchain Architecture
Architecture documentation for the CohereRerank class in cohere_rerank.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD dddf6c97_e67e_69d4_e813_fd10af808b8f["CohereRerank"] 56ee7e00_cbf2_37e6_b294_468dfe7f2941["BaseDocumentCompressor"] dddf6c97_e67e_69d4_e813_fd10af808b8f -->|extends| 56ee7e00_cbf2_37e6_b294_468dfe7f2941 38b769c4_c07d_3256_6870_9c6ee6931708["Document"] dddf6c97_e67e_69d4_e813_fd10af808b8f -->|extends| 38b769c4_c07d_3256_6870_9c6ee6931708 777f4ea1_c861_8949_b1d5_cc3140dcee73["cohere_rerank.py"] dddf6c97_e67e_69d4_e813_fd10af808b8f -->|defined in| 777f4ea1_c861_8949_b1d5_cc3140dcee73 6011faf4_4634_1dee_7212_ffb2dbbc0a19["validate_environment()"] dddf6c97_e67e_69d4_e813_fd10af808b8f -->|method| 6011faf4_4634_1dee_7212_ffb2dbbc0a19 a2f14c13_e647_4baa_6915_4434129acf54["rerank()"] dddf6c97_e67e_69d4_e813_fd10af808b8f -->|method| a2f14c13_e647_4baa_6915_4434129acf54 3ec3b839_5c8b_59b9_bad4_0ee7ebda1ab8["compress_documents()"] dddf6c97_e67e_69d4_e813_fd10af808b8f -->|method| 3ec3b839_5c8b_59b9_bad4_0ee7ebda1ab8
Relationship Graph
Source Code
libs/langchain/langchain_classic/retrievers/document_compressors/cohere_rerank.py lines 20–124
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
def rerank(
self,
documents: Sequence[str | Document | dict],
query: str,
*,
model: str | None = None,
top_n: int | None = -1,
max_chunks_per_doc: int | None = None,
) -> list[dict[str, Any]]:
"""Returns an ordered list of documents ordered by their relevance to the provided query.
Args:
query: The query to use for reranking.
documents: A sequence of documents to rerank.
model: The model to use for re-ranking. Default to self.model.
top_n : The number of results to return. If `None` returns all results.
max_chunks_per_doc : The maximum number of chunks derived from a document.
""" # noqa: E501
if len(documents) == 0: # to avoid empty api call
return []
docs = [
doc.page_content if isinstance(doc, Document) else doc for doc in documents
]
model = model or self.model
top_n = top_n if (top_n is None or top_n > 0) else self.top_n
results = self.client.rerank(
query=query,
documents=docs,
model=model,
top_n=top_n,
max_chunks_per_doc=max_chunks_per_doc,
)
if hasattr(results, "results"):
results = results.results
return [
{"index": res.index, "relevance_score": res.relevance_score}
for res in results
]
Extends
Source
Frequently Asked Questions
What is the CohereRerank class?
CohereRerank is a class in the langchain codebase, defined in libs/langchain/langchain_classic/retrievers/document_compressors/cohere_rerank.py.
Where is CohereRerank defined?
CohereRerank is defined in libs/langchain/langchain_classic/retrievers/document_compressors/cohere_rerank.py at line 20.
What does CohereRerank extend?
CohereRerank extends BaseDocumentCompressor, Document.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free