Files
magpie/internal/gateway/deepseek_output_effort_test.go
T
yetone e2543575a9 gateway: DeepSeek's own Anthropic endpoint is asked its effort in output_config (#3298 item 4)
DSH on magpie's magpie-anthropic route asks by thinking.budget_tokens
(pi-ai's anthropic-messages default). DeepSeek's Anthropic endpoint
ignores the budget, so every level ran at DeepSeek's default, high.
effortInOutputConfig matched only GLM-5.2/5.3 and MiniMax-M3.1-Flash.

What DeepSeek documents (read 2026-10-23):
- https://api-docs.deepseek.com/guides/anthropic_api, "Simple Fields":
  thinking "Supported (budget_tokens is ignored)"; output_config "Only
  effort is supported".
- https://api-docs.deepseek.com/guides/thinking_mode, "Thinking Mode
  Toggle and Effort Control": the Anthropic format's effort control is
  {"output_config": {"effort": "low/high/max"}}; minimal maps to low,
  medium and xhigh to high, ultra to max.

Change: outputEffortAt(host, model) is effortInOutputConfig or the
upstream host being api.deepseek.com. Both places that used the regex
take it: the Messages passthrough (serve) and the translated Messages
body (anthropicBody, which now gets the host from build). On the
passthrough, a model whose levels magpie doesn't know is fitted to
DeepSeek's low/high/max, so a 32000 budget asks max rather than xhigh
(which DeepSeek reads as high). Another host serving a DeepSeek model
(a relay) is not covered by those docs and goes as before, and so does
every other vendor's model.

Tests: TestDeepSeekAnthropicEffortInOutputConfig goes through the
server stack with the upstream dialled to a fake at api.deepseek.com:
budgets 4096/10000/32000 ask low/high/max, the budget still goes as
sent; deepseek-flash on a relay host and kimi-k2.5 on api.moonshot.ai
get no output_config. TestTranslatedDeepSeekAnthropicEffort does the
same for build(). Broken by hand: with the host clause removed, 5 cases
red ("output_config.effort \"\", want \"low\"" ...); with the rule widened
to any model naming deepseek or kimi and the level fit removed, 7 cases
red (relay and kimi got "xhigh"/"max", budget 32000 got "xhigh", 10000
"medium").

Run at 5adad1877 (this commit plus the next, item 3; same tree as both
pushed): gofmt -l (nothing), go vet ./..., GOOS=linux go vet -tags
nogui ./..., GOOS=windows go vet ./... (all clean), go build for
darwin, linux (nogui) and windows (ok), and go test -tags nogui
-timeout 45m ./...: all 69 packages ok, exit 0 (gateway 505s, agent
401s, provider 197s).

Not tried: a real DeepSeek key against api.deepseek.com/anthropic, and a
real DSH session on the magpie-anthropic route; the budget bodies in the
test are shaped like pi-ai's, not captured from it. output_config.format
(a JSON schema), which DeepSeek's page says it doesn't support, is
unchanged.
2026-10-23 01:20:37 +08:00

105 lines
4.1 KiB
Go

package gateway
import (
"context"
"encoding/json"
"net"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// sentOutputEffort is the output_config.effort a Messages body asks for.
func sentOutputEffort(t *testing.T, body []byte) string {
t.Helper()
var q struct {
OutputConfig struct {
Effort string `json:"effort"`
} `json:"output_config"`
}
if err := json.Unmarshal(body, &q); err != nil {
t.Fatalf("%v: %s", err, body)
}
return q.OutputConfig.Effort
}
// DeepSeek's Anthropic endpoint ignores budget_tokens and takes the
// effort in output_config.effort alone (api-docs.deepseek.com,
// guides/anthropic_api and guides/thinking_mode): a client that asks by
// budget, as DSH's magpie-anthropic route does through pi-ai, has its
// level said there too, fitted to DeepSeek's low, high and max (#3298).
// The same model behind another host, and another vendor's model, go as
// they were sent.
func TestDeepSeekAnthropicEffortInOutputConfig(t *testing.T) {
f := &fake{t: t, ctype: "application/json", reply: anthropicWholeOK}
setup(t, provider.Anthropic, f)
up := httptest.NewServer(f)
t.Cleanup(up.Close)
for _, p := range []provider.Provider{
{ID: "deepseek", Name: "DeepSeek", Key: "k", Models: []string{"deepseek-flash", "deepseek-v4-pro"}, Anthropic: "http://api.deepseek.com/anthropic"},
{ID: "relay", Name: "Relay", Key: "k", Models: []string{"deepseek-flash"}, Anthropic: "http://relay.example.com/anthropic"},
{ID: "kimi", Name: "Kimi", Key: "k", Models: []string{"kimi-k2.5"}, Anthropic: "http://api.moonshot.ai/anthropic"},
} {
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
}
s := New()
addr := strings.TrimPrefix(up.URL, "http://")
s.client = &http.Client{Transport: &http.Transport{DialContext: func(ctx context.Context, network, _ string) (net.Conn, error) {
return (&net.Dialer{}).DialContext(ctx, network, addr)
}}}
const body = `{"model":"%s","max_tokens":64000,"stream":false,"thinking":{"type":"enabled","budget_tokens":%s},"messages":[{"role":"user","content":"hi"}]}`
for _, c := range []struct{ model, budget, want string }{
{"deepseek/deepseek-flash", "4096", "low"},
{"deepseek/deepseek-flash", "10000", "high"},
{"deepseek/deepseek-v4-pro", "32000", "max"},
// not DeepSeek's endpoint: its docs say nothing of a relay's
{"relay/deepseek-flash", "32000", ""},
// another vendor's model on its own Anthropic endpoint
{"kimi/kimi-k2.5", "32000", ""},
} {
f.got = nil
req := strings.Replace(strings.Replace(body, "%s", c.model, 1), "%s", c.budget, 1)
code, reply := postTo(t, s, "/v1/messages", req)
if code != 200 {
t.Fatalf("%s: %d %s", c.model, code, reply)
}
if got := sentOutputEffort(t, f.got); got != c.want {
t.Errorf("%s budget %s: output_config.effort %q, want %q: %s", c.model, c.budget, got, c.want, f.got)
}
if !strings.Contains(string(f.got), `"budget_tokens":`+c.budget) {
t.Errorf("%s: the budget was not sent as it came: %s", c.model, f.got)
}
}
}
// Translated onto DeepSeek's Anthropic endpoint (a Chat client), the
// client's reasoning_effort goes in output_config beside the budget; the
// same model on another host, and another vendor's model on DeepSeek's
// builder path, get the budget alone as before.
func TestTranslatedDeepSeekAnthropicEffort(t *testing.T) {
r := &Request{Thinking: true, Effort: "max", Messages: []Message{{Role: "user", Parts: []Part{{Kind: Text, Text: "hi"}}}}}
for _, c := range []struct{ host, model, want string }{
{"api.deepseek.com", "deepseek-flash", "max"},
{"api.deepseek.com", "deepseek-v4-pro", "max"},
{"relay.example.com", "deepseek-flash", ""},
{"api.moonshot.ai", "kimi-k2.5", ""},
{"", "kimi-k2.5", ""},
} {
out, err := build(provider.Anthropic, r, c.model, c.host, false)
if err != nil {
t.Fatal(err)
}
if got := sentOutputEffort(t, out); got != c.want {
t.Errorf("%s at %q: output_config.effort %q, want %q: %s", c.model, c.host, got, c.want, out)
}
if !strings.Contains(string(out), `"budget_tokens":`) {
t.Errorf("%s at %q: no budget: %s", c.model, c.host, out)
}
}
}