feat(models): add Kimi (Moonshot AI) adaptation with dedicated provider and formatters#2320
Open
Leeaoyin wants to merge 2 commits into
Open
feat(models): add Kimi (Moonshot AI) adaptation with dedicated provider and formatters#2320Leeaoyin wants to merge 2 commits into
Leeaoyin wants to merge 2 commits into
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.
Contributor
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! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
AgentScope-Java Version
2.0.1-SNAPSHOT
Description
Fiexs #2213
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).
New implementations (
io.agentscope.extensions.model.openai.compat.kimi)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)