docs/docs/integrations/embedding-models/watsonx.md
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-watsonx</artifactId>
<version>1.11.7-beta19</version>
</dependency>
Watsonx.ai supports authentication via the Authenticator interface.
This allows to use different authentication mechanisms depending on your deployment:
apiKey(...) builder method.Authenticator interface can be used.The WatsonxEmbeddingModel and other service builders accept either a shortcut via .apiKey(...) or a full Authenticator instance via .authenticator(...).
WatsonxEmbeddingModel.builder()
.baseUrl(CloudRegion.FRANKFURT)
.apiKey("your-api-key") // Simple IBM Cloud authentication
.projectId("your-project-id")
.modelName("ibm/granite-embedding-278m-multilingual")
.build();
WatsonxEmbeddingModel.builder()
.baseUrl("https://my-instance-url")
.authenticator( // For Cloud Pak for Data deployments
CP4DAuthenticator.builder()
.baseUrl("https://my-instance-url")
.username("username")
.apiKey("api-key")
.build()
)
.projectId("my-project-id")
.modelName("ibm/granite-embedding-278m-multilingual")
.build();
All services and authenticators support a custom HttpClient instance through the builder pattern. This is particularly useful for Cloud Pak for Data environments where you may need to configure custom TLS/SSL settings, proxy configuration, or other HTTP client properties.
HttpClient httpClient = HttpClient.newBuilder()
.sslContext(createCustomSSLContext())
.executor(ExecutorProvider.ioExecutor())
.build();
EmbeddingModel embeddingModel = WatsonxEmbeddingModel.builder()
.baseUrl("https://my-instance-url")
.modelName("ibm/granite-embedding-278m-multilingual")
.projectId("project-id")
.httpClient(httpClient) // Custom HttpClient
.authenticator(
CP4DAuthenticator.builder()
.baseUrl("https://my-instance-url")
.username("username")
.apiKey("api-key")
.httpClient(httpClient) // Custom HttpClient
.build()
)
.build();
Note: When using a custom
HttpClientwith Cloud Pak for Data, make sure to set it on both the service builder and the authenticator builder to ensure consistent HTTP behavior across all requests.
If you only need to disable SSL certificate verification, you can use the verifySsl(false) option instead of providing a custom HttpClient:
EmbeddingModel embeddingModel = WatsonxEmbeddingModel.builder()
.baseUrl("https://my-instance-url")
.modelName("ibm/granite-embedding-278m-multilingual")
.projectId("project-id")
.verifySsl(false) // Disable SSL verification
.authenticator(
CP4DAuthenticator.builder()
.baseUrl("https://my-instance-url")
.username("username")
.apiKey("api-key")
.verifySsl(false) // Disable SSL verification
.build()
)
.build();
You can create an API key at https://cloud.ibm.com/iam/apikeys by clicking Create +.
The WatsonxEmbeddingModel enables you to generate embeddings using IBM watsonx.ai and integrate them with LangChain4j's vector-based operations such as search, retrieval-augmented generation (RAG), and similarity comparison.
It implements the LangChain4j EmbeddingModel interface.
EmbeddingModel embeddingModel = WatsonxEmbeddingModel.builder()
.baseUrl(CloudRegion.FRANKFURT)
.apiKey("your-api-key")
.projectId("your-project-id")
.modelName("ibm/granite-embedding-278m-multilingual")
.build();
System.out.println(embeddingModel.embed("Hello from watsonx.ai"));