DashScopeRerank#

class langchain_community.document_compressors.dashscope_rerank.DashScopeRerank[source]#

Bases: BaseDocumentCompressor

Document compressor that uses DashScope 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#

DashScope client to use for compressing documents.

param dashscope_api_key: str | None = None (alias 'api_key')#

DashScope API key. Must be specified directly or via environment variable DASHSCOPE_API_KEY.

param model: str | None = None#

Model to use for reranking.

param top_n: int | None = 3#

Number of documents to return.

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 DashScope’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, *, top_n: int | None = -1) → 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.

  • top_n (int | None) – The number of results to return. If None returns all results. Defaults to self.top_n.

Return type:

List[Dict[str, Any]]

Examples using DashScopeRerank