## IBM watsonx.ai

> WatsonxEmbeddings is a wrapper for IBM [watsonx.ai](https://www.ibm.com/products/watsonx-ai) foundation models.

This example shows how to communicate with `watsonx.ai` models using `LangChain`.

## Setting up[​](#setting-up "Direct link to Setting up")

Install the package `langchain-ibm`.

```
!pip install -qU langchain-ibm
```

This cell defines the WML credentials required to work with watsonx Embeddings.

**Action:** Provide the IBM Cloud user API key. For details, see [documentation](https://cloud.ibm.com/docs/account?topic=account-userapikey&interface=ui).

```
import osfrom getpass import getpasswatsonx_api_key = getpass()os.environ["WATSONX_APIKEY"] = watsonx_api_key
```

Additionaly you are able to pass additional secrets as an environment variable.

```
import osos.environ["WATSONX_URL"] = "your service instance url"os.environ["WATSONX_TOKEN"] = "your token for accessing the CPD cluster"os.environ["WATSONX_PASSWORD"] = "your password for accessing the CPD cluster"os.environ["WATSONX_USERNAME"] = "your username for accessing the CPD cluster"os.environ["WATSONX_INSTANCE_ID"] = "your instance_id for accessing the CPD cluster"
```

## Load the model[​](#load-the-model "Direct link to Load the model")

You might need to adjust model `parameters` for different models.

```
from ibm_watsonx_ai.metanames import EmbedTextParamsMetaNamesembed_params = {    EmbedTextParamsMetaNames.TRUNCATE_INPUT_TOKENS: 3,    EmbedTextParamsMetaNames.RETURN_OPTIONS: {"input_text": True},}
```

Initialize the `WatsonxEmbeddings` class with previously set parameters.

**Note**:

*   To provide context for the API call, you must add `project_id` or `space_id`. For more information see [documentation](https://www.ibm.com/docs/en/watsonx-as-a-service?topic=projects).
*   Depending on the region of your provisioned service instance, use one of the urls described [here](https://ibm.github.io/watsonx-ai-python-sdk/setup_cloud.html#authentication).

In this example, we’ll use the `project_id` and Dallas url.

You need to specify `model_id` that will be used for inferencing.

```
from langchain_ibm import WatsonxEmbeddingswatsonx_embedding = WatsonxEmbeddings(    model_id="ibm/slate-125m-english-rtrvr",    url="https://us-south.ml.cloud.ibm.com",    project_id="PASTE YOUR PROJECT_ID HERE",    params=embed_params,)
```

Alternatively you can use Cloud Pak for Data credentials. For details, see [documentation](https://ibm.github.io/watsonx-ai-python-sdk/setup_cpd.html).

```
watsonx_embedding = WatsonxEmbeddings(    model_id="ibm/slate-125m-english-rtrvr",    url="PASTE YOUR URL HERE",    username="PASTE YOUR USERNAME HERE",    password="PASTE YOUR PASSWORD HERE",    instance_id="openshift",    version="5.0",    project_id="PASTE YOUR PROJECT_ID HERE",    params=embed_params,)
```

## Usage[​](#usage "Direct link to Usage")

### Embed query[​](#embed-query "Direct link to Embed query")

```
text = "This is a test document."query_result = watsonx_embedding.embed_query(text)query_result[:5]
```

```
[0.0094472, -0.024981909, -0.026013248, -0.040483925, -0.057804465]
```

### Embed documents[​](#embed-documents "Direct link to Embed documents")

```
texts = ["This is a content of the document", "This is another document"]doc_result = watsonx_embedding.embed_documents(texts)doc_result[0][:5]
```

```
[0.009447193, -0.024981918, -0.026013244, -0.040483937, -0.057804447]
```