Skip to main content
The Vertex Search Ranking API is one of the standalone APIs in Vertex AI Agent Builder. It takes a list of documents and reranks those documents based on how relevant the documents are to a query. Compared to embeddings, which look only at the semantic similarity of a document and a query, the ranking API can give you precise scores for how well a document answers a given query. The ranking API can be used to improve the quality of search results after retrieving an initial set of candidate documents.
The ranking API is stateless so there’s no need to index documents before calling the API. All you need to do is pass in the query and documents. This makes the API well suited for reranking documents from any document retrievers.
For more information, see Rank and rerank documents.

Setup

Load and prepare data

For this example, we will be using the Google Wiki pageto demonstrate how the Vertex Ranking API works. We use a standard pipeline of load -> split -> embed data. The embeddings are created using the Vertex Embeddings API model - textembedding-gecko@003
The langchain-community package is no longer maintained. Examples that import from langchain_community may be outdated or broken. Use with caution.

Testing out the vertex ranking API

Let’s query both the basic_retriever and retriever_with_reranker with the same query and compare the retrieved documents. The Ranking API takes in the input from the basic_retriever and passes it to the Ranking API. The ranking API is used to improve the quality of the ranking and determine a score that indicates the relevance of each record to the query. You can see the difference between the Unranked and the Ranked Documents. The Ranking API moves the most semantically relevant documents to the top of the context window of the LLM thus helping it form a better answer with reasoning.
Let’s inspect a couple of reranked documents. We observe that the retriever still returns the relevant LangChain type documents but as part of the metadata field, we also receive the relevance_score from the Ranking API.

Putting it all together

This shows an example of a complete RAG chain with a simple prompt template on how you can perform reranking using the Vertex Ranking API.