mirror of
https://github.com/Aider-AI/aider.git
synced 2025-06-04 19:55:00 +00:00
refactor: Extract default model selection logic to onboarding module
This commit is contained in:
parent
7d013f35e2
commit
88a02723fa
2 changed files with 52 additions and 23 deletions
|
@ -30,6 +30,7 @@ from aider.history import ChatSummary
|
||||||
from aider.io import InputOutput
|
from aider.io import InputOutput
|
||||||
from aider.llm import litellm # noqa: F401; properly init litellm on launch
|
from aider.llm import litellm # noqa: F401; properly init litellm on launch
|
||||||
from aider.models import ModelSettings
|
from aider.models import ModelSettings
|
||||||
|
from aider.onboarding import select_default_model
|
||||||
from aider.repo import ANY_GIT_ERROR, GitRepo
|
from aider.repo import ANY_GIT_ERROR, GitRepo
|
||||||
from aider.report import report_uncaught_exceptions
|
from aider.report import report_uncaught_exceptions
|
||||||
from aider.versioncheck import check_version, install_from_main_branch, install_upgrade
|
from aider.versioncheck import check_version, install_from_main_branch, install_upgrade
|
||||||
|
@ -754,29 +755,11 @@ def main(argv=None, input=None, output=None, force_git_root=None, return_coder=F
|
||||||
alias, model = parts
|
alias, model = parts
|
||||||
models.MODEL_ALIASES[alias.strip()] = model.strip()
|
models.MODEL_ALIASES[alias.strip()] = model.strip()
|
||||||
|
|
||||||
if not args.model:
|
selected_model_name = select_default_model(args, io, analytics)
|
||||||
# Select model based on available API keys
|
if not selected_model_name:
|
||||||
model_key_pairs = [
|
# Error message and analytics event are handled within select_default_model
|
||||||
("ANTHROPIC_API_KEY", "sonnet"),
|
return 1
|
||||||
("DEEPSEEK_API_KEY", "deepseek"),
|
args.model = selected_model_name # Update args with the selected model
|
||||||
("OPENROUTER_API_KEY", "openrouter/anthropic/claude-3.7-sonnet"),
|
|
||||||
("OPENAI_API_KEY", "gpt-4o"),
|
|
||||||
("GEMINI_API_KEY", "gemini/gemini-2.5-pro-exp-03-25"),
|
|
||||||
("VERTEXAI_PROJECT", "vertex_ai/gemini-2.5-pro-exp-03-25"),
|
|
||||||
]
|
|
||||||
|
|
||||||
for env_key, model_name in model_key_pairs:
|
|
||||||
if os.environ.get(env_key):
|
|
||||||
args.model = model_name
|
|
||||||
io.tool_warning(f"Using {model_name} model with {env_key} environment variable.")
|
|
||||||
# Track which API key was used for auto-selection
|
|
||||||
analytics.event("auto_model_selection", api_key=env_key)
|
|
||||||
break
|
|
||||||
if not args.model:
|
|
||||||
io.tool_error("You need to specify a --model and an --api-key to use.")
|
|
||||||
io.offer_url(urls.models_and_keys, "Open documentation url for more info?")
|
|
||||||
analytics.event("auto_model_selection", api_key=None)
|
|
||||||
return 1
|
|
||||||
|
|
||||||
main_model = models.Model(
|
main_model = models.Model(
|
||||||
args.model,
|
args.model,
|
||||||
|
|
46
aider/onboarding.py
Normal file
46
aider/onboarding.py
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
import os
|
||||||
|
|
||||||
|
from aider import urls
|
||||||
|
|
||||||
|
|
||||||
|
def select_default_model(args, io, analytics):
|
||||||
|
"""
|
||||||
|
Selects a default model based on available API keys if no model is specified.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
args: The command line arguments object.
|
||||||
|
io: The InputOutput object for user interaction.
|
||||||
|
analytics: The Analytics object for tracking events.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The name of the selected model, or None if no suitable default is found.
|
||||||
|
"""
|
||||||
|
if args.model:
|
||||||
|
return args.model # Model already specified
|
||||||
|
|
||||||
|
# Select model based on available API keys
|
||||||
|
model_key_pairs = [
|
||||||
|
("ANTHROPIC_API_KEY", "sonnet"),
|
||||||
|
("DEEPSEEK_API_KEY", "deepseek"),
|
||||||
|
("OPENROUTER_API_KEY", "openrouter/anthropic/claude-3.7-sonnet"),
|
||||||
|
("OPENAI_API_KEY", "gpt-4o"),
|
||||||
|
("GEMINI_API_KEY", "gemini/gemini-2.5-pro-exp-03-25"),
|
||||||
|
("VERTEXAI_PROJECT", "vertex_ai/gemini-2.5-pro-exp-03-25"),
|
||||||
|
]
|
||||||
|
|
||||||
|
selected_model = None
|
||||||
|
for env_key, model_name in model_key_pairs:
|
||||||
|
if os.environ.get(env_key):
|
||||||
|
selected_model = model_name
|
||||||
|
io.tool_warning(f"Using {model_name} model with {env_key} environment variable.")
|
||||||
|
# Track which API key was used for auto-selection
|
||||||
|
analytics.event("auto_model_selection", api_key=env_key)
|
||||||
|
break
|
||||||
|
|
||||||
|
if not selected_model:
|
||||||
|
io.tool_error("You need to specify a --model and an --api-key to use.")
|
||||||
|
io.offer_url(urls.models_and_keys, "Open documentation url for more info?")
|
||||||
|
analytics.event("auto_model_selection", api_key=None)
|
||||||
|
return None
|
||||||
|
|
||||||
|
return selected_model
|
Loading…
Add table
Add a link
Reference in a new issue