diff --git a/internal/gateway/classify.go b/internal/gateway/classify.go index 9d3cfb152..17f807fbe 100644 --- a/internal/gateway/classify.go +++ b/internal/gateway/classify.go @@ -403,19 +403,60 @@ func readIntent(answer string, intents []string) (string, error) { return intents[n-1], nil } -// classifyEffort is the least reasoning model takes: none when it can go -// without, else its lowest level; "" when its levels aren't known, which -// leaves it to the vendor. +// classifyEffort is the reasoning model is asked to classify at: none, +// reasoning off, which each request fits to what its model and wire take +// (tangtang969 on X: a classifier that doesn't think answers sooner). +// Anthropic's API is sent thinking disabled and Gemini's its off budget, +// or least level, where the lowest level on the model's list asked them to +// think before; a Chat or Responses model without none is asked its +// lowest (fitFor, effortOf), and one that turns off away is asked again at +// its lowest and remembered (forwardTranslated, passthrough). A model +// whose levels aren't known is asked for none too, as auto mode's +// classifier is (fitAutoModeClassifier): left to its vendor, one that +// reasons did so at its default. "" for a model the user said takes no +// reasoning ask (Reasonless), and for one that isn't a provider's model (a +// routing group), left to it. func classifyEffort(model string) string { p, m, ok := provider.Resolve(model) - if !ok { + if !ok || provider.Reasonless(p.ID, m) { return "" } - levels := p.Efforts(m) - if len(levels) == 0 { - return "" + return "none" +} + +// classifierEffortField is the classifier's effort (classifyEffort) +// remembered in unfit for a model whose upstream turned the field away. +func classifierEffortField(model string) string { return "classifier effort\x00" + model } + +// askLeast asks model the classifier's question, body at the effort +// classifyEffort gives it. An upstream that turns the effort away — a +// relay that knows no reasoning_effort, one that takes none of the +// levels — is asked again without it, as before, and from then on: the +// classifier answers wherever it did. +func (s *Server) askLeast(wait time.Duration, model string, body func(effort string) []byte) (string, error) { + effort := classifyEffort(model) + p, m, ok := provider.Resolve(model) + if effort != "" && ok && !s.fits(p.ID, classifierEffortField(m), provider.Chat) { + effort = "" } - return fitEffort("none", levels) + a, err := s.askChat(wait, model, body(effort)) + var f chatFailed + if effort == "" || !errors.As(err, &f) { + return a, err + } + if refused, _ := ownEffortRefused(f.code, f.body, effort); !refused { + return a, err + } + s.markUnfit(p.ID, classifierEffortField(m), provider.Chat) + return s.askChat(wait, model, body("")) +} + +// chatFailed is a classifier's answer that was an error, with its status +// and body, for askLeast to read. +type chatFailed struct { + error + code int + body []byte } // askWaited is the classifier a request's routing groups ask, each @@ -472,7 +513,7 @@ func (s *Server) askClassifierIn(wait time.Duration, model string, intents []str go func() { defer wg.Done() var a string - if a, ierr = s.askChat(wait, model, classifyBody(model, classifyEffort(model), intents, prev, text)); ierr == nil { + if a, ierr = s.askLeast(wait, model, func(effort string) []byte { return classifyBody(model, effort, intents, prev, text) }); ierr == nil { v.Intent, ierr = readIntent(a, intents) } }() @@ -482,7 +523,7 @@ func (s *Server) askClassifierIn(wait time.Duration, model string, intents []str go func() { defer wg.Done() var a string - if a, eerr = s.askChat(wait, model, effortBody(model, classifyEffort(model), prev, text)); eerr == nil { + if a, eerr = s.askLeast(wait, model, func(effort string) []byte { return effortBody(model, effort, prev, text) }); eerr == nil { v.Effort, v.Score, eerr = readEffort(a) } }() @@ -586,7 +627,7 @@ func (s *Server) askChat(wait time.Duration, model string, body []byte) (string, if out.Error != nil && out.Error.Message != "" { msg = out.Error.Message } - return "", fmt.Errorf("%s: %s", model, msg) + return "", chatFailed{fmt.Errorf("%s: %s", model, msg), w.Code, w.Body.Bytes()} } if len(out.Choices) == 0 { return "", answerError{fmt.Errorf("%s gave no answer", model)} diff --git a/internal/gateway/classify_test.go b/internal/gateway/classify_test.go index 31744eb2c..537f4e53a 100644 --- a/internal/gateway/classify_test.go +++ b/internal/gateway/classify_test.go @@ -382,12 +382,13 @@ func TestIntentOddAnswerDoesNotRest(t *testing.T) { } // A classifier that reasons is asked to reason least, with room for its -// reasoning and the number; one whose levels aren't known is asked plainly. +// reasoning and the number; one whose levels aren't known is asked for +// none (TestClassifierAsksThinkingOff). func TestIntentClassifierEffort(t *testing.T) { s, _, _, c := intented(t, provider.Rule{Use: "b/big", Intent: "refactoring"}) c.set("1", 0) postOK(t, s, "e1", chat("rename this package", nil, 0, "")) - if asked := c.last(); strings.Contains(asked, "reasoning_effort") || !strings.Contains(asked, `"max_tokens":2048`) { + if asked := c.last(); !strings.Contains(asked, `"reasoning_effort":"none"`) || !strings.Contains(asked, `"max_tokens":2048`) { t.Fatalf("plain: %s", asked) } p, _ := provider.Find("c") diff --git a/internal/gateway/classify_think_off_test.go b/internal/gateway/classify_think_off_test.go new file mode 100644 index 000000000..6fd292ce4 --- /dev/null +++ b/internal/gateway/classify_think_off_test.go @@ -0,0 +1,238 @@ +package gateway + +import ( + "io" + "net/http" + "net/http/httptest" + "strings" + "sync" + "testing" + + "github.com/tidwall/gjson" + "github.com/yetone/magpie/internal/catalog" + "github.com/yetone/magpie/internal/provider" +) + +// thinkUp is a vendor answering the routing classifier "1" on Chat, +// Anthropic's Messages and Gemini's API, keeping each body it was sent; +// refuse, when it returns a message, answers 400 with it. +type thinkUp struct { + mu sync.Mutex + bodies []string + refuse func(body string) string +} + +func (u *thinkUp) ServeHTTP(w http.ResponseWriter, r *http.Request) { + b, _ := io.ReadAll(r.Body) + u.mu.Lock() + u.bodies = append(u.bodies, string(b)) + refuse := u.refuse + u.mu.Unlock() + if refuse != nil { + if msg := refuse(string(b)); msg != "" { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(400) + io.WriteString(w, msg) + return + } + } + stream := gjson.GetBytes(b, "stream").Bool() + switch p := r.URL.Path; { + case strings.HasSuffix(p, "/messages") && stream: + w.Header().Set("Content-Type", "text/event-stream") + io.WriteString(w, "event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"id\":\"msg_1\",\"type\":\"message\",\"role\":\"assistant\",\"model\":\"m\",\"content\":[],\"usage\":{\"input_tokens\":1,\"output_tokens\":0}}}\n\n"+ + "event: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"text\",\"text\":\"\"}}\n\n"+ + "event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"1\"}}\n\n"+ + "event: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\n"+ + "event: message_delta\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"end_turn\"},\"usage\":{\"output_tokens\":1}}\n\n"+ + "event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n") + case strings.HasSuffix(p, "/messages"): + w.Header().Set("Content-Type", "application/json") + io.WriteString(w, `{"id":"msg_1","type":"message","role":"assistant","model":"m","content":[{"type":"text","text":"1"}],"stop_reason":"end_turn","usage":{"input_tokens":1,"output_tokens":1}}`) + case strings.Contains(p, ":streamGenerateContent"): + w.Header().Set("Content-Type", "text/event-stream") + io.WriteString(w, sse(`data: {"candidates":[{"content":{"role":"model","parts":[{"text":"1"}]},"finishReason":"STOP"}],"usageMetadata":{"promptTokenCount":1,"candidatesTokenCount":1,"totalTokenCount":2}}`)) + case strings.Contains(p, ":generateContent"): + w.Header().Set("Content-Type", "application/json") + io.WriteString(w, `{"candidates":[{"content":{"role":"model","parts":[{"text":"1"}]},"finishReason":"STOP"}],"usageMetadata":{"promptTokenCount":1,"candidatesTokenCount":1,"totalTokenCount":2}}`) + case stream: + w.Header().Set("Content-Type", "text/event-stream") + io.WriteString(w, sse(`data: {"id":"c","choices":[{"index":0,"delta":{"role":"assistant","content":"1"}}]}`, + `data: {"id":"c","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}`, `data: [DONE]`)) + default: + w.Header().Set("Content-Type", "application/json") + io.WriteString(w, `{"id":"c","choices":[{"index":0,"message":{"role":"assistant","content":"1"},"finish_reason":"stop"}],"usage":{"prompt_tokens":5,"completion_tokens":1}}`) + } +} + +func (u *thinkUp) last() string { + u.mu.Lock() + defer u.mu.Unlock() + if len(u.bodies) == 0 { + return "" + } + return u.bodies[len(u.bodies)-1] +} + +func (u *thinkUp) count() int { + u.mu.Lock() + defer u.mu.Unlock() + return len(u.bodies) +} + +// thinkVendor saves p, served by a new thinkUp, with the levels given +// for model on its list (none when levels is nil). +func thinkVendor(t *testing.T, p provider.Provider, model string, levels []string) *thinkUp { + t.Helper() + u := &thinkUp{} + srv := httptest.NewServer(u) + t.Cleanup(srv.Close) + base := srv.URL + switch { + case p.Anthropic == "x": + p.Anthropic = base + case p.Gemini == "x": + p.Gemini, base = base+"/v1beta", base+"/v1beta" + default: + p.Chat, base = base+"/v1", base+"/v1" + } + p.Name, p.Key, p.Models = strings.ToUpper(p.ID), "k", []string{model} + if err := provider.Save(p); err != nil { + t.Fatal(err) + } + if err := catalog.SaveLive(p.ID, base, []catalog.Model{{ID: model, Context: 32000, Efforts: levels}}); err != nil { + t.Fatal(err) + } + if got, _ := provider.Find(p.ID); len(got.Efforts(model)) != len(levels) { + t.Fatalf("%s levels %v, want %v", model, got.Efforts(model), levels) + } + return u +} + +// classifyOnce asks s's routing classifier, model, which of two kinds a +// message is, and fails unless it answers the first. +func classifyOnce(t *testing.T, s *Server, model string) { + t.Helper() + v, err := s.askClassifier(model, []string{"refactoring", "tests"}, before{}, false, "rename this package") + if err != nil || v.Intent != "refactoring" { + t.Fatalf("%s: %+v %v", model, v, err) + } +} + +// tangtang969 on X: "classifier的模型调用可以加上不带思考的参数么?提速效果 +// 比较好". The routing classifier asks its model to reason not at all +// where its wire can say so: Anthropic's API thinking disabled where the +// model's lowest level asked it to think at low, Gemini 2.5 Flash its off +// budget, 0, where it went at low's 4096, and a Chat model whose levels +// aren't known effort none, where it was left to think at its vendor's +// default. +func TestClassifierAsksThinkingOff(t *testing.T) { + for _, c := range []struct { + name string + p provider.Provider + model string + levels []string + check func(body string) bool + }{ + {"anthropic", provider.Provider{ID: "an", Anthropic: "x"}, "claude-sonnet-4-5", []string{"low", "medium", "high"}, + func(b string) bool { + return gjson.Get(b, "thinking").Raw == `{"type":"disabled"}` && !gjson.Get(b, "output_config").Exists() + }}, + {"gemini 2.5 flash", provider.Provider{ID: "gm", Gemini: "x"}, "gemini-2.5-flash", []string{"low", "medium", "high"}, + func(b string) bool { + return gjson.Get(b, "generationConfig.thinkingConfig").Raw == `{"thinkingBudget":0}` + }}, + {"chat, levels unknown", provider.Provider{ID: "rl"}, "gpt6.1-sol", nil, + func(b string) bool { return gjson.Get(b, "reasoning_effort").String() == "none" }}, + } { + t.Run(c.name, func(t *testing.T) { + fresh(t) + u := thinkVendor(t, c.p, c.model, c.levels) + classifyOnce(t, New(), c.p.ID+"/"+c.model) + if !c.check(u.last()) { + t.Fatalf("sent %s", u.last()) + } + }) + } +} + +// What each vendor takes is unchanged: a Chat model with levels and no +// none is asked its lowest, one with none none, a model the user said +// takes no reasoning ask is sent none at all, and Gemini 2.5 Pro, which +// can't stop thinking, its least budget. A relay that turns the effort +// away still classifies, asked again without it. +func TestClassifierThinkingAsEachVendorTakes(t *testing.T) { + for _, c := range []struct { + name string + p provider.Provider + model string + levels []string + reasonless bool + check func(body string) bool + }{ + {"chat lowest", provider.Provider{ID: "lo"}, "cls", []string{"low", "high", "max"}, false, + func(b string) bool { return gjson.Get(b, "reasoning_effort").String() == "low" }}, + {"chat minimal", provider.Provider{ID: "mi"}, "cls", []string{"minimal", "medium"}, false, + func(b string) bool { return gjson.Get(b, "reasoning_effort").String() == "minimal" }}, + {"chat none", provider.Provider{ID: "no"}, "cls", []string{"none", "low"}, false, + func(b string) bool { return gjson.Get(b, "reasoning_effort").String() == "none" }}, + {"reasonless", provider.Provider{ID: "rs"}, "cls", nil, true, + func(b string) bool { + return !gjson.Get(b, "reasoning_effort").Exists() && !gjson.Get(b, "reasoning").Exists() + }}, + {"gemini 2.5 pro", provider.Provider{ID: "gp", Gemini: "x"}, "gemini-2.5-pro", []string{"low", "medium", "high"}, false, + func(b string) bool { + return gjson.Get(b, "generationConfig.thinkingConfig").Raw == `{"thinkingBudget":128}` + }}, + } { + t.Run(c.name, func(t *testing.T) { + fresh(t) + u := thinkVendor(t, c.p, c.model, c.levels) + if c.reasonless { + if err := provider.SetModelReasonless(c.p.ID+"/"+c.model, true); err != nil { + t.Fatal(err) + } + } + classifyOnce(t, New(), c.p.ID+"/"+c.model) + if !c.check(u.last()) { + t.Fatalf("sent %s", u.last()) + } + }) + } + + t.Run("effort turned away", func(t *testing.T) { + fresh(t) + u := thinkVendor(t, provider.Provider{ID: "rf"}, "gpt6.1-sol", nil) + u.refuse = func(b string) string { + if gjson.Get(b, "reasoning_effort").Exists() { + return `{"error":{"message":"Unrecognized request argument supplied: reasoning_effort","type":"invalid_request_error"}}` + } + return "" + } + s := New() + for i := range 2 { + n := u.count() + classifyOnce(t, s, "rf/gpt6.1-sol") + if gjson.Get(u.last(), "reasoning_effort").Exists() { + t.Fatalf("ask %d answered with %s", i, u.last()) + } + if i == 1 && u.count()-n != 1 { + t.Errorf("the next ask took %d requests, want 1", u.count()-n) + } + } + }) +} + +// The Image recognition model's own level is left as it was: the least +// of its levels, none asked of one whose levels aren't known. +func TestVisionEffortUnchangedByClassifier(t *testing.T) { + fresh(t) + thinkVendor(t, provider.Provider{ID: "vu"}, "seer", nil) + thinkVendor(t, provider.Provider{ID: "vl"}, "seer", []string{"low", "high"}) + if e := visionEffort("vu/seer"); e != "" { + t.Errorf("unknown levels: %q", e) + } + if e := visionEffort("vl/seer"); e != "low" { + t.Errorf("low, high: %q", e) + } +} diff --git a/internal/gateway/codeassist.go b/internal/gateway/codeassist.go index eef937519..f84041e79 100644 --- a/internal/gateway/codeassist.go +++ b/internal/gateway/codeassist.go @@ -761,6 +761,14 @@ func thinkingConfig(r *Request, model string, claude bool, at string) map[string } return tc } + if off, _, _, ok := vertexBudgets(strings.TrimPrefix(m, "models/")); ok && r.ThinkOff && !claude { + // reasoning turned off (a Chat client's effort none, the routing + // classifier's ask) goes as 2.5's off budget, as on Vertex AI: + // 0 for Flash and Flash-Lite, Pro's least, 128. It went as low's + // 4096 (tangtang969 on X: a classifier that doesn't think is + // faster) + return map[string]any{"thinkingBudget": off} + } budget := budgetOf(effortOf(r.Effort)) if claude && r.MaxTokens > 0 && budget >= r.MaxTokens { budget = r.MaxTokens - 1 diff --git a/internal/gateway/side_effort.go b/internal/gateway/side_effort.go index ab31a0960..5e3d32cef 100644 --- a/internal/gateway/side_effort.go +++ b/internal/gateway/side_effort.go @@ -34,12 +34,27 @@ func sideEffort(model, want string) string { // visionEffort is the level the Image recognition model is asked to read // an image at: the one picked in Settings, else magpie's own, the least -// it has (classifyEffort), as a description is wanted quickly. +// it has (leastEffort), as a description is wanted quickly. func visionEffort(model string) string { if e := sideEffort(model, provider.HeldSettings().VisionEffort); e != "" { return e } - return classifyEffort(model) + return leastEffort(model) +} + +// leastEffort is the least reasoning model takes: none when it can go +// without, else its lowest level; "" when its levels aren't known, which +// leaves it to the vendor. +func leastEffort(model string) string { + p, m, ok := provider.Resolve(model) + if !ok { + return "" + } + levels := p.Efforts(m) + if len(levels) == 0 { + return "" + } + return fitEffort("none", levels) } // VisionEffortNow is the level the Image recognition model in use is