WatsonxEmbeddings#
- class langchain_ibm.embeddings.WatsonxEmbeddings[source]#
Bases:
BaseModel
,Embeddings
IBM watsonx.ai embedding model integration.
???+ info “Setup”
To use, you should have langchain_ibm python package installed, and the environment variable WATSONX_APIKEY set with your API key, or pass it as a named parameter apikey to the constructor.
```bash pip install -U langchain-ibm
# or using uv uv add langchain-ibm ```
`bash export WATSONX_APIKEY="your-api-key" `
??? info “Instantiate”
```python from langchain_ibm import WatsonxEmbeddings
- embeddings = WatsonxEmbeddings(
model_id=”ibm/granite-embedding-278m-multilingual”, url=”https://us-south.ml.cloud.ibm.com”, project_id=”*”, # apikey=”*”
??? info “Embed single text”
`python input_text = "The meaning of life is 42" vector = embeddings.embed_query("hello") print(vector[:3]) `
`python [-0.0020519258, 0.0147288125, -0.0090887165] `
??? info “Embed multiple texts”
`python vectors = embeddings.embed_documents(["hello", "goodbye"]) # Showing only the first 3 coordinates print(len(vectors)) print(vectors[0][:3]) `
`python 2 [-0.0020519265, 0.01472881, -0.009088721] `
??? info “Async”
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- param apikey: SecretStr | None [Optional]#
API key to the Watson Machine Learning or CPD instance.
- param instance_id: SecretStr | None [Optional]#
Instance_id of the CPD instance.
- param model: str | None = None#
Name or alias of the foundation model to use. When using IBM’s watsonx.ai Model Gateway (public preview), you can specify any supported third-party model—OpenAI, Anthropic, NVIDIA, Cerebras, or IBM’s own Granite series—via a single, OpenAI-compatible interface. Models must be explicitly provisioned (opt-in) through the Gateway to ensure secure, vendor-agnostic access and easy switch-over without reconfiguration.
For more details on configuration and usage, see [IBM watsonx Model Gateway docs](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/fm-model-gateway.html?context=wx&audience=wdp)
- param model_id: str | None = None#
Type of model to use.
- param params: dict | None = None#
Model parameters to use during request generation.
- param password: SecretStr | None [Optional]#
Password to the CPD instance.
- param project_id: str | None = None#
ID of the Watson Studio project.
- param space_id: str | None = None#
ID of the Watson Studio space.
- param token: SecretStr | None [Optional]#
Token to the CPD instance.
- param url: SecretStr [Optional]#
URL to the Watson Machine Learning or CPD instance.
- param username: SecretStr | None [Optional]#
Username to the CPD instance.
- param verify: str | bool | None = None#
You can pass one of following as verify: * the path to a CA_BUNDLE file * the path of directory with certificates of trusted CAs * True - default path to truststore will be taken * False - no verification will be made
- param version: SecretStr | None = None#
Version of the CPD instance.
- async aembed_documents(
- texts: list[str],
- **kwargs: Any,
Asynchronous Embed search docs.
- Parameters:
texts (list[str])
kwargs (Any)
- Return type:
list[list[float]]
- async aembed_query(
- text: str,
- **kwargs: Any,
Asynchronous Embed query text.
- Parameters:
text (str)
kwargs (Any)
- Return type:
list[float]