# WatsonxLLM

> **Class** in `langchain_ibm`

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

`IBM watsonx.ai` large language models class.

???+ info "Setup"

    To use the large language models, you need to have the `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 WatsonxLLM
    from ibm_watsonx_ai.metanames import GenTextParamsMetaNames

    parameters = {
        GenTextParamsMetaNames.DECODING_METHOD: "sample",
        GenTextParamsMetaNames.MAX_NEW_TOKENS: 100,
        GenTextParamsMetaNames.MIN_NEW_TOKENS: 1,
        GenTextParamsMetaNames.TEMPERATURE: 0.5,
        GenTextParamsMetaNames.TOP_K: 50,
        GenTextParamsMetaNames.TOP_P: 1,
    }

    model = WatsonxLLM(
        model_id="google/flan-t5-xl",
        url="https://us-south.ml.cloud.ibm.com",
        project_id="*****",
        params=parameters,
        # api_key="*****"
    )
    ```

??? info "Invoke"

    ```python
    input_text = "The meaning of life is "
    response = model.invoke(input_text)
    print(response)
    ```

    ```txt
    "42, but what was the question?
    The answer to the ultimate question of life, the universe, and everything is 42.
    But what was the question? This is a reference to Douglas Adams' science fiction
    series "The Hitchhiker's Guide to the Galaxy."
    ```

??? info "Stream"

    ```python
    for chunk in model.stream(input_text):
        print(chunk, end="")
    ```

    ```txt
    "42, but what was the question?
    The answer to the ultimate question of life, the universe, and everything is 42.
    But what was the question? This is a reference to Douglas Adams' science fiction
    series "The Hitchhiker's Guide to the Galaxy."
    ```

??? info "Async"
    ```python
    response = await model.ainvoke(input_text)

    # stream:
    # async for chunk in model.astream(input_text):
    #     print(chunk, end="")

    # batch:
    # await model.abatch([input_text])
    ```

    ```txt
    "42, but what was the question?
    The answer to the ultimate question of life, the universe, and everything is 42.
    But what was the question? This is a reference to Douglas Adams' science fiction
    series "The Hitchhiker's Guide to the Galaxy."
    ```

## Signature

```python
WatsonxLLM()
```

## Extends

- `BaseLLM`

## Properties

- `model_id`
- `model`
- `deployment_id`
- `project_id`
- `space_id`
- `url`
- `apikey`
- `api_key`
- `token`
- `password`
- `username`
- `instance_id`
- `version`
- `params`
- `verify`
- `streaming`
- `watsonx_model`
- `watsonx_model_gateway`
- `watsonx_client`
- `model_config`
- `lc_secrets`

## Methods

- [`is_lc_serializable()`](https://reference.langchain.com/python/langchain-ibm/llms/WatsonxLLM/is_lc_serializable)
- [`validate_environment()`](https://reference.langchain.com/python/langchain-ibm/llms/WatsonxLLM/validate_environment)
- [`get_num_tokens()`](https://reference.langchain.com/python/langchain-ibm/llms/WatsonxLLM/get_num_tokens)
- [`get_token_ids()`](https://reference.langchain.com/python/langchain-ibm/llms/WatsonxLLM/get_token_ids)

---

[View source on GitHub](https://github.com/langchain-ai/langchain-ibm/blob/68e9b09f8cbc8d2310e57a6a7eb51cde0956a3b0/libs/ibm/langchain_ibm/llms.py#L46)