Improved auto mode model discovery when using OpenRouter
This commit is contained in:
20
config.py
20
config.py
@@ -24,26 +24,6 @@ __author__ = "Fahad Gilani" # Primary maintainer
|
|||||||
# Special value "auto" means Claude should pick the best model for each task
|
# Special value "auto" means Claude should pick the best model for each task
|
||||||
DEFAULT_MODEL = os.getenv("DEFAULT_MODEL", "auto")
|
DEFAULT_MODEL = os.getenv("DEFAULT_MODEL", "auto")
|
||||||
|
|
||||||
# Validate DEFAULT_MODEL and set to "auto" if invalid
|
|
||||||
# Only include actually supported models from providers
|
|
||||||
VALID_MODELS = [
|
|
||||||
"auto",
|
|
||||||
"flash",
|
|
||||||
"pro",
|
|
||||||
"o3",
|
|
||||||
"o3-mini",
|
|
||||||
"gemini-2.5-flash-preview-05-20",
|
|
||||||
"gemini-2.5-pro-preview-06-05",
|
|
||||||
]
|
|
||||||
if DEFAULT_MODEL not in VALID_MODELS:
|
|
||||||
import logging
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
logger.warning(
|
|
||||||
f"Invalid DEFAULT_MODEL '{DEFAULT_MODEL}'. Setting to 'auto'. Valid options: {', '.join(VALID_MODELS)}"
|
|
||||||
)
|
|
||||||
DEFAULT_MODEL = "auto"
|
|
||||||
|
|
||||||
# Auto mode detection - when DEFAULT_MODEL is "auto", Claude picks the model
|
# Auto mode detection - when DEFAULT_MODEL is "auto", Claude picks the model
|
||||||
IS_AUTO_MODE = DEFAULT_MODEL.lower() == "auto"
|
IS_AUTO_MODE = DEFAULT_MODEL.lower() == "auto"
|
||||||
|
|
||||||
|
|||||||
@@ -181,29 +181,58 @@ class BaseTool(ABC):
|
|||||||
model_desc_parts.append(f"- '{model}': {desc}")
|
model_desc_parts.append(f"- '{model}': {desc}")
|
||||||
|
|
||||||
if has_openrouter:
|
if has_openrouter:
|
||||||
# Add OpenRouter aliases from the registry
|
# Add OpenRouter models with descriptions
|
||||||
try:
|
try:
|
||||||
# Import registry directly to show available aliases
|
|
||||||
# This works even without an API key
|
|
||||||
from providers.openrouter_registry import OpenRouterModelRegistry
|
from providers.openrouter_registry import OpenRouterModelRegistry
|
||||||
|
import logging
|
||||||
|
|
||||||
registry = OpenRouterModelRegistry()
|
registry = OpenRouterModelRegistry()
|
||||||
aliases = registry.list_aliases()
|
|
||||||
|
|
||||||
# Show ALL aliases from the configuration
|
# Group models by their model_name to avoid duplicates
|
||||||
if aliases:
|
seen_models = set()
|
||||||
# Show all aliases so Claude knows every option available
|
model_configs = []
|
||||||
all_aliases = sorted(aliases)
|
|
||||||
alias_list = ", ".join(f"'{a}'" for a in all_aliases)
|
for alias in registry.list_aliases():
|
||||||
model_desc_parts.append(f"\nOpenRouter models available via aliases: {alias_list}")
|
config = registry.resolve(alias)
|
||||||
else:
|
if config and config.model_name not in seen_models:
|
||||||
model_desc_parts.append(
|
seen_models.add(config.model_name)
|
||||||
"\nOpenRouter models: If configured, you can also use ANY model available on OpenRouter."
|
model_configs.append((alias, config))
|
||||||
)
|
|
||||||
except Exception:
|
# Sort by context window (descending) then by alias
|
||||||
# Fallback if registry fails to load
|
model_configs.sort(key=lambda x: (-x[1].context_window, x[0]))
|
||||||
|
|
||||||
|
if model_configs:
|
||||||
|
model_desc_parts.append("\nOpenRouter models (use these aliases):")
|
||||||
|
for alias, config in model_configs[:10]: # Limit to top 10
|
||||||
|
# Format context window in human-readable form
|
||||||
|
context_tokens = config.context_window
|
||||||
|
if context_tokens >= 1_000_000:
|
||||||
|
context_str = f"{context_tokens // 1_000_000}M"
|
||||||
|
elif context_tokens >= 1_000:
|
||||||
|
context_str = f"{context_tokens // 1_000}K"
|
||||||
|
else:
|
||||||
|
context_str = str(context_tokens)
|
||||||
|
|
||||||
|
# Build description line
|
||||||
|
if config.description:
|
||||||
|
desc = f"- '{alias}' ({context_str} context): {config.description}"
|
||||||
|
else:
|
||||||
|
# Fallback to showing the model name if no description
|
||||||
|
desc = f"- '{alias}' ({context_str} context): {config.model_name}"
|
||||||
|
model_desc_parts.append(desc)
|
||||||
|
|
||||||
|
# Add note about additional models if any were cut off
|
||||||
|
total_models = len(model_configs)
|
||||||
|
if total_models > 10:
|
||||||
|
model_desc_parts.append(f"... and {total_models - 10} more models available")
|
||||||
|
except Exception as e:
|
||||||
|
# Log for debugging but don't fail
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logging.debug(f"Failed to load OpenRouter model descriptions: {e}")
|
||||||
|
# Fallback to simple message
|
||||||
model_desc_parts.append(
|
model_desc_parts.append(
|
||||||
"\nOpenRouter models: If configured, you can also use ANY model available on OpenRouter (e.g., 'gpt-4', 'claude-3-opus', 'mistral-large')."
|
"\nOpenRouter models: If configured, you can also use ANY model available on OpenRouter."
|
||||||
)
|
)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
Reference in New Issue
Block a user