rerank() — langchain Function Reference
Architecture documentation for the rerank() function in cohere_rerank.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD a2f14c13_e647_4baa_6915_4434129acf54["rerank()"] dddf6c97_e67e_69d4_e813_fd10af808b8f["CohereRerank"] a2f14c13_e647_4baa_6915_4434129acf54 -->|defined in| dddf6c97_e67e_69d4_e813_fd10af808b8f 3ec3b839_5c8b_59b9_bad4_0ee7ebda1ab8["compress_documents()"] 3ec3b839_5c8b_59b9_bad4_0ee7ebda1ab8 -->|calls| a2f14c13_e647_4baa_6915_4434129acf54 style a2f14c13_e647_4baa_6915_4434129acf54 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/langchain/langchain_classic/retrievers/document_compressors/cohere_rerank.py lines 62–99
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
]
Domain
Subdomains
Called By
Source
Frequently Asked Questions
What does rerank() do?
rerank() is a function in the langchain codebase, defined in libs/langchain/langchain_classic/retrievers/document_compressors/cohere_rerank.py.
Where is rerank() defined?
rerank() is defined in libs/langchain/langchain_classic/retrievers/document_compressors/cohere_rerank.py at line 62.
What calls rerank()?
rerank() is called by 1 function(s): compress_documents.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free