Skip to content

Use request model names for pricing#127

Open
yueziji wants to merge 12 commits into
Willxup:mainfrom
yueziji:pricing-model-names-fix
Open

Use request model names for pricing#127
yueziji wants to merge 12 commits into
Willxup:mainfrom
yueziji:pricing-model-names-fix

Conversation

@yueziji

@yueziji yueziji commented May 19, 2026

Copy link
Copy Markdown
Contributor

Summary: use stored usage_events.model values for pricing model options and validation; remove pricing dependency on CPA /v1/models aliases; cover alias-vs-request-model behavior in pricing service tests. Validation: fork PR yueziji#1 passed backend on macOS, Ubuntu, Windows and frontend checks. Local compile check: go test -run ^$ ./internal/service ./internal/api. Review note: existing alias-keyed pricing rows may remain visible after upgrade but will not be used for cost calculation; this PR restores correct keys for new and updated pricing.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the PricingService to remove its dependency on the external CPA models endpoint, simplifying the implementation to use local database records for identifying used models. This involves removing the ModelsFetcher interface and its associated logic. Feedback from the review focuses on the need to propagate the context.Context to all database operations within the service methods to ensure that GORM can correctly handle request cancellations and timeouts.

Comment thread internal/service/pricing_service.go Outdated
Comment thread internal/service/pricing_service.go Outdated
Comment thread internal/service/pricing_service.go Outdated
Comment thread internal/service/pricing_service.go Outdated
Comment thread internal/service/pricing_service.go Outdated
@Willxup

Willxup commented May 19, 2026

Copy link
Copy Markdown
Owner

模型价格这块恐怕不能这么改。
两种方式

  • 只取使用的模型
  • 计算价格时优先别名进行计算

你选择了第一种方式,但是这里有个问题是,数据来源于usage_events,需要对这个表做聚合。数据量大起来后,每次使用pricing都会做一次聚合,我觉得这样不是很好,我花了大量时间做用户看不到的增量聚合+边界补差,就是为了减少对usage_events的直接引用。

比较好的方案是:新建model表,在同步usage_events时进行同步。

但是现在的问题是,issue中有不少希望通过别名进行价格计算的。所以我可能更偏向于,模型别名优先计价的方式。
但我还没有分析cpa那边alias的真实数据情况下,别名的数据是什么样的。如果别名相同,也会造成一些问题。

这个我觉得可以先讨论一下。如果方便的话,咱们社交工具交流也可以的。

@yueziji

yueziji commented May 20, 2026

Copy link
Copy Markdown
Contributor Author

我的想法是现在是成本计算,应该是按照上游的模型名来计算成本,之前cpa没有移除统计的时候也是这个逻辑,不过新建model表也可以,我研究研究

@Willxup

Willxup commented May 20, 2026

Copy link
Copy Markdown
Owner

我的想法是现在是成本计算,应该是按照上游的模型名来计算成本,之前cpa没有移除统计的时候也是这个逻辑,不过新建model表也可以,我研究研究

我也考虑了一下,在不引入复杂度的情况下,按照上游模型来计算成本是改动量最小的了。新表可以做迁移计划,然后在可以写一个新的runner,固定时间去聚合model,做增量即可。我建议固定时间不要太短,1分钟左右应该是大家能接受的吧。

以上是我的建议,供参考。

@yueziji

yueziji commented May 21, 2026

Copy link
Copy Markdown
Contributor Author

我的想法是现在是成本计算,应该是按照上游的模型名来计算成本,之前cpa没有移除统计的时候也是这个逻辑,不过新建model表也可以,我研究研究

我也考虑了一下,在不引入复杂度的情况下,按照上游模型来计算成本是改动量最小的了。新表可以做迁移计划,然后在可以写一个新的runner,固定时间去聚合model,做增量即可。我建议固定时间不要太短,1分钟左右应该是大家能接受的吧。

以上是我的建议,供参考。

我想了想按别名主要是为了看真实花了多少钱,但是同一个别名可以指向不同的ai提供商,他们的价格可能并不一样,所以我想的是按照ai提供商+请求上游的模型名一起匹配,就比如 claude官渠/claude-opus-4-6 和 第三方渠道/claude-opus-4-6 这样的形式,如果没有ai提供商,就回落到单独模型名字,比如 claude-opus-4-6 来计费

@Willxup

Willxup commented May 21, 2026

Copy link
Copy Markdown
Owner

我的想法是现在是成本计算,应该是按照上游的模型名来计算成本,之前cpa没有移除统计的时候也是这个逻辑,不过新建model表也可以,我研究研究

我也考虑了一下,在不引入复杂度的情况下,按照上游模型来计算成本是改动量最小的了。新表可以做迁移计划,然后在可以写一个新的runner,固定时间去聚合model,做增量即可。我建议固定时间不要太短,1分钟左右应该是大家能接受的吧。
以上是我的建议,供参考。

我想了想按别名主要是为了看真实花了多少钱,但是同一个别名可以指向不同的ai提供商,他们的价格可能并不一样,所以我想的是按照ai提供商+请求上游的模型名一起匹配,就比如 claude官渠/claude-opus-4-6 和 第三方渠道/claude-opus-4-6 这样的形式,如果没有ai提供商,就回落到单独模型名字,比如 claude-opus-4-6 来计费

嗯嗯,这种方式肯定更好,但是还需要考虑cpa返回的model_alias是仅模型名的话,我们应该可以通过Prefix进行拼接吧,感觉这个还要相关的数据支撑才行。

@Willxup

Willxup commented May 24, 2026

Copy link
Copy Markdown
Owner

怎么样了,开发进度如何,有没有可以同步的信息?感谢你为keeper做的努力,我最近有些忙,更新的慢了。

yueziji added 2 commits May 25, 2026 15:24
Filter soft-deleted identities from pricing source lookups and
automatically rename source-qualified pricing keys (e.g.
"provider(old.host)/model" → "provider(new.host)/model") when a
provider's display name changes during metadata sync.
@Willxup

Willxup commented May 25, 2026

Copy link
Copy Markdown
Owner

我看了下代码,现在看上去后端分歧是比较大的,价格这块要不还是先放一放吧。我后面看看怎么调整后端价格计算吧。
因为我现在需要进行新的开发,当前PR可能会出现很大的冲突,我建议当前PR先关闭,后续再讨论继续吧。
再次感谢你的贡献。

@Willxup

Willxup commented May 25, 2026

Copy link
Copy Markdown
Owner

我看了下代码,现在看上去后端分歧是比较大的,价格这块要不还是先放一放吧。我后面看看怎么调整后端价格计算吧。 因为我现在需要进行新的开发,当前PR可能会出现很大的冲突,我建议当前PR先关闭,后续再讨论继续吧。 再次感谢你的贡献。

也可以保留该PR,咱们也可以随时沟通,我目前下一步是准备重构一下限额查询这块,因为也涉及了价格计算,所以我会把相关价格计算都放在helper目录下。

@yueziji

yueziji commented May 25, 2026

Copy link
Copy Markdown
Contributor Author

可以,等到稳定了我再改改吧

@Willxup

Willxup commented May 25, 2026

Copy link
Copy Markdown
Owner

可以,等到稳定了我再改改吧

OKOK,随时沟通

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.

2 participants