XYenon (Discord) has the same embedding model at several providers and
asked why it isn't grouped the way chat models are. It wasn't even
listed: a fetched list dropped every embedding, rerank, speech and
transcription model, so "embed-1" on /v1/embeddings was a 404 unless a
hand-made group named it.
- catalog: a fetched list keeps those models, tagged with their API
(Model.Serves, read from the id by ServesOf: rerank, embed/bge-,
whisper/transcribe, tts-/-tts). Chat() and Live() leave them out, so
agents' model lists, gateway keys and Routing's chat pickers never
see them; LiveServing(id, kind) reads them. The live file says
"kinded": true; a list fetched before this is fetched again once a
run (autolist), with no schedule needed.
- provider: KindGroup(kind, ref) is made per request, never stored, so
it is in neither Groups() nor Catalog(). Its id is
auto-<kind>:<model> ("group/auto-embedding:embed-1"); its members are
the providers that serve exactly that id for that API, in the order
they were added, none off, decide-only, serving none, or with the
model disabled.
- gateway: embeddings, rerank, speech, transcriptions and videos pick
the group by route (mediaMembers): a bare id is a kind group when
more than one provider serves it, the group's own id always; a
hand-made group still works through offChatList. A group is of its
own API: an embedding model's id on /v1/rerank is a 404.
Run at bd0f33828 + this change, on WSL magpie-desktop (mote was
running another session's full suite):
- go test -tags nogui -count=1 -timeout 45m ./internal/catalog
./internal/provider ./internal/gateway: ok, ok, ok (594s). The first
run was red in three tests that held a fetched list to leaving its
embedding model out (TestFetchArkMarksVideoModels,
TestNvidiaModelScopeLiveList, TestTencentTokenHubList); they now hold
it kept, tagged embedding, and out of catalog.Chat.
- 22 GUI files over the model lists and Routing pickers, both engines,
every language (group-pick-drawers image-models-hide video-models-hide
model-kind routing-kind agent-models agent-models-picker
agent-model-groups agent-models-only-picked gateway-key-models
gateway-key-groups group-from-model-add unlisted-models
models-select-all-api provider-add-models provider-detect-models
routing-rule-models panel-routing-pick fallback-group group-unlists
settings-groups i18n-keys): 229 of 244 pass. The 15 reds are in six
files that a control run of those six at bd0f33828 has red the same
(14 of 56): #4389, #4964, #5332, #4616 and, new, #5357
(routing-rule-models webkit 390). agent-models webkit zh's opacity
0.998 was red here only, a fixed 250ms wait: #5358. No GUI asset is
changed here; the GUI tests stub /api.
- on mote: TestProviderListBeforeSave (internal/gui; the add form's
list keeps an embedding model out) and
TestPiModelsAskedOnTheirNativeAPI (internal/agent), ok.
- gofmt -l clean; GOOS=linux and GOOS=windows go vet of catalog,
provider and gateway on mote at this head, ok.
Rebased then on origin/main d620ff6b5 (one commit, internal/agent and
internal/sessions only), and b63b28958 (one mover min line) after; at
d620ff6b5 + this on mote: go build ./..., go vet
of catalog, provider, gateway, agent and sessions, go test
./internal/catalog and -run 'Kind|Fetch|Nvidia|Tencent|Serving' of
provider and gateway: ok.
Red on the old code: TestKindGroupsServeEachAPIByRoute (embed-1 404)
and TestVideoKindGroupByTheModelsID (404).
Broken by hand, each red, then restored:
- the fetch dropping serves != "" again: TestFetchKeepsModelsOfOtherAPIs;
- the once-a-run refetch forced off: TestKindlessListFetchedOnce;
- KindGroup's disabled-model check removed:
TestKindGroupHoldsTheExactModel;
- LiveServing asked with "" (any kind): the rerank-by-embedding-id
check gets 200 instead of 404.
Known gaps, not done here:
- The Routing group editor still offers only chat and image models, so
a new hand-made group of different ids (bge-m3 / BAAI/bge-m3) can't
pick kind models: #5352.
- A bare id whose every member is disabled is a 404, not a 403.
- Rerank, Qwen3-Embedding and bge models that sat among a provider's
chat models before (textModel's words missed them) leave agents'
lists now. A hand-made group naming one keeps serving.
- /v1/models lists no kind model; the Routing live view may show the
raw kind id.
Not tried: real vendors' lists and routes; the full ./... suite at this
head (queued on mote after another session's run; reds get issues).
161 lines
6.7 KiB
Go
161 lines
6.7 KiB
Go
package gateway
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// kindVendor is an OpenAI-style vendor whose list has a chat model and one
|
|
// model of each other API, and that serves each of those APIs; busy
|
|
// answers every one of them 429, out of quota. It counts what it is asked.
|
|
type kindVendor struct {
|
|
*httptest.Server
|
|
busy bool
|
|
mu sync.Mutex
|
|
asked []string
|
|
}
|
|
|
|
func newKindVendor(t *testing.T, busy bool) *kindVendor {
|
|
t.Helper()
|
|
v := &kindVendor{busy: busy}
|
|
v.Server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
b, _ := io.ReadAll(r.Body)
|
|
v.mu.Lock()
|
|
v.asked = append(v.asked, r.Method+" "+r.URL.Path)
|
|
v.mu.Unlock()
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if r.Method == "GET" && r.URL.Path == "/v1/models" {
|
|
io.WriteString(w, `{"object":"list","data":[`+
|
|
`{"id":"chat-model","object":"model"},`+
|
|
`{"id":"embed-1","object":"model"},`+
|
|
`{"id":"bge-reranker-1","object":"model"},`+
|
|
`{"id":"tts-1","object":"model"},`+
|
|
`{"id":"whisper-1","object":"model"},`+
|
|
`{"id":"omni-moderation-latest","object":"model"}]}`)
|
|
return
|
|
}
|
|
if v.busy {
|
|
w.WriteHeader(429)
|
|
io.WriteString(w, `{"error":{"message":"quota exceeded"}}`)
|
|
return
|
|
}
|
|
switch r.URL.Path {
|
|
case "/v1/embeddings":
|
|
io.WriteString(w, `{"object":"list","data":[{"object":"embedding","index":0,"embedding":[0.5,0.25]}],"model":"embed-1","usage":{"prompt_tokens":1,"total_tokens":1}}`)
|
|
case "/v1/rerank":
|
|
io.WriteString(w, `{"results":[{"index":1,"relevance_score":0.75}]}`)
|
|
case "/v1/audio/speech":
|
|
w.Header().Set("Content-Type", "audio/mpeg")
|
|
w.Write([]byte("ID3 kind speech"))
|
|
case "/v1/audio/transcriptions":
|
|
io.WriteString(w, `{"text":"heard by kind"}`)
|
|
default:
|
|
w.WriteHeader(404)
|
|
io.WriteString(w, `{"error":{"message":"no route `+r.URL.Path+` `+string(b)+`"}}`)
|
|
}
|
|
}))
|
|
t.Cleanup(v.Close)
|
|
return v
|
|
}
|
|
|
|
func (v *kindVendor) log() []string {
|
|
v.mu.Lock()
|
|
defer v.mu.Unlock()
|
|
return append([]string(nil), v.asked...)
|
|
}
|
|
|
|
// #5339 (XYenon on Discord): a model of an API other than chat that two
|
|
// providers serve under one id is a group, found as a chat model's is,
|
|
// asked for by its id on its API's route or by the group's own id: the
|
|
// first provider out of quota passes the request to the next. The models
|
|
// come from the providers' fetched lists, which kept them apart from the
|
|
// chat models (an embedding model was dropped from the list before, so
|
|
// "embed-1" was a 404). None of it is offered to agents: /v1/models and
|
|
// the groups list have neither the models nor their groups. A group is of
|
|
// its own API: an embedding model's id on /v1/rerank is no reranker.
|
|
func TestKindGroupsServeEachAPIByRoute(t *testing.T) {
|
|
fresh(t)
|
|
busy, free := newKindVendor(t, true), newKindVendor(t, false)
|
|
// both have their chat model picked, as most providers do
|
|
saveFetched(t, provider.Provider{ID: "busy", Name: "Busy", Chat: busy.URL + "/v1", Key: "k", Models: []string{"chat-model"}})
|
|
saveFetched(t, provider.Provider{ID: "free", Name: "Free", Chat: free.URL + "/v1", Key: "k", Models: []string{"chat-model"}})
|
|
s := New()
|
|
|
|
if code, raw := postRetrieval(t, s, "/v1/embeddings", `{"model":"embed-1","input":"hi"}`); code != 200 || !strings.Contains(raw, `[0.5,0.25]`) {
|
|
t.Fatalf("embeddings by the model's id: %d %s", code, raw)
|
|
}
|
|
if code, raw := postRetrieval(t, s, "/v1/embeddings", `{"model":"group/auto-embedding:embed-1","input":"hi"}`); code != 200 || !strings.Contains(raw, `[0.5,0.25]`) {
|
|
t.Fatalf("embeddings by the group's id: %d %s", code, raw)
|
|
}
|
|
if code, raw := postRetrieval(t, s, "/v1/rerank", `{"model":"bge-reranker-1","query":"q","documents":["a","b"]}`); code != 200 || !strings.Contains(raw, `"relevance_score":0.75`) {
|
|
t.Fatalf("rerank: %d %s", code, raw)
|
|
}
|
|
if code, _, raw := postSpeech(t, s, "/v1/audio/speech", `{"model":"tts-1","input":"hello","voice":"alloy"}`); code != 200 || string(raw) != "ID3 kind speech" {
|
|
t.Fatalf("speech: %d %.200s", code, raw)
|
|
}
|
|
body, ctype := audioFormBody(t, "model", "whisper-1")
|
|
if code, _, raw := postAudio(t, s, "/v1/audio/transcriptions", ctype, body); code != 200 || !strings.Contains(raw, "heard by kind") {
|
|
t.Fatalf("transcription: %d %s", code, raw)
|
|
}
|
|
want := "GET /v1/models,POST /v1/embeddings,POST /v1/embeddings,POST /v1/rerank,POST /v1/audio/speech,POST /v1/audio/transcriptions"
|
|
if got := strings.Join(busy.log(), ","); got != want {
|
|
t.Fatalf("the busy provider was asked %s, want %s", got, want)
|
|
}
|
|
|
|
// a group is of its own API's route alone
|
|
for _, c := range []struct{ path, body string }{
|
|
{"/v1/rerank", `{"model":"embed-1","query":"q","documents":["a"]}`},
|
|
{"/v1/rerank", `{"model":"group/auto-embedding:embed-1","query":"q","documents":["a"]}`},
|
|
{"/v1/embeddings", `{"model":"tts-1","input":"hi"}`},
|
|
} {
|
|
if code, raw := postRetrieval(t, s, c.path, c.body); code != 404 {
|
|
t.Fatalf("%s %s: %d %s, want 404", c.path, c.body, code, raw)
|
|
}
|
|
}
|
|
|
|
// none of it is offered as a model to talk to
|
|
rec := httptest.NewRecorder()
|
|
s.Handler().ServeHTTP(rec, httptest.NewRequest("GET", "/v1/models", nil))
|
|
for _, id := range []string{"embed-1", "bge-reranker-1", "tts-1", "whisper-1", "auto-embedding", "moderation"} {
|
|
if strings.Contains(rec.Body.String(), id) {
|
|
t.Fatalf("/v1/models lists %s: %s", id, rec.Body.String())
|
|
}
|
|
}
|
|
for _, g := range provider.Groups() {
|
|
if strings.Contains(g.ID, ":") || strings.Contains(g.ID, "embed") || strings.Contains(g.ID, "tts") {
|
|
t.Fatalf("the groups list has %s", g.ID)
|
|
}
|
|
}
|
|
for _, e := range provider.Catalog() {
|
|
for _, id := range []string{"embed", "rerank", "tts", "whisper", "moderation"} {
|
|
if strings.Contains(e.ID, id) || strings.Contains(e.Model, id) {
|
|
t.Fatalf("agents are offered %s (%s)", e.ID, e.Model)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// A video model two providers list under one id is a group by that id on
|
|
// /v1/videos: the first that can't make it passes it to the next. Before,
|
|
// the bare id went to the first provider listing it and ended there.
|
|
func TestVideoKindGroupByTheModelsID(t *testing.T) {
|
|
fresh(t)
|
|
a, b := newOpenAIVideos(t), newOpenAIVideos(t)
|
|
a.lacks = "sk-a"
|
|
saveFetched(t, provider.Provider{ID: "a", Name: "A", Chat: a.URL + "/v1", Key: "sk-a", Models: []string{"gpt-5.5"}})
|
|
saveFetched(t, provider.Provider{ID: "b", Name: "B", Chat: b.URL + "/v1", Key: "sk-b", Models: []string{"gpt-5.5"}})
|
|
code, obj, raw := asKey(t, New().Handler(), "", "POST", "/v1/videos", `{"model":"vid-1","prompt":"a cat on a fence"}`)
|
|
if code != 200 || obj["model"] != "b/vid-1" {
|
|
t.Fatalf("video: %d %s", code, raw)
|
|
}
|
|
if got := strings.Join(a.log(), ","); !strings.Contains(got, "POST /v1/videos sk-a") {
|
|
t.Fatalf("the first provider was never asked: %v", a.log())
|
|
}
|
|
}
|