Class KimiFormatter
- All Implemented Interfaces:
io.agentscope.core.formatter.Formatter<OpenAIMessage,OpenAIResponse, OpenAIRequest>
Adapted to the latest Kimi open platform Chat Completions API
(https://api.moonshot.cn/v1/chat/completions):
- On
kimi-*models,temperature/top_p/n/frequency_penalty/presence_penaltyare fixed by the platform and the API rejects requests carrying other values; these parameters are stripped from the request. Themoonshot-v1series still accepts them, so they are kept there. reasoning_effort("low"/"high"/"max", default"max") is only supported bykimi-k3; it is stripped for other modelsthinking_budgetis not a Kimi parameter and is always stripped; K2.x thinking is controlled through thethinkingbody parameter instead (see below)- Only
max_completion_tokensis documented;max_tokensis mapped tomax_completion_tokenswhen the latter is not set. Reasoning tokens count towardsmax_completion_tokens, so the official guide recommendsmax_completion_tokens >= 16000for thinking models tool_choicesupportsauto/noneon all models;requiredis degraded toautoon the K2.x series; forcing a specific function is incompatible with thinking enabled (HTTP 400), so it is degraded toautoon always-thinking models (kimi-k3,kimi-k2.7-code) and onkimi-k2.6/kimi-k2.5unless thinking is explicitly disabled- The
strictparameter in tool definitions is not documented by Kimi and is not sent - Assistant
reasoning_contentis preserved in the request history (inherited from the base message converter), as required by Preserved Thinking onkimi-k3/kimi-k2.7-codeand bythinking.keep = "all"onkimi-k2.6
Thinking mode: kimi-k2.6 / kimi-k2.5 accept a thinking body
parameter (e.g. {"type": "disabled"}), which can be passed through
GenerateOptions.additionalBodyParam("thinking", Map.of("type", "disabled")).
kimi-k3 uses the top-level reasoning_effort option
(GenerateOptions.reasoningEffort) instead. JSON mode is enabled via
response_format = {"type": "json_object"}; json_schema is not supported.
Usage:
OpenAIChatModel.builder()
.formatter(new KimiFormatter())
.modelName("kimi-k3")
.baseUrl("https://api.moonshot.cn/v1")
.apiKey(apiKey)
.build();
-
Field Summary
Fields inherited from class io.agentscope.extensions.model.openai.formatter.OpenAIBaseFormatter
messageConverter, responseParser -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected static voidapplyKimiToolChoice(OpenAIRequest request, io.agentscope.core.model.ToolChoice toolChoice) Apply Kimi-specific tool choice handling.voidapplyOptions(OpenAIRequest request, io.agentscope.core.model.GenerateOptions options, io.agentscope.core.model.GenerateOptions defaultOptions) Apply generation options to the request.voidapplyToolChoice(OpenAIRequest request, io.agentscope.core.model.ToolChoice toolChoice) Apply tool choice configuration to the request.protected static voidsanitizeKimiRequest(OpenAIRequest request) Sanitize the request according to the Kimi Chat Completions API reference.protected booleanReturns whether this formatter's target API supports the strict parameter in tool definitions.Methods inherited from class io.agentscope.extensions.model.openai.formatter.OpenAIChatFormatter
applyAdditionalBodyParams, applyMaxTokens, applyTools, convertMessage, doFormat, doFormatMethods inherited from class io.agentscope.extensions.model.openai.formatter.OpenAIBaseFormatter
applyToolChoice, applyTools, buildRequest, buildRequest, parseResponseMethods inherited from class io.agentscope.core.formatter.AbstractBaseFormatter
convertToolResultToString, extractTextContent, extractTextContent, format, format, formatRoleLabel, getOptionOrDefault, hasMediaContent, saveBase64DataToTempFile, shouldBypassHistory
-
Constructor Details
-
KimiFormatter
public KimiFormatter()
-
-
Method Details
-
supportsStrict
protected boolean supportsStrict()Description copied from class:OpenAIChatFormatterReturns whether this formatter's target API supports the strict parameter in tool definitions.Subclasses can override this method to disable strict parameter for providers that don't support it (e.g., DeepSeek).
- Overrides:
supportsStrictin classOpenAIChatFormatter- Returns:
- true if strict parameter is supported, false otherwise
-
applyOptions
public void applyOptions(OpenAIRequest request, io.agentscope.core.model.GenerateOptions options, io.agentscope.core.model.GenerateOptions defaultOptions) Description copied from class:OpenAIBaseFormatterApply generation options to the request. Subclasses implement provider-specific option handling.- Specified by:
applyOptionsin interfaceio.agentscope.core.formatter.Formatter<OpenAIMessage,OpenAIResponse, OpenAIRequest> - Overrides:
applyOptionsin classOpenAIChatFormatter- Parameters:
request- OpenAI request DTOoptions- Generation options to applydefaultOptions- Default options to use if options parameter is null
-
applyToolChoice
Description copied from class:OpenAIBaseFormatterApply tool choice configuration to the request. Subclasses implement provider-specific tool choice handling.- Specified by:
applyToolChoicein interfaceio.agentscope.core.formatter.Formatter<OpenAIMessage,OpenAIResponse, OpenAIRequest> - Overrides:
applyToolChoicein classOpenAIChatFormatter- Parameters:
request- OpenAI request DTOtoolChoice- Tool choice configuration (null means auto)
-
sanitizeKimiRequest
Sanitize the request according to the Kimi Chat Completions API reference.Applied adaptations:
temperature/top_p/n/frequency_penalty/presence_penaltyare removed onkimi-*models (fixed by the platform; other values are rejected)reasoning_effortis removed on models other thankimi-k3thinking_budgetis always removed (not a Kimi parameter)max_tokensis mapped tomax_completion_tokenswhen the latter is not set
This method is static to allow sharing with
KimiMultiAgentFormatter.- Parameters:
request- the request to sanitize
-
applyKimiToolChoice
protected static void applyKimiToolChoice(OpenAIRequest request, io.agentscope.core.model.ToolChoice toolChoice) Apply Kimi-specific tool choice handling.Per the official tool choice guide:
auto/noneare supported on all modelsrequiredis only supported bykimi-k3; it is degraded toautoon the K2.x series, which rejects it- Forcing a specific function (
{"type": "function", ...}) is incompatible with thinking enabled (HTTP 400); it is degraded toautoon always-thinking models and onkimi-k2.6/kimi-k2.5unless thinking is explicitly disabled via thethinkingbody parameter
This method is static to allow sharing with
KimiMultiAgentFormatter.- Parameters:
request- the request to apply tool choice totoolChoice- the requested tool choice
-