Back to Llama Index

LlamaIndex Postprocessor Integration: Google Rerank

llama-index-integrations/postprocessor/llama-index-postprocessor-google-rerank/README.md

0.14.212.7 KB
Original Source

LlamaIndex Postprocessor Integration: Google Rerank

Uses Google's Discovery Engine Ranking API to rerank search results based on query relevance.

Installation

bash
pip install llama-index-postprocessor-google-rerank

Prerequisites

Usage

python
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.postprocessor.google_rerank import GoogleRerank

documents = SimpleDirectoryReader("./data/paul_graham/").load_data()
index = VectorStoreIndex.from_documents(documents=documents)

reranker = GoogleRerank(
    top_n=3,
    project_id="your-gcp-project-id",
    model="semantic-ranker-default-004",
)

query_engine = index.as_query_engine(
    similarity_top_k=10,
    node_postprocessors=[reranker],
)
response = query_engine.query("What did Sam Altman do in this essay?")
print(response)

Available Models

ModelContext WindowNotes
semantic-ranker-default-004 (default)1024 tokensLatest, multilingual
semantic-ranker-default-003512 tokensMultilingual
semantic-ranker-default-002512 tokensEnglish only

Configuration

ParameterTypeDefaultDescription
modelstr"semantic-ranker-default-004"Ranking model name
top_nint2Number of top results to return
project_idstrNoneGCP project ID (falls back to GOOGLE_CLOUD_PROJECT env var, then ADC)
locationstr"global"GCP location for the ranking config
ranking_configstr"default_ranking_config"Ranking config resource name
credentialsCredentialsNoneOptional Google auth credentials object