Class WatsonxClient
- All Implemented Interfaces:
com.tnsai.llm.LLMClient
watsonx.ai is IBM's enterprise LLM platform. The chat endpoint at
/ml/v1/text/chat returns OpenAI-shaped responses but the
request body uses IBM-specific fields (model_id instead of
model, plus a required project_id) — so it can't
reuse the same Request construction as the OpenAI-shape
providers in this module.
Auth: IBM Cloud IAM token exchange. Unlike bearer-token
cloud providers, watsonx requires an IAM access token obtained by
exchanging an IBM Cloud API key. The client does the exchange
lazily on first call against
https://iam.cloud.ibm.com/identity/token, caches the
resulting token until its expires_in window closes (minus a
5-minute safety margin), and re-exchanges transparently on cache
miss or when the cached token enters the safety window. The
exchange happens under the this monitor so concurrent
chat calls block on at most one refresh — never multiple racing
exchanges. Tokens themselves are never logged; only the expiry
timestamp is.
Supported models (built-in catalog at time of writing):
- ibm/granite-3-8b-instruct — IBM's own Granite 3
- meta-llama/llama-3-3-70b-instruct — Llama 3.3 hosted on watsonx
- mistralai/mistral-large — Mistral Large via watsonx
The catalog evolves; check the live list at cloud.ibm.com/apidocs/watsonx-ai.
Configuration via environment variables:
WATSONX_API_KEY— Required. The IBM Cloud API key; the client exchanges it for an IAM token automatically.WATSONX_PROJECT_ID— Required. The watsonx project ID that scopes model access and billing.WATSONX_BASE_URL— Optional. Defaults to theus-southregion endpoint (https://us-south.ml.cloud.ibm.com); override foreu-de,jp-tok, etc.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionWatsonxClient(String model) WatsonxClient(String model, float temperature) WatsonxClient(String model, float temperature, Float topP, Integer maxTokens) WatsonxClient(String model, float temperature, Integer maxTokens) WatsonxClient(String baseUrl, String model, float temperature, Float topP, Integer maxTokens, String apiKey, String projectId) -
Method Summary
Modifier and TypeMethodDescriptioncom.tnsai.llm.ChatResponsechat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) streamChat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) Methods inherited from class com.tnsai.llm.providers.AbstractLLMClient
getBaseUrl, getCallTimeoutMs, getMaxTokens, getModel, getTemperature, getTopPMethods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.tnsai.llm.LLMClient
chat, chat, chat, chat, chat, getCapabilities, getProviderName, listModels, streamChat, streamChat, streamChatWithHandler, streamChatWithHandler, streamChatWithSpec, streamChatWithSpec
-
Constructor Details
-
WatsonxClient
-
WatsonxClient
-
WatsonxClient
-
WatsonxClient
-
WatsonxClient
-
-
Method Details
-
chat
-
streamChat
-