BaseDocumentCompressor#

class langchain_core.documents.compressor.BaseDocumentCompressor[source]#

Bases: BaseModel, ABC

Base class for document compressors.

This abstraction is primarily used for post-processing of retrieved documents.

Documents matching a given query are first retrieved. Then the list of documents can be further processed.

For example, one could re-rank the retrieved documents using an LLM.

Note users should favor using a RunnableLambda instead of sub-classing from this interface.

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.

async acompress_documents(documents: Sequence[Document], query: str, callbacks: list[BaseCallbackHandler] | BaseCallbackManager | None = None) Sequence[Document][source]#

Async compress retrieved documents given the query context.

Parameters:
Returns:

The compressed documents.

Return type:

Sequence[Document]

abstract compress_documents(documents: Sequence[Document], query: str, callbacks: list[BaseCallbackHandler] | BaseCallbackManager | None = None) Sequence[Document][source]#

Compress retrieved documents given the query context.

Parameters:
Returns:

The compressed documents.

Return type:

Sequence[Document]