providers: add the Baidu Qianfan Token Plan (个人版) preset (#193)
This commit is contained in:
@@ -45,7 +45,7 @@ and there is a terminal version (`magpie tui`) and a plain CLI.
|
||||
use its models through the gateway, with nothing copied and no key to
|
||||
paste.
|
||||
- **Providers with one field.** Pick a preset (Anthropic, OpenAI, Gemini,
|
||||
DeepSeek, Kimi, GLM, MiniMax, StepFun, Qwen, Tencent Cloud Token Plan,
|
||||
DeepSeek, Kimi, GLM, MiniMax, StepFun, Qwen, Baidu Qianfan Token Plan, Tencent Cloud Token Plan,
|
||||
Huawei Cloud MaaS, Volcengine Ark, Mistral, Groq, xAI, OpenRouter, Together,
|
||||
Fireworks, SiliconFlow, AiHubMix, 302.AI, Ollama, LM Studio…),
|
||||
paste a key, done. Custom vendors need a name and a base URL. magpie never
|
||||
@@ -122,6 +122,16 @@ separate Responses endpoint, `catalog=` to borrow a models.dev list, and
|
||||
`models=` to name the models to expose. Anything a preset does not know can
|
||||
be overridden the same way.
|
||||
|
||||
Baidu Qianfan's [Token Plan Personal](https://cloud.baidu.com/doc/qianfan/s/Dmrabu8b6)
|
||||
is available as `qianfan-token-plan`, with its dedicated Chat Completions,
|
||||
Responses and Anthropic Messages endpoints. Add it with
|
||||
`magpie provider add qianfan-token-plan <personal-plan-api-key>`.
|
||||
`qianfan-code-latest` follows the model selected in the Qianfan console;
|
||||
explicit model IDs such as `glm-5.3` select that model directly. The preset
|
||||
uses the documented plan models because the plan has no model-list endpoint;
|
||||
new model IDs can also be entered by hand. Use a Token Plan Personal key:
|
||||
Coding Plan and enterprise plans have different endpoints.
|
||||
|
||||
### Routing groups
|
||||
|
||||
A routing group is several models, from one provider or many, that an agent
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
// qianfan plays the plan's own endpoints: chat completions and Responses
|
||||
// under /v2/tokenplan/personal, Anthropic messages under
|
||||
// /anthropic/tokenplan/personal.
|
||||
type qianfan struct {
|
||||
mu sync.Mutex
|
||||
calls []qianfanCall
|
||||
}
|
||||
|
||||
type qianfanCall struct {
|
||||
path, model string
|
||||
head http.Header
|
||||
}
|
||||
|
||||
func (q *qianfan) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
var v struct {
|
||||
Model string `json:"model"`
|
||||
}
|
||||
json.Unmarshal(body, &v)
|
||||
q.mu.Lock()
|
||||
q.calls = append(q.calls, qianfanCall{r.URL.Path, v.Model, r.Header.Clone()})
|
||||
q.mu.Unlock()
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
switch r.URL.Path {
|
||||
case "/v2/tokenplan/personal/chat/completions":
|
||||
io.WriteString(w, sse(
|
||||
`data: {"id":"c1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant","content":"from chat"}}]}`,
|
||||
`data: {"id":"c1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":5,"completion_tokens":2}}`,
|
||||
`data: [DONE]`))
|
||||
case "/v2/tokenplan/personal/responses":
|
||||
io.WriteString(w, sse(
|
||||
`data: {"type":"response.created","response":{"id":"resp_1"}}`,
|
||||
`data: {"type":"response.output_item.added","output_index":0,"item":{"id":"msg_1","type":"message","role":"assistant","content":[]}}`,
|
||||
`data: {"type":"response.output_text.delta","item_id":"msg_1","output_index":0,"content_index":0,"delta":"from responses"}`,
|
||||
`data: {"type":"response.completed","response":{"id":"resp_1","usage":{"input_tokens":5,"output_tokens":2}}}`))
|
||||
case "/anthropic/tokenplan/personal/v1/messages":
|
||||
io.WriteString(w, sse(
|
||||
`event: message_start`+"\n"+`data: {"type":"message_start","message":{"id":"msg_1","model":"`+v.Model+`","usage":{"input_tokens":5}}}`,
|
||||
`data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}`,
|
||||
`data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"from messages"}}`,
|
||||
`data: {"type":"content_block_stop","index":0}`,
|
||||
`data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":2}}`,
|
||||
`data: {"type":"message_stop"}`))
|
||||
default:
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
io.WriteString(w, `{"message":"no such route"}`)
|
||||
}
|
||||
}
|
||||
|
||||
func (q *qianfan) last() qianfanCall {
|
||||
q.mu.Lock()
|
||||
defer q.mu.Unlock()
|
||||
if len(q.calls) == 0 {
|
||||
return qianfanCall{}
|
||||
}
|
||||
return q.calls[len(q.calls)-1]
|
||||
}
|
||||
|
||||
// The plan at a fake Qianfan: every model is relayed on the client's own
|
||||
// API, and a request for an API the plan speaks is never translated away
|
||||
// from it.
|
||||
func TestQianfanTokenPlanRoutes(t *testing.T) {
|
||||
fresh(t)
|
||||
up := &qianfan{}
|
||||
srv := httptest.NewServer(up)
|
||||
t.Cleanup(srv.Close)
|
||||
p, err := provider.FromPreset("qianfan-token-plan")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
p.Key = "bce-v3-test"
|
||||
p.Chat, p.Responses = srv.URL+"/v2/tokenplan/personal", srv.URL+"/v2/tokenplan/personal"
|
||||
p.Anthropic = srv.URL+"/anthropic/tokenplan/personal"
|
||||
p.Models = []string{"deepseek-v4.1-flash", "deepseek-v4-pro"}
|
||||
if err := provider.Save(p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, tc := range []struct {
|
||||
name, path, body, path2, model, reply string
|
||||
bearer bool
|
||||
}{
|
||||
{"flash on responses", "/v1/responses",
|
||||
`{"model":"qianfan-token-plan/deepseek-v4.1-flash","input":"hi","stream":true}`,
|
||||
"/v2/tokenplan/personal/responses", "deepseek-v4.1-flash", "from responses", true},
|
||||
{"pro on responses", "/v1/responses",
|
||||
`{"model":"qianfan-token-plan/deepseek-v4-pro","input":"hi","stream":true}`,
|
||||
"/v2/tokenplan/personal/responses", "deepseek-v4-pro", "from responses", true},
|
||||
{"flash on messages", "/v1/messages",
|
||||
`{"model":"qianfan-token-plan/deepseek-v4.1-flash","max_tokens":20,"stream":true,"messages":[{"role":"user","content":"hi"}]}`,
|
||||
"/anthropic/tokenplan/personal/v1/messages", "deepseek-v4.1-flash", "from messages", false},
|
||||
{"pro on chat", "/v1/chat/completions",
|
||||
`{"model":"qianfan-token-plan/deepseek-v4-pro","stream":true,"messages":[{"role":"user","content":"hi"}]}`,
|
||||
"/v2/tokenplan/personal/chat/completions", "deepseek-v4-pro", "from chat", true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
code, body := post(t, tc.path, tc.body)
|
||||
if code != 200 || !strings.Contains(body, tc.reply) {
|
||||
t.Fatalf("status %d: %s", code, body)
|
||||
}
|
||||
c := up.last()
|
||||
if c.path != tc.path2 || c.model != tc.model {
|
||||
t.Fatalf("upstream: %s %q", c.path, c.model)
|
||||
}
|
||||
if tc.bearer {
|
||||
if c.head.Get("Authorization") != "Bearer bce-v3-test" {
|
||||
t.Fatalf("headers: %v", c.head)
|
||||
}
|
||||
} else if c.head.Get("x-api-key") != "bce-v3-test" || c.head.Get("anthropic-version") != "2023-06-01" {
|
||||
t.Fatalf("headers: %v", c.head)
|
||||
}
|
||||
})
|
||||
}
|
||||
up.mu.Lock()
|
||||
defer up.mu.Unlock()
|
||||
for _, c := range up.calls {
|
||||
if !strings.HasPrefix(c.path, "/v2/tokenplan/personal/") && !strings.HasPrefix(c.path, "/anthropic/tokenplan/personal/") {
|
||||
t.Errorf("asked at %s", c.path)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<svg height="1em" style="flex:none;line-height:1" viewBox="0 0 24 24" width="1em" xmlns="http://www.w3.org/2000/svg"><title>BaiduCloud</title><path d="M21.715 5.61l-3.983 2.31a.903.903 0 01-.896 0L12.44 5.384a.903.903 0 00-.897 0L7.156 7.92a.903.903 0 01-.896 0L2.276 5.617 12.002 0l9.713 5.61z" fill="#5BCA87"></path><path d="M18.641 9.467a.89.89 0 00-.438.77v5.072a.896.896 0 01-.445.77l-4.428 2.51a.884.884 0 00-.445.777v4.607l4.429-2.536 5.31-3.047V7.157l-3.983 2.31z" fill="#EC5D3E"></path><path d="M10.98 18.941a.936.936 0 00-.305-.352l-4.429-2.516a.903.903 0 01-.431-.764v-5.078a.89.89 0 00-.452-.757l-.451-.26L1.38 7.158V18.39l5.311 3.047L11.126 24v-4.608a.881.881 0 00-.146-.45z" fill="#2464F5"></path></svg>
|
||||
|
After Width: | Height: | Size: 717 B |
@@ -140,6 +140,20 @@ var presets = []PresetDef{
|
||||
{ID: "plan-ams", Name: "Plan · Europe", Chat: "https://token-plan-ams.xiaomimimo.com/v1", Responses: "https://token-plan-ams.xiaomimimo.com/v1", Anthropic: "https://token-plan-ams.xiaomimimo.com/anthropic"},
|
||||
{ID: "api", Name: "Pay as you go", Chat: "https://api.xiaomimimo.com/v1", Responses: "https://api.xiaomimimo.com/v1", Anthropic: "https://api.xiaomimimo.com/anthropic"},
|
||||
}},
|
||||
// Baidu Qianfan's Token Plan (个人版): a personal plan's quota is spent
|
||||
// only at its own endpoints under qianfan.baidubce.com (v2 for chat
|
||||
// completions and Responses, anthropic for messages), on a plan key of
|
||||
// its own that the pay-as-you-go API turns away. It serves no model
|
||||
// list, so the plan's documented models are given — deepseek-v4-flash
|
||||
// and kimi-k2.6 retire 2026-09-29; qianfan-code-latest is whichever the
|
||||
// console has picked.
|
||||
{ID: "qianfan-token-plan", Name: "Baidu Qianfan Token Plan", Icon: "baiducloud-color", Kind: KindVendor,
|
||||
Chat: "https://qianfan.baidubce.com/v2/tokenplan/personal", Responses: "https://qianfan.baidubce.com/v2/tokenplan/personal", Anthropic: "https://qianfan.baidubce.com/anthropic/tokenplan/personal",
|
||||
Note: "Token Plan · 个人版",
|
||||
Website: "https://cloud.baidu.com/doc/qianfan/s/Dmrabu8b6", KeysURL: "https://console.bce.baidu.com/qianfan/resource/token-plan",
|
||||
NoList: true,
|
||||
Models: []string{"qianfan-code-latest", "deepseek-v4.1-flash", "deepseek-v4-pro", "deepseek-v4-pro-0813", "deepseek-v4-flash-0731",
|
||||
"glm-5.3", "glm-5.3-flash", "glm-5.2", "glm-5.1", "kimi-k2.6"}},
|
||||
// Tencent Cloud's Token Plan (TokenHub): a general and a Hy plan on one
|
||||
// sk-tp- key, served at their own endpoints under /plan, chat completions
|
||||
// and Anthropic messages only (its Codex page asks for wire_api "chat").
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/catalog"
|
||||
)
|
||||
|
||||
func TestQianfanTokenPlan(t *testing.T) {
|
||||
p, err := FromPreset("qianfan-token-plan")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// the plan's own endpoints, one base per protocol family; the plan has
|
||||
// no model list, so NoList keeps the preset's models as its list
|
||||
if p.Chat != "https://qianfan.baidubce.com/v2/tokenplan/personal" || p.Responses != p.Chat ||
|
||||
p.Anthropic != "https://qianfan.baidubce.com/anthropic/tokenplan/personal" || !Preset("qianfan-token-plan").NoList {
|
||||
t.Fatalf("endpoints: %q %q %q", p.Chat, p.Responses, p.Anthropic)
|
||||
}
|
||||
for _, id := range Preset("qianfan-token-plan").Models {
|
||||
if id != strings.ToLower(id) {
|
||||
t.Fatalf("model ids are lowercase on the plan: %q", id)
|
||||
}
|
||||
}
|
||||
// the plan's models, qianfan-code-latest following the console's pick first
|
||||
if got := p.planModels(nil); len(got) != len(Preset("qianfan-token-plan").Models) || got[0].ID != "qianfan-code-latest" {
|
||||
t.Fatalf("plan's: %+v", got)
|
||||
}
|
||||
// a list the plan gave is kept whole
|
||||
if got := p.planModels([]catalog.Model{{ID: "glm-5.3"}}); len(got) != 1 || got[0].ID != "glm-5.3" {
|
||||
t.Fatalf("listed: %+v", got)
|
||||
}
|
||||
// an entry imported from another app at the plan's endpoints is the preset
|
||||
im, _ := imported("Qianfan", "bce-v3/x", endpoints{anthropic: "https://qianfan.baidubce.com/anthropic/tokenplan/personal"}, nil)
|
||||
if im.Preset != "qianfan-token-plan" || im.Icon != "baiducloud-color" {
|
||||
t.Fatalf("imported: %+v", im)
|
||||
}
|
||||
}
|
||||
@@ -281,7 +281,7 @@ href = <span class="s">"https://usemagpie.ai/import#"</span> + urlencode({
|
||||
const PRESETS = [
|
||||
["anthropic", "Anthropic"], ["openai", "OpenAI"], ["google", "Google Gemini"], ["deepseek", "DeepSeek"],
|
||||
["xai", "xAI"], ["moonshot", "Kimi"], ["moonshot-cn", "Kimi (China)"], ["zhipu", "Zhipu GLM"], ["zai", "Z.ai"],
|
||||
["minimax", "MiniMax"], ["minimax-cn", "MiniMax (China)"], ["qwen", "Qwen"], ["qwen-cn", "Qwen (China)"], ["tencent-token-plan", "Tencent Cloud Token Plan"], ["huaweicloud", "Huawei Cloud MaaS"], ["volcengine", "Volcengine Ark"],
|
||||
["minimax", "MiniMax"], ["minimax-cn", "MiniMax (China)"], ["qwen", "Qwen"], ["qwen-cn", "Qwen (China)"], ["qianfan-token-plan", "Baidu Qianfan Token Plan"], ["tencent-token-plan", "Tencent Cloud Token Plan"], ["huaweicloud", "Huawei Cloud MaaS"], ["volcengine", "Volcengine Ark"],
|
||||
["mistral", "Mistral"], ["groq", "Groq"], ["ollama-cloud", "Ollama Cloud"], ["openrouter", "OpenRouter"], ["opencode-go", "OpenCode Go"],
|
||||
["opencode-zen", "OpenCode Zen"], ["together", "Together AI"], ["fireworks", "Fireworks"],
|
||||
["siliconflow", "SiliconFlow"], ["aihubmix", "AiHubMix"], ["302ai", "302.AI"], ["yylx", "鱼鱼连线"],
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
<svg height="1em" style="flex:none;line-height:1" viewBox="0 0 24 24" width="1em" xmlns="http://www.w3.org/2000/svg"><title>BaiduCloud</title><path d="M21.715 5.61l-3.983 2.31a.903.903 0 01-.896 0L12.44 5.384a.903.903 0 00-.897 0L7.156 7.92a.903.903 0 01-.896 0L2.276 5.617 12.002 0l9.713 5.61z" fill="#5BCA87"></path><path d="M18.641 9.467a.89.89 0 00-.438.77v5.072a.896.896 0 01-.445.77l-4.428 2.51a.884.884 0 00-.445.777v4.607l4.429-2.536 5.31-3.047V7.157l-3.983 2.31z" fill="#EC5D3E"></path><path d="M10.98 18.941a.936.936 0 00-.305-.352l-4.429-2.516a.903.903 0 01-.431-.764v-5.078a.89.89 0 00-.452-.757l-.451-.26L1.38 7.158V18.39l5.311 3.047L11.126 24v-4.608a.881.881 0 00-.146-.45z" fill="#2464F5"></path></svg>
|
||||
|
After Width: | Height: | Size: 717 B |
@@ -95,6 +95,7 @@ dd .m { display: inline-block; font-size: 12.5px; border: 1px solid var(--line);
|
||||
deepseek: ["DeepSeek", "deepseek-color.svg"], xai: ["xAI", "xai.svg"], moonshot: ["Kimi", "kimi.svg"], "moonshot-cn": ["Kimi (China)", "kimi.svg"],
|
||||
zhipu: ["Zhipu GLM", "zhipu-color.svg"], zai: ["Z.ai", "zhipu-color.svg"], minimax: ["MiniMax", "minimax-color.svg"],
|
||||
"minimax-cn": ["MiniMax (China)", "minimax-color.svg"], qwen: ["Qwen", "qwen-color.svg"], "qwen-cn": ["Qwen (China)", "qwen-color.svg"],
|
||||
"qianfan-token-plan": ["Baidu Qianfan Token Plan", "baiducloud-color.svg"],
|
||||
"tencent-token-plan": ["Tencent Cloud Token Plan", "tencentcloud-color.svg"], huaweicloud: ["Huawei Cloud MaaS", "huaweicloud-color.svg"], volcengine: ["Volcengine Ark", "volcengine-color.svg"],
|
||||
mistral: ["Mistral", "mistral-color.svg"], groq: ["Groq", "groq.svg"], openrouter: ["OpenRouter", "openrouter.svg"],
|
||||
"opencode-go": ["OpenCode Go", "opencode.svg"], "opencode-zen": ["OpenCode Zen", "opencode.svg"], together: ["Together AI"],
|
||||
|
||||
Reference in New Issue
Block a user