# Where to Find Model Wrappers in CreativeMath: The Complete Guide to ModelWrapper

> Locate ModelWrapper code in CreativeMath easily. This guide points you to src/models/model_loader.py, explaining how ModelWrapper unifies API and local model execution.

- Repository: [Junyi Ye/creativemath](https://github.com/junyiye/creativemath)
- Tags: how-to-guide
- Published: 2026-03-05

---

**The** `ModelWrapper` **class serves as the central abstraction for model wrappers in CreativeMath, residing in** [`src/models/model_loader.py`](https://github.com/junyiye/creativemath/blob/main/src/models/model_loader.py) **and unifying API-based and local model execution behind a single interface.**

The CreativeMath repository provides a unified interface for interacting with diverse large language models through its wrapper system. Understanding where to locate and how to utilize these **model wrappers in CreativeMath** enables seamless integration of both cloud-based APIs and locally hosted models. The architecture centers on a single orchestration class that handles model detection, loading, and response generation.

## Locating the ModelWrapper Class in CreativeMath

### Primary Implementation File

The core implementation lives in [`src/models/model_loader.py`](https://github.com/junyiye/creativemath/blob/main/src/models/model_loader.py), specifically spanning **lines 9 through 35**. This file defines the `ModelWrapper` class that acts as the primary entry point for all model interactions within the codebase. According to the CreativeMath source, this is the definitive location where the wrapper abstraction is established.

## Architecture of the Model Wrapper System

The `ModelWrapper` class performs four critical functions to abstract away backend differences:

### Model Type Detection

Upon instantiation with a `model_name` parameter, the wrapper checks whether the identifier belongs to a hard-coded whitelist of API models or represents a local model. This determination happens internally before any loading occurs, ensuring the correct execution path is selected.

### Backend Loading

Based on the detection result, the wrapper delegates to specialized loaders:

- **API models**: Calls `load_api_model` from [`src/models/api_models.py`](https://github.com/junyiye/creativemath/blob/main/src/models/api_models.py)
- **Local models**: Calls `load_local_model` from [`src/models/local_models.py`](https://github.com/junyiye/creativemath/blob/main/src/models/local_models.py)

### Prompt Formatting

Before sending requests to the underlying backend, the wrapper invokes `load_messages` from [`src/models/prompt_utils.py`](https://github.com/junyiye/creativemath/blob/main/src/models/prompt_utils.py). This function transforms raw prompt strings into the specific message format required by the target model, handling differences between API specifications and local tokenizer expectations.

### Response Generation

The class routes execution to either `generate_api_response` for remote calls or `generate_local_response` for on-premise inference. Both paths return a unified string output, allowing calling code to remain agnostic about whether the model runs locally or in the cloud.

## Configuration and Dependencies

The wrapper accesses model parameters through [`src/config.py`](https://github.com/junyiye/creativemath/blob/main/src/config.py), specifically retrieving settings from the `config["model_config"]` dictionary. This centralized configuration manages API keys, endpoint URLs, and local model paths. The separation of configuration from implementation allows the `ModelWrapper` to remain focused on orchestration while externalizing environment-specific settings.

## Practical Usage Examples

The public API remains consistent across both deployment types. Instantiate `ModelWrapper` with your model identifier and call `generate_response()` with your prompt string.

Example using an API model:

```python
from src.models.model_loader import ModelWrapper

wrapper = ModelWrapper("gpt-4")
response = wrapper.generate_response(
    "Explain the concept of generating functions in combinatorics."
)
print(response)

```

Example using a local model:

```python
from src.models.model_loader import ModelWrapper

wrapper = ModelWrapper("llama-2-7b")
response = wrapper.generate_response(
    "Create a step-by-step solution for solving a quadratic equation."
)
print(response)

```

Both snippets illustrate the same public interface: instantiate `ModelWrapper` with the model identifier, invoke `generate_response(prompt)`, and obtain a string result regardless of the underlying execution path.

## Related Files in the Model Architecture

Understanding the complete ecosystem for **model wrappers in CreativeMath** requires familiarity with these supporting modules:

- [`src/models/api_models.py`](https://github.com/junyiye/creativemath/blob/main/src/models/api_models.py) – Implements `load_api_model` and `generate_api_response` for remote LLM services
- [`src/models/local_models.py`](https://github.com/junyiye/creativemath/blob/main/src/models/local_models.py) – Handles Hugging Face-style model loading and local inference via `load_local_model` and `generate_local_response`
- [`src/models/prompt_utils.py`](https://github.com/junyiye/creativemath/blob/main/src/models/prompt_utils.py) – Contains `load_messages` for prompt format translation between raw strings and backend-specific message structures
- [`src/config.py`](https://github.com/junyiye/creativemath/blob/main/src/config.py) – Provides configuration management including the `model_config` dictionary accessed by the wrapper

## Summary

- The `ModelWrapper` class in [`src/models/model_loader.py`](https://github.com/junyiye/creativemath/blob/main/src/models/model_loader.py) (lines 9-35) serves as the central hub for **model wrappers in CreativeMath**
- It automatically detects whether to use API or local execution paths based on the model identifier whitelist
- Backend delegation occurs through [`api_models.py`](https://github.com/junyiye/creativemath/blob/main/api_models.py) for remote calls and [`local_models.py`](https://github.com/junyiye/creativemath/blob/main/local_models.py) for on-premise models
- Prompt formatting uses [`prompt_utils.py`](https://github.com/junyiye/creativemath/blob/main/prompt_utils.py) to ensure compatible message structures across different backends
- Configuration settings load from [`src/config.py`](https://github.com/junyiye/creativemath/blob/main/src/config.py) via the `model_config` key
- Both API and local models share the same simple interface: instantiate and call `generate_response()`

## Frequently Asked Questions

### Where is the ModelWrapper class defined in CreativeMath?

The `ModelWrapper` class is defined in [`src/models/model_loader.py`](https://github.com/junyiye/creativemath/blob/main/src/models/model_loader.py) at lines 9-35. This file serves as the primary location for the model wrapper implementation, exposing the unified interface used throughout the CreativeMath codebase.

### How does ModelWrapper distinguish between API and local models?

The class maintains a hard-coded whitelist of API model names. When instantiated with a model identifier, it checks against this list to determine whether to invoke `load_api_model` from [`src/models/api_models.py`](https://github.com/junyiye/creativemath/blob/main/src/models/api_models.py) or `load_local_model` from [`src/models/local_models.py`](https://github.com/junyiye/creativemath/blob/main/src/models/local_models.py), routing the request to the appropriate backend.

### What files does the model wrapper depend on to function?

According to the CreativeMath source code, `ModelWrapper` depends on four key files: [`src/models/api_models.py`](https://github.com/junyiye/creativemath/blob/main/src/models/api_models.py) for API handling, [`src/models/local_models.py`](https://github.com/junyiye/creativemath/blob/main/src/models/local_models.py) for local inference, [`src/models/prompt_utils.py`](https://github.com/junyiye/creativemath/blob/main/src/models/prompt_utils.py) for prompt formatting via `load_messages`, and [`src/config.py`](https://github.com/junyiye/creativemath/blob/main/src/config.py) for configuration access including the `model_config` dictionary.

### Can I use the same code for both GPT-4 and local Llama models?

Yes. The `ModelWrapper` abstraction provides identical interfaces for both deployment types. You instantiate the class with either an API identifier like `"gpt-4"` or a local identifier like `"llama-2-7b"`, then call `generate_response()` using the same method signature regardless of whether the model runs locally or via remote API.