CohereRerank#
- class langchain.retrievers.document_compressors.cohere_rerank.CohereRerank[source]#
Bases:
BaseDocumentCompressor
Deprecated since version 0.0.30: Use
langchain_cohere.CohereRerank
instead.Document compressor that uses Cohere Rerank API.
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 client: Any = None#
Cohere client to use for compressing documents.
- param cohere_api_key: str | None = None#
Cohere API key. Must be specified directly or via environment variable COHERE_API_KEY.
- param model: str = 'rerank-english-v2.0'#
Model to use for reranking.
- param top_n: int | None = 3#
Number of documents to return.
- param user_agent: str = 'langchain'#
Identifier for the application making the request.
- async acompress_documents(documents: Sequence[Document], query: str, callbacks: List[BaseCallbackHandler] | BaseCallbackManager | None = None) Sequence[Document] #
Async compress retrieved documents given the query context.
- Parameters:
documents (Sequence[Document]) β The retrieved documents.
query (str) β The query context.
callbacks (List[BaseCallbackHandler] | BaseCallbackManager | None) β Optional callbacks to run during compression.
- Returns:
The compressed documents.
- Return type:
Sequence[Document]
- compress_documents(documents: Sequence[Document], query: str, callbacks: List[BaseCallbackHandler] | BaseCallbackManager | None = None) Sequence[Document] [source]#
Compress documents using Cohereβs rerank API.
- Parameters:
documents (Sequence[Document]) β A sequence of documents to compress.
query (str) β The query to use for compressing the documents.
callbacks (List[BaseCallbackHandler] | BaseCallbackManager | None) β Callbacks to run during the compression process.
- Returns:
A sequence of compressed documents.
- Return type:
Sequence[Document]
- rerank(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]] [source]#
Returns an ordered list of documents ordered by their relevance to the provided query.
- Parameters:
query (str) β The query to use for reranking.
documents (Sequence[str | Document | dict]) β A sequence of documents to rerank.
model (str | None) β The model to use for re-ranking. Default to self.model.
top_n (int | None) β The number of results to return. If None returns all results. Defaults to self.top_n.
max_chunks_per_doc (int | None) β The maximum number of chunks derived from a document.
- Return type:
List[Dict[str, Any]]