hayden4608 on Discord asked that time-of-day routing use the gap between peak and off-peak prices, citing https://ollama.com/pricing. That page (read live on 11-01) gives Off-Peak rows for two models only, deepseek-v4.1-flash ($0.30/$1.20 peak, $0.15/$0.60 off-peak) and deepseek-v4-pro ($1.32/$3.96 peak, $0.66/$1.98), and says off-peak is "outside 12:00 and 18:00 UTC on weekdays and all day on weekends". models.dev's ollama-cloud catalog lists the off-peak prices, so magpie priced Ollama Cloud's peak hours at half. - catalog: a new Peak kind, PeakOllama. Its hours are 12:00 to 18:00 UTC, Monday to Friday, with no holidays. PeakFactor (2) matches the page for both models, cache reads included. - provider: a peakBill for ollama.com (preset/catalog ollama-cloud) that names the two models. Dated tags (deepseek-v4-pro:1017) count as their model; the bare name before ':' is compared without allocating. Every other model there (glm-5.3, gpt-oss...) and every other vendor stays flat. A local Ollama, a relay that names itself ollama-cloud, and an ollama.com.evil host are not matched. - GUI: a model's price tooltip names Ollama's hours for peak "ollama". The Cheapest first and rate-window texts no longer say that only DeepSeek's hours count, in en, zh, zh-TW, ja and de. - docs/reference.md, site docs (en, zh, ja) and the gateway-routing and providers-accounts subsystem pages say the same. Run (on mote through mote-run, at this tree rebased onef3e74a07): - go test for ./internal/catalog ./internal/provider ./internal/gateway for the new and changed tests, under TZ=UTC, Pacific/Kiritimati, Pacific/Pago_Pago and Asia/Kathmandu: ok. The tests check 11:59, 12:00, 17:59, 18:00, 00:00 and 23:59 on weekdays, a Chinese holiday Monday, Saturday and Sunday, at +14, -11 and +0545 instants. - gofmt -l internal: empty. go vet ./..., GOOS=linux go vet -tags nogui ./... and GOOS=windows go vet ./...: ok. - GUI (Chromium and WebKit): model-price-edit, provider-rate-windows, routing-cheapest-first, i18n-keys, gui-de, gui-ja, gui-zh-tw. - go test -count=1 -timeout 45m ./... (one suite on mote): every package ok except internal/gui. TestCoreScriptBudget was 7 bytes over, so this commit drops a comment from the app.js change rather than raise the budget; it passes now. TestMainWindowCorners/ macOS_26_SDK (the window's toolbar taken off) is red on mote and touches nothing here: it is red the same way at origin/mainef3e74a07alone, and is #3949 (macOS 27). Broken by hand (each red, then restored): - the peak hours ending at 17:00: TestOllamaPeakHours red. - the Ollama bill removed: TestPeakOf, TestOllamaCloudOffPeakPrice red. - tagged false: the dated-tag rows red. - Ollama's hours used for every Peak kind: TestPeakHours, TestPriceOfPeak and TestCheapestFirstGroupFollowsPeakHours red. Not tried: a real Ollama Cloud account or bill; a local Ollama's ":cloud" models (sent through the local server, so not priced here); other vendors' off-peak schedules; the site is not deployed.
107 lines
3.9 KiB
Go
107 lines
3.9 KiB
Go
package catalog
|
|
|
|
import "time"
|
|
|
|
// Peak names the hours a list price is the off-peak price of: a call made
|
|
// in them is billed at PeakFactor times it. DeepSeek bills its models at
|
|
// half price off-peak, and models.dev lists that half as the price, with a
|
|
// multiplier for the peak hours its api.json leaves out
|
|
// (providers/deepseek/models/deepseek-flash.toml: "Base prices below are
|
|
// off-peak; the multiplier doubles them at peak"). Empty: the price holds
|
|
// at every hour.
|
|
type Peak string
|
|
|
|
const (
|
|
// PeakDeepSeek is DeepSeek's own: 01:00 to 04:00 and 06:00 to 10:00
|
|
// UTC, Monday to Friday, never on a Chinese public holiday
|
|
// (https://api-docs.deepseek.com/quick_start/pricing)
|
|
PeakDeepSeek Peak = "deepseek"
|
|
// PeakWeekdays is the same hours on every weekday, holidays too: what
|
|
// Command Code (https://commandcode.ai/docs/resources/pricing-limits)
|
|
// and OpenCode Go (https://opencode.ai/docs/go) bill DeepSeek's models
|
|
// at, neither of which names a holiday
|
|
PeakWeekdays Peak = "weekdays"
|
|
// PeakOllama is Ollama Cloud's: 12:00 to 18:00 UTC, Monday to Friday
|
|
// (https://ollama.com/pricing: off-peak outside 12:00 and 18:00 UTC on
|
|
// weekdays, and all of Saturday and Sunday), where its
|
|
// off-peak rows are half its peak ones and models.dev lists the
|
|
// off-peak row as the price. It names no holiday.
|
|
PeakOllama Peak = "ollama"
|
|
)
|
|
|
|
// PeakFactor is what a call in the peak hours costs, times the off-peak
|
|
// list price: DeepSeek's off-peak rates are half its peak rates, and so
|
|
// are Ollama Cloud's.
|
|
const PeakFactor = 2
|
|
|
|
// At is whether a call made at t is in the peak hours. A zero time, a
|
|
// call whose time isn't known, is not.
|
|
func (k Peak) At(t time.Time) bool {
|
|
if k == "" || t.IsZero() {
|
|
return false
|
|
}
|
|
u := t.UTC()
|
|
if wd := u.Weekday(); wd == time.Saturday || wd == time.Sunday {
|
|
return false
|
|
}
|
|
h := u.Hour()
|
|
if k == PeakOllama {
|
|
return h >= 12 && h < 18
|
|
}
|
|
if !(h >= 1 && h < 4 || h >= 6 && h < 10) {
|
|
return false
|
|
}
|
|
// the peak hours are 09:00 to 18:00 in China, so a holiday's date is
|
|
// the same in UTC
|
|
return k != PeakDeepSeek || !cnHoliday(u)
|
|
}
|
|
|
|
// In is the price a call made at t is billed at: PeakFactor times this one
|
|
// in its peak hours, else this one. The price it gives holds at every hour.
|
|
func (p Price) In(t time.Time) Price {
|
|
if !p.Peak.At(t) {
|
|
p.Peak = ""
|
|
return p
|
|
}
|
|
p = p.Times(PeakFactor)
|
|
p.Peak = ""
|
|
return p
|
|
}
|
|
|
|
// cnHolidays are the days off of China's public holidays, as the State
|
|
// Council's notice for each year lists them (a weekend day inside a break
|
|
// is a day off already, and a weekend day worked in exchange stays a
|
|
// weekend day for DeepSeek, whose peak is Monday to Friday). It needs a
|
|
// year added each November, when the notice for the next year comes out:
|
|
// TestCNHolidaysCoverThisYear fails in a year it doesn't have. A year it
|
|
// doesn't have counts no holiday, so its weekdays are all at peak hours'
|
|
// price, the most a call could have cost.
|
|
var cnHolidays = map[int][]string{
|
|
// 国务院办公厅关于2026年部分节假日安排的通知, 2025-11-04
|
|
// (https://www.news.cn/politics/20251104/88bcffd88ae249e58c699b8772548e3d/c.html)
|
|
2026: {
|
|
"01-01", "01-02", "01-03", // 元旦
|
|
"02-15", "02-16", "02-17", "02-18", "02-19", "02-20", "02-21", "02-22", "02-23", // 春节
|
|
"04-04", "04-05", "04-06", // 清明节
|
|
"05-01", "05-02", "05-03", "05-04", "05-05", // 劳动节
|
|
"06-19", "06-20", "06-21", // 端午节
|
|
"09-25", "09-26", "09-27", // 中秋节
|
|
"10-01", "10-02", "10-03", "10-04", "10-05", "10-06", "10-07", // 国庆节
|
|
},
|
|
}
|
|
|
|
// cnHoliday is whether t's date, in China, is a day off of a public holiday.
|
|
func cnHoliday(t time.Time) bool {
|
|
d := t.In(chinaTime).Format("01-02")
|
|
for _, h := range cnHolidays[t.In(chinaTime).Year()] {
|
|
if h == d {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// chinaTime is China's time, UTC+8 all year: a fixed zone, so it doesn't
|
|
// depend on the system's zone database (Windows has none Go can read).
|
|
var chinaTime = time.FixedZone("CST", 8*60*60)
|