feat: enhance cache_create_tokens calculation for OpenRouter
This commit is contained in:
@@ -178,6 +178,8 @@ type Usage struct {
|
|||||||
InputTokens int `json:"input_tokens"`
|
InputTokens int `json:"input_tokens"`
|
||||||
OutputTokens int `json:"output_tokens"`
|
OutputTokens int `json:"output_tokens"`
|
||||||
InputTokensDetails *InputTokenDetails `json:"input_tokens_details"`
|
InputTokensDetails *InputTokenDetails `json:"input_tokens_details"`
|
||||||
|
// OpenRouter Params
|
||||||
|
Cost float64 `json:"cost,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type InputTokenDetails struct {
|
type InputTokenDetails struct {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package service
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"one-api/common"
|
"one-api/common"
|
||||||
constant2 "one-api/constant"
|
constant2 "one-api/constant"
|
||||||
"one-api/dto"
|
"one-api/dto"
|
||||||
@@ -214,6 +215,11 @@ func PostClaudeConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo,
|
|||||||
cacheCreationRatio := priceData.CacheCreationRatio
|
cacheCreationRatio := priceData.CacheCreationRatio
|
||||||
cacheCreationTokens := usage.PromptTokensDetails.CachedCreationTokens
|
cacheCreationTokens := usage.PromptTokensDetails.CachedCreationTokens
|
||||||
|
|
||||||
|
if relayInfo.ChannelType == common.ChannelTypeOpenRouter && priceData.CacheCreationRatio != 1 {
|
||||||
|
cacheCreationTokens = CalcOpenRouterCacheCreateTokens(*usage, priceData)
|
||||||
|
promptTokens = promptTokens - cacheCreationTokens - cacheTokens
|
||||||
|
}
|
||||||
|
|
||||||
calculateQuota := 0.0
|
calculateQuota := 0.0
|
||||||
if !priceData.UsePrice {
|
if !priceData.UsePrice {
|
||||||
calculateQuota = float64(promptTokens)
|
calculateQuota = float64(promptTokens)
|
||||||
@@ -261,6 +267,27 @@ func PostClaudeConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo,
|
|||||||
tokenName, quota, logContent, relayInfo.TokenId, userQuota, int(useTimeSeconds), relayInfo.IsStream, relayInfo.Group, other)
|
tokenName, quota, logContent, relayInfo.TokenId, userQuota, int(useTimeSeconds), relayInfo.IsStream, relayInfo.Group, other)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CalcOpenRouterCacheCreateTokens(usage dto.Usage, priceData helper.PriceData) int {
|
||||||
|
if priceData.CacheCreationRatio == 1 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
quotaPrice := priceData.ModelRatio / common.QuotaPerUnit
|
||||||
|
promptCacheCreatePrice := quotaPrice * priceData.CacheCreationRatio
|
||||||
|
promptCacheReadPrice := quotaPrice * priceData.CacheRatio
|
||||||
|
completionPrice := quotaPrice * priceData.CompletionRatio
|
||||||
|
|
||||||
|
cost := usage.Cost
|
||||||
|
totalPromptTokens := float64(usage.PromptTokens)
|
||||||
|
completionTokens := float64(usage.CompletionTokens)
|
||||||
|
promptCacheReadTokens := float64(usage.PromptTokensDetails.CachedTokens)
|
||||||
|
|
||||||
|
return int(math.Round((cost -
|
||||||
|
totalPromptTokens*quotaPrice +
|
||||||
|
promptCacheReadTokens*(quotaPrice-promptCacheReadPrice) -
|
||||||
|
completionTokens*completionPrice) /
|
||||||
|
(promptCacheCreatePrice - quotaPrice)))
|
||||||
|
}
|
||||||
|
|
||||||
func PostAudioConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo,
|
func PostAudioConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo,
|
||||||
usage *dto.Usage, preConsumedQuota int, userQuota int, priceData helper.PriceData, extraContent string) {
|
usage *dto.Usage, preConsumedQuota int, userQuota int, priceData helper.PriceData, extraContent string) {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user