feat: implement tool pricing settings UI and enhance tool call quota calculations
This commit is contained in:
+14
-28
@@ -2,7 +2,6 @@ package service
|
||||
|
||||
import (
|
||||
"math"
|
||||
"strings"
|
||||
|
||||
"github.com/QuantumNous/new-api/common"
|
||||
"github.com/QuantumNous/new-api/setting/operation_setting"
|
||||
@@ -10,9 +9,9 @@ import (
|
||||
|
||||
// ToolCallUsage captures all tool call counts from a single request.
|
||||
type ToolCallUsage struct {
|
||||
ModelName string
|
||||
WebSearchCalls int
|
||||
WebSearchModelName string
|
||||
ClaudeWebSearchCalls int
|
||||
WebSearchToolName string // "web_search_preview", "web_search", etc.
|
||||
FileSearchCalls int
|
||||
ImageGenerationCall bool
|
||||
ImageGenerationQuality string
|
||||
@@ -34,33 +33,25 @@ type ToolCallResult struct {
|
||||
Items []ToolCallItem `json:"items,omitempty"`
|
||||
}
|
||||
|
||||
func getWebSearchPriceKey(modelName string) string {
|
||||
isNormalPrice :=
|
||||
strings.HasPrefix(modelName, "o3") ||
|
||||
strings.HasPrefix(modelName, "o4") ||
|
||||
strings.HasPrefix(modelName, "gpt-5")
|
||||
if isNormalPrice {
|
||||
return "web_search"
|
||||
}
|
||||
return "web_search_high"
|
||||
}
|
||||
|
||||
// ComputeToolCallQuota calculates the total quota for all tool calls in a
|
||||
// request. All tool prices are $/1K calls (configurable via ToolCallPrices
|
||||
// option). groupRatio is applied. Per-call billing (UsePrice) callers should
|
||||
// NOT add this result — per-call price already includes everything.
|
||||
// request. Tool prices are resolved via GetToolPriceForModel which supports
|
||||
// model-prefix overrides. groupRatio is applied.
|
||||
func ComputeToolCallQuota(usage ToolCallUsage, groupRatio float64) ToolCallResult {
|
||||
var items []ToolCallItem
|
||||
totalQuota := 0
|
||||
|
||||
addItem := func(name string, count int, pricePer1K float64) {
|
||||
if count <= 0 || pricePer1K <= 0 {
|
||||
addItem := func(toolName string, count int) {
|
||||
if count <= 0 {
|
||||
return
|
||||
}
|
||||
pricePer1K := operation_setting.GetToolPriceForModel(toolName, usage.ModelName)
|
||||
if pricePer1K <= 0 {
|
||||
return
|
||||
}
|
||||
totalPrice := pricePer1K * float64(count) / 1000
|
||||
quota := int(math.Round(totalPrice * common.QuotaPerUnit * groupRatio))
|
||||
items = append(items, ToolCallItem{
|
||||
Name: name,
|
||||
Name: toolName,
|
||||
CallCount: count,
|
||||
PricePer1K: pricePer1K,
|
||||
TotalPrice: totalPrice,
|
||||
@@ -69,17 +60,12 @@ func ComputeToolCallQuota(usage ToolCallUsage, groupRatio float64) ToolCallResul
|
||||
totalQuota += quota
|
||||
}
|
||||
|
||||
if usage.WebSearchCalls > 0 {
|
||||
priceKey := getWebSearchPriceKey(usage.WebSearchModelName)
|
||||
addItem("web_search", usage.WebSearchCalls, operation_setting.GetToolPrice(priceKey))
|
||||
}
|
||||
|
||||
if usage.ClaudeWebSearchCalls > 0 {
|
||||
addItem("claude_web_search", usage.ClaudeWebSearchCalls, operation_setting.GetToolPrice("claude_web_search"))
|
||||
if usage.WebSearchCalls > 0 && usage.WebSearchToolName != "" {
|
||||
addItem(usage.WebSearchToolName, usage.WebSearchCalls)
|
||||
}
|
||||
|
||||
if usage.FileSearchCalls > 0 {
|
||||
addItem("file_search", usage.FileSearchCalls, operation_setting.GetToolPrice("file_search"))
|
||||
addItem("file_search", usage.FileSearchCalls)
|
||||
}
|
||||
|
||||
if usage.ImageGenerationCall {
|
||||
|
||||
Reference in New Issue
Block a user