Files
magpie/internal/gateway/dashscope_stream_test.go
T
yetone 2de57456e6 gateway: a Qwen request for one JSON body is asked streamed and given back whole, so a routing group's classifier on the Qwen Token Plan works (ZekeXiao on X)
DashScope (Model Studio / Bailian, incl. the Qwen Token Plan) turns a
thinking model's non-streaming call away with 400 "parameter.enable_thinking
must be set to false for non-streaming calls". A routing group's classifier
asks its model for one body (stream:false), so qwen-token-plan's models
failed as the decision model ("400 · 失败" in the routing log).

A non-streaming Chat request to a DashScope provider (the qwen, qwen-cn and
qwen-token-plan presets, or a dashscope*/.maas.aliyuncs.com/
.maas.qianwenaiapi.com host) is no longer relayed as is: it goes through the
translator on Chat, which always asks streamed and collects the reply into
one body. Thinking is left as the model does it, rather than forcing
enable_thinking:false, which always-thinking models refuse. Other vendors
are unchanged (the test checks one).

The ask for per-model custom params on the decision model is not added:
the error was magpie's request shape, which is now right for every client.

Run: TestDashScopeWholeAskedStreamed fails on the old code with the
DashScope 400 and passes now; go test ./... green; gofmt; go vet for
darwin, linux and windows.
2026-10-10 17:39:56 +08:00

66 lines
2.8 KiB
Go

package gateway
import (
"encoding/json"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// dashScopeRefuse answers as DashScope's compatible mode does a thinking
// model asked for one JSON body with its thinking left on.
func dashScopeRefuse(body []byte) (int, string) {
var q struct {
Stream bool `json:"stream"`
EnableThinking *bool `json:"enable_thinking"`
}
json.Unmarshal(body, &q)
if q.Stream || q.EnableThinking != nil && !*q.EnableThinking {
return 0, ""
}
return 400, `{"error":{"message":"<400> InternalError.Algo.InvalidParameter: parameter.enable_thinking must be set to false for non-streaming calls","type":"invalid_request_error","param":null,"code":"invalid_parameter_error"},"id":"chatcmpl-1","request_id":"1"}`
}
// A group's classifier asks its model for one JSON body; on the Qwen
// Token Plan a model that thinks turns that away (ZekeXiao on X), so it is
// asked streamed and its answer given back whole.
func TestDashScopeWholeAskedStreamed(t *testing.T) {
f := &fake{t: t, refuse: dashScopeRefuse, reply: sse(
`data: {"id":"c1","object":"chat.completion.chunk","model":"m1","choices":[{"index":0,"delta":{"role":"assistant","reasoning_content":"A greeting."}}]}`,
`data: {"id":"c1","object":"chat.completion.chunk","model":"m1","choices":[{"index":0,"delta":{"content":"1"}}]}`,
`data: {"id":"c1","object":"chat.completion.chunk","model":"m1","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":40,"completion_tokens":5,"total_tokens":45}}`,
`data: [DONE]`)}
up := setup(t, provider.Chat, f)
if err := provider.Save(provider.Provider{ID: "fake", Name: "Qwen Token Plan", Key: "k", Models: []string{"m1"},
Preset: "qwen-token-plan", Chat: up.URL + "/compatible-mode/v1"}); err != nil {
t.Fatal(err)
}
srv := New()
code, body := postTo(t, srv, "/v1/chat/completions", `{"model":"m1","stream":false,"temperature":0,"max_tokens":2048,"messages":[{"role":"user","content":"hi"}]}`)
if code != 200 {
t.Fatalf("%d: %s", code, body)
}
var out struct {
Choices []struct {
Message struct {
Content string `json:"content"`
} `json:"message"`
} `json:"choices"`
}
if err := json.Unmarshal([]byte(body), &out); err != nil || len(out.Choices) != 1 || out.Choices[0].Message.Content != "1" {
t.Fatalf("answer %s (%v)", body, err)
}
if !strings.Contains(string(f.got), `"stream":true`) {
t.Errorf("asked %s", f.got)
}
// another vendor's request for one body goes as it was asked
f2 := &fake{t: t, reply: chatWholeOK, ctype: "application/json"}
setup(t, provider.Chat, f2)
code, body = postTo(t, New(), "/v1/chat/completions", `{"model":"m1","stream":false,"messages":[{"role":"user","content":"hi"}]}`)
if code != 200 || strings.Contains(string(f2.got), `"stream":true`) {
t.Fatalf("%d, asked %s: %s", code, f2.got, body)
}
}