Class AlloyDBRerank (0.4.0)

AlloyDBRerank(engine: llama_index_alloydb_pg.engine.AlloyDBEngine, model_id: str, top_n: int = 2, *, callback_manager: llama_index.core.callbacks.base.CallbackManager = <factory>)

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

The ranking runs inside the database through AlloyDB's Vertex AI integration. As listed in https://cloud.google.com/alloydb/docs/ai/rank-rerank-search-results-rag, it needs the google_ml_integration extension 1.5.2 or later, the google_ml_integration.enable_model_support and google_ml_integration.enable_ai_query_engine flags on, the Discovery Engine API enabled and roles/discoveryengine.viewer for the AlloyDB service agent. Nodes with no text (an ImageNode) or only whitespace or format characters are left out. Database errors are raised unchanged. Async apostprocess_nodes needs llama-index-core 0.12.32 or later.

Methods

AlloyDBRerank

AlloyDBRerank(engine: llama_index_alloydb_pg.engine.AlloyDBEngine, model_id: str, top_n: int = 2, *, callback_manager: llama_index.core.callbacks.base.CallbackManager = <factory>)

Create an AlloyDBRerank instance.

Parameters
Name Description
engine AlloyDBEngine

The AlloyDB engine to run the ranking on.

model_id str

A Vertex AI ranking model id, for example "semantic-ranker-default-003", or a registered reranking model. Unregistered ids raise no error and rank with the default model, while a registered non-reranking model raises.

top_n int

Number of nodes to return. Defaults to 2. Checked only here: assigning it later is not validated.

\*\*kwargs Any

Other BaseNodePostprocessor fields, for example callback_manager.

Exceptions
Type Description
ValueError If model_id is blank or top_n is less than 1.

__arank

__arank(
    nodes: typing.List[llama_index.core.schema.NodeWithScore], query: str
) -> typing.Sequence[sqlalchemy.engine.row.RowMapping]

Call google_ml.rank on the node texts.

__finish

__finish(
    event: llama_index.core.callbacks.base.EventContext,
    nodes: typing.List[llama_index.core.schema.NodeWithScore],
    rows: typing.Sequence[sqlalchemy.engine.row.RowMapping],
) -> typing.List[llama_index.core.schema.NodeWithScore]

Map the rows to new nodes and emit the rerank end events.

__payload

__payload(
    nodes: typing.List[llama_index.core.schema.NodeWithScore], query: str
) -> dict[str, typing.Any]

Build the payload of the reranking callback event.

__start

__start(
    nodes: typing.List[llama_index.core.schema.NodeWithScore],
    query_bundle: typing.Optional[llama_index.core.schema.QueryBundle],
) -> typing.Tuple[str, typing.List[llama_index.core.schema.NodeWithScore]]

Validate the query, skip nodes without text, emit the start event.

_apostprocess_nodes

_apostprocess_nodes(
    nodes: typing.List[llama_index.core.schema.NodeWithScore],
    query_bundle: typing.Optional[llama_index.core.schema.QueryBundle] = None,
) -> typing.List[llama_index.core.schema.NodeWithScore]

Async version of _postprocess_nodes, run on the engine's loop.

_postprocess_nodes

_postprocess_nodes(
    nodes: typing.List[llama_index.core.schema.NodeWithScore],
    query_bundle: typing.Optional[llama_index.core.schema.QueryBundle] = None,
) -> typing.List[llama_index.core.schema.NodeWithScore]

Rerank nodes by relevance to the query with google_ml.rank.

Returns new NodeWithScore objects with the rank score, sorted by score (highest first) and limited to top_n.

Exceptions
Type Description
ValueError If query_bundle is missing or its query is blank.

class_name

class_name() -> str

Get the class name, used as a unique ID in serialization.

This provides a key that makes serialization robust against actual class name changes.

model_post_init

model_post_init(context: Any, /) -> None

This function is meant to behave like a BaseModel method to initialize private attributes.

It takes context as an argument since that's what pydantic-core passes when calling it.