diff --git a/docs/subsystems/providers-accounts.md b/docs/subsystems/providers-accounts.md index 5a6f07bce..df742e254 100644 --- a/docs/subsystems/providers-accounts.md +++ b/docs/subsystems/providers-accounts.md @@ -160,6 +160,20 @@ that the upstream does not list are named against all listed names and ids; listed models keep their `Available` names unchanged ([`Exposed`](../../internal/provider/models.go)). +A listed model's window, reply limit and levels come from the vendor's +list first; only what it leaves out is filled from models.dev. `Decorate` +matches the provider's own catalog by id, then by the same model under +another spelling (`catalog.SameModel`, the rule routing groups use): Ark's +`glm-5-3-flash-260828` takes its plan catalog's `glm-5.3-flash`. +`ContextOf`, `OutputOf`, `EffortsOf`, `Thinks`, `SeesImages` and +`ListedBy` try another spelling last, only when no spelling matches as +given (`normalFor`, the spelling most entries list: Ark's +`kimi-k3-1-pro-261024` reads as `kimi-k3.1-pro`; `TestOtherSpellingLimits`). +The id stays the upstream's, and its name isn't borrowed. A price never is: +`PricedBy` matches only as before (`TestOtherSpellingIsNotPriced`). The +usage report sends a model id only when models.dev knows it as written +(`KnowsAsWritten`). + ## Constraints and failure behavior An allowance deliberately not requested (a saved Claude account outside diff --git a/internal/catalog/catalog.go b/internal/catalog/catalog.go index 2a991255b..9f9883460 100644 --- a/internal/catalog/catalog.go +++ b/internal/catalog/catalog.go @@ -525,6 +525,13 @@ var ( // under, which no catalog lists by it. One is a model as a listed one // is, so it is never taken for another either (renamed) canonical map[string]bool + // normal is the spelling, of the bare ids models.dev knows (listed, + // canonical or a lab's), that a model's SameModel name is looked up + // by when no spelling matches as given: kimi-k3.1-pro for Volcengine + // Ark's kimi-k3-1-pro-261024 (xyenon on Discord). Of several + // spellings, the one the most entries give (kimi-k3.1-pro over + // Venice's kimi-k3-1-pro); a tie goes to the first in order. + normal map[string]string syncCh = make(chan struct{}, 1) ) @@ -573,6 +580,7 @@ func loadGen() (map[string]mdProvider, int) { sizes, outs := map[string]map[int]int{}, map[string]map[int]int{} levels := map[string]map[string]int{} voted := map[string]bool{} + spelt := map[string]int{} listed = map[string]bool{} canonical = map[string]bool{} // vote is what one entry says of a model, counted for @@ -583,6 +591,7 @@ func loadGen() (map[string]mdProvider, int) { // deepinfra/…, xiaomi/…) votes its levels once, not // once a host. vote := func(who, own, by string, x mdModel) { + spelt[who]++ if x.Name != "" && x.Name != own && x.Name != who { if named[who] == nil { named[who] = map[string]int{} @@ -689,6 +698,13 @@ func loadGen() (map[string]mdProvider, int) { for id, by := range levels { efforts[id] = strings.Split(mostListed(by), ",") } + normal = map[string]string{} + for id, n := range spelt { + k := SameModel(id) + if was, ok := normal[k]; !ok || n > spelt[was] || n == spelt[was] && id < was { + normal[k] = id + } + } names = map[string]string{} for id, by := range named { names[id] = mostNamed(by) @@ -723,7 +739,7 @@ func Reset() { loaded = false built = nil gen++ - mdev, images, windows, outputs, efforts, thinks, names, listed, canonical, labs = nil, nil, nil, nil, nil, nil, nil, nil, nil, nil + mdev, images, windows, outputs, efforts, thinks, names, listed, canonical, labs, normal = nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil loadMu.Unlock() if Forget != nil { Forget() @@ -1123,10 +1139,28 @@ func fillNames(ms []Model, fallback []string, reserved ...Model) { // Knows reports whether models.dev lists a model of this id at all, under // any provider, as ContextOf and EffortsOf match it. +// +// A model it lists under another spelling alone (normalFor) counts. func Knows(id string) bool { return ContextOf(id) > 0 || len(EffortsOf(id)) > 0 || Thinks(id) } +// KnowsAsWritten is Knows without another spelling (normalFor): the +// usage report sends a model's id only when models.dev knows it as +// written, so an id a user typed that only folds into a known one +// ("Kimi K3.1 Pro 261024") is not sent. +func KnowsAsWritten(id string) bool { + if contextAs(id, false) > 0 { + return true + } + load() + if e, _ := lookupAs(id, efforts, false); len(e) > 0 { + return true + } + v, _ := lookupAs(id, thinks, false) + return v +} + // Lists reports whether models.dev lists a model by this very id (its // last part, any case), not one found by another spelling: claude-opus-5.5 // only when that is how one of its providers spells it. @@ -1155,7 +1189,15 @@ func SeesImages(id string) bool { // A relay's rename of a model it knows (renamed) that names its window // ("[1M]", "[200K]", Claude Code's "[1m]") has the window it names: what // the relay serves it at, as Claude Code takes claude-opus-5[1m] for 1M. -func ContextOf(id string) int { +// +// Last, a model it lists under another spelling (normalFor): Volcengine +// Ark's kimi-k3-1-pro-261024 has kimi-k3.1-pro's window. A window the +// vendor's own list gives is read before this, by the caller, and stays. +func ContextOf(id string) int { return contextAs(id, true) } + +// contextAs is ContextOf, with another spelling (normalFor) only when +// spellings is true. +func contextAs(id string, spellings bool) int { load() b := bareID(id) if w, ok := found(b, windows); ok { @@ -1165,7 +1207,12 @@ func ContextOf(id string) int { if w > 0 { return w } - w, _ = found(r, windows) + if w, ok := found(r, windows); ok { + return w + } + } + if n, ok := normalFor(b); spellings && ok { + w, _ := found(n, windows) return w } return 0 @@ -1192,21 +1239,47 @@ func EffortsOf(id string) []string { } // lookup is what table holds for a model of this id: as found finds it, -// else as the relay's rename of a model found finds (renamed). Every -// capability is looked up the same way, so a model the catalog knows has -// all of them or none. +// else as the relay's rename of a model found finds (renamed), else as +// the spelling models.dev lists it by (normalFor). Every capability is +// looked up the same way, so a model the catalog knows has all of them +// or none. func lookup[T any](id string, table map[string]T) (T, bool) { + return lookupAs(id, table, true) +} + +// lookupAs is lookup, with another spelling (normalFor) only when +// spellings is true. +func lookupAs[T any](id string, table map[string]T, spellings bool) (T, bool) { b := bareID(id) if v, ok := found(b, table); ok { return v, true } if r, _, ok := renamed(b); ok { - return found(r, table) + if v, ok := found(r, table); ok { + return v, true + } + } + if n, ok := normalFor(b); spellings && ok { + return found(n, table) } var zero T return zero, false } +// normalFor is the bare id models.dev knows a model by when it knows it +// under another spelling alone: the same SameModel name, so a version's +// dot or dash ("kimi-k3.1-pro", "kimi-k3-1-pro"), case and gaps, and a +// snapshot date (Ark's six digits, "-261024"; "-20260801") don't keep +// them apart, while a variant does (-pro-max is not -pro, -mini not the +// model, k3.1 not k3). False for an id it knows as given, or none. +func normalFor(b string) (string, bool) { + n, ok := normal[SameModel(b)] + if !ok || n == b { + return "", false + } + return n, true +} + // found is what table holds for a bare id: by the id itself, a Bedrock // profile's without its geography, "gpt-5.4(high)" (CLIProxyAPI's level in // the id) as gpt-5.4, or "glm-5:free" (":batch", ":thinking") as glm-5. @@ -1357,22 +1430,50 @@ func listedBy(providers []string, id string, providerOverrides bool) ([]string, if r, ok := unprofiled(b); ok { b = r } + pick := func(pid string, m mdModel) ([]string, bool) { + providerID := "" + if providerOverrides { + providerID = pid + } + if e := all[pid].levels(m, providerID); len(e) > 0 || !m.budgeted() { + return slices.Clone(e), true + } + return nil, false + } for _, want := range []string{b, cutAt(b, '('), cutAt(b, ':')} { for _, pid := range providers { for key, m := range all[pid].Models { if bareID(key) != want { continue } - providerID := "" - if providerOverrides { - providerID = pid - } - if e := all[pid].levels(m, providerID); len(e) > 0 || !m.budgeted() { - return slices.Clone(e), true + if e, ok := pick(pid, m); ok { + return e, true } } } } + // and last under another spelling (SameModel), only where no provider + // lists it as given, and only for a name models.dev knows (normal): Ark + // lists glm-5.3 on its plan, glm-5-3-flash-260828 pay-as-you-go. Of a + // provider's several such entries, the first in key order. + n := SameModel(b) + if _, ok := normal[n]; !ok { + return nil, false + } + for _, pid := range providers { + var keys []string + for key := range all[pid].Models { + if SameModel(bareID(key)) == n { + keys = append(keys, key) + } + } + slices.Sort(keys) + for _, key := range keys { + if e, ok := pick(pid, all[pid].Models[key]); ok { + return e, true + } + } + } return nil, false } diff --git a/internal/catalog/live.go b/internal/catalog/live.go index 778d05824..34ec9fe59 100644 --- a/internal/catalog/live.go +++ b/internal/catalog/live.go @@ -831,6 +831,7 @@ func targetAPIs(target string) []string { // catalog's entry for the same id, keeping the live order. func Decorate(live []Model, known []Model) []Model { byID := make(map[string]Model, len(known)) + var bySame map[string]Model for _, m := range known { byID[m.ID] = m } @@ -841,6 +842,20 @@ func Decorate(live []Model, known []Model) []Model { if i := strings.LastIndexByte(m.ID, '/'); !ok && i >= 0 { k, ok = byID[m.ID[i+1:]] // a gateway's "deepseek/deepseek-chat" } + if !ok && len(known) > 0 { + // the same model under another spelling (SameModel): Ark's + // list says glm-5-3-flash-260828 where its plan's catalog + // says glm-5.3-flash. Of several, the first known. + if bySame == nil { + bySame = make(map[string]Model, len(known)) + for _, x := range known { + if _, dup := bySame[SameModel(x.ID)]; !dup { + bySame[SameModel(x.ID)] = x + } + } + } + k, ok = bySame[SameModel(m.ID)] + } if ok { if m.Name == "" || m.Name == m.ID { fallback[i] = k.Name diff --git a/internal/catalog/samemodel.go b/internal/catalog/samemodel.go new file mode 100644 index 000000000..3d11791e9 --- /dev/null +++ b/internal/catalog/samemodel.go @@ -0,0 +1,69 @@ +package catalog + +import ( + "regexp" + "strings" +) + +// SameModel is a model's name as vendors agree on it: lowercase, without +// the vendor's own prefix ("anthropic/claude-sonnet-5" is claude-sonnet-5, +// "accounts/fireworks/models/…" too), with a version's dot as Anthropic +// writes it ("claude-opus-5.5" is claude-opus-5-5, and so is Fireworks' +// "p" for the dot, "deepseek-v4p1"), a run of spaces, "_" and "-" as one +// "-" (a relay that lists its display name as the id, "GPT-6 Sol", is +// gpt-6-sol: Chill on Discord), and without the snapshot +// date some add: "claude-opus-5-5-20260801", Vertex's "…@20260801", and +// Volcengine Ark's six digits ("deepseek-v4-1-flash-260910" is +// deepseek-v4-1-flash, #583). Anything else stays: a variant after ":" +// (":batch"), -flash, -thinking, and a four-digit release (qwen's -2507, +// kimi-k2-0905) that is a model of its own. So does OpenAI's dashed date +// ("gpt-4o-2024-08-06"): a pinned snapshot apart from the alias that +// moves, and an id that has had a group of its own (auto-gpt-4o-2024-08-06) +// agents and saved groups name. An id spelt with spaces merges with its +// dashed spelling, whose group id (Slug's) it had already. +// +// The provider package groups models by it, and the catalog finds a model +// models.dev lists under another spelling by it (normalFor). +func SameModel(id string) string { + k := strings.ToLower(strings.TrimSpace(id)) + if i := strings.LastIndex(k, "/"); i >= 0 { + k = k[i+1:] + } + k = strings.Trim(modelGaps.ReplaceAllString(k, "-"), "-") + b := []byte(k) + for i := 1; i+1 < len(b); i++ { + if (b[i] == '.' || b[i] == 'p') && digit(b[i-1]) && digit(b[i+1]) { + b[i] = '-' + } + } + k = string(b) + if i := strings.LastIndexAny(k, "-@"); i > 0 && snapshotDate(k[i+1:]) { + k = k[:i] + } + return k +} + +// snapshotDate is whether s is the date a vendor dates a snapshot of a +// model by: YYYYMMDD from 2000 on, or Ark's YYMMDD from 2023 on, with a +// month and a day that are one. +func snapshotDate(s string) bool { + if strings.Trim(s, "0123456789") != "" { + return false + } + switch { + case len(s) == 8 && strings.HasPrefix(s, "20"): + return true + case len(s) == 6: + yy, mm, dd := atoi2(s[0:2]), atoi2(s[2:4]), atoi2(s[4:6]) + return yy >= 23 && yy <= 39 && mm >= 1 && mm <= 12 && dd >= 1 && dd <= 31 + } + return false +} + +// modelGaps are what SameModel spells as one "-": a run of spaces (a +// no-break one too), "_" and "-". +var modelGaps = regexp.MustCompile(`[\s\p{Zs}_-]+`) + +func atoi2(s string) int { return int(s[0]-'0')*10 + int(s[1]-'0') } + +func digit(c byte) bool { return c >= '0' && c <= '9' } diff --git a/internal/catalog/spelling_test.go b/internal/catalog/spelling_test.go new file mode 100644 index 000000000..f595bf9df --- /dev/null +++ b/internal/catalog/spelling_test.go @@ -0,0 +1,163 @@ +package catalog + +import ( + "strings" + "testing" +) + +// spellingCatalog is models.dev's rows for the models of xyenon's report +// (Discord #feedback, 10-29), as models.dev/api.json had them on 10-29 and +// trimmed to their limits, levels and prices, beside near misses: Ark's +// pay-as-you-go list says kimi-k3-1-pro-261024 for what Moonshot and the +// relays list as kimi-k3.1-pro, and Venice as kimi-k3-1-pro. +const spellingCatalog = `{ + "moonshotai": {"models": { + "kimi-k3.1-pro": {"id":"kimi-k3.1-pro","name":"Kimi K3.1 Pro","limit":{"context":1048576,"output":1048576}, + "reasoning_options":[{"type":"effort","values":["low","high"]}], + "cost":{"input":2,"output":10,"cache_read":0.2,"cache_write":2}}, + "kimi-k3.1-pro-highspeed": {"id":"kimi-k3.1-pro-highspeed","limit":{"context":1048576,"output":1048576}, + "cost":{"input":4,"output":20,"cache_read":0.2,"cache_write":4}}, + "kimi-k3": {"id":"kimi-k3","limit":{"context":1048576,"output":131072}, + "cost":{"input":3,"output":15,"cache_read":0.3,"cache_write":3}} + }}, + "vercel": {"models": { + "moonshotai/kimi-k3.1-pro": {"id":"moonshotai/kimi-k3.1-pro","limit":{"context":1048576,"output":1048576}, + "cost":{"input":2,"output":10,"cache_read":0.2,"cache_write":2}} + }}, + "venice": {"models": { + "kimi-k3-1-pro": {"id":"kimi-k3-1-pro","limit":{"context":1000000,"output":131072}, + "cost":{"input":3,"output":15,"cache_read":0.3}} + }}, + "near-miss": {"models": { + "kimi-k3.1-pro-max": {"id":"kimi-k3.1-pro-max","limit":{"context":524288,"output":65536}}, + "kimi-k3.1-mini": {"id":"kimi-k3.1-mini","limit":{"context":262144,"output":32768}}, + "kimi-k3.2-pro": {"id":"kimi-k3.2-pro","limit":{"context":2097152,"output":262144}} + }}, + "volcengine": {"models": { + "glm-5-3-flash-260828": {"id":"glm-5-3-flash-260828","limit":{"context":1000000,"output":131072}, + "cost":{"input":0.11875,"output":0.41563,"cache_read":0.03414}} + }}, + "volcengine-coding-plan": {"models": { + "glm-5.3-flash": {"id":"glm-5.3-flash","name":"GLM-5.3-Flash","limit":{"context":1000000,"output":131072}, + "reasoning_options":[{"type":"effort","values":["low","high","max"]}], + "cost":{"input":0,"output":0,"cache_read":0}}, + "kimi-k3": {"id":"kimi-k3","limit":{"context":1048576,"output":131072}, + "cost":{"input":0,"output":0,"cache_read":0}} + }} +}` + +// A model models.dev lists under another spelling alone has its window, +// reply limit and levels: the one its spelling (SameModel) folds into, as +// most entries spell it. A different model (-pro-max, -mini, another +// version, a four-digit release) folds into nothing it lists. +func TestOtherSpellingLimits(t *testing.T) { + writeCatalog(t, spellingCatalog) + for _, c := range []struct { + id string + window, out int + }{ + // the report: Ark's id for kimi-k3.1-pro + {"kimi-k3-1-pro-261024", 1048576, 1048576}, + {"volcengine/kimi-k3-1-pro-261024", 1048576, 1048576}, + {"Kimi-K3.1-Pro-20261024", 1048576, 1048576}, + // listed as given: Venice's own word, not the other spelling's + {"kimi-k3-1-pro", 1000000, 131072}, + {"kimi-k3.1-pro", 1048576, 1048576}, + // near misses keep their own, or none + {"kimi-k3-1-pro-max-261024", 524288, 65536}, + {"kimi-k3-1-mini-261024", 262144, 32768}, + {"kimi-k3-1-pro-mini-261024", 0, 0}, + {"kimi-k3-2-pro-261024", 2097152, 262144}, + {"kimi-k3-3-pro-261024", 0, 0}, + {"kimi-k3-pro-261024", 0, 0}, + {"kimi-k3-1-261024", 0, 0}, + {"kimi-k3-1-pro-highspeed-261024", 1048576, 1048576}, + {"kimi-k3-1-pro-2610", 0, 0}, // a four-digit release is a model of its own + {"kimi-k3-1-pro-261324", 0, 0}, // no month 13: not a date + {"kimi-k3-1-pro-fast-261024", 0, 0}, + } { + if got := ContextOf(c.id); got != c.window { + t.Errorf("ContextOf(%q) = %d, want %d", c.id, got, c.window) + } + if got := OutputOf(c.id); got != c.out { + t.Errorf("OutputOf(%q) = %d, want %d", c.id, got, c.out) + } + } + if got := strings.Join(EffortsOf("kimi-k3-1-pro-261024"), ","); got != "low,high" { + t.Errorf("EffortsOf = %q, want low,high", got) + } + if !Knows("kimi-k3-1-pro-261024") { + t.Error("Knows: the other spelling is a model models.dev knows") + } + // the usage report sends only an id models.dev knows as written + if KnowsAsWritten("kimi-k3-1-pro-261024") || !KnowsAsWritten("kimi-k3.1-pro") || !KnowsAsWritten("kimi-k3-1-pro") { + t.Error("KnowsAsWritten took another spelling") + } + // the id itself is kept: only what is known of it is borrowed + if got := NameOf("kimi-k3-1-pro-261024"); got != "" { + t.Errorf("NameOf = %q: the other spelling's name stands for a dated id", got) + } +} + +// A price is never borrowed by another spelling: neither the maker's nor +// another host's prices Ark's dated id. +func TestOtherSpellingIsNotPriced(t *testing.T) { + writeCatalog(t, spellingCatalog) + for _, ps := range [][]string{{"volcengine"}, {"moonshotai"}, {"volcengine", "moonshotai", "vercel", "venice"}} { + if pr, ok := PricedBy(ps, "kimi-k3-1-pro-261024"); ok { + t.Errorf("PricedBy(%v) = %+v", ps, pr) + } + } + if pr, ok := PricedBy([]string{"volcengine-coding-plan"}, "glm-5-3-flash-260828"); ok { + t.Errorf("the plan's price taken for pay-as-you-go's id: %+v", pr) + } +} + +// The provider's own catalog answers first: Ark's pay-as-you-go list says +// glm-5-3-flash-260828 where its plan's catalog says glm-5.3-flash. What +// the vendor's list says itself stays, and no price comes with it. +func TestDecorateOtherSpelling(t *testing.T) { + writeCatalog(t, spellingCatalog) + known := Provider("volcengine-coding-plan") + out := Decorate([]Model{ + {ID: "glm-5-3-flash-260828", Context: 200000}, + {ID: "glm-5-3-flash-max-260828"}, + {ID: "kimi-k3-1-pro-261024"}, + }, known) + g := out[0] + if g.ID != "glm-5-3-flash-260828" || g.Context != 200000 || g.Output != 131072 || g.Price != nil { + t.Errorf("glm: %+v", g) + } + if strings.Join(g.Efforts, ",") != "low,high,max" || g.Name != "GLM-5.3-Flash" { + t.Errorf("glm levels/name: %v %q", g.Efforts, g.Name) + } + if m := out[1]; m.Context != 0 || m.Output != 0 || len(m.Efforts) > 0 { + t.Errorf("a -max variant took glm-5.3-flash's: %+v", m) + } + // the plan doesn't list kimi-k3.1-pro, and kimi-k3 is another model + if m := out[2]; m.Context != 0 || m.Output != 0 { + t.Errorf("kimi-k3.1-pro took kimi-k3's: %+v", m) + } + if got, ok := ListedBy([]string{"volcengine-coding-plan"}, "glm-5-3-flash-260828"); !ok || strings.Join(got, ",") != "low,high,max" { + t.Errorf("ListedBy = %v %v", got, ok) + } + if _, ok := ListedBy([]string{"volcengine-coding-plan"}, "kimi-k3-1-pro-261024"); ok { + t.Error("ListedBy took kimi-k3 for kimi-k3.1-pro") + } +} + +// SameModel folds what the report's ids differ by, and nothing that names +// another model. +func TestSameModelReportIDs(t *testing.T) { + k := SameModel("kimi-k3.1-pro") + for _, id := range []string{"kimi-k3-1-pro-261024", "kimi-k3-1-pro", "moonshotai/Kimi-K3.1-Pro", "kimi_k3_1_pro"} { + if SameModel(id) != k { + t.Errorf("SameModel(%q) = %q, want %q", id, SameModel(id), k) + } + } + for _, id := range []string{"kimi-k3.1-pro-max", "kimi-k3.1-mini", "kimi-k3.2-pro", "kimi-k3-pro", "kimi-k3.1-pro-2610", "kimi-k31-pro"} { + if SameModel(id) == k { + t.Errorf("SameModel(%q) folds into kimi-k3.1-pro", id) + } + } +} diff --git a/internal/provider/group.go b/internal/provider/group.go index 2bf3394c0..d0f263e41 100644 --- a/internal/provider/group.go +++ b/internal/provider/group.go @@ -718,63 +718,9 @@ func autoGroups(entries []Entry, same map[string]string) []Group { return out } -// sameModel is a model's name as vendors agree on it: lowercase, without -// the vendor's own prefix ("anthropic/claude-sonnet-5" is claude-sonnet-5, -// "accounts/fireworks/models/…" too), with a version's dot as Anthropic -// writes it ("claude-opus-5.5" is claude-opus-5-5, and so is Fireworks' -// "p" for the dot, "deepseek-v4p1"), a run of spaces, "_" and "-" as one -// "-" (a relay that lists its display name as the id, "GPT-6 Sol", is -// gpt-6-sol: Chill on Discord), and without the snapshot -// date some add: "claude-opus-5-5-20260801", Vertex's "…@20260801", and -// Volcengine Ark's six digits ("deepseek-v4-1-flash-260910" is -// deepseek-v4-1-flash, #583). Anything else stays: a variant after ":" -// (":batch"), -flash, -thinking, and a four-digit release (qwen's -2507, -// kimi-k2-0905) that is a model of its own. So does OpenAI's dashed date -// ("gpt-4o-2024-08-06"): a pinned snapshot apart from the alias that -// moves, and an id that has had a group of its own (auto-gpt-4o-2024-08-06) -// agents and saved groups name. An id spelt with spaces merges with its -// dashed spelling, whose group id (Slug's) it had already. -func sameModel(id string) string { - k := strings.ToLower(strings.TrimSpace(id)) - if i := strings.LastIndex(k, "/"); i >= 0 { - k = k[i+1:] - } - k = strings.Trim(modelGaps.ReplaceAllString(k, "-"), "-") - b := []byte(k) - for i := 1; i+1 < len(b); i++ { - if (b[i] == '.' || b[i] == 'p') && isDigit(b[i-1]) && isDigit(b[i+1]) { - b[i] = '-' - } - } - k = string(b) - if i := strings.LastIndexAny(k, "-@"); i > 0 && snapshotDate(k[i+1:]) { - k = k[:i] - } - return k -} - -// snapshotDate is whether s is the date a vendor dates a snapshot of a -// model by: YYYYMMDD from 2000 on, or Ark's YYMMDD from 2023 on, with a -// month and a day that are one. -func snapshotDate(s string) bool { - if strings.Trim(s, "0123456789") != "" { - return false - } - switch { - case len(s) == 8 && strings.HasPrefix(s, "20"): - return true - case len(s) == 6: - yy, mm, dd := atoi2(s[0:2]), atoi2(s[2:4]), atoi2(s[4:6]) - return yy >= 23 && yy <= 39 && mm >= 1 && mm <= 12 && dd >= 1 && dd <= 31 - } - return false -} - -// modelGaps are what sameModel spells as one "-": a run of spaces (a -// no-break one too), "_" and "-". -var modelGaps = regexp.MustCompile(`[\s\p{Zs}_-]+`) - -func atoi2(s string) int { return int(s[0]-'0')*10 + int(s[1]-'0') } +// sameModel is a model's name as vendors agree on it: catalog.SameModel, +// which says what it keeps and what it folds. +func sameModel(id string) string { return catalog.SameModel(id) } func isDigit(c byte) bool { return c >= '0' && c <= '9' } diff --git a/internal/stats/stats_test.go b/internal/stats/stats_test.go index 06a1b8209..b137b446c 100644 --- a/internal/stats/stats_test.go +++ b/internal/stats/stats_test.go @@ -124,7 +124,7 @@ func TestAgentLabel(t *testing.T) { // A model on a key's provider goes by its id only when models.dev knows // it: one the user named (an Ollama model, a fine-tune naming their -// organisation) is "other". +// organisation, or a known one spelt their way) is "other". func TestModelLabelKeepsTheUsersOwnModelIDs(t *testing.T) { cache := map[string]any{"deepseek": map[string]any{"id": "deepseek", "models": map[string]any{ "deepseek-v4": map[string]any{"id": "deepseek-v4", "limit": map[string]any{"context": 128000}}, @@ -139,7 +139,7 @@ func TestModelLabelKeepsTheUsersOwnModelIDs(t *testing.T) { catalog.Reset() t.Cleanup(func() { os.Remove(catalog.CachePath()); catalog.Reset() }) for _, p := range []provider.Provider{ - {ID: "my-deepseek", Name: "DS", Preset: "deepseek", Chat: "https://api.deepseek.com/v1", Key: "sk-x", Models: []string{"deepseek-v4"}}, + {ID: "my-deepseek", Name: "DS", Preset: "deepseek", Chat: "https://api.deepseek.com/v1", Key: "sk-x", Models: []string{"deepseek-v4", "DeepSeek_V4-260101"}}, {ID: "my-openai", Name: "OA", Preset: "openai", Chat: "https://api.openai.com/v1", Key: "sk-y", Models: []string{"ft:gpt-4o:acme-corp::abc123"}}, {ID: "my-ollama", Name: "Local", Preset: "ollama", Chat: "http://127.0.0.1:11434/v1", Models: []string{"acme-internal-llm"}}, } { @@ -148,7 +148,10 @@ func TestModelLabelKeepsTheUsersOwnModelIDs(t *testing.T) { } } for ref, want := range map[string]string{ - "my-deepseek/deepseek-v4": "deepseek/deepseek-v4", + "my-deepseek/deepseek-v4": "deepseek/deepseek-v4", + // known only under another spelling (catalog.SameModel): the + // user's own spelling isn't sent + "my-deepseek/DeepSeek_V4-260101": "deepseek/other", "my-openai/ft:gpt-4o:acme-corp::abc123": "openai/other", "my-ollama/acme-internal-llm": "ollama/other", } { diff --git a/internal/stats/usage.go b/internal/stats/usage.go index 3b6ba4dca..dcd877130 100644 --- a/internal/stats/usage.go +++ b/internal/stats/usage.go @@ -102,7 +102,7 @@ func modelLabel(ref string) string { if l == "custom" || l == "plugin" { return l } - if p.Account == nil && !p.IsPlugin() && !catalog.Knows(m) { + if p.Account == nil && !p.IsPlugin() && !catalog.KnowsAsWritten(m) { return l + "/other" } return l + "/" + m