liyuanzheng (#general) and lam_em (#feedback) both asked for the same thing. Off-peak, DeepSeek's own API should come first for deepseek-flash. At DeepSeek's peak (weekdays 01-04 and 06-10 UTC, billed 2x since v0.1.2173), another provider should come first, Ollama say. A schedule editor would make the user copy DeepSeek's hours into magpie. Instead, a group gets one more routing, "Cheapest first" (`cheap`). Each request sorts the members by what they cost right then: input + output price per million, at the peak rate when it is peak hours. A tie goes to the lower cache-read price. Free models count as 0. A member with no known price keeps its listed place after the priced ones. Prices come from the same priceOf the usage ledger bills with, so the user's own ModelPrices, list prices and maker prices all count, and so does a provider's rate windows. - routes: Seat carries Priced/Cost/CacheCost; Strategy.ByPrice; Cheapest is a stable sort, so equal prices keep the user's order. - gateway: weighRouted prices each seat at priceClock() (time.Now; a test holds it) through the new provider.PriceAt. The trace's Weighed row gets a price (input/output/cacheRead/peak) so Routing can say why. - It is a group's alone, like Manual. A provider saved with it drops it, and `magpie provider set … routing=cheap` refuses it. Groups take it in the GUI menu (one more item, no new controls), the TUI and `magpie group set <id> routing=cheap` (aliases cheapest, price, cost). - Routing's mode line says why the first one is first, e.g. "Cheapest first: deepseek-v4-flash costs least on ollama-cloud now — $0.150 in, $0.600 out a million tokens.", with peak and unpriced variants. en/zh/zh-TW/ja/de. - Docs: gateway-routing, plugin-runtime, reference (it also points at Time window and Pause rules for a fixed schedule) and the site's start pages (en/zh/ja). Run, atf7f2d75f2(this change rebased onto it): - Red on the old code: TestCheapestFirstGroupFollowsPeakHours (deepseek vs ollama with ModelPrices, priceClock held at 14:00 UTC off-peak and 02:00 UTC peak) and TestCheapestOrdersByPriceNow did not compile without Cheapest. TestCheapestFirstIsAGroupsOnly checks that a provider drops it and a group keeps it. - Broken by hand: without `pr = pr.In(at)` the gateway test is red ("at DeepSeek's peak ... went to deepseek"). With Cheapest returning the order unchanged, both the routes test and the gateway test are red. Both were restored. - Full Go suite on mote (-count=1 -timeout 45m ./..., log kept): every package ok except two. internal/provider had TestQuotaSpanCarriesEveryWindowField, which is red on origin/main (#4457, fixed since inccbefe2a2). internal/gui had ten AppKit window tests killed at their 20-40s deadlines (TestPanelCloseHidesIt, TestMainWindowCorners and others). Three of them are red the same way on a fresh origin/mainf7f2d75f2copy, under #2645/#4125 (mote has several GUI suites running). After the rebase, go vet and the Cheapest, SpeedFirstGroup, ParseRouting, ApplyGroupPairs and ProviderRouting tests are ok. go vet darwin, GOOS=linux -tags nogui and GOOS=windows are ok, and gofmt is clean. - GUI on WSL (Linux, chromium + webkit): routing-cheapest-first (new, 3 scenes x en/zh/zh-TW/ja/de x 2 engines), routing-group-strategy, builtins, i18n-keys and routing-speed-first gave 95 of 96. The one red, routing-group-strategy "webkit en light ... the pick moved nothing", is a wheel-scroll timing red. It fails 3 of 3 on origin/main and 1 of 3 here (#4459). Core-script budget ok (app.js +9 bytes). - The whole GUI suite (1025 files) was started on WSL at this tree and was still running when this was pushed, at load ~160. Not tried: real DeepSeek or Ollama accounts, and a real request during DeepSeek's peak hours. Ollama Cloud's price here is the catalog's, not checked against a bill.
49 lines
1.6 KiB
Go
49 lines
1.6 KiB
Go
// Package rotation is the routings a provider or group can be set to
|
|
// besides Smart: In order, In turn, Least used first, Weekly pace, By
|
|
// weight, Free first, Speed first and Cheapest first (a group's). While a provider or group is routed by one, it
|
|
// stays on.
|
|
//
|
|
// It is the first consumer of the gateway's router strategy point (P3 of
|
|
// docs/subsystems/plugin-runtime.md): Apply registers internal/routes'
|
|
// strategies, and off, a pool set to one of them is tried in its listed
|
|
// order and the window offers Smart alone.
|
|
package rotation
|
|
|
|
import (
|
|
"github.com/yetone/magpie/internal/pluginrt"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/routes"
|
|
)
|
|
|
|
func init() {
|
|
pluginrt.Register(pluginrt.Manifest{
|
|
ID: "rotation", API: "^1", Requires: []string{"providers", "gateway"}, Essential: true,
|
|
Name: "Routing strategies", Sub: "The routings besides Smart: In order, In turn, Least used first, Weekly pace, By weight, Free first, Speed first and Cheapest first",
|
|
One: "1 provider or group routed so", Many: "{n} providers and groups routed so",
|
|
InUse: inUse,
|
|
Parts: []pluginrt.Part{{Slot: "part", Key: "rotation"}},
|
|
Apply: func(c *pluginrt.Ctx) error {
|
|
for name, s := range routes.Builtin {
|
|
c.OnDispose(routes.Register(name, s))
|
|
}
|
|
return nil
|
|
},
|
|
})
|
|
}
|
|
|
|
// inUse counts the providers and groups routed by one of its strategies;
|
|
// a providers.json that can't be read is not known, so it stays on.
|
|
func inUse() (int, error) {
|
|
routings, err := provider.Routings()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
n := 0
|
|
for _, r := range routings {
|
|
if _, ok := routes.Builtin[r]; ok {
|
|
n++
|
|
}
|
|
}
|
|
return n, nil
|
|
}
|