feat(models): add Kimi (Moonshot AI) adaptation with dedicated provider and formatters - #2320
Conversation
- Introduced KimiFormatter for adapting requests to the Kimi Chat Completions API. - Updated OpenAIChatModel documentation to include Kimi model integration. - Added KimiModelProvider to the service provider configuration. - Enhanced environment variable handling to support MOONSHOT_API_KEY for Kimi models. - Updated relevant documentation in both English and Chinese to reflect new Kimi model capabilities.
…rovider - Updated the supports() method to ensure model IDs with only whitespace are not considered valid. - Added a test to verify that model names are trimmed correctly during creation. - Improved error messages for missing API keys to clarify requirements for model creation.
There was a problem hiding this comment.
Pull request overview
This PR adds first-class support for Kimi (Moonshot AI) on top of the existing OpenAI-compatible model extension by introducing Kimi-specific formatters and a dedicated ModelProvider (SPI-registered), along with docs and unit tests.
Changes:
- Add
KimiFormatter/KimiMultiAgentFormatterto apply Kimi model-series-specific request sanitization and tool-choice handling. - Add
KimiModelProviderto resolvekimi:<model>IDs viaModelRegistryand create preconfiguredOpenAIChatModelinstances (Kimi base URL, env-var API key fallback, structured output defaults). - Add docs (EN/ZH) and comprehensive unit tests for formatter behavior and provider resolution.
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| docs/v2/zh/integration/model/openai.md | Document Kimi usage (kimi:<model>) and Kimi-specific request adaptations (ZH). |
| docs/v2/zh/docs/building-blocks/model.md | Add MOONSHOT_API_KEY to environment variable list (ZH). |
| docs/v2/en/integration/model/openai.md | Document Kimi usage (kimi:<model>) and Kimi-specific request adaptations (EN). |
| docs/v2/en/docs/building-blocks/model.md | Add MOONSHOT_API_KEY to environment variable list (EN). |
| agentscope-extensions/.../KimiMultiAgentFormatterTest.java | Unit tests for multi-agent formatting + shared Kimi sanitization behavior. |
| agentscope-extensions/.../KimiModelProviderTest.java | Unit tests for provider ID/supports/create defaults and ServiceLoader resolution. |
| agentscope-extensions/.../KimiFormatterTest.java | Unit tests for Kimi request sanitization, tool_choice matrix, and preserved reasoning content. |
| agentscope-extensions/.../META-INF/services/io.agentscope.core.model.spi.ModelProvider | Register KimiModelProvider for SPI discovery. |
| agentscope-extensions/.../OpenAIChatModel.java | Extend Javadoc to mention KimiFormatter as a provider-specific formatter. |
| agentscope-extensions/.../KimiMultiAgentFormatter.java | New multi-agent Kimi formatter that reuses shared sanitization/tool-choice logic. |
| agentscope-extensions/.../KimiModelProvider.java | New ModelProvider for kimi:<model> IDs with Kimi defaults and option overrides. |
| agentscope-extensions/.../KimiFormatter.java | New Kimi-aware formatter implementing model-series-specific request sanitization and tool-choice behavior. |
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
…rove test coverage - Restrict isAlwaysThinkingModel to the documented kimi-k2.7-code series - Add provider tests for missing API key, option validation and component overrides
| * <li>Only {@code max_tokens} is documented; {@code max_completion_tokens} is mapped to | ||
| * {@code max_tokens} when the latter is not set. Reasoning tokens count towards | ||
| * {@code max_tokens}, so the official guide recommends {@code max_tokens >= 16000} for | ||
| * thinking models</li> |
There was a problem hiding this comment.
这里是不是写反了,官方推荐用 max_completion_tokens,max_tokens 已经废弃了:
https://platform.kimi.com/docs/api/chat#body-one-of-0-max-tokens
(我只是想确认一下,因为kimi官方文档很多地方不一致。确定的话,我本地直接修复。)
There was a problem hiding this comment.
对的,确认过 API reference 的 schema 了,是写反了,感谢~🙂
There was a problem hiding this comment.
对的,确认过 API reference 的 schema 了,是写反了,感谢~🙂
OK
AgentScope-Java Version
2.0.1-SNAPSHOT
Description
Fixes #2213
New implementations (
io.agentscope.extensions.model.openai.compat.kimi)This PR adds first-class Kimi (Moonshot AI) support on top of the OpenAI-compatible extension module, adapted to the latest official Kimi open platform documentation (kimi-k3 / kimi-k2.7-code / kimi-k2.6 / kimi-k2.5 / moonshot-v1 series).
KimiFormatter— extendsOpenAIChatFormatterwith Kimi-specific request adaptations. Unlike other vendors, Kimi's constraints differ per model series, so the sanitization is model-aware:temperature/top_p/frequency_penalty/presence_penaltyare fixed by the platform onkimi-*models (the API rejects other values) and are stripped from requests; they are passed through for themoonshot-v1series, which still accepts them.reasoning_effort(low/high/max) is only supported bykimi-k3and stripped on other models;thinking_budgetis not a Kimi parameter and always stripped (K2.x thinking is controlled via thethinkingbody param instead).max_tokensis documented:max_completion_tokens(newer OpenAI style) is mapped onto it.tool_choicematrix per the official guide:auto/nonepass through everywhere;requiredis kimi-k3-only and 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 passed through otherwise.strictparameter in tool definitions is not sent (not documented by Kimi).reasoning_contentis preserved in the request history, satisfying Preserved Thinking onkimi-k3/kimi-k2.7-codeandthinking.keep = "all"onkimi-k2.6.KimiMultiAgentFormatter— extendsOpenAIMultiAgentFormatter, reusing the same Kimi-specific sanitization and tool-choice handling.KimiModelProvider— resolveskimi:<model>ids throughModelRegistry(Java SPI). CreatesOpenAIChatModelinstances preconfigured with:https://api.moonshot.cn/v1(consistent withKimiCredential.DEFAULT_BASE_URL);ModelCreationContext, thenMOONSHOT_API_KEY, thenKIMI_API_KEY;KimiFormatteras the default formatter (a customFormattercomponent takes precedence);nativeStructuredOutputdisabled by default (Kimiresponse_formatonly supportsjson_object, agents fall back to thegenerate_responsetool) andnativeStructuredOutputWithToolsdisabled by default (Kimi prioritisesresponse_formatover tool invocations); both re-enablable via context options.Other changes
KimiModelProviderinMETA-INF/services/io.agentscope.core.model.spi.ModelProvider.OpenAIChatModeljavadoc now listsKimiFormatteramong provider-specific formatters.Tests
KimiFormatterTest(27 cases): model classification helpers, fixed-sampling-param stripping vs moonshot-v1 pass-through,reasoning_efforthandling,max_completion_tokensmapping,thinkingbody-param pass-through, the fulltool_choicedegradation matrix, andreasoning_contentpreservation in formatted history.KimiMultiAgentFormatterTest(8 cases): multi-agent merging plus shared sanitization behavior.KimiModelProviderTest(8 cases): provider id / supports / creation withModelCreationContext, structured-output defaults and overrides, ServiceLoader resolution viaModelRegistry.mvn spotless:checkpasses; module tests (422) and the full-repositorymvn testall pass.Documentation
docs/v2/{zh,en}/integration/model/openai.md: new "Kimi (Moonshot AI)" section withkimi:<model>and explicit-builder examples, the full list of request adaptations, and thinking-related parameter usage (thinkingbody param for K2.x,reasoning_effortfor kimi-k3, Preserved Thinking notes).docs/v2/{zh,en}/docs/building-blocks/model.md: environment variable list now includesMOONSHOT_API_KEY.References
Checklist
mvn spotless:applymvn test)