Skip to content

Model Utilities

Convenience functions for selecting and configuring models.

minisweagent.models.get_model

get_model(
    input_model_name: str | None = None,
    config: dict | None = None,
) -> Model

Get an initialized model object from any kind of user input or settings.

Source code in src/minisweagent/models/__init__.py
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def get_model(input_model_name: str | None = None, config: dict | None = None) -> Model:
    """Get an initialized model object from any kind of user input or settings."""
    resolved_model_name = get_model_name(input_model_name, config)
    if config is None:
        config = {}
    config = copy.deepcopy(config)
    config["model_name"] = resolved_model_name

    model_class = get_model_class(resolved_model_name, config.pop("model_class", ""))

    if (from_env := os.getenv("MSWEA_MODEL_API_KEY")) and not str(type(model_class)).endswith("DeterministicModel"):
        config.setdefault("model_kwargs", {})["api_key"] = from_env

    return model_class(**config)

minisweagent.models.get_model_name

get_model_name(
    input_model_name: str | None = None,
    config: dict | None = None,
) -> str

Get a model name from any kind of user input or settings.

Source code in src/minisweagent/models/__init__.py
61
62
63
64
65
66
67
68
69
70
71
def get_model_name(input_model_name: str | None = None, config: dict | None = None) -> str:
    """Get a model name from any kind of user input or settings."""
    if config is None:
        config = {}
    if input_model_name:
        return input_model_name
    if from_config := config.get("model_name"):
        return from_config
    if from_env := os.getenv("MSWEA_MODEL_NAME"):
        return from_env
    raise ValueError("No default model set. Please run `mini-extra config setup` to set one.")

minisweagent.models.get_model_class

get_model_class(
    model_name: str, model_class: str = ""
) -> type

Select the best model class.

If a model_class is provided (as shortcut name, or as full import path, e.g., "anthropic" or "minisweagent.models.anthropic.AnthropicModel"), it takes precedence over the model_name. Otherwise, the model_name is used to select the best model class.

Source code in src/minisweagent/models/__init__.py
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
def get_model_class(model_name: str, model_class: str = "") -> type:
    """Select the best model class.

    If a model_class is provided (as shortcut name, or as full import path,
    e.g., "anthropic" or "minisweagent.models.anthropic.AnthropicModel"),
    it takes precedence over the `model_name`.
    Otherwise, the model_name is used to select the best model class.
    """
    if model_class:
        full_path = _MODEL_CLASS_MAPPING.get(model_class, model_class)
        try:
            module_name, class_name = full_path.rsplit(".", 1)
            module = importlib.import_module(module_name)
            return getattr(module, class_name)
        except (ValueError, ImportError, AttributeError):
            msg = f"Unknown model class: {model_class} (resolved to {full_path}, available: {_MODEL_CLASS_MAPPING})"
            raise ValueError(msg)

    if any(s in model_name.lower() for s in ["anthropic", "sonnet", "opus", "claude"]):
        from minisweagent.models.anthropic import AnthropicModel

        return AnthropicModel

    # Default to LitellmModel
    from minisweagent.models.litellm_model import LitellmModel

    return LitellmModel