WatsonxRerank is a wrapper for IBM watsonx.ai foundation models.This notebook shows how to use watsonx’s rerank endpoint in a retriever.
Overview
Integration details
| Class | Package | JS support | Downloads | Version |
|---|---|---|---|---|
| WatsonxRerank | langchain-ibm | ✅ |
Setup
To access IBM watsonx.ai models you’ll need to create an IBM watsonx.ai account, get an API key, and install thelangchain-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.Installation
The LangChain IBM integration lives in thelangchain-ibm package:
faiss or faiss-cpu package:
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 theWatsonxEmbeddings. For more details see WatsonxEmbeddings.
Note:
- To provide context for the API call, you must add
project_idorspace_id. For more information see documentation. - Depending on the region of your provisioned service instance, use one of the urls described here.
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 aContextualCompressionRetriever. 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 theChatWatsonx. For more details see ChatWatsonx.
API reference
For detailed documentation of allWatsonxRerank features and configurations head to the API reference.
Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.