-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
feat: 新增 Xiaomi 和 Xiaomi Token Plan LLM 提供商 #7744
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HosisoraLing
wants to merge
2
commits into
AstrBotDevs:master
Choose a base branch
from
HosisoraLing:feat/xiaomi-provider
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| from astrbot import logger | ||
| from astrbot.core.provider.sources.openai_source import ProviderOpenAIOfficial | ||
|
|
||
| from ..register import register_provider_adapter | ||
|
|
||
| XIAOMI_MODELS = [ | ||
| "mimo-v2.5-pro", | ||
| "mimo-v2.5", | ||
| "mimo-v2-pro", | ||
| "mimo-v2-omni", | ||
| "mimo-v2-flash", | ||
| ] | ||
|
|
||
|
|
||
| @register_provider_adapter( | ||
| "xiaomi_chat_completion", | ||
| "Xiaomi API 提供商适配器 (OpenAI 兼容)", | ||
| default_config_tmpl={ | ||
| "id": "xiaomi", | ||
| "provider": "xiaomi", | ||
| "type": "xiaomi_chat_completion", | ||
| "provider_type": "chat_completion", | ||
| "enable": True, | ||
| "key": [], | ||
| "api_base": "https://api.xiaomimimo.com/v1", | ||
| "timeout": 120, | ||
| "proxy": "", | ||
| "custom_headers": {}, | ||
| "custom_extra_body": {"temperature": 1, "top_p": 0.95}, | ||
| }, | ||
| ) | ||
| class ProviderXiaomi(ProviderOpenAIOfficial): | ||
| """Xiaomi provider using OpenAI-compatible API. | ||
|
|
||
| Supports both standard API and multimodal capabilities. | ||
| See https://platform.xiaomimimo.com/docs/api/chat/openai-api | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| provider_config, | ||
| provider_settings, | ||
| ) -> None: | ||
| # Ensure api_base is set to Xiaomi endpoint if not provided | ||
| if not provider_config.get("api_base"): | ||
| provider_config["api_base"] = "https://api.xiaomimimo.com/v1" | ||
|
|
||
| super().__init__( | ||
| provider_config, | ||
| provider_settings, | ||
| ) | ||
|
|
||
| configured_model = provider_config.get("model", "mimo-v2.5") | ||
| self.set_model(configured_model) | ||
|
|
||
| logger.debug(f"Xiaomi provider initialized with model: {self.get_model()}") | ||
|
|
||
| async def get_models(self) -> list[str]: | ||
| """Return the list of known Xiaomi models. | ||
|
|
||
| Tries to fetch from API first, falls back to hard-coded list if unavailable. | ||
| """ | ||
| try: | ||
| models = await super().get_models() | ||
| if models: | ||
| return models | ||
| except Exception as e: | ||
| logger.debug(f"Failed to fetch models from Xiaomi API: {e}") | ||
|
|
||
| return XIAOMI_MODELS.copy() |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| from astrbot import logger | ||
| from astrbot.core.provider.sources.anthropic_source import ProviderAnthropic | ||
|
|
||
| from ..register import register_provider_adapter | ||
|
|
||
| XIAOMI_TOKEN_PLAN_MODELS = [ | ||
| "mimo-v2.5-pro", | ||
| "mimo-v2.5", | ||
| "mimo-v2-pro", | ||
| "mimo-v2-omni", | ||
| "mimo-v2-flash", | ||
| ] | ||
|
|
||
|
|
||
| @register_provider_adapter( | ||
| "xiaomi_token_plan", | ||
| "Xiaomi Token Plan 提供商适配器", | ||
| default_config_tmpl={ | ||
| "id": "xiaomi-token-plan", | ||
| "provider": "xiaomi-token-plan", | ||
| "type": "xiaomi_token_plan", | ||
| "provider_type": "chat_completion", | ||
| "enable": True, | ||
| "key": [], | ||
| "api_base": "https://token-plan-cn.xiaomimimo.com/anthropic", | ||
| "timeout": 120, | ||
| "proxy": "", | ||
| "custom_headers": {"User-Agent": "claude-code/0.1.0"}, | ||
| "custom_extra_body": {"temperature": 1, "top_p": 0.95}, | ||
| "anth_thinking_config": {"type": "", "budget": 0, "effort": ""}, | ||
| }, | ||
| ) | ||
| class ProviderXiaomiTokenPlan(ProviderAnthropic): | ||
| """Xiaomi Token Plan provider. | ||
|
|
||
| The Token Plan API uses Anthropic-compatible endpoint with Bearer token auth. | ||
| See https://platform.xiaomimimo.com/docs/tokenplan/quick-access | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| provider_config, | ||
| provider_settings, | ||
| ) -> None: | ||
| # Keep api_base fixed; Token Plan users do not need to configure it. | ||
| provider_config["api_base"] = "https://token-plan-cn.xiaomimimo.com/anthropic" | ||
|
|
||
| # Xiaomi Token Plan requires the Authorization: Bearer <token> header. | ||
| keys = provider_config.get("key", []) | ||
| actual_key = keys[0] if isinstance(keys, list) and keys else keys | ||
| if actual_key: | ||
| provider_config.setdefault("custom_headers", {})["Authorization"] = ( | ||
| f"Bearer {actual_key}" | ||
| ) | ||
|
|
||
| super().__init__( | ||
| provider_config, | ||
| provider_settings, | ||
| ) | ||
|
|
||
| configured_model = provider_config.get("model", "mimo-v2.5") | ||
| if configured_model not in XIAOMI_TOKEN_PLAN_MODELS: | ||
| logger.warning( | ||
| f"Configured model {configured_model!r} is not in the known " | ||
| f"Token Plan model list " | ||
| f"({', '.join(XIAOMI_TOKEN_PLAN_MODELS)}). " | ||
| f"The model may still work if your plan supports it. " | ||
| f"If you encounter errors, please check your plan's " | ||
| f"model availability." | ||
| ) | ||
|
|
||
| self.set_model(configured_model) | ||
|
|
||
| async def get_models(self) -> list[str]: | ||
| """Return the hard-coded known model list because Token Plan cannot fetch it dynamically.""" | ||
| return XIAOMI_TOKEN_PLAN_MODELS.copy() | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Avoid duplication of Xiaomi model lists across providers to reduce drift risk.
The Xiaomi Token Plan and standard Xiaomi provider each maintain their own, nearly identical model lists. To avoid them drifting out of sync when models change, centralize this list (e.g., in a shared module/constant) and reuse it in both providers, or derive one from the other so updates happen in only one place.
Suggested implementation:
To fully avoid duplication and drift, you should also:
astrbot/core/provider/sources/xiaomi_models.pythat defines the single source of truth, for example:XIAOMI_MODELS = ["mimo-v2.5-pro", "mimo-v2.5", "mimo-v2-pro", "mimo-v2-omni", "mimo-v2-flash"]astrbot/core/provider/sources/xiaomi_source.py) to:XIAOMI_MODELSfrom.xiaomi_modelsinstead.XIAOMI_TOKEN_PLAN_MODELSfromXIAOMI_MODELS(e.g., filtering) rather than hardcoding a separate list.