Merge pull request #1590 from yyhhyyyyyy/fix/openrouter-custom-ratio-billing
fix: prevent OpenRouter cache calculation with custom model ratios
This commit is contained in:
@@ -38,6 +38,14 @@ type QuotaInfo struct {
|
|||||||
GroupRatio float64
|
GroupRatio float64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func hasCustomModelRatio(modelName string, currentRatio float64) bool {
|
||||||
|
defaultRatio, exists := ratio_setting.GetDefaultModelRatioMap()[modelName]
|
||||||
|
if !exists {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return currentRatio != defaultRatio
|
||||||
|
}
|
||||||
|
|
||||||
func calculateAudioQuota(info QuotaInfo) int {
|
func calculateAudioQuota(info QuotaInfo) int {
|
||||||
if info.UsePrice {
|
if info.UsePrice {
|
||||||
modelPrice := decimal.NewFromFloat(info.ModelPrice)
|
modelPrice := decimal.NewFromFloat(info.ModelPrice)
|
||||||
@@ -246,9 +254,10 @@ func PostClaudeConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo,
|
|||||||
|
|
||||||
if relayInfo.ChannelType == constant.ChannelTypeOpenRouter {
|
if relayInfo.ChannelType == constant.ChannelTypeOpenRouter {
|
||||||
promptTokens -= cacheTokens
|
promptTokens -= cacheTokens
|
||||||
if cacheCreationTokens == 0 && relayInfo.PriceData.CacheCreationRatio != 1 && usage.Cost != 0 {
|
isUsingCustomSettings := relayInfo.PriceData.UsePrice || hasCustomModelRatio(modelName, relayInfo.PriceData.ModelRatio)
|
||||||
|
if cacheCreationTokens == 0 && relayInfo.PriceData.CacheCreationRatio != 1 && usage.Cost != 0 && !isUsingCustomSettings {
|
||||||
maybeCacheCreationTokens := CalcOpenRouterCacheCreateTokens(*usage, relayInfo.PriceData)
|
maybeCacheCreationTokens := CalcOpenRouterCacheCreateTokens(*usage, relayInfo.PriceData)
|
||||||
if promptTokens >= maybeCacheCreationTokens {
|
if maybeCacheCreationTokens >= 0 && promptTokens >= maybeCacheCreationTokens {
|
||||||
cacheCreationTokens = maybeCacheCreationTokens
|
cacheCreationTokens = maybeCacheCreationTokens
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user