tools.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package operation_setting
  2. import (
  3. "sort"
  4. "strings"
  5. "sync/atomic"
  6. "github.com/QuantumNous/new-api/setting/config"
  7. )
  8. // ---------------------------------------------------------------------------
  9. // Tool call prices ($/1K calls, admin-configurable)
  10. // DB key: tool_price_setting.prices
  11. //
  12. // Key format:
  13. // - "tool_name" → default price for all models
  14. // - "tool_name:model_prefix*" → override for models matching the prefix
  15. //
  16. // Lookup order: longest prefix match → default → hardcoded fallback → 0
  17. // ---------------------------------------------------------------------------
  18. var defaultToolPrices = map[string]float64{
  19. "web_search": 10.0, // OpenAI web search (all models) / Claude web search
  20. "web_search_preview": 10.0, // OpenAI web search preview (default: reasoning models)
  21. "file_search": 2.5, // OpenAI file search (Responses API)
  22. "google_search": 14.0, // Gemini Grounding with Google Search
  23. }
  24. var defaultToolPriceOverrides = map[string]float64{
  25. "web_search_preview:gpt-4o*": 25.0, // non-reasoning models
  26. "web_search_preview:gpt-4.1*": 25.0,
  27. "web_search_preview:gpt-4o-mini*": 25.0,
  28. "web_search_preview:gpt-4.1-mini*": 25.0,
  29. }
  30. // ToolPriceSetting is managed by config.GlobalConfig.Register.
  31. type ToolPriceSetting struct {
  32. Prices map[string]float64 `json:"prices"`
  33. }
  34. var toolPriceSetting = ToolPriceSetting{
  35. Prices: func() map[string]float64 {
  36. m := make(map[string]float64, len(defaultToolPrices)+len(defaultToolPriceOverrides))
  37. for k, v := range defaultToolPrices {
  38. m[k] = v
  39. }
  40. for k, v := range defaultToolPriceOverrides {
  41. m[k] = v
  42. }
  43. return m
  44. }(),
  45. }
  46. func init() {
  47. config.GlobalConfig.Register("tool_price_setting", &toolPriceSetting)
  48. RebuildToolPriceIndex()
  49. }
  50. // ---------------------------------------------------------------------------
  51. // Precomputed price index (atomic, lock-free on read path)
  52. // ---------------------------------------------------------------------------
  53. type prefixEntry struct {
  54. prefix string
  55. price float64
  56. }
  57. type toolPriceIndex struct {
  58. defaults map[string]float64
  59. prefixes map[string][]prefixEntry
  60. }
  61. var currentIndex atomic.Pointer[toolPriceIndex]
  62. // RebuildToolPriceIndex rebuilds the lookup index from the current config.
  63. // Called on init and after config updates. Not on the billing hot path.
  64. func RebuildToolPriceIndex() {
  65. merged := make(map[string]float64, len(defaultToolPrices)+len(defaultToolPriceOverrides)+len(toolPriceSetting.Prices))
  66. for k, v := range defaultToolPrices {
  67. merged[k] = v
  68. }
  69. for k, v := range defaultToolPriceOverrides {
  70. merged[k] = v
  71. }
  72. for k, v := range toolPriceSetting.Prices {
  73. merged[k] = v
  74. }
  75. idx := &toolPriceIndex{
  76. defaults: make(map[string]float64),
  77. prefixes: make(map[string][]prefixEntry),
  78. }
  79. for key, price := range merged {
  80. colonIdx := strings.IndexByte(key, ':')
  81. if colonIdx < 0 {
  82. idx.defaults[key] = price
  83. continue
  84. }
  85. toolName := key[:colonIdx]
  86. modelPart := key[colonIdx+1:]
  87. prefix := strings.TrimSuffix(modelPart, "*")
  88. idx.prefixes[toolName] = append(idx.prefixes[toolName], prefixEntry{prefix: prefix, price: price})
  89. }
  90. for tool := range idx.prefixes {
  91. entries := idx.prefixes[tool]
  92. sort.Slice(entries, func(i, j int) bool {
  93. return len(entries[i].prefix) > len(entries[j].prefix)
  94. })
  95. idx.prefixes[tool] = entries
  96. }
  97. currentIndex.Store(idx)
  98. }
  99. // GetToolPriceForModel returns the price ($/1K calls) for a tool given a model name.
  100. // Lookup: longest prefix match → tool default → 0.
  101. func GetToolPriceForModel(toolName, modelName string) float64 {
  102. idx := currentIndex.Load()
  103. if idx == nil {
  104. if v, ok := defaultToolPrices[toolName]; ok {
  105. return v
  106. }
  107. return 0
  108. }
  109. if entries, ok := idx.prefixes[toolName]; ok && modelName != "" {
  110. for _, e := range entries {
  111. if strings.HasPrefix(modelName, e.prefix) {
  112. return e.price
  113. }
  114. }
  115. }
  116. if p, ok := idx.defaults[toolName]; ok {
  117. return p
  118. }
  119. return 0
  120. }
  121. // GetToolPrice is a convenience wrapper when no model name is needed.
  122. func GetToolPrice(toolName string) float64 {
  123. return GetToolPriceForModel(toolName, "")
  124. }
  125. // ---------------------------------------------------------------------------
  126. // GPT Image 1 per-call pricing (special: depends on quality + size)
  127. // ---------------------------------------------------------------------------
  128. const (
  129. GPTImage1Low1024x1024 = 0.011
  130. GPTImage1Low1024x1536 = 0.016
  131. GPTImage1Low1536x1024 = 0.016
  132. GPTImage1Medium1024x1024 = 0.042
  133. GPTImage1Medium1024x1536 = 0.063
  134. GPTImage1Medium1536x1024 = 0.063
  135. GPTImage1High1024x1024 = 0.167
  136. GPTImage1High1024x1536 = 0.25
  137. GPTImage1High1536x1024 = 0.25
  138. )
  139. func GetGPTImage1PriceOnceCall(quality string, size string) float64 {
  140. prices := map[string]map[string]float64{
  141. "low": {
  142. "1024x1024": GPTImage1Low1024x1024,
  143. "1024x1536": GPTImage1Low1024x1536,
  144. "1536x1024": GPTImage1Low1536x1024,
  145. },
  146. "medium": {
  147. "1024x1024": GPTImage1Medium1024x1024,
  148. "1024x1536": GPTImage1Medium1024x1536,
  149. "1536x1024": GPTImage1Medium1536x1024,
  150. },
  151. "high": {
  152. "1024x1024": GPTImage1High1024x1024,
  153. "1024x1536": GPTImage1High1024x1536,
  154. "1536x1024": GPTImage1High1536x1024,
  155. },
  156. }
  157. if qualityMap, exists := prices[quality]; exists {
  158. if price, exists := qualityMap[size]; exists {
  159. return price
  160. }
  161. }
  162. return GPTImage1High1024x1024
  163. }
  164. // ---------------------------------------------------------------------------
  165. // Gemini audio input pricing (per-million tokens, model-specific)
  166. // ---------------------------------------------------------------------------
  167. const (
  168. Gemini25FlashPreviewInputAudioPrice = 1.00
  169. Gemini25FlashProductionInputAudioPrice = 1.00
  170. Gemini25FlashLitePreviewInputAudioPrice = 0.50
  171. Gemini25FlashNativeAudioInputAudioPrice = 3.00
  172. Gemini20FlashInputAudioPrice = 0.70
  173. GeminiRoboticsER15InputAudioPrice = 1.00
  174. )
  175. func GetGeminiInputAudioPricePerMillionTokens(modelName string) float64 {
  176. if strings.HasPrefix(modelName, "gemini-2.5-flash-preview-native-audio") {
  177. return Gemini25FlashNativeAudioInputAudioPrice
  178. } else if strings.HasPrefix(modelName, "gemini-2.5-flash-preview-lite") {
  179. return Gemini25FlashLitePreviewInputAudioPrice
  180. } else if strings.HasPrefix(modelName, "gemini-2.5-flash-preview") {
  181. return Gemini25FlashPreviewInputAudioPrice
  182. } else if strings.HasPrefix(modelName, "gemini-2.5-flash") {
  183. return Gemini25FlashProductionInputAudioPrice
  184. } else if strings.HasPrefix(modelName, "gemini-2.0-flash") {
  185. return Gemini20FlashInputAudioPrice
  186. } else if strings.HasPrefix(modelName, "gemini-robotics-er-1.5") {
  187. return GeminiRoboticsER15InputAudioPrice
  188. }
  189. return 0
  190. }