diff --git a/internal/provider/account.go b/internal/provider/account.go index b6a58482a..3677b28ea 100644 --- a/internal/provider/account.go +++ b/internal/provider/account.go @@ -133,8 +133,10 @@ func (p Provider) APIs(model string) []Protocol { return p.apisAmong(p.Speaks(), // apisAmong is APIs with the protocols p speaks found already. func (p Provider) apisAmong(speaks []Protocol, model string) []Protocol { // the one the user said it is asked on, whatever the list says - if proto, ok := p.modelAPIAmong(speaks, model); ok { - return []Protocol{proto} + if proto, ok := p.apiSet(model); ok { + if proto, ok := p.modelAPIAmong(len(speaks), proto); ok { + return []Protocol{proto} + } } return p.ListedAPIs(model) } diff --git a/internal/provider/modelapi_count_test.go b/internal/provider/modelapi_count_test.go new file mode 100644 index 000000000..df47feff8 --- /dev/null +++ b/internal/provider/modelapi_count_test.go @@ -0,0 +1,40 @@ +package provider + +import ( + "path/filepath" + "testing" +) + +// ModelAPI is asked of each model of every provider's list on the +// Providers page: one with no API set is answered without listing the +// protocols the provider speaks, and one set is answered as before. +func TestModelAPIListsNoProtocols(t *testing.T) { + isolate(t) + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config")) + t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache")) + p := Provider{ID: "relay", Name: "Relay", Key: "k", Chat: "http://127.0.0.1:1/v1", Anthropic: "http://127.0.0.1:1", Models: []string{"a", "b"}} + if err := Save(p); err != nil { + t.Fatal(err) + } + if err := SetModelAPI("relay/b", string(Anthropic)); err != nil { + t.Fatal(err) + } + defer Hold()() + if _, ok := p.ModelAPI("a"); ok { + t.Fatal("a model with no API set has one") + } + if api, ok := p.ModelAPI("b"); !ok || api != Anthropic { + t.Fatalf("the model set on Anthropic's API is on %q (%v)", api, ok) + } + if one := (Provider{ID: "relay", Chat: p.Chat}); func() bool { _, ok := one.ModelAPI("b"); return ok }() { + t.Fatal("a provider with one API keeps a model set on another") + } + listed := allocatedBy(func() { _ = p.Speaks() }) + asked := allocatedBy(func() { _, _ = p.ModelAPI("a") }) + if listed == 0 || asked*4 > listed { + t.Fatalf("ModelAPI allocated %d bytes, listing the protocols %d", asked, listed) + } +} diff --git a/internal/provider/modelprefs.go b/internal/provider/modelprefs.go index 20061ecce..49a4c7e96 100644 --- a/internal/provider/modelprefs.go +++ b/internal/provider/modelprefs.go @@ -469,16 +469,40 @@ func setModelAPI(ref, api string) (bool, error) { // ModelAPI is the API the user said p's model is asked on, when p has a // URL for it still. -func (p Provider) ModelAPI(model string) (Protocol, bool) { return p.modelAPIAmong(p.Speaks(), model) } - -// modelAPIAmong is ModelAPI with the protocols p speaks found already. -func (p Provider) modelAPIAmong(speaks []Protocol, model string) (Protocol, bool) { - // Vertex AI's model id says its one API (ListedAPIs) - if len(speaks) < 2 || p.IsVertex() { +// +// The Providers page asks it of each model of every provider's list, and +// most have no API set: that is looked up first, and the protocols p +// speaks are counted, not listed (a list made for each model was 12 MB of +// what forty polls allocated with real accounts). +func (p Provider) ModelAPI(model string) (Protocol, bool) { + proto, ok := p.apiSet(model) + if !ok { return "", false } + n := 0 + for range p.speaking { + if n++; n == 2 { + break + } + } + return p.modelAPIAmong(n, proto) +} + +// apiSet is the API the user set model to be asked on, false with +// none. +func (p Provider) apiSet(model string) (Protocol, bool) { proto := Protocol(heldSettings().ModelAPIs[p.ID+"/"+model]) - if proto == "" || !modelAPI(proto) || p.Base(proto) == "" { + return proto, proto != "" +} + +// modelAPIAmong is ModelAPI with the number of protocols p speaks (up to +// two) and the one set found already. +func (p Provider) modelAPIAmong(speaks int, proto Protocol) (Protocol, bool) { + // Vertex AI's model id says its one API (ListedAPIs) + if speaks < 2 || p.IsVertex() { + return "", false + } + if !modelAPI(proto) || p.Base(proto) == "" { return "", false } return proto, true