gateway: an image model the user marked draws at the images API first
A Discord user asked how to add an image model to a custom provider. The way is: Add model by its id in the provider's editor, right-click it, Makes: Image model, Save, then pick it in Settings > Images. The menu says such a model "draws at the provider's images API (/images/generations)", but viaFor ignored the mark: a model whose name isn't on catalog.ImagesAPI's list (a relay's jimeng-4.0) was asked on chat completions first, and reached the images API only if chat answered 404/405 or named images/generations. A relay that answers chat with any other error never drew it. Now a marked image model is asked at the images API first; a provider without one (404/405) still has it drawn on chat completions, by draw's existing fallback. Run (mote, at3cabaab54plus this change; the two commits since,8a58c0e30anddc098c1e8, touch no gateway, provider, catalog or settings file): - go test ./internal/gateway -count=1 -timeout 45m: ok (1808s) - GOOS=linux and GOOS=windows go vet ./internal/gateway: ok; gofmt -l clean Broken by hand: with the new viaFor case turned off, TestMarkedImageModelDrawsAtTheImagesAPI fails ("images API sent []"). Not tried: a real relay's image model; other packages' suites (only internal/gateway changed). No GUI change.
This commit is contained in:
@@ -1134,6 +1134,12 @@ func viaFor(p provider.Provider, model string) drawVia {
|
||||
return viaImages
|
||||
case isGoogle(p) && strings.Contains(strings.ToLower(model), "gemini"):
|
||||
return viaGemini
|
||||
case p.ModelKind(model) == provider.KindImage:
|
||||
// one the user said is an image model is asked at the images API
|
||||
// first, as the editor's Makes: Image model says (a relay's
|
||||
// jimeng-4.0 isn't named for one); a provider without that API
|
||||
// (404, 405) has it drawn on chat completions, as draw falls back
|
||||
return viaImages
|
||||
case isOpenRouter(p):
|
||||
if catalog.ImagesAPI(model) || answersInImages(p, model) {
|
||||
return viaImages
|
||||
|
||||
@@ -256,6 +256,65 @@ func TestViaFor(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// A model added by hand to a custom provider and marked Makes: Image model
|
||||
// (a relay's jimeng-4.0, named for nothing magpie knows) is asked at the
|
||||
// provider's images API, as the editor's menu says, not on chat
|
||||
// completions first; listed under Image generation, it draws as the
|
||||
// Settings' pick. On a provider with no images API (404) it is drawn on
|
||||
// chat completions all the same.
|
||||
func TestMarkedImageModelDrawsAtTheImagesAPI(t *testing.T) {
|
||||
s, e := easeled(t)
|
||||
p, _ := provider.Find("art")
|
||||
p.Models = append(p.Models, "jimeng-4.0")
|
||||
if err := provider.Save(*p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := provider.SetModelKind("art/jimeng-4.0", provider.KindImage); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
p, _ = provider.Find("art")
|
||||
if !slices.ContainsFunc(Drawers(*p), func(m catalog.Model) bool { return m.ID == "jimeng-4.0" }) {
|
||||
t.Fatalf("not listed to draw: %v", Drawers(*p))
|
||||
}
|
||||
st := settings.Load()
|
||||
st.ImageGen = "art/jimeng-4.0"
|
||||
if err := settings.Save(st); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
code, a, raw := postImages(t, s, "/v1/images/generations", "application/json", `{"prompt":"a magpie"}`)
|
||||
if code != 200 || a.Model != "art/jimeng-4.0" || len(a.Data) != 1 {
|
||||
t.Fatalf("%d %s", code, raw)
|
||||
}
|
||||
if got := e.got("/v1/images/generations"); len(got) != 1 || !strings.Contains(got[0], `"model":"jimeng-4.0"`) {
|
||||
t.Fatalf("images API sent %v", got)
|
||||
}
|
||||
if got := e.got("/v1/chat/completions"); len(got) != 0 {
|
||||
t.Fatalf("asked on chat first: %v", got)
|
||||
}
|
||||
|
||||
// a relay with chat completions alone: the 404 sends it there
|
||||
chat := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/v1/chat/completions" {
|
||||
w.WriteHeader(404)
|
||||
io.WriteString(w, `{"error":{"message":"not found"}}`)
|
||||
return
|
||||
}
|
||||
b64 := base64.StdEncoding.EncodeToString(pngBytes)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
io.WriteString(w, `{"choices":[{"message":{"role":"assistant","content":"ok","images":[{"type":"image_url","image_url":{"url":"data:image/png;base64,`+b64+`"}}]}}]}`)
|
||||
}))
|
||||
t.Cleanup(chat.Close)
|
||||
if err := provider.Save(provider.Provider{ID: "talk", Name: "Talk", Chat: chat.URL + "/v1", Key: "key", Models: []string{"painter-x"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := provider.SetModelKind("talk/painter-x", provider.KindImage); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if code, a, raw := postImages(t, s, "/v1/images/generations", "application/json", `{"model":"talk/painter-x","prompt":"a magpie"}`); code != 200 || len(a.Data) != 1 {
|
||||
t.Fatalf("chat-only relay: %d %s", code, raw)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenRouterImagesAPIDrawsImageModels(t *testing.T) {
|
||||
fresh(t)
|
||||
p := provider.Provider{ID: "openrouter", Name: "OpenRouter", Chat: "https://openrouter.ai/api/v1", Key: "key", Models: []string{"openai/gpt-image-2"}}
|
||||
|
||||
Reference in New Issue
Block a user