docs/examples/embeddings/ibm_watsonx.ipynb
<a href="https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/embeddings/ibm_watsonx.ipynb" target="_parent"></a>
WatsonxEmbeddings is a wrapper for IBM watsonx.ai embedding models.
This example shows how to communicate with watsonx.ai embedding models using the LlamaIndex Embeddings API.
Install the llama-index-embeddings-ibm package:
!pip install -qU llama-index-embeddings-ibm
The cell below defines the credentials required to work with watsonx Embeddings.
Action: Provide the IBM Cloud user API key. For details, see Managing user API keys.
import os
from getpass import getpass
watsonx_api_key = getpass()
os.environ["WATSONX_APIKEY"] = watsonx_api_key
Additionally, you can pass additional secrets as an environment variable:
import os
os.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"
You might need to adjust embedding parameters for different tasks:
truncate_input_tokens = 3
Initialize the WatsonxEmbeddings class with the previously set parameter.
Note:
project_id or space_id. To get your project or space ID, open your project or space, go to the Manage tab, and click General. For more information see: Project documentation or Deployment space documentation.In this example, we’ll use the project_id and Dallas URL.
You need to specify the model_id that will be used for inferencing. You can find the list of all the available models in Supported foundation models.
from llama_index.embeddings.ibm import WatsonxEmbeddings
watsonx_embedding = WatsonxEmbeddings(
model_id="ibm/slate-125m-english-rtrvr",
url="https://us-south.ml.cloud.ibm.com",
project_id="PASTE YOUR PROJECT_ID HERE",
truncate_input_tokens=truncate_input_tokens,
)
Alternatively, you can use Cloud Pak for Data credentials. For details, see watsonx.ai software setup.
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="4.8",
project_id="PASTE YOUR PROJECT_ID HERE",
truncate_input_tokens=truncate_input_tokens,
)
query = "Example query."
query_result = watsonx_embedding.get_query_embedding(query)
print(query_result[:5])
texts = ["This is a content of one document", "This is another document"]
doc_result = watsonx_embedding.get_text_embedding_batch(texts)
print(doc_result[0][:5])