compatible_handler.go 21 KB

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