Files
magpie/internal/gateway/deepseek_tokens_test.go
T
ggbdpq 8807f621f2 gateway: a relay's DeepSeek model reads max_tokens as its own (#5030)
deepseekTokenField named a chat request's reply length max_tokens for
the DeepSeek preset and api.deepseek.com alone; a relay on its own
domain serving a DeepSeek model got max_completion_tokens as it came,
DeepSeek behind it ignored it and answered with its default, 8K
without thinking (#5030, NOMOAX) — the gate the tool-schema
normalization's had too (#4852, ethanfly). DeepSeek's quirks are told
by the model named in the body now, as Grok's are (grokModel): a
model a relay names deepseek-whatever gets the field DeepSeek reads,
whoever serves it; another vendor's model through the same relay goes
as it came.

Generated-by: GLM-5.3-Flash (ZCode)

Reviewed and ported. The model's name decides only on a host that isn't
DeepSeek's: chatTokenField runs after deepseekTokenField in forwardOnce,
so OpenAI's own hosts still get max_completion_tokens and a relay whose
400 names max_completion_tokens is still asked that way from then on.
The model test (provider/test.go probe) already sends max_tokens to a
host that isn't OpenAI's, so it needs nothing. The tool-schema \0 gate,
the same narrow gate, is #4852's, open.

Run on mote (darwin arm64, Go 1.26.8): go test -tags nogui -count=1
-run 'TokenField|MaxTokens|MaxCompletionTokens|DeepSeek'
./internal/gateway/ ok.
Broken by hand: deepseek_schema.go back at main, test kept:
TestDeepSeekRelayChatGetsMaxTokens FAIL "upstream got
{...max_completion_tokens:393216...}".
Not tried: a real request through AI Flash API.

Co-authored-by: ggbdpq <ggbdpq@gmail.com>
2026-11-06 05:42:22 +08:00

127 lines
5.1 KiB
Go

package gateway
import (
"encoding/json"
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// A chat client that asks DeepSeek for its reply length OpenAI's way
// (max_completion_tokens, as DSH's pi-ai does for a custom provider) gets
// it as max_tokens, the field DeepSeek reads: before, DeepSeek got the
// field it ignores and answered with its default 8K (#2355, h4rk8s).
// Another vendor's chat API gets the request as the client sent it.
func TestDeepSeekChatGetsMaxTokens(t *testing.T) {
fresh(t)
f := &fake{reply: sse(
`data: {"id":"test","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"ok"}}]}`,
`data: {"id":"test","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}`,
`data: [DONE]`,
)}
up := httptest.NewServer(f)
defer up.Close()
p, err := provider.FromPreset("deepseek")
if err != nil {
t.Fatal(err)
}
p.Key = "fixture-key"
p.Chat = up.URL + "/v1"
p.Models = []string{"deepseek-flash"}
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
other := provider.Provider{ID: "relay", Name: "Relay", Key: "k", Chat: up.URL + "/v1", Models: []string{"m"}}
if err := provider.Save(other); err != nil {
t.Fatal(err)
}
// a message that names the field keeps its text
said := `{"role":"user","content":"what is \"max_completion_tokens\"?"}`
for _, c := range []struct {
model, fields string
maxTokens, maxCompl any
}{
{"deepseek/deepseek-flash", `"max_completion_tokens":393216`, float64(393216), nil},
{"deepseek/deepseek-flash", `"max_completion_tokens" : 5`, float64(5), nil},
{"deepseek/deepseek-flash", `"max_tokens":5`, float64(5), nil},
// both named: DeepSeek reads max_tokens, which goes as it is
{"deepseek/deepseek-flash", `"max_tokens":7,"max_completion_tokens":5`, float64(7), float64(5)},
{"relay/m", `"max_completion_tokens":5`, nil, float64(5)},
} {
body := `{"model":"` + c.model + `","stream":true,` + c.fields + `,"messages":[` + said + `]}`
code, reply := post(t, "/v1/chat/completions", body)
if code != 200 {
t.Fatalf("%s: HTTP %d: %s", c.fields, code, reply)
}
var sent map[string]any
if err := json.Unmarshal(f.got, &sent); err != nil {
t.Fatal(err)
}
if sent["max_tokens"] != c.maxTokens || sent["max_completion_tokens"] != c.maxCompl {
t.Fatalf("%s %s: upstream got %s", c.model, c.fields, f.got)
}
if !strings.Contains(string(f.got), `what is \"max_completion_tokens\"?`) {
t.Fatalf("%s: message text changed: %s", c.fields, f.got)
}
}
}
// A relay that serves DeepSeek's models under a custom domain gets the
// same field DeepSeek's own API reads: before, a request that named the
// reply length OpenAI's way (max_completion_tokens, as DSH's pi-ai does
// for a custom provider) went as it came, DeepSeek behind the relay
// ignored it and answered with its default 8K (#5030, NOMOAX), the same
// gate as the tool-schema normalization's (#4852, ethanfly). It is
// DeepSeek's whoever serves it, the model named in the body as it goes
// upstream; another vendor's model through the same relay goes as it
// came, as does a request that names max_tokens too (DeepSeek reads
// that one), and a message that names the field keeps its text.
func TestDeepSeekRelayChatGetsMaxTokens(t *testing.T) {
fresh(t)
f := &fake{reply: sse(
`data: {"id":"test","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"ok"}}]}`,
`data: {"id":"test","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}`,
`data: [DONE]`,
)}
up := httptest.NewServer(f)
defer up.Close()
p := provider.Provider{ID: "relay", Name: "Relay", Key: "k", Chat: up.URL + "/v1", Models: []string{"deepseek-flash", "deepseek-v4.1-flash", "gpt-5.3"}}
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
said := `{"role":"user","content":"what is \"max_completion_tokens\"?"}`
for _, c := range []struct {
model, fields string
maxTokens, maxCompl any
}{
{"deepseek-flash", `"max_completion_tokens":393216`, float64(393216), nil},
{"deepseek-v4.1-flash", `"max_completion_tokens":5`, float64(5), nil},
{"deepseek-flash", `"max_tokens":5`, float64(5), nil},
// both named: DeepSeek reads max_tokens, which goes as it is
{"deepseek-flash", `"max_tokens":7,"max_completion_tokens":5`, float64(7), float64(5)},
// another vendor's model through the same relay goes as it came
{"gpt-5.3", `"max_completion_tokens":5`, nil, float64(5)},
} {
body := `{"model":"` + c.model + `","stream":true,` + c.fields + `,"messages":[` + said + `]}`
code, reply := post(t, "/v1/chat/completions", body)
if code != 200 {
t.Fatalf("%s: HTTP %d: %s", c.fields, code, reply)
}
var sent map[string]any
if err := json.Unmarshal(f.got, &sent); err != nil {
t.Fatal(err)
}
if sent["model"] != c.model {
t.Fatalf("%s: upstream got model %s", c.model, sent["model"])
}
if sent["max_tokens"] != c.maxTokens || sent["max_completion_tokens"] != c.maxCompl {
t.Fatalf("%s %s: upstream got %s", c.model, c.fields, f.got)
}
if !strings.Contains(string(f.got), `what is \"max_completion_tokens\"?`) {
t.Fatalf("%s: message text changed: %s", c.fields, f.got)
}
}
}