diff --git a/backend/src/models/__init__.py b/backend/src/models/__init__.py new file mode 100644 index 0000000..88db4b7 --- /dev/null +++ b/backend/src/models/__init__.py @@ -0,0 +1,3 @@ +from .factory import create_chat_model + +__all__ = ["create_chat_model"] diff --git a/backend/src/models/factory.py b/backend/src/models/factory.py new file mode 100644 index 0000000..879ca0a --- /dev/null +++ b/backend/src/models/factory.py @@ -0,0 +1,43 @@ +from langchain.chat_models import BaseChatModel + +from src.config import get_app_config +from src.reflection import resolve_class + + +def create_chat_model( + name: str | None = None, thinking_enabled: bool = False, **kwargs +) -> BaseChatModel: + """Create a chat model instance from the config. + + Args: + name: The name of the model to create. If None, the first model in the config will be used. + + Returns: + A chat model instance. + """ + config = get_app_config() + if name is None: + name = config.models[0].name + model_config = config.get_model_config(name) + if model_config is None: + raise ValueError(f"Model {name} not found in config") from None + model_class = resolve_class(model_config.use, BaseChatModel) + model_settings_from_config = model_config.model_dump( + exclude_none=True, + exclude={ + "use", + "name", + "display_name", + "description", + "supports_thinking", + "when_thinking_enabled", + }, + ) + if thinking_enabled and model_config.when_thinking_enabled is not None: + if not model_config.supports_thinking: + raise ValueError( + f"Model {name} does not support thinking. Set `supports_thinking` to true in the `config.yaml` to enable thinking." + ) from None + model_settings_from_config.update(model_config.when_thinking_enabled) + model_instance = model_class(**kwargs, **model_settings_from_config) + return model_instance