Class AlloyDBDocumentCompressor (0.17.0)

AlloyDBDocumentCompressor(
    *,
    engine: langchain_google_alloydb_pg.engine.AlloyDBEngine,
    model_id: str,
    top_n: typing.Optional[int] = None
)

Reranks documents with AlloyDB AI's google_ml.rank() function.

The ranking runs inside the database through AlloyDB's Vertex AI integration. model_id is required, for example "semantic-ranker-default-003".

compressor = AlloyDBDocumentCompressor(
    engine=engine, model_id="semantic-ranker-default-003", top_n=3
)
docs = compressor.compress_documents(documents, "What is AlloyDB?")

Methods

AlloyDBDocumentCompressor

AlloyDBDocumentCompressor(
    *,
    engine: langchain_google_alloydb_pg.engine.AlloyDBEngine,
    model_id: str,
    top_n: typing.Optional[int] = None
)

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.

__acompress_documents

__acompress_documents(
    documents: typing.Sequence[langchain_core.documents.base.Document], query: str
) -> list[langchain_core.documents.base.Document]

Validate the input, call google_ml.rank and map the rows back.

acompress_documents

acompress_documents(
    documents: typing.Sequence[langchain_core.documents.base.Document],
    query: str,
    callbacks: typing.Optional[
        typing.Union[
            list[langchain_core.callbacks.base.BaseCallbackHandler],
            langchain_core.callbacks.base.BaseCallbackManager,
        ]
    ] = None,
) -> typing.Sequence[langchain_core.documents.base.Document]

Rerank documents by relevance to the query.

Parameters
Name Description
documents Sequence[Document]

Documents to rerank.

query str

The query to rank the documents against.

callbacks Optional[Callbacks]

Unused. Defaults to None.

Returns
Type Description
Sequence[Document] Copies of the documents, sorted by relevance_score (highest first) and limited to top_n.

compress_documents

compress_documents(
    documents: typing.Sequence[langchain_core.documents.base.Document],
    query: str,
    callbacks: typing.Optional[
        typing.Union[
            list[langchain_core.callbacks.base.BaseCallbackHandler],
            langchain_core.callbacks.base.BaseCallbackManager,
        ]
    ] = None,
) -> typing.Sequence[langchain_core.documents.base.Document]

Rerank documents by relevance to the query.

Parameters
Name Description
documents Sequence[Document]

Documents to rerank.

query str

The query to rank the documents against.

callbacks Optional[Callbacks]

Unused. Defaults to None.

Exceptions
Type Description
RuntimeError If called from the engine's background event loop.
Returns
Type Description
Sequence[Document] Copies of the documents, sorted by relevance_score (highest first) and limited to top_n.