Class DashScopeChatModel.Builder

java.lang.Object
io.agentscope.extensions.model.dashscope.DashScopeChatModel.Builder
Enclosing class:
DashScopeChatModel

public static class DashScopeChatModel.Builder extends Object
  • Constructor Details

    • Builder

      public Builder()
  • Method Details

    • apiKey

      public DashScopeChatModel.Builder apiKey(String apiKey)
      Sets the API key for DashScope authentication.
      Parameters:
      apiKey - the API key
      Returns:
      this builder instance
    • modelName

      public DashScopeChatModel.Builder modelName(String 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

      public DashScopeChatModel.Builder stream(boolean stream)
      Sets whether streaming should be enabled.
      Parameters:
      stream - true to enable streaming, false for non-streaming
      Returns:
      this builder instance
    • enableThinking

      public DashScopeChatModel.Builder enableThinking(Boolean 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

      public DashScopeChatModel.Builder enableSearch(Boolean 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

      public DashScopeChatModel.Builder endpointType(EndpointType 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

      public DashScopeChatModel.Builder defaultOptions(io.agentscope.core.model.GenerateOptions options)
      Sets the default generation options.
      Parameters:
      options - the default options to use (null for defaults)
      Returns:
      this builder instance
    • baseUrl

      public DashScopeChatModel.Builder baseUrl(String 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 HttpTransportFactory will 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 httpTransport is 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 calling httpTransport(), for simple proxy-only customization.
      • Configure the proxy directly within the transport's HttpTransportConfig when building the transport instance.

      If both httpTransport() and proxy() are called, httpTransport() takes full precedence and proxy() is ignored (a warning is logged at build time).

      Parameters:
      httpTransport - the HTTP transport (null for default from factory)
      Returns:
      this builder instance
    • proxy

      public DashScopeChatModel.Builder proxy(io.agentscope.core.model.transport.ProxyConfig proxyConfig)
      Sets the proxy configuration for HTTP traffic.

      Interaction with httpTransport(HttpTransport): Because httpTransport is 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 default OkHttpTransport is 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 own HttpTransportConfig).
      • If both are called: httpTransport() takes full precedence. The proxy() setting is ignored and a warning is logged. To configure proxy with a custom transport, set it within the transport's HttpTransportConfig directly.
      • If neither is called: the default transport from HttpTransportFactory is used (no proxy).
      Parameters:
      proxyConfig - the proxy configuration (see ProxyConfig)
      Returns:
      this builder instance
    • enableEncrypt

      public DashScopeChatModel.Builder enableEncrypt(boolean 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

      public DashScopeChatModel.Builder contextWindowSize(int 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

      public DashScopeChatModel.Builder nativeStructuredOutput(boolean nativeStructuredOutput)
      Sets whether this model supports native structured output via response_format with json_schema type.

      Defaults to false. DashScope's native endpoint only supports json_object (free-form JSON), not json_schema (strict schema validation). When false, the framework uses the generate_response tool fallback for structured output requests.

      Set to true only if your model/endpoint is confirmed to support json_schema in response_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 from nativeStructuredOutput(boolean)). Set to true only for models that support both response_format and 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.AUTO is 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 of DashScopeHttpClient.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

      public DashScopeChatModel 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) and httpTransport(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