IBM watsonx.ai
WatsonxEmbeddings is a wrapper for IBM watsonx.ai foundation models.
This example shows how to communicate with watsonx.ai
models using LangChain
.
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.
import os
from getpass import getpass
watsonx_api_key = getpass()
os.environ["WATSONX_APIKEY"] = watsonx_api_key
Additionaly you are able to 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"
Load the modelโ
You might need to adjust model parameters
for different models.
from ibm_watsonx_ai.metanames import EmbedTextParamsMetaNames
embed_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
orspace_id
. For more information see documentation. - Depending on the region of your provisioned service instance, use one of the urls described here.
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 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",
params=embed_params,
)
Alternatively you can use Cloud Pak for Data credentials. For details, see documentation.
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",
params=embed_params,
)
For certain requirements, there is an option to pass the IBM's APIClient
object into the WatsonxEmbeddings
class.
from ibm_watsonx_ai import APIClient
api_client = APIClient(...)
watsonx_llm = WatsonxEmbeddings(
model_id="ibm/slate-125m-english-rtrvr",
watsonx_client=api_client,
)
Usageโ
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โ
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]