You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

550 rivejä
22 KiB

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