Class WatsonxChatModel.Builder
java.lang.Object
dev.langchain4j.model.watsonx.WatsonxChatModel.Builder
- Enclosing class:
WatsonxChatModel
Builder class for constructing
WatsonxChatModel instances with configurable parameters.-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionSets the IBM Cloud API key used to generate IAM access tokens for authentication.authenticator(com.ibm.watsonx.ai.core.auth.Authenticator authenticator) Sets a customAuthenticatorfor generating bearer tokens.baseUrl(com.ibm.watsonx.ai.CloudRegion baseUrl) Sets the IBM watsonx.ai endpoint from a predefinedCloudRegion.Sets the base URL of the IBM watsonx.ai API.Sets the base URL of the IBM watsonx.ai API as aURI.build()defaultRequestParameters(ChatRequestParameters defaultRequestParameters) Sets default request parameters that are merged into every chat request.deploymentId(String deploymentId) frequencyPenalty(Double frequencyPenalty) Sets the frequency penalty in the range[-2.0, 2.0].guidedChoice(String... guidedChoice) Constrains the model output to one of the given string choices (guided decoding).guidedChoice(Set<String> guidedChoices) Constrains the model output to one of the given string choices (guided decoding).guidedGrammar(String guidedGrammar) Constrains the model output to conform to the given EBNF grammar (guided decoding).guidedRegex(String guidedRegex) Constrains the model output to match the given regular expression (guided decoding).httpClient(HttpClient httpClient) Sets a customHttpClientto use for all API calls.lengthPenalty(Double lengthPenalty) Sets the length penalty applied to the sequence score during beam search.listeners(List<ChatModelListener> listeners) Sets the list ofChatModelListenerinstances for observing chat model interactions.Sets per-token logit biases to increase or decrease the likelihood of specific tokens.Enables returning log probabilities of the output tokens.logRequests(Boolean logRequests) Enables debug logging of request bodies sent to the watsonx.ai API.logResponses(Boolean logResponses) Enables debug logging of response bodies received from the watsonx.ai API.maxOutputTokens(Integer maxOutputTokens) Sets the maximum number of tokens to generate in the response.Sets the watsonx.ai model ID, e.g.presencePenalty(Double presencePenalty) Sets the presence penalty in the range[-2.0, 2.0].Sets the IBM Cloud project ID that owns the watsonx.ai resources.repetitionPenalty(Double repetitionPenalty) Sets the repetition penalty.responseFormat(ResponseFormat responseFormat) Sets the response format to control structured output, e.g.Sets the random seed for deterministic sampling.Sets the IBM Cloud deployment space ID.stopSequences(String... stopSequences) Sets the sequences that will stop generation when encountered.stopSequences(List<String> stopSequences) Sets the sequences that will stop generation when encountered.supportedCapabilities(Capability... supportedCapabilities) Declares the capabilities supported by this model instance.supportedCapabilities(Set<Capability> supportedCapabilities) Declares the capabilities supported by this model instance, e.g. vision or token-level streaming.temperature(Double temperature) Sets the sampling temperature in the range[0.0, 2.0].thinking(boolean enabled) Enables or disables extended thinking (chain-of-thought reasoning before the response).thinking(com.ibm.watsonx.ai.chat.model.ExtractionTags tags) Configures extended thinking with custom extraction tags for parsing the thinking block.thinking(com.ibm.watsonx.ai.chat.model.Thinking thinking) Sets a fully configuredThinkingobject for extended thinking.thinking(com.ibm.watsonx.ai.chat.model.ThinkingEffort thinkingEffort) Configures extended thinking with a specific effort level.Sets the HTTP request timeout.toolChoice(ToolChoice toolChoice) Sets how the model selects tools.toolChoiceName(String toolChoiceName) Sets the name of the specific tool to force whenWatsonxChat.Builder.toolChoiceis set to force a particular tool.toolSpecifications(ToolSpecification... toolSpecifications) Sets the tool definitions available to the model for function calling.toolSpecifications(List<ToolSpecification> toolSpecifications) Sets the tool definitions available to the model for function calling.topLogprobs(Integer topLogprobs) Sets the number of most likely tokens to return log probabilities for at each position.Sets the nucleus sampling probability in the range(0.0, 1.0].verifySsl(boolean verifySsl) Controls whether SSL certificate verification is performed.Sets the watsonx.ai API version date, e.g.
-
Field Details
-
baseUrl
-
version
-
apiKey
-
projectId
-
spaceId
-
logRequests
-
logResponses
-
timeout
-
authenticator
protected com.ibm.watsonx.ai.core.auth.Authenticator authenticator -
httpClient
-
verifySsl
protected boolean verifySsl
-
-
Method Details
-
build
-
modelName
Sets the watsonx.ai model ID, e.g."ibm/granite-3-8b-instruct".- Parameters:
modelName- the model ID- Returns:
this
-
temperature
Sets the sampling temperature in the range[0.0, 2.0]. Higher values produce more random output; lower values are more deterministic.- Parameters:
temperature- the sampling temperature- Returns:
this
-
topP
Sets the nucleus sampling probability in the range(0.0, 1.0]. The model considers only the tokens whose cumulative probability reaches this threshold.- Parameters:
topP- the nucleus sampling threshold- Returns:
this
-
frequencyPenalty
Sets the frequency penalty in the range[-2.0, 2.0]. Positive values reduce the likelihood of repeating tokens proportional to how often they have appeared.- Parameters:
frequencyPenalty- the frequency penalty- Returns:
this
-
presencePenalty
Sets the presence penalty in the range[-2.0, 2.0]. Positive values reduce the likelihood of repeating any token that has already appeared in the output.- Parameters:
presencePenalty- the presence penalty- Returns:
this
-
maxOutputTokens
Sets the maximum number of tokens to generate in the response.- Parameters:
maxOutputTokens- the maximum number of output tokens- Returns:
this
-
stopSequences
Sets the sequences that will stop generation when encountered.- Parameters:
stopSequences- the stop sequences- Returns:
this
-
stopSequences
Sets the sequences that will stop generation when encountered.- Parameters:
stopSequences- the stop sequences- Returns:
this
-
toolChoice
Sets how the model selects tools. Controls whether tool use is automatic, forced, or disabled.- Parameters:
toolChoice- the tool choice mode- Returns:
this
-
responseFormat
Sets the response format to control structured output, e.g. JSON mode.- Parameters:
responseFormat- the response format- Returns:
this
-
logitBias
Sets per-token logit biases to increase or decrease the likelihood of specific tokens. Keys are token IDs; values are bias offsets in the range[-100, 100].- Parameters:
logitBias- the logit bias map- Returns:
this
-
logprobs
Enables returning log probabilities of the output tokens.- Parameters:
logprobs-trueto include log probabilities in the response- Returns:
this
-
topLogprobs
Sets the number of most likely tokens to return log probabilities for at each position. RequiresWatsonxChat.Builder.logprobsto betrue. Value must be between 0 and 20.- Parameters:
topLogprobs- the number of top log probabilities to return- Returns:
this
-
seed
Sets the random seed for deterministic sampling. Using the same seed and parameters should produce the same output across calls.- Parameters:
seed- the random seed- Returns:
this
-
toolChoiceName
Sets the name of the specific tool to force whenWatsonxChat.Builder.toolChoiceis set to force a particular tool.- Parameters:
toolChoiceName- the tool name to force- Returns:
this
-
supportedCapabilities
Declares the capabilities supported by this model instance, e.g. vision or token-level streaming.- Parameters:
supportedCapabilities- the set of supported capabilities- Returns:
this
-
supportedCapabilities
Declares the capabilities supported by this model instance.- Parameters:
supportedCapabilities- the supported capabilities- Returns:
this
-
toolSpecifications
Sets the tool definitions available to the model for function calling.- Parameters:
toolSpecifications- the list of tool specifications- Returns:
this
-
toolSpecifications
Sets the tool definitions available to the model for function calling.- Parameters:
toolSpecifications- the tool specifications- Returns:
this
-
listeners
Sets the list ofChatModelListenerinstances for observing chat model interactions.- Parameters:
listeners- the listeners to register- Returns:
this
-
defaultRequestParameters
public WatsonxChatModel.Builder defaultRequestParameters(ChatRequestParameters defaultRequestParameters) Sets default request parameters that are merged into every chat request.- Parameters:
defaultRequestParameters- the default request parameters- Returns:
this
-
deploymentId
-
thinking
Enables or disables extended thinking (chain-of-thought reasoning before the response).- Parameters:
enabled-trueto enable extended thinking- Returns:
this
-
thinking
Configures extended thinking with custom extraction tags for parsing the thinking block. Passingnulldisables thinking.- Parameters:
tags- the extraction tags, ornullto disable thinking- Returns:
this
-
thinking
public WatsonxChatModel.Builder thinking(com.ibm.watsonx.ai.chat.model.ThinkingEffort thinkingEffort) Configures extended thinking with a specific effort level. Passingnulldisables thinking.- Parameters:
thinkingEffort- the thinking effort level, ornullto disable thinking- Returns:
this
-
thinking
Sets a fully configuredThinkingobject for extended thinking.- Parameters:
thinking- the thinking configuration- Returns:
this
-
guidedChoice
Constrains the model output to one of the given string choices (guided decoding).- Parameters:
guidedChoice- the allowed output values- Returns:
this
-
guidedChoice
Constrains the model output to one of the given string choices (guided decoding).- Parameters:
guidedChoices- the set of allowed output values- Returns:
this
-
guidedRegex
Constrains the model output to match the given regular expression (guided decoding).- Parameters:
guidedRegex- the regular expression pattern- Returns:
this
-
guidedGrammar
Constrains the model output to conform to the given EBNF grammar (guided decoding).- Parameters:
guidedGrammar- the EBNF grammar string- Returns:
this
-
repetitionPenalty
Sets the repetition penalty. Values greater than1.0discourage repetition; values less than1.0encourage it.- Parameters:
repetitionPenalty- the repetition penalty- Returns:
this
-
lengthPenalty
Sets the length penalty applied to the sequence score during beam search. Values greater than1.0favor longer sequences.- Parameters:
lengthPenalty- the length penalty- Returns:
this
-
baseUrl
Sets the IBM watsonx.ai endpoint from a predefinedCloudRegion.- Parameters:
baseUrl- the IBM Cloud region whose ML endpoint will be used- Returns:
this
-
baseUrl
Sets the base URL of the IBM watsonx.ai API.- Parameters:
url- the base URL string, e.g."https://us-south.ml.cloud.ibm.com"- Returns:
this
-
baseUrl
Sets the base URL of the IBM watsonx.ai API as aURI.- Parameters:
url- the base URL URI- Returns:
this
-
version
Sets the watsonx.ai API version date, e.g."2024-05-31".- Parameters:
version- the API version date string- Returns:
this
-
projectId
Sets the IBM Cloud project ID that owns the watsonx.ai resources. Exactly one ofprojectIdorspaceIdmust be set.- Parameters:
projectId- the IBM Cloud project ID- Returns:
this
-
spaceId
Sets the IBM Cloud deployment space ID. Exactly one ofprojectIdorspaceIdmust be set.- Parameters:
spaceId- the IBM Cloud deployment space ID- Returns:
this
-
apiKey
Sets the IBM Cloud API key used to generate IAM access tokens for authentication.- Parameters:
apiKey- the IBM Cloud API key- Returns:
this
-
logRequests
Enables debug logging of request bodies sent to the watsonx.ai API.- Parameters:
logRequests-trueto enable request logging- Returns:
this
-
logResponses
Enables debug logging of response bodies received from the watsonx.ai API.- Parameters:
logResponses-trueto enable response logging- Returns:
this
-
timeout
Sets the HTTP request timeout. Defaults to 60 seconds.- Parameters:
timeout- the request timeout- Returns:
this
-
authenticator
public WatsonxChatModel.Builder authenticator(com.ibm.watsonx.ai.core.auth.Authenticator authenticator) Sets a customAuthenticatorfor generating bearer tokens. Use this instead ofapiKeywhen you need a non-standard authentication flow.- Parameters:
authenticator- the authenticator- Returns:
this
-
httpClient
Sets a customHttpClientto use for all API calls.- Parameters:
httpClient- the HTTP client- Returns:
this
-
verifySsl
Controls whether SSL certificate verification is performed. Defaults totrue. Set tofalseonly in non-production environments.- Parameters:
verifySsl-falseto disable SSL verification- Returns:
this
-