tools.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package operation_setting
  2. import (
  3. "strings"
  4. "github.com/QuantumNous/new-api/setting/config"
  5. )
  6. // ---------------------------------------------------------------------------
  7. // Tool call prices ($/1K calls, admin-configurable)
  8. // DB keys: tool_price_setting.prices
  9. // ---------------------------------------------------------------------------
  10. var defaultToolPrices = map[string]float64{
  11. "web_search": 10.0,
  12. "web_search_high": 25.0,
  13. "claude_web_search": 10.0,
  14. "file_search": 2.5,
  15. }
  16. // ToolPriceSetting is managed by config.GlobalConfig.Register.
  17. type ToolPriceSetting struct {
  18. Prices map[string]float64 `json:"prices"`
  19. }
  20. var toolPriceSetting = ToolPriceSetting{
  21. Prices: func() map[string]float64 {
  22. m := make(map[string]float64, len(defaultToolPrices))
  23. for k, v := range defaultToolPrices {
  24. m[k] = v
  25. }
  26. return m
  27. }(),
  28. }
  29. func init() {
  30. config.GlobalConfig.Register("tool_price_setting", &toolPriceSetting)
  31. }
  32. // GetToolPrice returns the configured price for a tool key ($/1K calls),
  33. // falling back to hardcoded default if not overridden.
  34. func GetToolPrice(key string) float64 {
  35. if v, ok := toolPriceSetting.Prices[key]; ok {
  36. return v
  37. }
  38. if v, ok := defaultToolPrices[key]; ok {
  39. return v
  40. }
  41. return 0
  42. }
  43. // ---------------------------------------------------------------------------
  44. // GPT Image 1 per-call pricing (special: depends on quality + size)
  45. // ---------------------------------------------------------------------------
  46. const (
  47. GPTImage1Low1024x1024 = 0.011
  48. GPTImage1Low1024x1536 = 0.016
  49. GPTImage1Low1536x1024 = 0.016
  50. GPTImage1Medium1024x1024 = 0.042
  51. GPTImage1Medium1024x1536 = 0.063
  52. GPTImage1Medium1536x1024 = 0.063
  53. GPTImage1High1024x1024 = 0.167
  54. GPTImage1High1024x1536 = 0.25
  55. GPTImage1High1536x1024 = 0.25
  56. )
  57. func GetGPTImage1PriceOnceCall(quality string, size string) float64 {
  58. prices := map[string]map[string]float64{
  59. "low": {
  60. "1024x1024": GPTImage1Low1024x1024,
  61. "1024x1536": GPTImage1Low1024x1536,
  62. "1536x1024": GPTImage1Low1536x1024,
  63. },
  64. "medium": {
  65. "1024x1024": GPTImage1Medium1024x1024,
  66. "1024x1536": GPTImage1Medium1024x1536,
  67. "1536x1024": GPTImage1Medium1536x1024,
  68. },
  69. "high": {
  70. "1024x1024": GPTImage1High1024x1024,
  71. "1024x1536": GPTImage1High1024x1536,
  72. "1536x1024": GPTImage1High1536x1024,
  73. },
  74. }
  75. if qualityMap, exists := prices[quality]; exists {
  76. if price, exists := qualityMap[size]; exists {
  77. return price
  78. }
  79. }
  80. return GPTImage1High1024x1024
  81. }
  82. // ---------------------------------------------------------------------------
  83. // Gemini audio input pricing (per-million tokens, model-specific)
  84. // ---------------------------------------------------------------------------
  85. const (
  86. Gemini25FlashPreviewInputAudioPrice = 1.00
  87. Gemini25FlashProductionInputAudioPrice = 1.00
  88. Gemini25FlashLitePreviewInputAudioPrice = 0.50
  89. Gemini25FlashNativeAudioInputAudioPrice = 3.00
  90. Gemini20FlashInputAudioPrice = 0.70
  91. GeminiRoboticsER15InputAudioPrice = 1.00
  92. )
  93. func GetGeminiInputAudioPricePerMillionTokens(modelName string) float64 {
  94. if strings.HasPrefix(modelName, "gemini-2.5-flash-preview-native-audio") {
  95. return Gemini25FlashNativeAudioInputAudioPrice
  96. } else if strings.HasPrefix(modelName, "gemini-2.5-flash-preview-lite") {
  97. return Gemini25FlashLitePreviewInputAudioPrice
  98. } else if strings.HasPrefix(modelName, "gemini-2.5-flash-preview") {
  99. return Gemini25FlashPreviewInputAudioPrice
  100. } else if strings.HasPrefix(modelName, "gemini-2.5-flash") {
  101. return Gemini25FlashProductionInputAudioPrice
  102. } else if strings.HasPrefix(modelName, "gemini-2.0-flash") {
  103. return Gemini20FlashInputAudioPrice
  104. } else if strings.HasPrefix(modelName, "gemini-robotics-er-1.5") {
  105. return GeminiRoboticsER15InputAudioPrice
  106. }
  107. return 0
  108. }