# WatsonxRerank

> **Class** in `langchain_ibm`

📖 [View in docs](https://reference.langchain.com/python/langchain-ibm/rerank/WatsonxRerank)

Document compressor that uses `watsonx Rerank API`.

???+ info "Setup"

    To use, you should have `langchain_ibm` python package installed,
    and the environment variable `WATSONX_API_KEY` set with your API key, or pass
    it as a named parameter `api_key` to the constructor.

    ```bash
    pip install -U langchain-ibm

    # or using uv
    uv add langchain-ibm
    ```

    ```bash
    export WATSONX_API_KEY="your-api-key"
    ```

    !!! deprecated
        `apikey` and `WATSONX_APIKEY` are deprecated and will be removed in
        version `2.0.0`. Use `api_key` and `WATSONX_API_KEY` instead.

??? 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,
        # api_key="*****"
    )
    ```

??? 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)
    ```

    ```python
    [
        {"index": 0, "relevance_score": 0.8719543218612671},
        {"index": 2, "relevance_score": 0.6520894169807434},
        {"index": 1, "relevance_score": 0.6270776391029358},
        {"index": 3, "relevance_score": 0.4607713520526886},
    ]
    ```

## Signature

```python
WatsonxRerank()
```

## Extends

- `BaseDocumentCompressor`

## Properties

- `model_id`
- `project_id`
- `space_id`
- `url`
- `apikey`
- `api_key`
- `token`
- `password`
- `username`
- `instance_id`
- `version`
- `params`
- `verify`
- `validate_model`
- `streaming`
- `watsonx_rerank`
- `watsonx_client`
- `model_config`
- `lc_secrets`

## Methods

- [`validate_environment()`](https://reference.langchain.com/python/langchain-ibm/rerank/WatsonxRerank/validate_environment)
- [`rerank()`](https://reference.langchain.com/python/langchain-ibm/rerank/WatsonxRerank/rerank)
- [`compress_documents()`](https://reference.langchain.com/python/langchain-ibm/rerank/WatsonxRerank/compress_documents)

---

[View source on GitHub](https://github.com/langchain-ai/langchain-ibm/blob/f414e4c168b2691392484289663a20a1e0ece459/libs/ibm/langchain_ibm/rerank.py#L31)