-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
fix: filter stale fallback chat models #8480
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
Clhikari
wants to merge
1
commit into
AstrBotDevs:master
Choose a base branch
from
Clhikari:fix/provider-fallback-filter
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from astrbot.core import logger | ||
|
|
||
|
|
||
| def _get_provider_source_types(config: dict) -> dict[str, str]: | ||
| provider_source_types: dict[str, str] = {} | ||
| for provider_source in config.get("provider_sources", []): | ||
| if not isinstance(provider_source, dict): | ||
| continue | ||
| provider_source_id = provider_source.get("id") | ||
| if not isinstance(provider_source_id, str) or not provider_source_id: | ||
| continue | ||
| provider_source_types[provider_source_id] = ( | ||
| provider_source.get("provider_type") or "chat_completion" | ||
| ) | ||
| return provider_source_types | ||
|
|
||
|
|
||
| def get_enabled_chat_provider_ids(config: dict) -> set[str]: | ||
| """Return provider IDs that can be used as chat fallback providers.""" | ||
| provider_ids: set[str] = set() | ||
| provider_source_types = _get_provider_source_types(config) | ||
| for provider in config.get("provider", []): | ||
| if not isinstance(provider, dict): | ||
| continue | ||
| provider_id = provider.get("id") | ||
| if not isinstance(provider_id, str) or not provider_id: | ||
| continue | ||
| if provider.get("enable") is False: | ||
| continue | ||
| provider_type = provider.get("provider_type") | ||
| provider_source_id = provider.get("provider_source_id") | ||
| if not provider_type: | ||
| if provider_source_id: | ||
| if not isinstance(provider_source_id, str): | ||
| continue | ||
| provider_type = provider_source_types.get(provider_source_id) | ||
| else: | ||
| provider_type = "chat_completion" | ||
| if provider_type != "chat_completion": | ||
| continue | ||
| provider_ids.add(provider_id) | ||
| return provider_ids | ||
|
|
||
|
|
||
| def prune_fallback_chat_models(config: dict) -> list[str]: | ||
| """Drop stale or disabled provider IDs from provider_settings.fallback_chat_models.""" | ||
| provider_settings = config.get("provider_settings") | ||
| if not isinstance(provider_settings, dict): | ||
| return [] | ||
|
|
||
| fallback_ids = provider_settings.get("fallback_chat_models") | ||
| if not isinstance(fallback_ids, list): | ||
| return [] | ||
|
|
||
| valid_provider_ids = get_enabled_chat_provider_ids(config) | ||
| seen: set[str] = set() | ||
| pruned_fallback_ids: list[str] = [] | ||
| removed_fallback_ids: list[str] = [] | ||
|
|
||
| for fallback_id in fallback_ids: | ||
| if not isinstance(fallback_id, str) or not fallback_id: | ||
| removed_fallback_ids.append(str(fallback_id)) | ||
| continue | ||
| if fallback_id in seen: | ||
| removed_fallback_ids.append(fallback_id) | ||
| continue | ||
| seen.add(fallback_id) | ||
| if fallback_id not in valid_provider_ids: | ||
| removed_fallback_ids.append(fallback_id) | ||
| continue | ||
| pruned_fallback_ids.append(fallback_id) | ||
|
|
||
| if pruned_fallback_ids != fallback_ids: | ||
| provider_settings["fallback_chat_models"] = pruned_fallback_ids | ||
| logger.info( | ||
| "Removed stale fallback chat providers from config: %s", | ||
| removed_fallback_ids, | ||
| ) | ||
|
|
||
| return removed_fallback_ids | ||
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,56 @@ | ||
| from astrbot.dashboard.routes.config import ( | ||
| _provider_config_enabled, | ||
| _provider_config_selectable, | ||
| ) | ||
|
|
||
|
|
||
| def test_provider_config_enabled_excludes_only_explicitly_disabled_provider(): | ||
| assert _provider_config_enabled({"id": "enabled-provider", "enable": True}) | ||
| assert _provider_config_enabled({"id": "legacy-provider-without-enable"}) | ||
| assert not _provider_config_enabled({"id": "disabled-provider", "enable": False}) | ||
|
|
||
|
|
||
| def test_provider_config_selectable_requires_loaded_non_agent_provider(): | ||
| inst_map = {"loaded-provider": object()} | ||
|
|
||
| assert _provider_config_selectable( | ||
| {"id": "loaded-provider", "enable": True}, | ||
| ["chat_completion"], | ||
| inst_map, | ||
| ) | ||
| assert not _provider_config_selectable( | ||
| {"enable": True}, | ||
| ["chat_completion"], | ||
| inst_map, | ||
| ) | ||
| assert not _provider_config_selectable( | ||
| {"id": 123, "enable": True}, | ||
| ["chat_completion"], | ||
| inst_map, | ||
| ) | ||
| assert not _provider_config_selectable( | ||
|
Clhikari marked this conversation as resolved.
|
||
| {"id": "unloaded-provider", "enable": True}, | ||
| ["chat_completion"], | ||
| inst_map, | ||
| ) | ||
| assert not _provider_config_selectable( | ||
| {"id": "disabled-provider", "enable": False}, | ||
| ["chat_completion"], | ||
| inst_map, | ||
| ) | ||
|
|
||
|
|
||
| def test_provider_config_selectable_keeps_agent_runner_configs(): | ||
| assert _provider_config_selectable( | ||
| {"id": "agent-runner", "enable": True, "provider_type": "agent_runner"}, | ||
| ["agent_runner"], | ||
| {}, | ||
| ) | ||
|
Clhikari marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def test_provider_config_selectable_does_not_bypass_loaded_check_for_mixed_types(): | ||
| assert not _provider_config_selectable( | ||
| {"id": "unloaded-provider", "enable": True, "provider_type": "chat_completion"}, | ||
| ["agent_runner", "chat_completion"], | ||
| {}, | ||
| ) | ||
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,118 @@ | ||
| from astrbot.core.provider.fallbacks import prune_fallback_chat_models | ||
|
|
||
|
|
||
| def test_prune_fallback_chat_models_removes_disabled_missing_and_duplicate_ids(): | ||
| config = { | ||
| "provider": [ | ||
| { | ||
| "id": "enabled-chat", | ||
| "provider_type": "chat_completion", | ||
| "enable": True, | ||
| }, | ||
| { | ||
| "id": "disabled-chat", | ||
| "provider_type": "chat_completion", | ||
| "enable": False, | ||
| }, | ||
| { | ||
| "id": "tts-provider", | ||
| "provider_type": "text_to_speech", | ||
| "enable": True, | ||
| }, | ||
| ], | ||
| "provider_settings": { | ||
| "fallback_chat_models": [ | ||
| "enabled-chat", | ||
| "disabled-chat", | ||
| "missing-chat", | ||
| "tts-provider", | ||
| "enabled-chat", | ||
| "", | ||
| ], | ||
| }, | ||
| } | ||
|
|
||
| removed_ids = prune_fallback_chat_models(config) | ||
|
|
||
| assert config["provider_settings"]["fallback_chat_models"] == ["enabled-chat"] | ||
| assert removed_ids == [ | ||
| "disabled-chat", | ||
| "missing-chat", | ||
| "tts-provider", | ||
| "enabled-chat", | ||
| "", | ||
| ] | ||
|
|
||
|
|
||
| def test_prune_fallback_chat_models_ignores_non_list_setting(): | ||
| config = { | ||
| "provider": [{"id": "enabled-chat", "enable": True}], | ||
| "provider_settings": {"fallback_chat_models": "enabled-chat"}, | ||
| } | ||
|
|
||
| removed_ids = prune_fallback_chat_models(config) | ||
|
|
||
| assert config["provider_settings"]["fallback_chat_models"] == "enabled-chat" | ||
| assert removed_ids == [] | ||
|
|
||
|
|
||
| def test_prune_fallback_chat_models_uses_provider_source_type(): | ||
| config = { | ||
| "provider_sources": [ | ||
| {"id": "chat-source", "provider_type": "chat_completion"}, | ||
| {"id": "legacy-chat-source"}, | ||
| {"id": "empty-chat-source", "provider_type": ""}, | ||
| {"id": "embedding-source", "provider_type": "embedding"}, | ||
| ], | ||
| "provider": [ | ||
| { | ||
| "id": "chat-model", | ||
| "provider_source_id": "chat-source", | ||
| "enable": True, | ||
| }, | ||
| { | ||
| "id": "legacy-chat-model", | ||
| "provider_source_id": "legacy-chat-source", | ||
| "enable": True, | ||
| }, | ||
| { | ||
| "id": "empty-chat-model", | ||
| "provider_source_id": "empty-chat-source", | ||
| "enable": True, | ||
| }, | ||
| { | ||
| "id": "inline-legacy-chat", | ||
| "enable": True, | ||
| }, | ||
| { | ||
| "id": "embedding-model", | ||
| "provider_source_id": "embedding-source", | ||
| "enable": True, | ||
| }, | ||
| { | ||
| "id": "missing-source-model", | ||
| "provider_source_id": "missing-source", | ||
| "enable": True, | ||
| }, | ||
| ], | ||
| "provider_settings": { | ||
| "fallback_chat_models": [ | ||
| "chat-model", | ||
| "legacy-chat-model", | ||
| "empty-chat-model", | ||
| "inline-legacy-chat", | ||
| "embedding-model", | ||
| "missing-source-model", | ||
| ], | ||
| }, | ||
| } | ||
|
|
||
| removed_ids = prune_fallback_chat_models(config) | ||
|
|
||
| assert config["provider_settings"]["fallback_chat_models"] == [ | ||
| "chat-model", | ||
| "legacy-chat-model", | ||
| "empty-chat-model", | ||
| "inline-legacy-chat", | ||
| ] | ||
| assert removed_ids == ["embedding-model", "missing-source-model"] |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.