接口 Formatter<TReq,TResp,TParams>

类型参数:
TReq - Provider-specific request message type (e.g., com.alibaba.dashscope.common.Message for DashScope, or ChatCompletionMessageParam for OpenAI)
TResp - Provider-specific response type (e.g., GenerationResult for DashScope, or ChatCompletion/ChatCompletionChunk for OpenAI)
TParams - Provider-specific request parameters builder type (e.g., GenerationParam for DashScope, or ChatCompletionCreateParams.Builder for OpenAI)
所有已知实现类:
AbstractBaseFormatter, AnthropicBaseFormatter, AnthropicChatFormatter, AnthropicMultiAgentFormatter, DashScopeChatFormatter, DashScopeMultiAgentFormatter, DeepSeekFormatter, DeepSeekMultiAgentFormatter, GeminiChatFormatter, GeminiMultiAgentFormatter, GLMFormatter, GLMMultiAgentFormatter, OllamaChatFormatter, OllamaMultiAgentFormatter, OpenAIBaseFormatter, OpenAIChatFormatter, OpenAIMultiAgentFormatter

public interface Formatter<TReq,TResp,TParams>
Formatter interface for converting between AgentScope and provider-specific formats. This is an internal interface used by Model implementations.

Formatters are responsible for: 1. Converting Msg objects to provider-specific request format 2. Converting provider-specific responses back to AgentScope ChatResponse 3. Applying generation options to provider-specific request builders 4. Applying tool schemas to provider-specific request builders

Each formatter is type-safe and handles the exact types expected by the provider's SDK.

  • 方法详细资料

    • format

      List<TReq> format(List<Msg> msgs)
      Format AgentScope messages to provider-specific request format.
      参数:
      msgs - List of AgentScope messages
      返回:
      List of provider-specific request messages
    • parseResponse

      ChatResponse parseResponse(TResp response, Instant startTime)
      Parse provider-specific response to AgentScope ChatResponse.
      参数:
      response - Provider-specific response object
      startTime - Request start time for calculating duration
      返回:
      AgentScope ChatResponse
    • applyOptions

      void applyOptions(TParams paramsBuilder, GenerateOptions options, GenerateOptions defaultOptions)
      Apply generation options to provider-specific request parameters.
      参数:
      paramsBuilder - Provider-specific request parameters builder
      options - Generation options to apply
      defaultOptions - Default options to use if options parameter is null
    • applyTools

      void applyTools(TParams paramsBuilder, List<ToolSchema> tools)
      Apply tool schemas to provider-specific request parameters.
      参数:
      paramsBuilder - Provider-specific request parameters builder
      tools - List of tool schemas to apply (may be null or empty)
    • applyTools

      default void applyTools(TParams paramsBuilder, List<ToolSchema> tools, String baseUrl, String modelName)
      Apply tool schemas to provider-specific request parameters with provider compatibility handling.

      This method allows formatters to detect the provider from baseUrl/modelName and adjust tool definitions for compatibility (e.g., removing unsupported parameters like 'strict').

      The default implementation delegates to applyTools(TParams, List). Formatters that support provider-specific tool handling should override this method.

      参数:
      paramsBuilder - Provider-specific request parameters builder
      tools - Tool schemas to apply (may be null or empty)
      baseUrl - API base URL for provider detection (null for default)
      modelName - Model name for provider detection fallback (null)
    • applyToolChoice

      default void applyToolChoice(TParams paramsBuilder, ToolChoice toolChoice)
      Apply tool choice configuration to provider-specific request parameters.

      This method controls how the model uses tools. The default implementation does nothing, allowing formatters to override for providers that support tool choice configuration.

      参数:
      paramsBuilder - Provider-specific request parameters builder
      toolChoice - Tool choice configuration (null means provider default)
    • applyToolChoice

      default void applyToolChoice(TParams paramsBuilder, ToolChoice toolChoice, String baseUrl, String modelName)
      Apply tool choice configuration to provider-specific request parameters with provider detection.

      This method allows formatters to detect the provider from baseUrl/modelName and adjust tool_choice format or gracefully degrade when the provider doesn't support certain options.

      The default implementation delegates to applyToolChoice(TParams, ToolChoice). Formatters that support provider-specific tool_choice handling should override this method.

      参数:
      paramsBuilder - Provider-specific request parameters builder
      toolChoice - Tool choice configuration (null means provider default)
      baseUrl - API base URL for provider detection (null for default)
      modelName - Model name for provider detection fallback (null)