Class DashScopeChatModel.Builder
- Enclosing class:
- DashScopeChatModel
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionSets the API key for DashScope authentication.Sets a custom base URL for DashScope API.build()Builds the DashScopeChatModel instance.contextWindowSize(int contextWindowSize) Overrides the inferred context window size (in tokens).defaultOptions(io.agentscope.core.model.GenerateOptions options) Sets the default generation options.enableEncrypt(boolean enableEncrypt) Sets whether encryption should be enabled.enableSearch(Boolean enableSearch) Sets whether search enhancement should be enabled.enableThinking(Boolean enableThinking) Sets whether thinking mode should be enabled.endpointType(EndpointType endpointType) Sets the endpoint type to use for endpoint routing.formatter(io.agentscope.core.formatter.Formatter<DashScopeMessage, DashScopeResponse, DashScopeRequest> formatter) Sets the message formatter to use.httpTransport(io.agentscope.core.model.transport.HttpTransport httpTransport) Sets the HTTP transport to use.Sets the model name to use.multimodalModelPatterns(Collection<String> multimodalModelPatterns) Sets additional case-insensitive substring patterns for multimodal model detection.nativeStructuredOutput(boolean nativeStructuredOutput) Sets whether this model supports native structured output viaresponse_formatwithjson_schematype.nativeStructuredOutputWithTools(boolean nativeStructuredOutputWithTools) Sets whether this model correctly handles native structured output (response_format) alongside tool calling.proxy(io.agentscope.core.model.transport.ProxyConfig proxyConfig) Sets the proxy configuration for HTTP traffic.stream(boolean stream) Sets whether streaming should be enabled.
-
Constructor Details
-
Builder
public Builder()
-
-
Method Details
-
apiKey
Sets the API key for DashScope authentication.- Parameters:
apiKey- the API key- Returns:
- this builder instance
-
modelName
Sets the model name to use.The model name determines which API is used when endpointType is AUTO.
- Parameters:
modelName- the model name (e.g., "qwen-max", "qwen-vl-plus")- Returns:
- this builder instance
- See Also:
-
stream
Sets whether streaming should be enabled.- Parameters:
stream- true to enable streaming, false for non-streaming- Returns:
- this builder instance
-
enableThinking
Sets whether thinking mode should be enabled.- Parameters:
enableThinking- true to enable thinking mode, false to disable, null for default- Returns:
- this builder instance
-
enableSearch
Sets whether search enhancement should be enabled.When enabled, the model can access internet search to provide more up-to-date and accurate responses.
- Parameters:
enableSearch- true to enable search mode, false to disable, null for default (disabled)- Returns:
- this builder instance
-
endpointType
Sets the endpoint type to use for endpoint routing.- Parameters:
endpointType- the endpoint type to use (null for AUTO)- Returns:
- this builder instance
- See Also:
-
defaultOptions
Sets the default generation options.- Parameters:
options- the default options to use (null for defaults)- Returns:
- this builder instance
-
baseUrl
Sets a custom base URL for DashScope API.- Parameters:
baseUrl- the base URL (null for default)- Returns:
- this builder instance
-
formatter
public DashScopeChatModel.Builder formatter(io.agentscope.core.formatter.Formatter<DashScopeMessage, DashScopeResponse, DashScopeRequest> formatter) Sets the message formatter to use.- Parameters:
formatter- the formatter (null for default DashScope formatter)- Returns:
- this builder instance
-
httpTransport
public DashScopeChatModel.Builder httpTransport(io.agentscope.core.model.transport.HttpTransport httpTransport) Sets the HTTP transport to use.If not set, the default transport from
HttpTransportFactorywill be used. This allows sharing a single transport instance across multiple models for better resource management.Example:
HttpTransport custom = OkHttpTransport.builder() .config(HttpTransportConfig.builder() .connectTimeout(Duration.ofSeconds(30)) .build()) .build(); DashScopeChatModel model = DashScopeChatModel.builder() .apiKey("xxx") .modelName("qwen-plus") .httpTransport(custom) .build();Note on proxy configuration: Because
httpTransportis a fully-constructed HTTP client instance, its configuration (including proxy) cannot be modified after creation. If you need to configure a proxy, either:- Use
proxy(ProxyConfig)without callinghttpTransport(), for simple proxy-only customization. - Configure the proxy directly within the transport's
HttpTransportConfigwhen building the transport instance.
If both
httpTransport()andproxy()are called,httpTransport()takes full precedence andproxy()is ignored (a warning is logged at build time).- Parameters:
httpTransport- the HTTP transport (null for default from factory)- Returns:
- this builder instance
- Use
-
proxy
Sets the proxy configuration for HTTP traffic.Interaction with
httpTransport(HttpTransport): BecausehttpTransportis a fully-constructed HTTP client instance, its configuration cannot be modified after creation. The final transport is determined as follows:- If only
proxy()is called: the defaultOkHttpTransportis used with the specified proxy configuration applied. - If only
httpTransport()is called: the provided transport is used as-is (proxy must be configured within the transport's ownHttpTransportConfig). - If both are called:
httpTransport()takes full precedence. Theproxy()setting is ignored and a warning is logged. To configure proxy with a custom transport, set it within the transport'sHttpTransportConfigdirectly. - If neither is called: the default transport from
HttpTransportFactoryis used (no proxy).
- Parameters:
proxyConfig- the proxy configuration (seeProxyConfig)- Returns:
- this builder instance
- If only
-
enableEncrypt
Sets whether encryption should be enabled.When enabled, the model will automatically fetch the latest RSA public key from DashScope API and use it to encrypt requests and responses using AES-GCM with RSA key exchange, following Aliyun's encryption protocol. This enables secure access to Aliyun models while complying with enterprise security policies (e.g., TLS encryption, token-based authentication).
If fetching the public key fails during build(), an exception will be thrown to prevent creating a model with incorrect encryption configuration.
Example:
DashScopeChatModel model = DashScopeChatModel.builder() .apiKey("sk-xxx") .modelName("qwen-max") .enableEncrypt(true) .build();- Parameters:
enableEncrypt- true to enable encryption (will fetch public key automatically), false to disable encryption- Returns:
- this builder instance
-
contextWindowSize
Overrides the inferred context window size (in tokens). When not set (-1), the size is inferred from the model name using a built-in lookup table. -
nativeStructuredOutput
Sets whether this model supports native structured output viaresponse_formatwithjson_schematype.Defaults to
false. DashScope's native endpoint only supportsjson_object(free-form JSON), notjson_schema(strict schema validation). Whenfalse, the framework uses thegenerate_responsetool fallback for structured output requests.Set to
trueonly if your model/endpoint is confirmed to supportjson_schemainresponse_format.- Parameters:
nativeStructuredOutput- true to enable native json_schema path- Returns:
- this builder instance
-
nativeStructuredOutputWithTools
public DashScopeChatModel.Builder nativeStructuredOutputWithTools(boolean nativeStructuredOutputWithTools) Sets whether this model correctly handles native structured output (response_format) alongside tool calling.Defaults to
false(inherits fromnativeStructuredOutput(boolean)). Set totrueonly for models that support bothresponse_formatand tool calling simultaneously.- Parameters:
nativeStructuredOutputWithTools- false to use fallback when tools are present- Returns:
- this builder instance
-
multimodalModelPatterns
public DashScopeChatModel.Builder multimodalModelPatterns(Collection<String> multimodalModelPatterns) Sets additional case-insensitive substring patterns for multimodal model detection.When
EndpointType.AUTOis used, a model name matching any of these patterns (as a substring, case-insensitively) is routed to the multimodal generation API, in addition to the built-in model-name rules ofDashScopeHttpClient.isMultimodalModel(String). This lets you use multimodal models not yet covered by the built-in whitelist (e.g."deepseek-v4.1") without waiting for a framework update.Example:
DashScopeChatModel model = DashScopeChatModel.builder() .apiKey("sk-xxx") .modelName("deepseek-v4.1") .multimodalModelPatterns(List.of("deepseek-v4")) .build();- Parameters:
multimodalModelPatterns- case-insensitive substring patterns (may be null)- Returns:
- this builder instance
- See Also:
-
build
Builds the DashScopeChatModel instance.This method ensures that the defaultOptions always has proper executionConfig applied.
If encryption is enabled, this method will automatically fetch the public key from DashScope API. If the fetch fails, an exception will be thrown.
Proxy resolution: If both
proxy(ProxyConfig)andhttpTransport(HttpTransport)are set,httpTransport()takes precedence and a warning is logged. Otherwise, the proxy is applied to a default transport, or the factory default is used.- Returns:
- configured DashScopeChatModel instance
- Throws:
DashScopeHttpClient.DashScopeHttpException- if encryption is enabled and public key fetching fails
-