Version: 0.9.80.dev.260506
后端: 1. LLM 独立服务与统一计费出口落地:新增 `cmd/llm`、`client/llm` 与 `services/llm/rpc`,补齐 BillingContext、CreditBalanceGuard、价格规则解析、stream usage 归集与 `credit.charge.requested` outbox 发布,active-scheduler / agent / course / memory / gateway fallback 全部改走 llm zrpc,不再各自本地初始化模型。 2. TokenStore 收口为 Credit 权威账本:新增 credit account / ledger / product / order / price-rule / reward-rule 能力与 Redis 快照缓存,扩展 tokenstore rpc/client 支撑余额快照、消耗看板、商品、订单、流水、价格规则和奖励规则,并接入 LLM charge 事件消费完成 Credit 扣费落账。 3. 计费旧链路下线与网关切口切换:`/token-store` 语义整体切到 `/credit-store`,agent chat 移除旧 TokenQuotaGuard,userauth 的 CheckTokenQuota / AdjustTokenUsage 改为废弃,聊天历史落库不再同步旧 token 额度账本,course 图片解析请求补 user_id 进入新计费口径。 前端: 4. 计划广场从 mock 数据切到真实接口:新增 forum api/types,首页支持真实列表、标签、搜索、防抖、点赞、导入和发布计划,详情页补齐帖子详情、评论树、回复和删除评论链路,同时补上“至少一个标签”的前后端约束与默认标签兜底。 5. 商店页切到 Credit 体系并重做展示:顶部改为余额 + Credit/Token 消耗看板,支持 24h/7d/30d/all 周期切换;套餐区展示原价与当前价;历史区改为当前用户 Credit 流水并支持查看更多,整体视觉和交互同步收口。 仓库: 6. 配置与本地启动体系补齐 llm / outbox 编排:`config.example.yaml` 增加 llm rpc 和统一 outbox service 配置,`dev-common.ps1` 把 llm 纳入多服务依赖并自动建 Kafka topic,`docker-compose.yml` 同步初始化 agent/task/memory/active-scheduler/notification/taskclass-forum/llm/token-store 全量 outbox topic。
This commit is contained in:
207
backend/services/llm/pricing.go
Normal file
207
backend/services/llm/pricing.go
Normal file
@@ -0,0 +1,207 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
llmdao "github.com/LoveLosita/smartflow/backend/services/llm/dao"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultPriceRuleCacheTTL = time.Minute
|
||||
tokenPriceScalePer1K = int64(1000)
|
||||
rmbMicrosPerYuan = int64(1_000_000)
|
||||
)
|
||||
|
||||
type UsagePricingInput struct {
|
||||
Scene string
|
||||
ProviderName string
|
||||
ModelName string
|
||||
InputTokens int64
|
||||
OutputTokens int64
|
||||
CachedTokens int64
|
||||
ReasoningTokens int64
|
||||
}
|
||||
|
||||
type UsagePriceQuote struct {
|
||||
RuleID uint64
|
||||
RMBCostMicros int64
|
||||
CreditCost int64
|
||||
MatchedScene string
|
||||
MatchedProvider string
|
||||
MatchedModel string
|
||||
}
|
||||
|
||||
type UsagePricingResolver interface {
|
||||
Resolve(ctx context.Context, input UsagePricingInput) (UsagePriceQuote, error)
|
||||
}
|
||||
|
||||
type CreditPriceResolverOptions struct {
|
||||
DAO *llmdao.PriceRuleDAO
|
||||
CacheTTL time.Duration
|
||||
}
|
||||
|
||||
type CreditPriceResolver struct {
|
||||
dao *llmdao.PriceRuleDAO
|
||||
cacheTTL time.Duration
|
||||
|
||||
mu sync.RWMutex
|
||||
cachedAt time.Time
|
||||
cachedSet []llmdao.CreditPriceRule
|
||||
}
|
||||
|
||||
func NewCreditPriceResolver(opts CreditPriceResolverOptions) *CreditPriceResolver {
|
||||
cacheTTL := opts.CacheTTL
|
||||
if cacheTTL <= 0 {
|
||||
cacheTTL = defaultPriceRuleCacheTTL
|
||||
}
|
||||
return &CreditPriceResolver{
|
||||
dao: opts.DAO,
|
||||
cacheTTL: cacheTTL,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *CreditPriceResolver) Resolve(ctx context.Context, input UsagePricingInput) (UsagePriceQuote, error) {
|
||||
if r == nil || r.dao == nil {
|
||||
return UsagePriceQuote{}, nil
|
||||
}
|
||||
|
||||
rules, err := r.loadRules(ctx)
|
||||
if err != nil {
|
||||
return UsagePriceQuote{}, err
|
||||
}
|
||||
if len(rules) == 0 {
|
||||
return UsagePriceQuote{}, nil
|
||||
}
|
||||
|
||||
scene := strings.TrimSpace(input.Scene)
|
||||
providerName := strings.TrimSpace(input.ProviderName)
|
||||
modelName := strings.TrimSpace(input.ModelName)
|
||||
|
||||
for _, rule := range rules {
|
||||
if !matchesPriceRuleField(rule.Scene, scene) {
|
||||
continue
|
||||
}
|
||||
if !matchesPriceRuleField(rule.ProviderName, providerName) {
|
||||
continue
|
||||
}
|
||||
if !matchesPriceRuleField(rule.ModelName, modelName) {
|
||||
continue
|
||||
}
|
||||
return quoteUsagePrice(rule, input), nil
|
||||
}
|
||||
|
||||
return UsagePriceQuote{}, nil
|
||||
}
|
||||
|
||||
func (r *CreditPriceResolver) loadRules(ctx context.Context) ([]llmdao.CreditPriceRule, error) {
|
||||
now := time.Now()
|
||||
|
||||
r.mu.RLock()
|
||||
if len(r.cachedSet) > 0 && now.Sub(r.cachedAt) < r.cacheTTL {
|
||||
rules := clonePriceRules(r.cachedSet)
|
||||
r.mu.RUnlock()
|
||||
return rules, nil
|
||||
}
|
||||
r.mu.RUnlock()
|
||||
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
|
||||
if len(r.cachedSet) > 0 && now.Sub(r.cachedAt) < r.cacheTTL {
|
||||
return clonePriceRules(r.cachedSet), nil
|
||||
}
|
||||
|
||||
rules, err := r.dao.ListActiveRules(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
r.cachedSet = clonePriceRules(rules)
|
||||
r.cachedAt = now
|
||||
return clonePriceRules(r.cachedSet), nil
|
||||
}
|
||||
|
||||
func clonePriceRules(input []llmdao.CreditPriceRule) []llmdao.CreditPriceRule {
|
||||
if len(input) == 0 {
|
||||
return nil
|
||||
}
|
||||
output := make([]llmdao.CreditPriceRule, len(input))
|
||||
copy(output, input)
|
||||
return output
|
||||
}
|
||||
|
||||
func matchesPriceRuleField(ruleValue string, actual string) bool {
|
||||
ruleValue = strings.TrimSpace(ruleValue)
|
||||
actual = strings.TrimSpace(actual)
|
||||
|
||||
if ruleValue == "" || ruleValue == "*" {
|
||||
return true
|
||||
}
|
||||
return strings.EqualFold(ruleValue, actual)
|
||||
}
|
||||
|
||||
func quoteUsagePrice(rule llmdao.CreditPriceRule, input UsagePricingInput) UsagePriceQuote {
|
||||
inputTokens := maxInt64(input.InputTokens, 0)
|
||||
outputTokens := maxInt64(input.OutputTokens, 0)
|
||||
cachedTokens := clampInt64(input.CachedTokens, 0, inputTokens)
|
||||
reasoningTokens := clampInt64(input.ReasoningTokens, 0, outputTokens)
|
||||
|
||||
nonCachedInputTokens := inputTokens - cachedTokens
|
||||
nonReasoningOutputTokens := outputTokens - reasoningTokens
|
||||
|
||||
cachedPriceMicros := rule.CachedPriceMicros
|
||||
if cachedPriceMicros <= 0 {
|
||||
cachedPriceMicros = rule.InputPriceMicros
|
||||
}
|
||||
reasoningPriceMicros := rule.ReasoningPriceMicros
|
||||
if reasoningPriceMicros <= 0 {
|
||||
reasoningPriceMicros = rule.OutputPriceMicros
|
||||
}
|
||||
|
||||
totalMicrosScaled := nonCachedInputTokens*maxInt64(rule.InputPriceMicros, 0) +
|
||||
cachedTokens*maxInt64(cachedPriceMicros, 0) +
|
||||
nonReasoningOutputTokens*maxInt64(rule.OutputPriceMicros, 0) +
|
||||
reasoningTokens*maxInt64(reasoningPriceMicros, 0)
|
||||
|
||||
rmbCostMicros := ceilDivInt64(totalMicrosScaled, tokenPriceScalePer1K)
|
||||
creditCost := int64(0)
|
||||
if rmbCostMicros > 0 && rule.CreditPerYuan > 0 {
|
||||
creditCost = ceilDivInt64(rmbCostMicros*rule.CreditPerYuan, rmbMicrosPerYuan)
|
||||
}
|
||||
|
||||
return UsagePriceQuote{
|
||||
RuleID: rule.ID,
|
||||
RMBCostMicros: rmbCostMicros,
|
||||
CreditCost: creditCost,
|
||||
MatchedScene: strings.TrimSpace(rule.Scene),
|
||||
MatchedProvider: strings.TrimSpace(rule.ProviderName),
|
||||
MatchedModel: strings.TrimSpace(rule.ModelName),
|
||||
}
|
||||
}
|
||||
|
||||
func ceilDivInt64(numerator int64, denominator int64) int64 {
|
||||
if numerator <= 0 || denominator <= 0 {
|
||||
return 0
|
||||
}
|
||||
return (numerator + denominator - 1) / denominator
|
||||
}
|
||||
|
||||
func clampInt64(value int64, minValue int64, maxValue int64) int64 {
|
||||
if value < minValue {
|
||||
return minValue
|
||||
}
|
||||
if value > maxValue {
|
||||
return maxValue
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func maxInt64(value int64, minValue int64) int64 {
|
||||
if value < minValue {
|
||||
return minValue
|
||||
}
|
||||
return value
|
||||
}
|
||||
Reference in New Issue
Block a user