类 DashScopeHttpClient

java.lang.Object
io.agentscope.core.model.DashScopeHttpClient

public class DashScopeHttpClient extends Object
HTTP client for DashScope API.

This client handles communication with DashScope's text-generation and multimodal-generation APIs using the native DashScope protocol.

Features:

  • Automatic API endpoint routing based on model name
  • Synchronous and streaming request support
  • SSE stream parsing
  • JSON serialization/deserialization

API endpoints:

  • Text generation: /api/v1/services/aigc/text-generation/generation
  • Multimodal generation: /api/v1/services/aigc/multimodal-generation/generation
  • 字段详细资料

    • DEFAULT_BASE_URL

      public static final String DEFAULT_BASE_URL
      Default base URL for DashScope API.
      另请参阅:
    • TEXT_GENERATION_ENDPOINT

      public static final String TEXT_GENERATION_ENDPOINT
      Text generation API endpoint.
      另请参阅:
    • MULTIMODAL_GENERATION_ENDPOINT

      public static final String MULTIMODAL_GENERATION_ENDPOINT
      Multimodal generation API endpoint.
      另请参阅:
    • PUBLIC_KEYS_ENDPOINT

      public static final String PUBLIC_KEYS_ENDPOINT
      Public keys API endpoint.
      另请参阅:
  • 构造器详细资料

    • DashScopeHttpClient

      public DashScopeHttpClient(HttpTransport transport, String apiKey, String baseUrl, String publicKeyId, String publicKey)
      Create a new DashScopeHttpClient.
      参数:
      transport - the HTTP transport to use
      apiKey - the DashScope API key
      baseUrl - the base URL (null for default)
      publicKeyId - the RSA public key ID for encryption (null to disable encryption)
      publicKey - the RSA public key for encryption (Base64-encoded, null to disable encryption)
    • DashScopeHttpClient

      public DashScopeHttpClient(String apiKey, String baseUrl)
      Create a new DashScopeHttpClient.
      参数:
      apiKey - the DashScope API key
      baseUrl - the base URL (null for default)
    • DashScopeHttpClient

      public DashScopeHttpClient(String apiKey, String baseUrl, String publicKeyId, String publicKey)
      Create a new DashScopeHttpClient with default transport from factory.

      Uses HttpTransportFactory.getDefault() for the transport, which provides automatic lifecycle management and cleanup on JVM shutdown.

      参数:
      apiKey - the DashScope API key
      baseUrl - the base URL (null for default)
      publicKeyId - the RSA public key ID for encryption (null to disable encryption)
      publicKey - the RSA public key for encryption (Base64-encoded, null to disable encryption)
    • DashScopeHttpClient

      public DashScopeHttpClient(String apiKey)
      Create a new DashScopeHttpClient with default transport and base URL.
      参数:
      apiKey - the DashScope API key
  • 方法详细资料

    • isEncryptionEnabled

      public boolean isEncryptionEnabled()
      Check if encryption is enabled.
      返回:
      true if both publicKeyId and publicKey are set
    • call

      public DashScopeResponse call(DashScopeRequest request, Map<String,String> additionalHeaders, Map<String,Object> additionalBodyParams, Map<String,String> additionalQueryParams)
      Make a synchronous API call with additional HTTP parameters.
      参数:
      request - the DashScope request
      additionalHeaders - additional HTTP headers to merge (may be null)
      additionalBodyParams - additional body parameters to merge (may be null)
      additionalQueryParams - additional query parameters to append (may be null)
      返回:
      the DashScope response
      抛出:
      DashScopeHttpClient.DashScopeHttpException - if the request fails
    • stream

      public reactor.core.publisher.Flux<DashScopeResponse> stream(DashScopeRequest request, Map<String,String> additionalHeaders, Map<String,Object> additionalBodyParams, Map<String,String> additionalQueryParams)
      Make a streaming API call with additional HTTP parameters.
      参数:
      request - the DashScope request
      additionalHeaders - additional HTTP headers to merge (may be null)
      additionalBodyParams - additional body parameters to merge (may be null)
      additionalQueryParams - additional query parameters to append (may be null)
      返回:
      a Flux of DashScope responses (one per SSE event)
    • selectEndpoint

      public String selectEndpoint(String modelName)
      Select the appropriate API endpoint based on model name.

      This is a convenience method that uses EndpointType.AUTO for automatic detection.

      参数:
      modelName - the model name
      返回:
      the API endpoint path
    • selectEndpoint

      public String selectEndpoint(String modelName, EndpointType endpointType)
      Select the appropriate API endpoint based on model name and endpoint type.

      Routing logic:

      • If endpointType is EndpointType.TEXT → text generation API
      • If endpointType is EndpointType.MULTIMODAL → multimodal API
      • If endpointType is EndpointType.AUTO:
        • Models starting with "qvq" → multimodal API
        • Models containing "-vl" → multimodal API
        • Models containing "-asr" → multimodal API
        • Models starting with "qwen3.5" → multimodal API
        • Models starting with "qwen3.6" → multimodal API
        • All other models → text generation API
      参数:
      modelName - the model name
      endpointType - the endpoint type to use (AUTO for automatic detection)
      返回:
      the API endpoint path
    • isMultimodalModel

      public static boolean isMultimodalModel(String modelName)
      Check if a model is a multimodal model that requires the multimodal-generation API.

      Supported multimodal model patterns (used when endpointType is AUTO):

      • Models starting with "qvq" (e.g., qvq-72b, qvq-max)
      • Models containing "-vl" (e.g., qwen-vl-plus, qwen3-vl-max)
      • Models containing "-asr" (e.g., qwen3-asr-flash)
      • Models starting with "qwen3.5"/"qwen3.6" (e.g., qwen3.5-plus, qwen3.5-flash, qwen3.6-plus)
      • Models containing "kimi-k2.5"/"kimi-k2.6" (e.g., kimi-k2.6, kimi/kimi-k2.5)
      参数:
      modelName - the model name
      返回:
      true if the model is a multimodal model
    • requiresMultimodalApi

      public boolean requiresMultimodalApi(String modelName)
      Check if a model requires the multimodal API.

      This is a convenience method that uses EndpointType.AUTO for automatic detection.

      参数:
      modelName - the model name
      返回:
      true if the model requires multimodal API
    • requiresMultimodalApi

      public boolean requiresMultimodalApi(String modelName, EndpointType endpointType)
      Check if a model requires the multimodal API based on the specified endpoint type.
      参数:
      modelName - the model name
      endpointType - the endpoint type to use
      返回:
      true if the model requires multimodal API
    • fetchPublicKey

      public static DashScopeHttpClient.PublicKeyResult fetchPublicKey(String apiKey, String baseUrl, HttpTransport transport)
      Fetch the latest RSA public key from DashScope API.

      This method calls the DashScope public keys API to retrieve the latest public key and key ID for encryption purposes.

      参数:
      apiKey - the DashScope API key
      baseUrl - the base URL (null for default)
      transport - the HTTP transport to use
      返回:
      PublicKeyResult containing the public key ID and public key
      抛出:
      DashScopeHttpClient.DashScopeHttpException - if the request fails
    • getBaseUrl

      public String getBaseUrl()
      Get the base URL.
      返回:
      the base URL
    • builder

      public static DashScopeHttpClient.Builder builder()
      Create a new builder for DashScopeHttpClient.
      返回:
      a new Builder instance