Skip to content

Expose per-model group ratios on the public PLG pricing payload - #692

Merged
think-back merged 1 commit into
mainfrom
fix/pricing-group-model-ratio
Aug 11, 2026
Merged

Expose per-model group ratios on the public PLG pricing payload#692
think-back merged 1 commit into
mainfrom
fix/pricing-group-model-ratio

Conversation

@think-back

Copy link
Copy Markdown
Collaborator

问题

后端计费按模型级倍率优先、分组倍率兜底的顺序取折扣,但官网 /models 只拿得到分组倍率。一旦某个模型(比如国产模型)单独配了比分组更低的价,页面会按更高的价报出去

证据

计费路径 relay/helper/price.go:47ratio_setting.GetEffectiveGroupRatio(userGroup, usingGroup, model),优先级(setting/ratio_setting/group_ratio.go:170):

来源
1 GetGroupModelRatio(usingGroup, model) — 分组 × 模型
2 GetGroupGroupRatio(userGroup, usingGroup)
3 GetGroupRatio(usingGroup) — plg = 0.9

命中第 1 层就返回,不再看第 3 层。

plg 用户的 token 被强制锁在 group = "plg"controller/token.go:573),所以走的正是 GetEffectiveGroupRatio("plg", "plg", model)

线上 ?group=plg 的 payload 实测:

"group_ratio": {"plg": 0.9}
"group_model_ratio": null      ← 缺失

对比不带 group 的默认 payload —— 它是 group_model_ratio 的(buildWebsitePricingPayloadDefault)。只有 buildWebsitePublicGroupPricingPayload 这条漏了。

假设给 glm-5GroupModelRatio.plg = 0.6model_ratio = 0.3):

计算 结果
实际计费 0.3 × 2 × 0.6 $0.36
页面显示 0.3 × 2 × 0.9 $0.54 ← 虚高 50%

线上目前 group_model_rationull(尚未配置任何模型级倍率),所以还没暴雷 —— 但机制是坏的,一配就出问题。

改动

buildWebsitePublicGroupPricingPayload 补上 group_model_ratio,复用既有的 filterGroupModelRatioByUsableGroupsAndModels

  • 只保留 plg 分组的条目
  • 只保留 payload 实际暴露的模型
  • 因为传入的是已过滤的 pricing 列表,被隐藏名单摘掉的模型不会从这里泄漏倍率

pricing_version 升为 website-public-plg-v2

前端零改动

enrichVendorNamespricing-page.tsx:1174)本来就调 buildEffectiveGroupRatio 把模型级倍率合进 model.group_ratiogetBestGroupRatio / getMinGroupRatio 也从那里读 —— 前端逻辑一直是对的,只是这个字段从没送到过

新增的前端测试全部一次通过,正好证明了这点。

验证

后端

go build / go vet    干净
go test ./controller/ -run Pricing    全绿

新增 2 个测试:

  • TestBuildWebsitePublicGroupPricingPayloadExposesGroupModelRatio —— 断言只下发 plg 分组、只含可见模型(vip 分组和不可见模型都被剔除)
  • TestBuildWebsitePublicGroupPricingPayloadOmitsHiddenModelRatios —— 断言隐藏模型的倍率不泄漏

同步更新了 TestBuildWebsitePublicGroupPricingPayloadIncludesHiddenPLGOnly 的 JSON 断言(新增字段 + 版本号)。

前端

bun run typecheck    通过
bun test             119 pass / 26 fail

新增 5 个测试(group-model-ratio-pricing.test.ts)钉住优先级:buildEffectiveGroupRatio / getBestGroupRatio / formatModelPrice / 目录表格行,外加一个"未 enrich 时回退分组倍率"的对照。

既有失败(与本 PR 无关,已对照验证)

  • go test ./controller/ 全量有 TestAssetTaskWorkerFailsClosedWhenQueuedUserIsNoLongerUsable 失败 —— stash 后在干净 main 上单跑,同样 FAIL
  • website 26 个失败是 next/server 模块缺失的环境问题,此前多次确认基线相同

部署

  • Router deploy: not required —— 只改了 controller/pricing.go 里公开定价 payload 的构造,不触及 /v1、relay、provider 路由、计费结算或共享中间件。GetGroupModelRatioCopy 是只读取快照。
  • Other deploy targets: newapi-console/api/website/pricing 由控制台服务提供)。website 无代码改动,但要等新 payload 才生效。
  • Risk / validation: 纯展示修正,不改变任何实际扣费。上线后核对 /api/website/pricing?group=plg 返回中含 group_model_ratiopricing_versionwebsite-public-plg-v2;配一个模型级倍率后确认 /zh/models 显示的是折后价。
  • 多节点: 无状态读取,各节点从同一份 option 快照构造,不涉及跨节点协调。该分支绕过 websitePricingCache(响应头 no-store),无缓存一致性问题。

Billing resolves a model's discount through GetEffectiveGroupRatio, which
checks GroupModelRatio (group x model) before falling back to the flat
group ratio. The public PLG payload only carried the flat ratio, so any
model priced below its group — a domestic model set to 0.6 while plg sits
at 0.9, say — was quoted on /models at a price higher than it is charged.

buildWebsitePublicGroupPricingPayload now emits group_model_ratio, reusing
filterGroupModelRatioByUsableGroupsAndModels so the map is restricted to
the plg group and to models the payload actually exposes. Models removed
by the hidden-models filter drop out of the ratio map too, since it is
built from the already-filtered pricing list. pricing_version moves to
website-public-plg-v2.

No website change was needed: enrichVendorNames already folds
group_model_ratio into each model's group_ratio via buildEffectiveGroupRatio,
and getBestGroupRatio reads from there. The override simply never arrived.
Tests were added on both sides to pin the precedence — per-model first,
group ratio second — so the wiring cannot silently regress.

This is display-only. Actual billing already applied the override.
@think-back
think-back merged commit e7d4241 into main Aug 11, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant