Skip to main content
WatsonxRerank is a wrapper for IBM watsonx.ai foundation models.
This notebook shows how to use watsonx’s rerank endpoint in a retriever. This builds on top of ideas in the ContextualCompressionRetriever.

Overview

Integration details

Setup

To access IBM watsonx.ai models you’ll need to create an IBM watsonx.ai account, get an API key, and install the langchain-ibm integration package.

Credentials

The cell below defines the credentials required to work with watsonx Foundation Model inferencing. Action: Provide the IBM Cloud user API key. For details, see Managing user API keys.
Additionally you are able to pass additional secrets as an environment variable.

Installation

The LangChain IBM integration lives in the langchain-ibm package:
For experiment purpose please also install faiss or faiss-cpu package:
Helper function for printing docs

Instantiation

Set up the base vector store retriever

Let’s start by initializing a simple vector store retriever and storing the 2023 State of the Union speech (in chunks). We can set up the retriever to retrieve a high number (20) of docs. Initialize the WatsonxEmbeddings. For more details see WatsonxEmbeddings. Note:
  • To provide context for the API call, you must add project_id or space_id. For more information see documentation.
  • Depending on the region of your provisioned service instance, use one of the urls described here.
In this example, we’ll use the project_id and Dallas url. You need to specify model_id that will be used for embedding. All available models you can find in documentation.

Usage

Doing reranking with WatsonxRerank

Now let’s wrap our base retriever with a ContextualCompressionRetriever. We’ll add an WatsonxRerank, uses the watsonx rerank endpoint to rerank the returned results. Do note that it is mandatory to specify the model name in WatsonxRerank!

Use within a chain

You can of course use this retriever within a QA pipeline Initialize the ChatWatsonx. For more details see ChatWatsonx.

API reference

For detailed documentation of all WatsonxRerank features and configurations head to the API reference.