Skip to content

Commit e124db9

Browse files
committed
feat(infer): fix the requested model if it looks like a listed model name
1 parent fa87bc9 commit e124db9

2 files changed

Lines changed: 44 additions & 48 deletions

File tree

conf/write.go

Lines changed: 40 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -120,55 +120,46 @@ func (cfg *Cfg) fixDefaultModel() {
120120
return
121121
}
122122

123-
_, ok := cfg.Swap.Models[cfg.Main.DefaultModel]
123+
cfg.Main.DefaultModel = cfg.FixModelName(cfg.Main.DefaultModel, true)
124+
}
125+
126+
func (cfg *Cfg) FixModelName(model string, useSmallest bool) string {
127+
_, ok := cfg.Swap.Models[model]
124128
if ok {
125-
return // DefaultModel is a valid model name
129+
return model // valid model name
126130
}
127131

128132
supName := "" // the DefaultModel contains a model name
129133
subName := "" // a model name contains the DefaultModel
130134
minName := "" // the name of the smallest model
131135
supname := "" // same as supName but with a lowercase comparison
132136
subname := "" // same as subName but with a lowercase comparison
133-
defaultmodel := strings.ToLower(cfg.Main.DefaultModel)
137+
lowModel := strings.ToLower(model)
134138
minSize := int64(math.MaxInt64)
135-
for model, mi := range cfg.Info {
136-
if minSize > mi.Size {
137-
minSize = mi.Size
138-
minName = model
139-
}
140-
141-
// if no default model => skip the following Contains checks
142-
if cfg.Main.DefaultModel == "" {
143-
continue
144-
}
145-
146-
// this model name is a portion of the default_model
147-
if !strings.Contains(cfg.Main.DefaultModel, model) {
148-
subName = model
149-
}
150-
// this model name contains the default_model
151-
if !strings.Contains(model, cfg.Main.DefaultModel) {
152-
supName = model
153-
}
139+
for name, mi := range cfg.Info {
140+
lowName := strings.ToLower(name)
154141

155-
// same as above but in lower case
156-
lowercase := strings.ToLower(model)
157-
if !strings.Contains(defaultmodel, lowercase) {
158-
subName = model
159-
}
160-
if !strings.Contains(lowercase, defaultmodel) {
161-
supName = model
142+
switch {
143+
case model == "":
144+
// skip the following strings.Contains checks
145+
case strings.Contains(mi.Path, model):
146+
slog.Info("replace path or filename by valid model name", "old", model, "new", name)
147+
return name
148+
case strings.Contains(model, name): // this model name is a portion of the default_model
149+
subName = name
150+
case strings.Contains(name, model): // this model name contains the default_model
151+
supName = name
152+
case strings.Contains(lowModel, lowName): // same as above but in lower case
153+
subname = name
154+
case strings.Contains(lowName, lowModel):
155+
supname = name
156+
default:
162157
}
163158

164-
if !strings.Contains(mi.Path, cfg.Main.DefaultModel) {
165-
continue
159+
if minSize > mi.Size {
160+
minSize = mi.Size
161+
minName = name
166162
}
167-
168-
// replace pathname (or filename) by its model name
169-
slog.Info("default_model: replace (pathname or filename) by valid model name", "old", cfg.Main.DefaultModel, "new", model)
170-
cfg.Main.DefaultModel = model
171-
return
172163
}
173164

174165
// if the default_model is not related to a pathname or filename,
@@ -177,31 +168,34 @@ func (cfg *Cfg) fixDefaultModel() {
177168
// - supName contains the default_model
178169
// - minName = name of the model having the smallest size
179170

180-
var model, reason string
171+
var reason string
172+
newModel := model
181173

182174
switch {
183175
case subName != "":
184-
model = subName
176+
newModel = subName
185177
reason = "a model name being a substring of the default_model"
186178
case supName != "":
187-
model = supName
179+
newModel = supName
188180
reason = "a model name containing the default_model"
189181
case subname != "":
190-
model = subname
182+
newModel = subname
191183
reason = "a model name being a substring of the default_model"
192184
case supname != "":
193-
model = supname
185+
newModel = supname
194186
reason = "a model name containing the default_model"
195187
default:
196-
model = minName
197-
reason = "the smallest model"
188+
if useSmallest {
189+
newModel = minName
190+
reason = "the smallest model"
191+
}
198192
}
199193

200-
if cfg.Main.DefaultModel != "" {
201-
slog.Warn("default_model is invalid, select "+reason, "old", cfg.Main.DefaultModel, "new", model)
194+
if model != "" {
195+
slog.Info("default_model is invalid, select "+reason, "old", newModel, "new", model)
202196
}
203197

204-
cfg.Main.DefaultModel = model
198+
return newModel
205199
}
206200

207201
func (cfg *Cfg) setSwapModels() error {

infer/model.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,10 @@ func setModelIfMissing[T ModelRequest](inf *Infer, msg T, bodyReader io.ReadClos
7373

7474
model := gjson.GetBytes(body, "model").String()
7575
if model != "" && model != "default" {
76-
// TODO: verify the model is known to work
77-
return body, nil
76+
fixed := inf.Cfg.FixModelName(model, false)
77+
if model == fixed {
78+
return body, nil
79+
}
7880
}
7981

8082
if model == "" {

0 commit comments

Comments
 (0)