Module document_compressor (0.17.0)

API documentation for document_compressor module.

Classes

AlloyDBDocumentCompressor

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?")

Modules Functions

_to_ranked_documents

_to_ranked_documents(
    documents: typing.Sequence[langchain_core.documents.base.Document],
    rows: typing.Sequence[typing.Sequence[typing.Any]],
) -> list[langchain_core.documents.base.Document]

Map (index, score) rows from google_ml.rank back to documents.

index is the 1-based position of the document in the input array. Rows with a NULL score or an index outside the input are skipped. The input documents are not modified; each result is a copy whose metadata includes relevance_score. Results are sorted by score, highest first.