DecidesModel answered for a remote magpie, and for a provider that lists
its decision models (OpenRouter's), by building decideListed's whole
list (catalog.Live copies every chat model) and scanning it. Every model
of such a provider is asked this from available(), pluginDecisions and
the decider list, so a list of n models was copied n times per look:
0.43 GB of the 4.1 GB a real config allocated over 20 polls of
/api/drift, /api/state and /api/providers (v0.1.2430,
DecidesModel -> decideListed -> catalog.Live -> catalog.Chat).
It now looks the model up in the list as kept (catalog.LiveByID, which
readLive indexes once per read), and catalog.LiveListed says whether the
list has any model, for the Jev fallback decideListed has. The answers
are decideListed's: a remote magpie's models its list marks Decides; a
listed decision list's models; with none listed, OpenRouter's Jev unless
the server marks its own.
TestDecidesModelCopiesNoList: 2000 decision models, each ask under a
twentieth of the list's bytes, for both kinds, and the answers above.
Red on the old code (an ask 869,336 bytes, the list 869,829).
Run at 6a900e0d0 plus this change, on mote: go vet (darwin, windows, linux)
and gofmt of internal/provider and internal/catalog clean;
go test -count=1 -timeout 45m ./internal/provider ./internal/catalog
./internal/agent ./internal/gateway all ok (126s, 0.6s, 612s, 1305s).
88 lines
2.9 KiB
Go
88 lines
2.9 KiB
Go
package provider
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"runtime"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
)
|
|
|
|
// Each of a provider's models is asked whether it decides, and each ask
|
|
// copied the provider's whole decision list (0.4 GB of a real config's
|
|
// 4 GB over 20 polls of the Providers and Agents pages). An ask reads the
|
|
// list kept, and answers as the list does.
|
|
func TestDecidesModelCopiesNoList(t *testing.T) {
|
|
h := t.TempDir()
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("USERPROFILE", h)
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
|
p := Provider{ID: "or-many", Name: "OpenRouter", Key: "k", Chat: "https://openrouter.ai/api/v1", Decide: "https://openrouter.ai/api/v1"}
|
|
if !p.listsDecisions() {
|
|
t.Fatal("not a provider that lists its decision models")
|
|
}
|
|
// none listed yet: OpenRouter's Jev
|
|
if !p.DecidesModel(OpenRouterJev) || p.DecidesModel("liquid/d1") {
|
|
t.Fatal("with no list, only Jev decides")
|
|
}
|
|
var ms []catalog.Model
|
|
for i := range 2000 {
|
|
ms = append(ms, catalog.Model{ID: fmt.Sprintf("vendor/decider-%d", i)})
|
|
}
|
|
if err := catalog.SaveLive(decisionsID(p.ID), p.Decide, ms); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fetchedEarlier(t, catalog.LivePath(decisionsID(p.ID)))
|
|
if !p.DecidesModel("vendor/decider-1999") || p.DecidesModel(OpenRouterJev) || p.DecidesModel("vendor/chat") {
|
|
t.Fatal("decision models are not the list's")
|
|
}
|
|
listed := bytesPer(func() { p.decideListed() })
|
|
if n := bytesPer(func() { p.DecidesModel("vendor/decider-1999") }); n*20 > listed {
|
|
t.Fatalf("an ask took %d bytes, the list %d", n, listed)
|
|
}
|
|
|
|
// a remote magpie's: the models its list marks
|
|
r := normalize(Provider{ID: "office", Preset: RemoteMagpiePreset, Chat: "http://127.0.0.1:1/typesafe"})
|
|
ms = ms[:0]
|
|
for i := range 2000 {
|
|
ms = append(ms, catalog.Model{ID: fmt.Sprintf("m-%d", i), Decides: i%2 == 0})
|
|
}
|
|
if err := catalog.SaveLive(r.ID, r.Chat, ms); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fetchedEarlier(t, catalog.LivePath(r.ID))
|
|
if !r.DecidesModel("m-1998") || r.DecidesModel("m-1999") || r.DecidesModel("m-x") {
|
|
t.Fatal("a remote magpie's decision models are not the ones its list marks")
|
|
}
|
|
listed = bytesPer(func() { r.decideListed() })
|
|
if n := bytesPer(func() { r.DecidesModel("m-1998") }); n*20 > listed {
|
|
t.Fatalf("a remote magpie's ask took %d bytes, the list %d", n, listed)
|
|
}
|
|
}
|
|
|
|
// bytesPer is what one call of f allocates, in bytes, over 20 calls.
|
|
func bytesPer(f func()) uint64 {
|
|
f()
|
|
var a, b runtime.MemStats
|
|
runtime.ReadMemStats(&a)
|
|
for range 20 {
|
|
f()
|
|
}
|
|
runtime.ReadMemStats(&b)
|
|
return (b.TotalAlloc - a.TotalAlloc) / 20
|
|
}
|
|
|
|
// fetchedEarlier dates f an hour back, as a list fetched earlier is: a file
|
|
// written just now is read again on each look, its time too recent to
|
|
// tell a later write apart.
|
|
func fetchedEarlier(t *testing.T, f string) {
|
|
t.Helper()
|
|
old := time.Now().Add(-time.Hour)
|
|
if err := os.Chtimes(f, old, old); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|