選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

521 行
20 KiB

  1. package relay
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "strings"
  8. "time"
  9. "github.com/QuantumNous/new-api/common"
  10. "github.com/QuantumNous/new-api/constant"
  11. "github.com/QuantumNous/new-api/dto"
  12. "github.com/QuantumNous/new-api/logger"
  13. "github.com/QuantumNous/new-api/model"
  14. relaycommon "github.com/QuantumNous/new-api/relay/common"
  15. relayconstant "github.com/QuantumNous/new-api/relay/constant"
  16. "github.com/QuantumNous/new-api/relay/helper"
  17. "github.com/QuantumNous/new-api/service"
  18. "github.com/QuantumNous/new-api/setting/model_setting"
  19. "github.com/QuantumNous/new-api/setting/operation_setting"
  20. "github.com/QuantumNous/new-api/setting/ratio_setting"
  21. "github.com/QuantumNous/new-api/types"
  22. "github.com/shopspring/decimal"
  23. "github.com/gin-gonic/gin"
  24. )
  25. func shouldUseChatCompletionsViaResponses(info *relaycommon.RelayInfo, passThroughGlobal bool) bool {
  26. if info == nil {
  27. return false
  28. }
  29. if info.RelayMode != relayconstant.RelayModeChatCompletions {
  30. return false
  31. }
  32. if info.ChannelType == constant.ChannelTypeCodex {
  33. return true
  34. }
  35. if passThroughGlobal || info.ChannelSetting.PassThroughBodyEnabled {
  36. return false
  37. }
  38. return service.ShouldChatCompletionsUseResponsesGlobal(info.ChannelId, info.ChannelType, info.OriginModelName)
  39. }
  40. func TextHelper(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError *types.NewAPIError) {
  41. info.InitChannelMeta(c)
  42. textReq, ok := info.Request.(*dto.GeneralOpenAIRequest)
  43. if !ok {
  44. return types.NewErrorWithStatusCode(fmt.Errorf("invalid request type, expected dto.GeneralOpenAIRequest, got %T", info.Request), types.ErrorCodeInvalidRequest, http.StatusBadRequest, types.ErrOptionWithSkipRetry())
  45. }
  46. request, err := common.DeepCopy(textReq)
  47. if err != nil {
  48. return types.NewError(fmt.Errorf("failed to copy request to GeneralOpenAIRequest: %w", err), types.ErrorCodeInvalidRequest, types.ErrOptionWithSkipRetry())
  49. }
  50. if request.WebSearchOptions != nil {
  51. c.Set("chat_completion_web_search_context_size", request.WebSearchOptions.SearchContextSize)
  52. }
  53. err = helper.ModelMappedHelper(c, info, request)
  54. if err != nil {
  55. return types.NewError(err, types.ErrorCodeChannelModelMappedError, types.ErrOptionWithSkipRetry())
  56. }
  57. includeUsage := true
  58. // 判断用户是否需要返回使用情况
  59. if request.StreamOptions != nil {
  60. includeUsage = request.StreamOptions.IncludeUsage
  61. }
  62. // 如果不支持StreamOptions,将StreamOptions设置为nil
  63. if !info.SupportStreamOptions || !request.Stream {
  64. request.StreamOptions = nil
  65. } else {
  66. // 如果支持StreamOptions,且请求中没有设置StreamOptions,根据配置文件设置StreamOptions
  67. if constant.ForceStreamOption {
  68. request.StreamOptions = &dto.StreamOptions{
  69. IncludeUsage: true,
  70. }
  71. }
  72. }
  73. info.ShouldIncludeUsage = includeUsage
  74. adaptor := GetAdaptor(info.ApiType)
  75. if adaptor == nil {
  76. return types.NewError(fmt.Errorf("invalid api type: %d", info.ApiType), types.ErrorCodeInvalidApiType, types.ErrOptionWithSkipRetry())
  77. }
  78. adaptor.Init(info)
  79. passThroughGlobal := model_setting.GetGlobalSettings().PassThroughRequestEnabled
  80. if shouldUseChatCompletionsViaResponses(info, passThroughGlobal) {
  81. applySystemPromptIfNeeded(c, info, request)
  82. usage, newApiErr := chatCompletionsViaResponses(c, info, adaptor, request)
  83. if newApiErr != nil {
  84. return newApiErr
  85. }
  86. var containAudioTokens = usage.CompletionTokenDetails.AudioTokens > 0 || usage.PromptTokensDetails.AudioTokens > 0
  87. var containsAudioRatios = ratio_setting.ContainsAudioRatio(info.OriginModelName) || ratio_setting.ContainsAudioCompletionRatio(info.OriginModelName)
  88. if containAudioTokens && containsAudioRatios {
  89. service.PostAudioConsumeQuota(c, info, usage, "")
  90. } else {
  91. postConsumeQuota(c, info, usage)
  92. }
  93. return nil
  94. }
  95. var requestBody io.Reader
  96. if passThroughGlobal || info.ChannelSetting.PassThroughBodyEnabled {
  97. storage, err := common.GetBodyStorage(c)
  98. if err != nil {
  99. return types.NewErrorWithStatusCode(err, types.ErrorCodeReadRequestBodyFailed, http.StatusBadRequest, types.ErrOptionWithSkipRetry())
  100. }
  101. if common.DebugEnabled {
  102. if debugBytes, bErr := storage.Bytes(); bErr == nil {
  103. println("requestBody: ", string(debugBytes))
  104. }
  105. }
  106. requestBody = common.ReaderOnly(storage)
  107. } else {
  108. convertedRequest, err := adaptor.ConvertOpenAIRequest(c, info, request)
  109. if err != nil {
  110. return types.NewError(err, types.ErrorCodeConvertRequestFailed, types.ErrOptionWithSkipRetry())
  111. }
  112. relaycommon.AppendRequestConversionFromRequest(info, convertedRequest)
  113. if info.ChannelSetting.SystemPrompt != "" {
  114. // 如果有系统提示,则将其添加到请求中
  115. request, ok := convertedRequest.(*dto.GeneralOpenAIRequest)
  116. if ok {
  117. containSystemPrompt := false
  118. for _, message := range request.Messages {
  119. if message.Role == request.GetSystemRoleName() {
  120. containSystemPrompt = true
  121. break
  122. }
  123. }
  124. if !containSystemPrompt {
  125. // 如果没有系统提示,则添加系统提示
  126. systemMessage := dto.Message{
  127. Role: request.GetSystemRoleName(),
  128. Content: info.ChannelSetting.SystemPrompt,
  129. }
  130. request.Messages = append([]dto.Message{systemMessage}, request.Messages...)
  131. } else if info.ChannelSetting.SystemPromptOverride {
  132. common.SetContextKey(c, constant.ContextKeySystemPromptOverride, true)
  133. // 如果有系统提示,且允许覆盖,则拼接到前面
  134. for i, message := range request.Messages {
  135. if message.Role == request.GetSystemRoleName() {
  136. if message.IsStringContent() {
  137. request.Messages[i].SetStringContent(info.ChannelSetting.SystemPrompt + "\n" + message.StringContent())
  138. } else {
  139. contents := message.ParseContent()
  140. contents = append([]dto.MediaContent{
  141. {
  142. Type: dto.ContentTypeText,
  143. Text: info.ChannelSetting.SystemPrompt,
  144. },
  145. }, contents...)
  146. request.Messages[i].Content = contents
  147. }
  148. break
  149. }
  150. }
  151. }
  152. }
  153. }
  154. jsonData, err := common.Marshal(convertedRequest)
  155. if err != nil {
  156. return types.NewError(err, types.ErrorCodeJsonMarshalFailed, types.ErrOptionWithSkipRetry())
  157. }
  158. // remove disabled fields for OpenAI API
  159. jsonData, err = relaycommon.RemoveDisabledFields(jsonData, info.ChannelOtherSettings, info.ChannelSetting.PassThroughBodyEnabled)
  160. if err != nil {
  161. return types.NewError(err, types.ErrorCodeConvertRequestFailed, types.ErrOptionWithSkipRetry())
  162. }
  163. // apply param override
  164. if len(info.ParamOverride) > 0 {
  165. jsonData, err = relaycommon.ApplyParamOverrideWithRelayInfo(jsonData, info)
  166. if err != nil {
  167. return types.NewError(err, types.ErrorCodeChannelParamOverrideInvalid, types.ErrOptionWithSkipRetry())
  168. }
  169. }
  170. logger.LogDebug(c, fmt.Sprintf("text request body: %s", string(jsonData)))
  171. requestBody = bytes.NewBuffer(jsonData)
  172. }
  173. var httpResp *http.Response
  174. resp, err := adaptor.DoRequest(c, info, requestBody)
  175. if err != nil {
  176. return types.NewOpenAIError(err, types.ErrorCodeDoRequestFailed, http.StatusInternalServerError)
  177. }
  178. statusCodeMappingStr := c.GetString("status_code_mapping")
  179. if resp != nil {
  180. httpResp = resp.(*http.Response)
  181. info.IsStream = info.IsStream || strings.HasPrefix(httpResp.Header.Get("Content-Type"), "text/event-stream")
  182. if httpResp.StatusCode != http.StatusOK {
  183. newApiErr := service.RelayErrorHandler(c.Request.Context(), httpResp, false)
  184. // reset status code 重置状态码
  185. service.ResetStatusCode(newApiErr, statusCodeMappingStr)
  186. return newApiErr
  187. }
  188. }
  189. usage, newApiErr := adaptor.DoResponse(c, httpResp, info)
  190. if newApiErr != nil {
  191. // reset status code 重置状态码
  192. service.ResetStatusCode(newApiErr, statusCodeMappingStr)
  193. return newApiErr
  194. }
  195. var containAudioTokens = usage.(*dto.Usage).CompletionTokenDetails.AudioTokens > 0 || usage.(*dto.Usage).PromptTokensDetails.AudioTokens > 0
  196. var containsAudioRatios = ratio_setting.ContainsAudioRatio(info.OriginModelName) || ratio_setting.ContainsAudioCompletionRatio(info.OriginModelName)
  197. if containAudioTokens && containsAudioRatios {
  198. service.PostAudioConsumeQuota(c, info, usage.(*dto.Usage), "")
  199. } else {
  200. postConsumeQuota(c, info, usage.(*dto.Usage))
  201. }
  202. return nil
  203. }
  204. func postConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, usage *dto.Usage, extraContent ...string) {
  205. originUsage := usage
  206. if usage == nil {
  207. usage = &dto.Usage{
  208. PromptTokens: relayInfo.GetEstimatePromptTokens(),
  209. CompletionTokens: 0,
  210. TotalTokens: relayInfo.GetEstimatePromptTokens(),
  211. }
  212. extraContent = append(extraContent, "上游无计费信息")
  213. }
  214. if originUsage != nil {
  215. service.ObserveChannelAffinityUsageCacheByRelayFormat(ctx, usage, relayInfo.GetFinalRequestRelayFormat())
  216. }
  217. adminRejectReason := common.GetContextKeyString(ctx, constant.ContextKeyAdminRejectReason)
  218. useTimeSeconds := time.Now().Unix() - relayInfo.StartTime.Unix()
  219. promptTokens := usage.PromptTokens
  220. cacheTokens := usage.PromptTokensDetails.CachedTokens
  221. imageTokens := usage.PromptTokensDetails.ImageTokens
  222. audioTokens := usage.PromptTokensDetails.AudioTokens
  223. completionTokens := usage.CompletionTokens
  224. cachedCreationTokens := usage.PromptTokensDetails.CachedCreationTokens
  225. modelName := relayInfo.OriginModelName
  226. tokenName := ctx.GetString("token_name")
  227. completionRatio := relayInfo.PriceData.CompletionRatio
  228. cacheRatio := relayInfo.PriceData.CacheRatio
  229. imageRatio := relayInfo.PriceData.ImageRatio
  230. modelRatio := relayInfo.PriceData.ModelRatio
  231. groupRatio := relayInfo.PriceData.GroupRatioInfo.GroupRatio
  232. modelPrice := relayInfo.PriceData.ModelPrice
  233. cachedCreationRatio := relayInfo.PriceData.CacheCreationRatio
  234. // Convert values to decimal for precise calculation
  235. dPromptTokens := decimal.NewFromInt(int64(promptTokens))
  236. dCacheTokens := decimal.NewFromInt(int64(cacheTokens))
  237. dImageTokens := decimal.NewFromInt(int64(imageTokens))
  238. dAudioTokens := decimal.NewFromInt(int64(audioTokens))
  239. dCompletionTokens := decimal.NewFromInt(int64(completionTokens))
  240. dCachedCreationTokens := decimal.NewFromInt(int64(cachedCreationTokens))
  241. dCompletionRatio := decimal.NewFromFloat(completionRatio)
  242. dCacheRatio := decimal.NewFromFloat(cacheRatio)
  243. dImageRatio := decimal.NewFromFloat(imageRatio)
  244. dModelRatio := decimal.NewFromFloat(modelRatio)
  245. dGroupRatio := decimal.NewFromFloat(groupRatio)
  246. dModelPrice := decimal.NewFromFloat(modelPrice)
  247. dCachedCreationRatio := decimal.NewFromFloat(cachedCreationRatio)
  248. dQuotaPerUnit := decimal.NewFromFloat(common.QuotaPerUnit)
  249. ratio := dModelRatio.Mul(dGroupRatio)
  250. // openai web search 工具计费
  251. var dWebSearchQuota decimal.Decimal
  252. var webSearchPrice float64
  253. // response api 格式工具计费
  254. if relayInfo.ResponsesUsageInfo != nil {
  255. if webSearchTool, exists := relayInfo.ResponsesUsageInfo.BuiltInTools[dto.BuildInToolWebSearchPreview]; exists && webSearchTool.CallCount > 0 {
  256. // 计算 web search 调用的配额 (配额 = 价格 * 调用次数 / 1000 * 分组倍率)
  257. webSearchPrice = operation_setting.GetWebSearchPricePerThousand(modelName, webSearchTool.SearchContextSize)
  258. dWebSearchQuota = decimal.NewFromFloat(webSearchPrice).
  259. Mul(decimal.NewFromInt(int64(webSearchTool.CallCount))).
  260. Div(decimal.NewFromInt(1000)).Mul(dGroupRatio).Mul(dQuotaPerUnit)
  261. extraContent = append(extraContent, fmt.Sprintf("Web Search 调用 %d 次,上下文大小 %s,调用花费 %s",
  262. webSearchTool.CallCount, webSearchTool.SearchContextSize, dWebSearchQuota.String()))
  263. }
  264. } else if strings.HasSuffix(modelName, "search-preview") {
  265. // search-preview 模型不支持 response api
  266. searchContextSize := ctx.GetString("chat_completion_web_search_context_size")
  267. if searchContextSize == "" {
  268. searchContextSize = "medium"
  269. }
  270. webSearchPrice = operation_setting.GetWebSearchPricePerThousand(modelName, searchContextSize)
  271. dWebSearchQuota = decimal.NewFromFloat(webSearchPrice).
  272. Div(decimal.NewFromInt(1000)).Mul(dGroupRatio).Mul(dQuotaPerUnit)
  273. extraContent = append(extraContent, fmt.Sprintf("Web Search 调用 1 次,上下文大小 %s,调用花费 %s",
  274. searchContextSize, dWebSearchQuota.String()))
  275. }
  276. // claude web search tool 计费
  277. var dClaudeWebSearchQuota decimal.Decimal
  278. var claudeWebSearchPrice float64
  279. claudeWebSearchCallCount := ctx.GetInt("claude_web_search_requests")
  280. if claudeWebSearchCallCount > 0 {
  281. claudeWebSearchPrice = operation_setting.GetClaudeWebSearchPricePerThousand()
  282. dClaudeWebSearchQuota = decimal.NewFromFloat(claudeWebSearchPrice).
  283. Div(decimal.NewFromInt(1000)).Mul(dGroupRatio).Mul(dQuotaPerUnit).Mul(decimal.NewFromInt(int64(claudeWebSearchCallCount)))
  284. extraContent = append(extraContent, fmt.Sprintf("Claude Web Search 调用 %d 次,调用花费 %s",
  285. claudeWebSearchCallCount, dClaudeWebSearchQuota.String()))
  286. }
  287. // file search tool 计费
  288. var dFileSearchQuota decimal.Decimal
  289. var fileSearchPrice float64
  290. if relayInfo.ResponsesUsageInfo != nil {
  291. if fileSearchTool, exists := relayInfo.ResponsesUsageInfo.BuiltInTools[dto.BuildInToolFileSearch]; exists && fileSearchTool.CallCount > 0 {
  292. fileSearchPrice = operation_setting.GetFileSearchPricePerThousand()
  293. dFileSearchQuota = decimal.NewFromFloat(fileSearchPrice).
  294. Mul(decimal.NewFromInt(int64(fileSearchTool.CallCount))).
  295. Div(decimal.NewFromInt(1000)).Mul(dGroupRatio).Mul(dQuotaPerUnit)
  296. extraContent = append(extraContent, fmt.Sprintf("File Search 调用 %d 次,调用花费 %s",
  297. fileSearchTool.CallCount, dFileSearchQuota.String()))
  298. }
  299. }
  300. var dImageGenerationCallQuota decimal.Decimal
  301. var imageGenerationCallPrice float64
  302. if ctx.GetBool("image_generation_call") {
  303. imageGenerationCallPrice = operation_setting.GetGPTImage1PriceOnceCall(ctx.GetString("image_generation_call_quality"), ctx.GetString("image_generation_call_size"))
  304. dImageGenerationCallQuota = decimal.NewFromFloat(imageGenerationCallPrice).Mul(dGroupRatio).Mul(dQuotaPerUnit)
  305. extraContent = append(extraContent, fmt.Sprintf("Image Generation Call 花费 %s", dImageGenerationCallQuota.String()))
  306. }
  307. var quotaCalculateDecimal decimal.Decimal
  308. var audioInputQuota decimal.Decimal
  309. var audioInputPrice float64
  310. isClaudeUsageSemantic := relayInfo.GetFinalRequestRelayFormat() == types.RelayFormatClaude
  311. if !relayInfo.PriceData.UsePrice {
  312. baseTokens := dPromptTokens
  313. // 减去 cached tokens
  314. // Anthropic API 的 input_tokens 已经不包含缓存 tokens,不需要减去
  315. // OpenAI/OpenRouter 等 API 的 prompt_tokens 包含缓存 tokens,需要减去
  316. var cachedTokensWithRatio decimal.Decimal
  317. if !dCacheTokens.IsZero() {
  318. if !isClaudeUsageSemantic {
  319. baseTokens = baseTokens.Sub(dCacheTokens)
  320. }
  321. cachedTokensWithRatio = dCacheTokens.Mul(dCacheRatio)
  322. }
  323. var dCachedCreationTokensWithRatio decimal.Decimal
  324. if !dCachedCreationTokens.IsZero() {
  325. if !isClaudeUsageSemantic {
  326. baseTokens = baseTokens.Sub(dCachedCreationTokens)
  327. }
  328. dCachedCreationTokensWithRatio = dCachedCreationTokens.Mul(dCachedCreationRatio)
  329. }
  330. // 减去 image tokens
  331. var imageTokensWithRatio decimal.Decimal
  332. if !dImageTokens.IsZero() {
  333. baseTokens = baseTokens.Sub(dImageTokens)
  334. imageTokensWithRatio = dImageTokens.Mul(dImageRatio)
  335. }
  336. // 减去 Gemini audio tokens
  337. if !dAudioTokens.IsZero() {
  338. audioInputPrice = operation_setting.GetGeminiInputAudioPricePerMillionTokens(modelName)
  339. if audioInputPrice > 0 {
  340. // 重新计算 base tokens
  341. baseTokens = baseTokens.Sub(dAudioTokens)
  342. audioInputQuota = decimal.NewFromFloat(audioInputPrice).Div(decimal.NewFromInt(1000000)).Mul(dAudioTokens).Mul(dGroupRatio).Mul(dQuotaPerUnit)
  343. extraContent = append(extraContent, fmt.Sprintf("Audio Input 花费 %s", audioInputQuota.String()))
  344. }
  345. }
  346. promptQuota := baseTokens.Add(cachedTokensWithRatio).
  347. Add(imageTokensWithRatio).
  348. Add(dCachedCreationTokensWithRatio)
  349. completionQuota := dCompletionTokens.Mul(dCompletionRatio)
  350. quotaCalculateDecimal = promptQuota.Add(completionQuota).Mul(ratio)
  351. if !ratio.IsZero() && quotaCalculateDecimal.LessThanOrEqual(decimal.Zero) {
  352. quotaCalculateDecimal = decimal.NewFromInt(1)
  353. }
  354. } else {
  355. quotaCalculateDecimal = dModelPrice.Mul(dQuotaPerUnit).Mul(dGroupRatio)
  356. }
  357. // 添加 responses tools call 调用的配额
  358. quotaCalculateDecimal = quotaCalculateDecimal.Add(dWebSearchQuota)
  359. quotaCalculateDecimal = quotaCalculateDecimal.Add(dFileSearchQuota)
  360. // 添加 audio input 独立计费
  361. quotaCalculateDecimal = quotaCalculateDecimal.Add(audioInputQuota)
  362. // 添加 image generation call 计费
  363. quotaCalculateDecimal = quotaCalculateDecimal.Add(dImageGenerationCallQuota)
  364. if len(relayInfo.PriceData.OtherRatios) > 0 {
  365. for key, otherRatio := range relayInfo.PriceData.OtherRatios {
  366. dOtherRatio := decimal.NewFromFloat(otherRatio)
  367. quotaCalculateDecimal = quotaCalculateDecimal.Mul(dOtherRatio)
  368. extraContent = append(extraContent, fmt.Sprintf("其他倍率 %s: %f", key, otherRatio))
  369. }
  370. }
  371. quota := int(quotaCalculateDecimal.Round(0).IntPart())
  372. totalTokens := promptTokens + completionTokens
  373. //var logContent string
  374. // record all the consume log even if quota is 0
  375. if totalTokens == 0 {
  376. // in this case, must be some error happened
  377. // we cannot just return, because we may have to return the pre-consumed quota
  378. quota = 0
  379. extraContent = append(extraContent, "上游没有返回计费信息,无法扣费(可能是上游超时)")
  380. logger.LogError(ctx, fmt.Sprintf("total tokens is 0, cannot consume quota, userId %d, channelId %d, "+
  381. "tokenId %d, model %s, pre-consumed quota %d", relayInfo.UserId, relayInfo.ChannelId, relayInfo.TokenId, modelName, relayInfo.FinalPreConsumedQuota))
  382. } else {
  383. if !ratio.IsZero() && quota == 0 {
  384. quota = 1
  385. }
  386. model.UpdateUserUsedQuotaAndRequestCount(relayInfo.UserId, quota)
  387. model.UpdateChannelUsedQuota(relayInfo.ChannelId, quota)
  388. }
  389. if err := service.SettleBilling(ctx, relayInfo, quota); err != nil {
  390. logger.LogError(ctx, "error settling billing: "+err.Error())
  391. }
  392. logModel := modelName
  393. if strings.HasPrefix(logModel, "gpt-4-gizmo") {
  394. logModel = "gpt-4-gizmo-*"
  395. extraContent = append(extraContent, fmt.Sprintf("模型 %s", modelName))
  396. }
  397. if strings.HasPrefix(logModel, "gpt-4o-gizmo") {
  398. logModel = "gpt-4o-gizmo-*"
  399. extraContent = append(extraContent, fmt.Sprintf("模型 %s", modelName))
  400. }
  401. logContent := strings.Join(extraContent, ", ")
  402. other := service.GenerateTextOtherInfo(ctx, relayInfo, modelRatio, groupRatio, completionRatio, cacheTokens, cacheRatio, modelPrice, relayInfo.PriceData.GroupRatioInfo.GroupSpecialRatio)
  403. if adminRejectReason != "" {
  404. other["reject_reason"] = adminRejectReason
  405. }
  406. // For chat-based calls to the Claude model, tagging is required. Using Claude's rendering logs, the two approaches handle input rendering differently.
  407. if isClaudeUsageSemantic {
  408. other["claude"] = true
  409. other["usage_semantic"] = "anthropic"
  410. }
  411. if imageTokens != 0 {
  412. other["image"] = true
  413. other["image_ratio"] = imageRatio
  414. other["image_output"] = imageTokens
  415. }
  416. if cachedCreationTokens != 0 {
  417. other["cache_creation_tokens"] = cachedCreationTokens
  418. other["cache_creation_ratio"] = cachedCreationRatio
  419. }
  420. if !dWebSearchQuota.IsZero() {
  421. if relayInfo.ResponsesUsageInfo != nil {
  422. if webSearchTool, exists := relayInfo.ResponsesUsageInfo.BuiltInTools[dto.BuildInToolWebSearchPreview]; exists {
  423. other["web_search"] = true
  424. other["web_search_call_count"] = webSearchTool.CallCount
  425. other["web_search_price"] = webSearchPrice
  426. }
  427. } else if strings.HasSuffix(modelName, "search-preview") {
  428. other["web_search"] = true
  429. other["web_search_call_count"] = 1
  430. other["web_search_price"] = webSearchPrice
  431. }
  432. } else if !dClaudeWebSearchQuota.IsZero() {
  433. other["web_search"] = true
  434. other["web_search_call_count"] = claudeWebSearchCallCount
  435. other["web_search_price"] = claudeWebSearchPrice
  436. }
  437. if !dFileSearchQuota.IsZero() && relayInfo.ResponsesUsageInfo != nil {
  438. if fileSearchTool, exists := relayInfo.ResponsesUsageInfo.BuiltInTools[dto.BuildInToolFileSearch]; exists {
  439. other["file_search"] = true
  440. other["file_search_call_count"] = fileSearchTool.CallCount
  441. other["file_search_price"] = fileSearchPrice
  442. }
  443. }
  444. if !audioInputQuota.IsZero() {
  445. other["audio_input_seperate_price"] = true
  446. other["audio_input_token_count"] = audioTokens
  447. other["audio_input_price"] = audioInputPrice
  448. }
  449. if !dImageGenerationCallQuota.IsZero() {
  450. other["image_generation_call"] = true
  451. other["image_generation_call_price"] = imageGenerationCallPrice
  452. }
  453. model.RecordConsumeLog(ctx, relayInfo.UserId, model.RecordConsumeLogParams{
  454. ChannelId: relayInfo.ChannelId,
  455. PromptTokens: promptTokens,
  456. CompletionTokens: completionTokens,
  457. ModelName: logModel,
  458. TokenName: tokenName,
  459. Quota: quota,
  460. Content: logContent,
  461. TokenId: relayInfo.TokenId,
  462. UseTimeSeconds: int(useTimeSeconds),
  463. IsStream: relayInfo.IsStream,
  464. Group: relayInfo.UsingGroup,
  465. Other: other,
  466. })
  467. }