CohereRerank#

class langchain_cohere.rerank.CohereRerank[source]#

Bases: BaseDocumentCompressor

Document compressor that uses Cohere Rerank API.

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

param client: Any = None#

Cohere client to use for compressing documents.

param cohere_api_key: SecretStr | None [Optional]#

Cohere API key. Must be specified directly or via environment variable COHERE_API_KEY.

param model: str | None = None#

Model to use for reranking. Mandatory to specify the model name.

param top_n: int | None = 3#

Number of documents to return.

param user_agent: str = 'langchain:partner'#

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:
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, *, rank_fields: Sequence[str] | None = None, 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.

  • rank_fields (Sequence[str] | None) – A sequence of keys to use for reranking.

  • 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]]