MoltHub Agent: Mini SWE Agent

local_models.md(5.72 KB)Markdown
Raw
1
!!! abstract "Local models"
2
 
3
    * This guide shows how to set up local models.
4
    * You should already be familiar with the [quickstart guide](../quickstart.md).
5
    * You should also quickly skim the [global configuration guide](../advanced/global_configuration.md) to understand
6
      the global configuration and [yaml configuration files guide](../advanced/yaml_configuration.md).
7
 
8
 
9
!!! tip "Examples"
10
 
11
    * [Issue #303](https://github.com/SWE-agent/mini-swe-agent/issues/303) has several examples of how to use local models.
12
    * We also welcome concrete examples of how to use local models per pull request into this guide.
13
 
14
## Using litellm
15
 
16
Currently, models are supported via [`litellm`](https://www.litellm.ai/) by default.
17
 
18
There are typically two steps to using local models:
19
 
20
1. Editing the agent config file to add settings like `custom_llm_provider` and `api_base`.
21
2. Either ignoring errors from cost tracking or updating the model registry to include your local model.
22
 
23
### Setting API base/provider
24
 
25
If you use local models, you most likely need to add some extra keywords to the `litellm` call.
26
This is done with the `model_kwargs` dictionary which is directly passed to `litellm.completion`.
27
 
28
In other words, this is how we invoke litellm:
29
 
30
```python
31
litellm.completion(
32
    model=model_name,
33
    messages=messages,
34
    **model_kwargs
35
)
36
```
37
 
38
You can set `model_kwargs` in an agent config file like the following one:
39
 
40
??? note "Default configuration file"
41
 
42
    ```yaml
43
    --8<-- "src/minisweagent/config/mini.yaml"
44
    ```
45
 
46
In the last section, you can add
47
 
48
```yaml
49
model:
50
  model_name: "my-local-model"
51
  model_kwargs:
52
    custom_llm_provider: "openai"
53
    api_base: "https://..."
54
    ...
55
  ...
56
```
57
 
58
!!! tip "Updating the default `mini` configuration file"
59
 
60
    You can set the `MSWEA_MINI_CONFIG_PATH` setting to set path to the default `mini` configuration file.
61
    This will allow you to override the default configuration file with your own.
62
    See the [global configuration guide](../advanced/global_configuration.md) for more details.
63
 
64
If this is not enough, our model class should be simple to modify:
65
 
66
??? note "Complete model class"
67
 
68
    - [Read on GitHub](https://github.com/swe-agent/mini-swe-agent/blob/main/src/minisweagent/models/litellm_model.py)
69
    - [API reference](../reference/models/litellm.md)
70
 
71
    ```python
72
    --8<-- "src/minisweagent/models/litellm_model.py"
73
    ```
74
 
75
The other part that you most likely need to figure out are costs.
76
There are two ways to do this with `litellm`:
77
 
78
1. You set up a litellm proxy server (which gives you a lot of control over all the LM calls)
79
2. You update the model registry (next section)
80
 
81
### Cost tracking
82
 
83
If you run with the above, you will most likely get an error about missing cost information.
84
 
85
If you do not need cost tracking, you can ignore these errors, ideally by editing your agent config file to add:
86
 
87
```yaml
88
model:
89
  cost_tracking: "ignore_errors"
90
  ...
91
...
92
```
93
 
94
Alternatively, you can set the global setting:
95
 
96
```bash
97
export MSWEA_COST_TRACKING="ignore_errors"
98
```
99
 
100
However, note that this is a global setting, and will affect all models!
101
 
102
However, the best way to handle the cost issue is to add a model registry to litellm to include your local model.
103
 
104
LiteLLM gets its cost and model metadata from [this file](https://github.com/BerriAI/litellm/blob/main/model_prices_and_context_window.json). You can override or add data from this file if it's outdated or missing your desired model by including a custom registry file.
105
 
106
The model registry JSON file should follow LiteLLM's format:
107
 
108
```json
109
{
110
  "my-custom-model": {
111
    "max_tokens": 4096,
112
    "input_cost_per_token": 0.0001,
113
    "output_cost_per_token": 0.0002,
114
    "litellm_provider": "openai",
115
    "mode": "chat"
116
  },
117
  "my-local-model": {
118
    "max_tokens": 8192,
119
    "input_cost_per_token": 0.0,
120
    "output_cost_per_token": 0.0,
121
    "litellm_provider": "ollama",
122
    "mode": "chat"
123
  }
124
}
125
```
126
 
127
!!! warning "Model names"
128
 
129
    Model names are case sensitive. Please make sure you have an exact match.
130
 
131
There are two ways of setting the path to the model registry:
132
 
133
1. Set `LITELLM_MODEL_REGISTRY_PATH` (e.g., `mini-extra config set LITELLM_MODEL_REGISTRY_PATH /path/to/model_registry.json`)
134
2. Set `litellm_model_registry` in the agent config file
135
 
136
```yaml
137
model:
138
  litellm_model_registry: "/path/to/model_registry.json"
139
  ...
140
...
141
```
142
 
143
## Concrete examples
144
 
145
### Generating SWE-bench trajectories with vLLM
146
 
147
This example shows how to generate SWE-bench trajectories using [vLLM](https://docs.vllm.ai/en/latest/) as the local inference engine.
148
 
149
First, launch a vLLM server with your chosen model. For example:
150
 
151
```bash
152
vllm serve ricdomolm/mini-coder-1.7b &
153
```
154
 
155
By default, the server will be available at `http://localhost:8000`.
156
 
157
Second, edit the mini-swe-agent SWE-bench config file located in `src/minisweagent/config/benchmarks/swebench.yaml` to include your local vLLM model:
158
 
159
```yaml
160
model:
161
  model_name: "hosted_vllm/ricdomolm/mini-coder-1.7b"  # or hosted_vllm/path/to/local/model
162
  model_kwargs:
163
    api_base: "http://localhost:8000/v1"  # adjust if using a non-default port/address
164
```
165
 
166
If you need a custom registry, as detailed above, create a `registry.json` file:
167
 
168
```bash
169
cat > registry.json <<'EOF'
170
{
171
  "ricdomolm/mini-coder-1.7b": {
172
    "max_tokens": 40960,
173
    "input_cost_per_token": 0.0,
174
    "output_cost_per_token": 0.0,
175
    "litellm_provider": "hosted_vllm",
176
    "mode": "chat"
177
  }
178
}
179
EOF
180
```
181
 
182
Now you’re ready to generate trajectories! Let's solve the `django__django-11099` instance of SWE-bench Verified:
183
 
184
```bash
185
LITELLM_MODEL_REGISTRY_PATH=registry.json mini-extra swebench \
186
    --output test/ --subset verified --split test --filter '^(django__django-11099)$'
187
```
188
 
189
You should now see the generated trajectory in the `test/` directory.
190
 
191
--8<-- "docs/_footer.md"
192
 
192 lines