接口 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
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.
-
方法概要
修饰符和类型方法说明voidapplyOptions(TParams paramsBuilder, GenerateOptions options, GenerateOptions defaultOptions) Apply generation options to provider-specific request parameters.default voidapplyToolChoice(TParams paramsBuilder, ToolChoice toolChoice) Apply tool choice configuration to provider-specific request parameters.default voidapplyToolChoice(TParams paramsBuilder, ToolChoice toolChoice, String baseUrl, String modelName) Apply tool choice configuration to provider-specific request parameters with provider detection.voidapplyTools(TParams paramsBuilder, List<ToolSchema> tools) Apply tool schemas to provider-specific request parameters.default voidapplyTools(TParams paramsBuilder, List<ToolSchema> tools, String baseUrl, String modelName) Apply tool schemas to provider-specific request parameters with provider compatibility handling.Format AgentScope messages to provider-specific request format.parseResponse(TResp response, Instant startTime) Parse provider-specific response to AgentScope ChatResponse.
-
方法详细资料
-
format
Format AgentScope messages to provider-specific request format.- 参数:
msgs- List of AgentScope messages- 返回:
- List of provider-specific request messages
-
parseResponse
Parse provider-specific response to AgentScope ChatResponse.- 参数:
response- Provider-specific response objectstartTime- Request start time for calculating duration- 返回:
- AgentScope ChatResponse
-
applyOptions
Apply generation options to provider-specific request parameters.- 参数:
paramsBuilder- Provider-specific request parameters builderoptions- Generation options to applydefaultOptions- Default options to use if options parameter is null
-
applyTools
Apply tool schemas to provider-specific request parameters.- 参数:
paramsBuilder- Provider-specific request parameters buildertools- 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 buildertools- 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
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 buildertoolChoice- 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 buildertoolChoice- 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)
-