IBM watsonx.ai embeddings wrapper.
Async decorator to catch ApiRequestFailure on Model Gateway calls.
Log a uniform warning when the Model Gateway is misused.
Extract params.
Decorator to catch ApiRequestFailure on Model Gateway calls.
Logs a uniform warning when the model is not properly registered.
Normalize deprecated 'apikey' to 'api_key'.
Resolve watsonx credentials.
Return default factory that yields a SecretStr from the first non-empty env var.
The factory:
names_priority).deprecated.IBM watsonx.ai embedding model integration.
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.
pip install -U langchain-ibm
# or using uv
uv add langchain-ibm
export WATSONX_API_KEY="your-api-key"
apikey and WATSONX_APIKEY are deprecated and will be removed in
version 2.0.0. Use api_key and WATSONX_API_KEY instead.
from langchain_ibm import WatsonxEmbeddings
embeddings = WatsonxEmbeddings(
model_id="ibm/granite-embedding-278m-multilingual",
url="https://us-south.ml.cloud.ibm.com",
project_id="*****",
# api_key="*****"
)input_text = "The meaning of life is 42"
vector = embeddings.embed_query("hello")
print(vector[:3])
[-0.0020519258, 0.0147288125, -0.0090887165]vectors = embeddings.embed_documents(["hello", "goodbye"])
# Showing only the first 3 coordinates
print(len(vectors))
print(vectors[0][:3])
2
[-0.0020519265, 0.01472881, -0.009088721]await embeddings.aembed_query(input_text)
print(vector[:3])
# multiple:
# await embeddings.aembed_documents(input_texts)
[-0.0020519258, 0.0147288125, -0.0090887165]