provider: which models a provider has no API for is found once per list
NoAPIFor found the protocols the provider speaks for every model it was asked about, and APIs did again through ModelAPI, so a list of N models built 2N Speaks slices. The providers' page, ExposedOf and servable ask it for each model of each provider: 16 MB of what twenty window polls allocated with real accounts (m2463 profile). NoAPI() finds the protocols once and gives a func for each model; APIs and ModelAPI take them through apisAmong and modelAPIAmong. NoAPIFor, APIs and ModelAPI answer as before. Test: TestNoAPIFindsTheProvidersAPIsOnce. 100 models of a relay, half listed on chat: Speaks looked up once (5 at most, for another test's look), the chat half kept, NoAPIFor the same for each. Run on mote, at this tree: - gofmt, go vet (darwin, windows, linux): ok - the new test with TestExposedOnlyOnTheAPIsTheProviderHas and TestListedAPIsCost, 3 times: ok - broken by hand (NoAPI back to a per-model NoAPIFor): red, "100 models looked up the provider's APIs 200 times" - go test ./internal/provider: ok - go test ./internal/gui (before the apisAmong step, which touches internal/provider only): every test ok except the known TestSettingsSaveKeepsWhatItDoesNotSend (#4514), TestMainWindowCorners (#3949) and TestColdTurnsTaken (#4510) Not run: an A/B with the real-account sandbox for this change alone.
This commit is contained in:
@@ -821,6 +821,7 @@ func providerInfoWith(p provider.Provider, ls provider.Listing, agents []agentUs
|
||||
// was most of the Providers page's wait with many models (lml on
|
||||
// Discord, Windows)
|
||||
set := held
|
||||
noAPI := p.NoAPI()
|
||||
named := func(m catalog.Model, on bool) modelJSON {
|
||||
images := m.Images || catalog.SeesImages(m.ID)
|
||||
if m.ImageInput != nil {
|
||||
@@ -850,7 +851,7 @@ func providerInfoWith(p provider.Provider, ls provider.Listing, agents []agentUs
|
||||
j.Auto = append(j.Auto, string(a))
|
||||
}
|
||||
}
|
||||
j.NoAPI = p.NoAPIFor(m.ID)
|
||||
j.NoAPI = noAPI(m.ID)
|
||||
j.Same = sames[p.ID+"/"+m.ID]
|
||||
if own := p.OwnModelID(m.ID); own != m.ID {
|
||||
j.Vendor = own
|
||||
|
||||
@@ -128,9 +128,12 @@ type Account struct {
|
||||
// Claude models on Chat and Anthropic's. nil is not known, and every API
|
||||
// the provider speaks may be tried. One the user set for the model
|
||||
// (SetModelAPI) is the only one.
|
||||
func (p Provider) APIs(model string) []Protocol {
|
||||
func (p Provider) APIs(model string) []Protocol { return p.apisAmong(p.Speaks(), model) }
|
||||
|
||||
// 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.ModelAPI(model); ok {
|
||||
if proto, ok := p.modelAPIAmong(speaks, model); ok {
|
||||
return []Protocol{proto}
|
||||
}
|
||||
return p.ListedAPIs(model)
|
||||
|
||||
@@ -469,9 +469,12 @@ 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) {
|
||||
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(p.Speaks()) < 2 || p.IsVertex() {
|
||||
if len(speaks) < 2 || p.IsVertex() {
|
||||
return "", false
|
||||
}
|
||||
proto := Protocol(heldSettings().ModelAPIs[p.ID+"/"+model])
|
||||
|
||||
@@ -1580,7 +1580,8 @@ func (p Provider) ExposedOf(avail []catalog.Model) []catalog.Model {
|
||||
picks = slices.DeleteFunc(slices.Clone(picks), p.Account.unusable)
|
||||
}
|
||||
if len(picks) > 0 {
|
||||
return slices.DeleteFunc(pick(p.withNewlyListed(picks, avail)), func(m catalog.Model) bool { return p.NoAPIFor(m.ID) })
|
||||
noAPI := p.NoAPI()
|
||||
return slices.DeleteFunc(pick(p.withNewlyListed(picks, avail)), func(m catalog.Model) bool { return noAPI(m.ID) })
|
||||
}
|
||||
// a model listed on none of the APIs the provider has a URL for is
|
||||
// left out before the first 24 are taken (#2094)
|
||||
@@ -1601,7 +1602,8 @@ func (p Provider) ExposedOf(avail []catalog.Model) []catalog.Model {
|
||||
|
||||
// servable is avail less the models NoAPIFor leaves out.
|
||||
func (p Provider) servable(avail []catalog.Model) []catalog.Model {
|
||||
return slices.DeleteFunc(slices.Clone(avail), func(m catalog.Model) bool { return p.NoAPIFor(m.ID) })
|
||||
noAPI := p.NoAPI()
|
||||
return slices.DeleteFunc(slices.Clone(avail), func(m catalog.Model) bool { return noAPI(m.ID) })
|
||||
}
|
||||
|
||||
// NewestInFamily is the models Best in family would serve of avail, the
|
||||
@@ -1646,16 +1648,23 @@ var listingsBuilt atomic.Int64
|
||||
// a provider of keys, whose URLs the user typed: a sign-in's APIs are
|
||||
// magpie's own. A model whose list says no API, and a provider with no URL
|
||||
// at all, are never left out here.
|
||||
func (p Provider) NoAPIFor(model string) bool {
|
||||
func (p Provider) NoAPIFor(model string) bool { return p.NoAPI()(model) }
|
||||
|
||||
// NoAPI is NoAPIFor for each model of a list: the APIs the provider has a
|
||||
// URL for are found once for the list, not once for each model (they were
|
||||
// most of what NoAPIFor allocated, 16 MB of twenty window polls).
|
||||
func (p Provider) NoAPI() func(model string) bool {
|
||||
if p.Account != nil {
|
||||
return false
|
||||
}
|
||||
apis := p.APIs(model)
|
||||
if len(apis) == 0 {
|
||||
return false
|
||||
return func(string) bool { return false }
|
||||
}
|
||||
speaks := p.Speaks()
|
||||
return len(speaks) > 0 && !slices.ContainsFunc(speaks, func(s Protocol) bool { return slices.Contains(apis, s) })
|
||||
if len(speaks) == 0 {
|
||||
return func(string) bool { return false }
|
||||
}
|
||||
return func(model string) bool {
|
||||
apis := p.apisAmong(speaks, model)
|
||||
return len(apis) > 0 && !slices.ContainsFunc(speaks, func(s Protocol) bool { return slices.Contains(apis, s) })
|
||||
}
|
||||
}
|
||||
|
||||
// Picks are the models the user picked, with those the vendor has listed
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/catalog"
|
||||
)
|
||||
|
||||
// Which models a provider can't be asked for (#2094) is weighed for each
|
||||
// model of a list, and the APIs the provider has a URL for are found once
|
||||
// for the list, not once for each model.
|
||||
func TestNoAPIFindsTheProvidersAPIsOnce(t *testing.T) {
|
||||
oneHome(t)
|
||||
p := Provider{ID: "relay", Name: "Relay", Key: "sk-x", Chat: "http://127.0.0.1:1/v1"}
|
||||
var ms []catalog.Model
|
||||
var chat []string
|
||||
for i := range 100 {
|
||||
m := catalog.Model{ID: fmt.Sprintf("m%02d", i), APIs: []string{"anthropic"}}
|
||||
if i%2 == 0 {
|
||||
m.APIs = []string{"chat"}
|
||||
chat = append(chat, m.ID)
|
||||
}
|
||||
ms = append(ms, m)
|
||||
}
|
||||
if err := Save(p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := catalog.SaveLive(p.ID, p.Chat, ms); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
before := speaksLooked.Load()
|
||||
var kept []string
|
||||
for _, m := range p.servable(ms) {
|
||||
kept = append(kept, m.ID)
|
||||
}
|
||||
// once, give or take a look another test left running takes
|
||||
if n := speaksLooked.Load() - before; n > 5 {
|
||||
t.Fatalf("100 models looked up the provider's APIs %d times", n)
|
||||
}
|
||||
if !slices.Equal(kept, chat) {
|
||||
t.Fatalf("kept %v, want the models listed on chat %v", kept, chat)
|
||||
}
|
||||
for _, m := range ms {
|
||||
if p.NoAPIFor(m.ID) != !slices.Contains(chat, m.ID) {
|
||||
t.Fatalf("NoAPIFor(%s) = %v", m.ID, p.NoAPIFor(m.ID))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1681,8 +1681,12 @@ func (p Provider) Base(proto Protocol) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// speaksLooked counts Speaks' looks, for a test.
|
||||
var speaksLooked atomic.Int64
|
||||
|
||||
// Speaks lists the protocols the vendor serves natively, preferred first.
|
||||
func (p Provider) Speaks() []Protocol {
|
||||
speaksLooked.Add(1)
|
||||
// a Google sign-in speaks Code Assist, and only that
|
||||
if p.Account != nil && p.Account.codeAssist != "" && !p.IsPlugin() {
|
||||
return []Protocol{CodeAssist}
|
||||
|
||||
Reference in New Issue
Block a user