WatsonxRerank#
- class langchain_ibm.rerank.WatsonxRerank[source]#
Bases:
BaseDocumentCompressor
Document compressor that uses watsonx Rerank API.
???+ info “Setup”
To use, you should have langchain_ibm python package installed, and the environment variable WATSONX_APIKEY set with your API key, or pass it as a named parameter apikey to the constructor.
```bash pip install -U langchain-ibm
# or using uv uv add langchain-ibm ```
`bash export WATSONX_APIKEY="your-api-key" `
??? info “Instantiate”
```python from langchain_ibm import WatsonxRerank from ibm_watsonx_ai.foundation_models.schema import RerankParameters
parameters = RerankParameters(truncate_input_tokens=20)
- ranker = WatsonxRerank(
model_id=”cross-encoder/ms-marco-minilm-l-12-v2”, url=”https://us-south.ml.cloud.ibm.com”, project_id=”*”, params=parameters, # apikey=”*”
??? info “Rerank”
```python query = “red cat chasing a laser pointer” documents = [
“A red cat darts across the living room, pouncing on a red laser dot.”, “Two dogs play fetch in the park with a tennis ball.”, “The tabby cat naps on a sunny windowsill all afternoon.”, “A recipe for tuna casserole with crispy breadcrumbs.”,
]
ranker.rerank(documents=documents, query=query) ```
{“index”: 0, “relevance_score”: 0.8719543218612671}, {“index”: 2, “relevance_score”: 0.6520894169807434}, {“index”: 1, “relevance_score”: 0.6270776391029358}, {“index”: 3, “relevance_score”: 0.4607713520526886},
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- param apikey: SecretStr | None [Optional]#
API key to the Watson Machine Learning or CPD instance.
- param instance_id: SecretStr | None [Optional]#
Instance_id of the CPD instance.
- param model_id: str [Required]#
Type of model to use.
- param params: dict | RerankParameters | None = None#
Model parameters to use during request generation.
- param password: SecretStr | None [Optional]#
Password to the CPD instance.
- param project_id: str | None = None#
ID of the Watson Studio project.
- param space_id: str | None = None#
ID of the Watson Studio space.
- param streaming: bool = False#
Whether to stream the results or not.
- param token: SecretStr | None [Optional]#
Token to the CPD instance.
- param url: SecretStr [Optional]#
URL to the Watson Machine Learning or CPD instance.
- param username: SecretStr | None [Optional]#
Username to the CPD instance.
- param validate_model: bool = True#
Model ID validation.
- param verify: str | bool | None = None#
You can pass one of following as verify: * the path to a CA_BUNDLE file * the path of directory with certificates of trusted CAs * True - default path to truststore will be taken * False - no verification will be made
- param version: SecretStr | None = None#
Version of the CPD instance.
- param watsonx_client: APIClient | None = None#
- async acompress_documents(
- documents: Sequence[Document],
- query: str,
- callbacks: Callbacks | None = None,
Async compress retrieved documents given the query context.
- compress_documents(
- documents: Sequence[Document],
- query: str,
- callbacks: Callbacks | None = None,
- **kwargs: Any,
Compress documents using watsonx’s rerank API.
- Parameters:
documents (Sequence[Document]) – A sequence of documents to compress
query (str) – The query to use for compressing the documents
callbacks (Callbacks | None) – Callbacks to run during the compression process
kwargs (Any) – Additional keyword args
- Returns:
A sequence of compressed documents
- Return type:
Sequence[Document]