From ae254f5368ff3764ab0d23b4f6dd2c54411a359c Mon Sep 17 00:00:00 2001 From: creamlike1024 Date: Wed, 7 May 2025 19:33:32 +0800 Subject: [PATCH] fix: tool quota calculate --- relay/relay-text.go | 6 +++--- web/src/helpers/render.js | 21 ++++++++++++++------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/relay/relay-text.go b/relay/relay-text.go index 89a6a973..e0b6ad0e 100644 --- a/relay/relay-text.go +++ b/relay/relay-text.go @@ -364,11 +364,11 @@ func postConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, var webSearchPrice float64 if relayInfo.ResponsesUsageInfo != nil { if webSearchTool, exists := relayInfo.ResponsesUsageInfo.BuiltInTools[dto.BuildInToolWebSearchPreview]; exists && webSearchTool.CallCount > 0 { - // 计算 web search 调用的配额 (配额 = 价格 * 调用次数 / 1000) + // 计算 web search 调用的配额 (配额 = 价格 * 调用次数 / 1000 * 分组倍率) webSearchPrice = operation_setting.GetWebSearchPricePerThousand(modelName, webSearchTool.SearchContextSize) dWebSearchQuota = decimal.NewFromFloat(webSearchPrice). Mul(decimal.NewFromInt(int64(webSearchTool.CallCount))). - Div(decimal.NewFromInt(1000)) + Div(decimal.NewFromInt(1000)).Mul(dGroupRatio).Mul(dQuotaPerUnit) extraContent += fmt.Sprintf("Web Search 调用 %d 次,上下文大小 %s,调用花费 $%s", webSearchTool.CallCount, webSearchTool.SearchContextSize, dWebSearchQuota.String()) } @@ -381,7 +381,7 @@ func postConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, fileSearchPrice = operation_setting.GetFileSearchPricePerThousand() dFileSearchQuota = decimal.NewFromFloat(fileSearchPrice). Mul(decimal.NewFromInt(int64(fileSearchTool.CallCount))). - Div(decimal.NewFromInt(1000)) + Div(decimal.NewFromInt(1000)).Mul(dGroupRatio).Mul(dQuotaPerUnit) extraContent += fmt.Sprintf("File Search 调用 %d 次,调用花费 $%s", fileSearchTool.CallCount, dFileSearchQuota.String()) } diff --git a/web/src/helpers/render.js b/web/src/helpers/render.js index fb4c3dbd..5a59356b 100644 --- a/web/src/helpers/render.js +++ b/web/src/helpers/render.js @@ -354,8 +354,8 @@ export function renderModelPrice( let price = (effectiveInputTokens / 1000000) * inputRatioPrice * groupRatio + (completionTokens / 1000000) * completionRatioPrice * groupRatio + - (webSearchCallCount / 1000) * webSearchPrice + - (fileSearchCallCount / 1000) * fileSearchPrice; + (webSearchCallCount / 1000) * webSearchPrice * groupRatio + + (fileSearchCallCount / 1000) * fileSearchPrice * groupRatio; return ( <> @@ -446,7 +446,7 @@ export function renderModelPrice( ) : webSearch && webSearchCallCount > 0 && !image && !fileSearch ? i18next.t( - '输入 {{input}} tokens / 1M tokens * ${{price}} + 输出 {{completion}} tokens / 1M tokens * ${{compPrice}} * 分组 {{ratio}} + Web搜索 {{webSearchCallCount}}次 / 1K 次 * ${{webSearchPrice}} = ${{total}}', + '输入 {{input}} tokens / 1M tokens * ${{price}} + 输出 {{completion}} tokens / 1M tokens * ${{compPrice}} * 分组 {{ratio}} + Web搜索 {{webSearchCallCount}}次 / 1K 次 * ${{webSearchPrice}} * {{ratio}} = ${{total}}', { input: inputTokens, price: inputRatioPrice, @@ -458,9 +458,12 @@ export function renderModelPrice( total: price.toFixed(6), }, ) - : fileSearch && fileSearchCallCount > 0 && !image && !webSearch + : fileSearch && + fileSearchCallCount > 0 && + !image && + !webSearch ? i18next.t( - '输入 {{input}} tokens / 1M tokens * ${{price}} + 输出 {{completion}} tokens / 1M tokens * ${{compPrice}} * 分组 {{ratio}} + 文件搜索 {{fileSearchCallCount}}次 / 1K 次 * ${{fileSearchPrice}} = ${{total}}', + '输入 {{input}} tokens / 1M tokens * ${{price}} + 输出 {{completion}} tokens / 1M tokens * ${{compPrice}} * 分组 {{ratio}} + 文件搜索 {{fileSearchCallCount}}次 / 1K 次 * ${{fileSearchPrice}} * {{ratio}}= ${{total}}', { input: inputTokens, price: inputRatioPrice, @@ -472,9 +475,13 @@ export function renderModelPrice( total: price.toFixed(6), }, ) - : webSearch && webSearchCallCount > 0 && fileSearch && fileSearchCallCount > 0 && !image + : webSearch && + webSearchCallCount > 0 && + fileSearch && + fileSearchCallCount > 0 && + !image ? i18next.t( - '输入 {{input}} tokens / 1M tokens * ${{price}} + 输出 {{completion}} tokens / 1M tokens * ${{compPrice}} * 分组 {{ratio}} + Web搜索 {{webSearchCallCount}}次 / 1K 次 * ${{webSearchPrice}} + 文件搜索 {{fileSearchCallCount}}次 / 1K 次 * ${{fileSearchPrice}} = ${{total}}', + '输入 {{input}} tokens / 1M tokens * ${{price}} + 输出 {{completion}} tokens / 1M tokens * ${{compPrice}} * 分组 {{ratio}} + Web搜索 {{webSearchCallCount}}次 / 1K 次 * ${{webSearchPrice}} * {{ratio}}+ 文件搜索 {{fileSearchCallCount}}次 / 1K 次 * ${{fileSearchPrice}} * {{ratio}}= ${{total}}', { input: inputTokens, price: inputRatioPrice,