KimiFormatter
public class KimiFormatter extends OpenAIChatFormatter
io/agentscope/extensions/model/openai/compat/kimi/KimiFormatter.java:82–317Kimi 格式化器,与模型服务通信并返回生成或向量化结果。位于 io.agentscope.extensions.model.openai.compat.kimi 包,继承自 OpenAIChatFormatter。核心成员包括 supportsStrict、applyOptions、applyToolChoice、sanitizeKimiRequest、applyKimiToolChoice、KimiFormatter。
Formatter for Kimi (Moonshot AI) models (kimi-k3, kimi-k2.7-code, kimi-k2.6, kimi-k2.5, moonshot-v1 series).
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 models -
thinking_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();
`
构造器摘要Constructor Summary
| 构造器Constructor | 说明Description |
|---|---|
public KimiFormatter() | 初始化 `KimiFormatter` 实例。 Creates a new instance. |
方法摘要Method Summary
| 方法Method | 说明Description |
|---|---|
protected boolean supportsStrict() | 返回 `supportsStrict` 结果。 Returns the supports strict. |
public void applyOptions(OpenAIRequest request, GenerateOptions options, GenerateOptions defaultOptions) | 应用选项。 Performs the apply options operation. |
public void applyToolChoice(OpenAIRequest request, ToolChoice toolChoice) | 应用工具选项。 Performs the apply tool choice operation. |
protected static void sanitizeKimiRequest(OpenAIRequest request)static | 设置该参数。 Sanitize the request according to the Kimi Chat Completions API reference. |
protected static void applyKimiToolChoice(OpenAIRequest request, ToolChoice toolChoice)static | 应用 `applyKimiToolChoice`。 Apply Kimi-specific tool choice handling. |
构造器详情Constructor Detail
KimiFormatter
public KimiFormatter()
初始化 KimiFormatter 实例。
Creates a new instance.
io/agentscope/extensions/model/openai/compat/kimi/KimiFormatter.java:86–88方法详情Method Detail
supportsStrict
protected boolean supportsStrict()
返回 supportsStrict 结果。
Returns the supports strict.
booleanio/agentscope/extensions/model/openai/compat/kimi/KimiFormatter.java:90–93applyOptions
public void applyOptions(OpenAIRequest request, GenerateOptions options, GenerateOptions defaultOptions)
应用选项。
Performs the apply options operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
request | OpenAIRequest | 请求(类型 `OpenAIRequest`)。The request. |
options | GenerateOptions | 选项(类型 `GenerateOptions`)。The options. |
defaultOptions | GenerateOptions | 默认选项(类型 `GenerateOptions`)。The default options. |
io/agentscope/extensions/model/openai/compat/kimi/KimiFormatter.java:95–100applyToolChoice
public void applyToolChoice(OpenAIRequest request, ToolChoice toolChoice)
应用工具选项。
Performs the apply tool choice operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
request | OpenAIRequest | 请求(类型 `OpenAIRequest`)。The request. |
toolChoice | ToolChoice | 工具选项(类型 `ToolChoice`)。The tool choice. |
io/agentscope/extensions/model/openai/compat/kimi/KimiFormatter.java:102–105sanitizeKimiRequest
protected static void sanitizeKimiRequest(OpenAIRequest request)
设置该参数。
Sanitize the request according to the Kimi Chat Completions API reference.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
request | OpenAIRequest | 请求(类型 `OpenAIRequest`)。the request to sanitize |
io/agentscope/extensions/model/openai/compat/kimi/KimiFormatter.java:181–236applyKimiToolChoice
protected static void applyKimiToolChoice(OpenAIRequest request, ToolChoice toolChoice)
应用 applyKimiToolChoice。
Apply Kimi-specific tool choice handling.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
request | OpenAIRequest | 请求(类型 `OpenAIRequest`)。the request to apply tool choice to |
toolChoice | ToolChoice | 工具选项(类型 `ToolChoice`)。the requested tool choice |
io/agentscope/extensions/model/openai/compat/kimi/KimiFormatter.java:257–302