A Discord user's POST /v1/decisions was answered 404 ("magpie serves
/v1/chat/completions, ..."). It is OpenAI's Decisions API (gpt-6-luna):
one input, up to 400 questions, typed answers, input tokens billed only.
The shape was taken from openai-python (resources/decisions.py,
types/decision_create_params.py, types/decision.py, added in #4033 on
10-06) and the Decisions guide, not guessed: POST /v1/decisions, a JSON
body of model, input (a string or messages/items with input_text,
input_image as a data or http(s) URL, JSON parts; function and custom
tool calls and their outputs), questions (predicate / choice / score),
and optional prompt_cache_key, prompt_cache_options, safety_identifier.
The answer is {answers, model, usage}, not streamed; usage.input_tokens
includes input_tokens_details.cached_tokens and cache_write_tokens. The
Azure client sends it on the v1 API (/openai/v1/decisions), the model in
the body.
- decide (decisions.go) goes through serveRetrieval as embeddings and
rerank do, body as sent with only model changed. So a disabled model,
a key's models, accounts and spent limits, pause rules, keys/accounts
of the provider, masking and the Routing page hold here as on every
route.
- Only a provider canDecide takes is sent it: an OpenAI API key on
api.openai.com, Azure OpenAI (IsAzure, at its OpenAIBase, so
/openai/v1/decisions with api-key), or another magpie. Anyone else (a
relay, a plugin, a ChatGPT sign-in) gets a 400 that says who serves
it, checked in serveRetrieval's try loop beside the no-OpenAI-API
check (refusingRetrieval), never the vendor's 404; a group moves on.
- A bare gpt-6-luna is a kind group "decision" (servesDecision), so a
relay that lists the same id is never its member.
- Usage is recorded as "decisions" (countedRetrieval): input less the
cached and cache-written tokens, those as CacheRead/CacheWrite, and
no output, so gpt-6-luna's own models.dev rates price it as OpenAI
bills ($0.10/1M input, cache read/write at their rates).
- /decisions joins the one-port client prefixes, another magpie's URL
suffixes, the 404's route list and info's apis. The readonly, LAN
and CORS guard tests list the route.
- native_endpoints is left alone: it names the chat APIs a model is
relayed on (Cursor's names, a remote's API choice).
- docs: gateway-routing.md has a Decisions row; reference.md a section.
Run on mote at this tree (mote-run): gofmt -l clean; go vet of gateway
and provider; GOOS=linux go vet -tags nogui ./...; GOOS=windows go vet
./...; go build ./..., linux nogui and windows builds ok;
go test -count=1 -timeout 45m ./internal/gateway ./internal/provider
./internal/usage: ok (gateway 413s, provider 88s, usage 18s).
Red on the old code (e0930b1de, the new tests copied in): every
TestDecisions* is red with magpie's 404.
Broken by hand, each red: refuses always "" (the relay is sent it:
TestDecisionsRefusedWhereNotServed); the cache split not read
(Input 1200, CacheRead 0: TestDecisionsReachOpenAIAndAzure); canDecide
without the api.openai.com check (TestCanDecide, the relay sent it);
/v1/decisions out of the remote suffixes (TestRemoteMagpie).
Not tried: a real OpenAI Decisions request, or a real Azure resource.
Tests reach only httptest servers (OpenAI's host through the provider's
proxy).
125 lines
4.6 KiB
Go
125 lines
4.6 KiB
Go
package gateway
|
|
|
|
import (
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// mediaMembers is the routing group ref with its members ready now for
|
|
// an API that is no chat, kind's (catalog.Serves*, provider.KindVideo):
|
|
// embeddings, rerank, speech, transcriptions, videos and decisions.
|
|
//
|
|
// A group the user made: a provider's picks are of its chat models, and
|
|
// these models are in no chat list (a fetched list keeps them apart,
|
|
// catalog.Model's Serves, Draws and Films), so a member of a provider
|
|
// with picks was dropped as unpicked and a group of them was a 404
|
|
// (XYenon on Discord). Such a member is kept, as a drawing's image models
|
|
// are (FindDrawingGroup, jzw690 on X). A chat model the user unticked
|
|
// (#3911) is on its provider's list and still stays out.
|
|
//
|
|
// A group magpie finds (#5339): the providers that serve a model of kind
|
|
// under exactly the same id, asked for by the group's id
|
|
// ("group/auto-embedding:embed-1") or by the model's own on its API's
|
|
// route ("embed-1" on /v1/embeddings) when more than one provider serves
|
|
// it (provider.KindGroup). It is in no list of groups, so agents, keys
|
|
// and Routing never see it.
|
|
func mediaMembers(ref, kind string) (provider.Group, []provider.Member, bool) {
|
|
if provider.IsGroupRef(ref) {
|
|
if g, ms, ok := provider.KindGroup(kind, ref, servesKind(kind)); ok {
|
|
return g, ms, true
|
|
}
|
|
return provider.FindGroupAlso(ref, offChatList)
|
|
}
|
|
if !bareRef(ref) {
|
|
return provider.Group{}, nil, false
|
|
}
|
|
if g, ms, ok := provider.KindGroup(kind, ref, servesKind(kind)); ok && len(ms) > 1 {
|
|
return g, ms, true
|
|
}
|
|
return provider.Group{}, nil, false
|
|
}
|
|
|
|
// bareRef is whether ref names no provider: "embed-1", or a vendor's id
|
|
// with a slash of its own ("BAAI/bge-m3") where no provider has the part
|
|
// before it as its id.
|
|
func bareRef(ref string) bool {
|
|
pid, _, ok := strings.Cut(strings.TrimSpace(ref), "/")
|
|
if !ok {
|
|
return true
|
|
}
|
|
_, err := provider.Find(pid)
|
|
return err != nil
|
|
}
|
|
|
|
// servesKind says whether a provider serves a model of kind's under that
|
|
// id: its fetched list says so (catalog.LiveServing; Videomakers for
|
|
// videos), its chat list has an id that says so (another magpie's list,
|
|
// which keeps them as it lists them), or, for embeddings and rerank, its
|
|
// preset names it (PlanRetrieval).
|
|
func servesKind(kind string) func(p provider.Provider, model string) bool {
|
|
is := func(m catalog.Model, model string) bool { return m.ID == model }
|
|
if kind == kindDecision {
|
|
return servesDecision
|
|
}
|
|
if kind == provider.KindVideo {
|
|
return func(p provider.Provider, model string) bool {
|
|
return slices.ContainsFunc(Videomakers(p), func(m catalog.Model) bool { return is(m, model) })
|
|
}
|
|
}
|
|
return func(p provider.Provider, model string) bool {
|
|
if slices.ContainsFunc(catalog.LiveServing(p.ID, kind), func(m catalog.Model) bool { return is(m, model) }) {
|
|
return true
|
|
}
|
|
if catalog.ServesOf(model) == kind && slices.ContainsFunc(provider.KeptListing(p).Available, func(m catalog.Model) bool { return is(m, model) }) {
|
|
return true
|
|
}
|
|
return slices.ContainsFunc(p.PlanRetrieval(kind), func(m catalog.Model) bool { return is(m, model) })
|
|
}
|
|
}
|
|
|
|
// retrievalKind is the kind of model an operation of serveRetrieval's is
|
|
// asked of.
|
|
func retrievalKind(operation string) string {
|
|
switch operation {
|
|
case "rerank":
|
|
return catalog.ServesRerank
|
|
case "speech":
|
|
return catalog.ServesSpeech
|
|
case "transcriptions", "translations":
|
|
return catalog.ServesTranscription
|
|
case "decisions":
|
|
return kindDecision
|
|
}
|
|
return catalog.ServesEmbedding
|
|
}
|
|
|
|
// offChatList is whether p's list of models to chat with lacks model:
|
|
// one made for another API, which no pick can name.
|
|
func offChatList(p provider.Provider, model string) bool {
|
|
return !slices.ContainsFunc(provider.KeptListing(p).Available, func(m catalog.Model) bool { return m.ID == model })
|
|
}
|
|
|
|
// mediaGroupFirst is the model ref names for an API that is no chat,
|
|
// kind's: a group's first member (mediaMembers), or the one provider that
|
|
// serves a bare id of that kind (provider.KindGroup). Such a group or
|
|
// model is none agents are offered, so Resolve, which reads their
|
|
// catalog, doesn't take it.
|
|
func mediaGroupFirst(ref, kind string) (provider.Provider, string, bool) {
|
|
if !provider.IsGroupRef(ref) && !bareRef(ref) {
|
|
return provider.Provider{}, "", false
|
|
}
|
|
if _, ms, ok := mediaMembers(ref, kind); ok && len(ms) > 0 {
|
|
return ms[0].Provider, ms[0].Model, true
|
|
}
|
|
if provider.IsGroupRef(ref) {
|
|
return provider.Provider{}, "", false
|
|
}
|
|
if _, ms, ok := provider.KindGroup(kind, ref, servesKind(kind)); ok {
|
|
return ms[0].Provider, ms[0].Model, true
|
|
}
|
|
return provider.Provider{}, "", false
|
|
}
|