relay-text.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. package relay
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "math"
  9. "net/http"
  10. "one-api/common"
  11. "one-api/constant"
  12. "one-api/dto"
  13. "one-api/model"
  14. relaycommon "one-api/relay/common"
  15. relayconstant "one-api/relay/constant"
  16. "one-api/relay/helper"
  17. "one-api/service"
  18. "one-api/setting"
  19. "one-api/setting/model_setting"
  20. "one-api/setting/operation_setting"
  21. "one-api/types"
  22. "strings"
  23. "time"
  24. "unicode"
  25. "github.com/bytedance/gopkg/util/gopool"
  26. "github.com/shopspring/decimal"
  27. "github.com/gin-gonic/gin"
  28. )
  29. // 提取用户输入内容(从 messages 中提取用户发送的消息)
  30. // 修改为只提取最后一条用户消息,避免记录历史输入
  31. func extractUserInputFromMessages(messages []dto.Message) string {
  32. if len(messages) == 0 {
  33. return ""
  34. }
  35. // 从后往前查找,只提取最后一条用户消息
  36. for i := len(messages) - 1; i >= 0; i-- {
  37. message := messages[i]
  38. if message.Role == "user" {
  39. content := message.StringContent()
  40. if content != "" {
  41. // 过滤代码类内容
  42. if isCodeContent(content) {
  43. continue
  44. }
  45. // 只保留汉字内容
  46. filteredContent := filterChineseContent(content)
  47. if filteredContent != "" {
  48. return filteredContent
  49. }
  50. }
  51. }
  52. }
  53. // 如果没有找到合适的用户消息,返回最后一条消息的内容(作为备选)
  54. lastContent := messages[len(messages)-1].StringContent()
  55. // 过滤代码类内容
  56. if isCodeContent(lastContent) {
  57. return ""
  58. }
  59. // 只保留汉字内容
  60. return filterChineseContent(lastContent)
  61. }
  62. // 检查是否为代码类内容
  63. func isCodeContent(content string) bool {
  64. // 定义代码类关键词
  65. codeKeywords := []string{
  66. "VSCode Open Tabs",
  67. "Current Time",
  68. "Current Cost",
  69. "Current Mode",
  70. "REMINDERS",
  71. "VSCode Visible Files",
  72. }
  73. // 检查是否包含代码类关键词
  74. for _, keyword := range codeKeywords {
  75. if strings.Contains(content, keyword) {
  76. return true
  77. }
  78. }
  79. return false
  80. }
  81. // 过滤并只保留汉字内容
  82. func filterChineseContent(content string) string {
  83. // 使用正则表达式匹配汉字
  84. var chineseContent strings.Builder
  85. for _, r := range content {
  86. // 检查字符是否为汉字
  87. if unicode.Is(unicode.Scripts["Han"], r) {
  88. chineseContent.WriteRune(r)
  89. }
  90. }
  91. // 返回过滤后的汉字内容
  92. return chineseContent.String()
  93. }
  94. func getAndValidateTextRequest(c *gin.Context, relayInfo *relaycommon.RelayInfo) (*dto.GeneralOpenAIRequest, error) {
  95. textRequest := &dto.GeneralOpenAIRequest{}
  96. err := common.UnmarshalBodyReusable(c, textRequest)
  97. if err != nil {
  98. return nil, err
  99. }
  100. if relayInfo.RelayMode == relayconstant.RelayModeModerations && textRequest.Model == "" {
  101. textRequest.Model = "text-moderation-latest"
  102. }
  103. if relayInfo.RelayMode == relayconstant.RelayModeEmbeddings && textRequest.Model == "" {
  104. textRequest.Model = c.Param("model")
  105. }
  106. if textRequest.MaxTokens > math.MaxInt32/2 {
  107. return nil, errors.New("max_tokens is invalid")
  108. }
  109. if textRequest.Model == "" {
  110. return nil, errors.New("model is required")
  111. }
  112. if textRequest.WebSearchOptions != nil {
  113. if textRequest.WebSearchOptions.SearchContextSize != "" {
  114. validSizes := map[string]bool{
  115. "high": true,
  116. "medium": true,
  117. "low": true,
  118. }
  119. if !validSizes[textRequest.WebSearchOptions.SearchContextSize] {
  120. return nil, errors.New("invalid search_context_size, must be one of: high, medium, low")
  121. }
  122. } else {
  123. textRequest.WebSearchOptions.SearchContextSize = "medium"
  124. }
  125. }
  126. switch relayInfo.RelayMode {
  127. case relayconstant.RelayModeCompletions:
  128. if textRequest.Prompt == "" {
  129. return nil, errors.New("field prompt is required")
  130. }
  131. case relayconstant.RelayModeChatCompletions:
  132. if len(textRequest.Messages) == 0 {
  133. return nil, errors.New("field messages is required")
  134. }
  135. case relayconstant.RelayModeEmbeddings:
  136. case relayconstant.RelayModeModerations:
  137. if textRequest.Input == nil || textRequest.Input == "" {
  138. return nil, errors.New("field input is required")
  139. }
  140. case relayconstant.RelayModeEdits:
  141. if textRequest.Instruction == "" {
  142. return nil, errors.New("field instruction is required")
  143. }
  144. }
  145. relayInfo.IsStream = textRequest.Stream
  146. return textRequest, nil
  147. }
  148. func TextHelper(c *gin.Context) (newAPIError *types.NewAPIError) {
  149. relayInfo := relaycommon.GenRelayInfo(c)
  150. // get & validate textRequest 获取并验证文本请求
  151. textRequest, err := getAndValidateTextRequest(c, relayInfo)
  152. if err != nil {
  153. return types.NewError(err, types.ErrorCodeInvalidRequest)
  154. }
  155. // 将 textRequest 存储到 context 中供后续使用
  156. c.Set("text_request", textRequest)
  157. if textRequest.WebSearchOptions != nil {
  158. c.Set("chat_completion_web_search_context_size", textRequest.WebSearchOptions.SearchContextSize)
  159. }
  160. if setting.ShouldCheckPromptSensitive() {
  161. words, err := checkRequestSensitive(textRequest, relayInfo)
  162. if err != nil {
  163. common.LogWarn(c, fmt.Sprintf("user sensitive words detected: %s", strings.Join(words, ", ")))
  164. return types.NewError(err, types.ErrorCodeSensitiveWordsDetected)
  165. }
  166. }
  167. err = helper.ModelMappedHelper(c, relayInfo, textRequest)
  168. if err != nil {
  169. return types.NewError(err, types.ErrorCodeChannelModelMappedError)
  170. }
  171. // 获取 promptTokens,如果上下文中已经存在,则直接使用
  172. var promptTokens int
  173. if value, exists := c.Get("prompt_tokens"); exists {
  174. promptTokens = value.(int)
  175. relayInfo.PromptTokens = promptTokens
  176. } else {
  177. promptTokens, err = getPromptTokens(textRequest, relayInfo)
  178. // count messages token error 计算promptTokens错误
  179. if err != nil {
  180. return types.NewError(err, types.ErrorCodeCountTokenFailed)
  181. }
  182. c.Set("prompt_tokens", promptTokens)
  183. }
  184. priceData, err := helper.ModelPriceHelper(c, relayInfo, promptTokens, int(math.Max(float64(textRequest.MaxTokens), float64(textRequest.MaxCompletionTokens))))
  185. if err != nil {
  186. return types.NewError(err, types.ErrorCodeModelPriceError)
  187. }
  188. // pre-consume quota 预消耗配额
  189. preConsumedQuota, userQuota, newApiErr := preConsumeQuota(c, priceData.ShouldPreConsumedQuota, relayInfo)
  190. if newApiErr != nil {
  191. return newApiErr
  192. }
  193. defer func() {
  194. if newApiErr != nil {
  195. returnPreConsumedQuota(c, relayInfo, userQuota, preConsumedQuota)
  196. }
  197. }()
  198. includeUsage := false
  199. // 判断用户是否需要返回使用情况
  200. if textRequest.StreamOptions != nil && textRequest.StreamOptions.IncludeUsage {
  201. includeUsage = true
  202. }
  203. // 如果不支持StreamOptions,将StreamOptions设置为nil
  204. if !relayInfo.SupportStreamOptions || !textRequest.Stream {
  205. textRequest.StreamOptions = nil
  206. } else {
  207. // 如果支持StreamOptions,且请求中没有设置StreamOptions,根据配置文件设置StreamOptions
  208. if constant.ForceStreamOption {
  209. textRequest.StreamOptions = &dto.StreamOptions{
  210. IncludeUsage: true,
  211. }
  212. }
  213. }
  214. if includeUsage {
  215. relayInfo.ShouldIncludeUsage = true
  216. }
  217. adaptor := GetAdaptor(relayInfo.ApiType)
  218. if adaptor == nil {
  219. return types.NewError(fmt.Errorf("invalid api type: %d", relayInfo.ApiType), types.ErrorCodeInvalidApiType)
  220. }
  221. adaptor.Init(relayInfo)
  222. var requestBody io.Reader
  223. if model_setting.GetGlobalSettings().PassThroughRequestEnabled {
  224. body, err := common.GetRequestBody(c)
  225. if err != nil {
  226. return types.NewErrorWithStatusCode(err, types.ErrorCodeReadRequestBodyFailed, http.StatusBadRequest)
  227. }
  228. requestBody = bytes.NewBuffer(body)
  229. } else {
  230. convertedRequest, err := adaptor.ConvertOpenAIRequest(c, relayInfo, textRequest)
  231. if err != nil {
  232. return types.NewError(err, types.ErrorCodeConvertRequestFailed)
  233. }
  234. jsonData, err := json.Marshal(convertedRequest)
  235. if err != nil {
  236. return types.NewError(err, types.ErrorCodeConvertRequestFailed)
  237. }
  238. // apply param override
  239. if len(relayInfo.ParamOverride) > 0 {
  240. reqMap := make(map[string]interface{})
  241. _ = common.Unmarshal(jsonData, &reqMap)
  242. for key, value := range relayInfo.ParamOverride {
  243. reqMap[key] = value
  244. }
  245. jsonData, err = common.Marshal(reqMap)
  246. if err != nil {
  247. return types.NewError(err, types.ErrorCodeChannelParamOverrideInvalid)
  248. }
  249. }
  250. if common.DebugEnabled {
  251. println("requestBody: ", string(jsonData))
  252. }
  253. requestBody = bytes.NewBuffer(jsonData)
  254. }
  255. var httpResp *http.Response
  256. resp, err := adaptor.DoRequest(c, relayInfo, requestBody)
  257. if err != nil {
  258. return types.NewOpenAIError(err, types.ErrorCodeDoRequestFailed, http.StatusInternalServerError)
  259. }
  260. statusCodeMappingStr := c.GetString("status_code_mapping")
  261. if resp != nil {
  262. httpResp = resp.(*http.Response)
  263. relayInfo.IsStream = relayInfo.IsStream || strings.HasPrefix(httpResp.Header.Get("Content-Type"), "text/event-stream")
  264. if httpResp.StatusCode != http.StatusOK {
  265. newApiErr = service.RelayErrorHandler(httpResp, false)
  266. // reset status code 重置状态码
  267. service.ResetStatusCode(newApiErr, statusCodeMappingStr)
  268. return newApiErr
  269. }
  270. }
  271. usage, newApiErr := adaptor.DoResponse(c, httpResp, relayInfo)
  272. if newApiErr != nil {
  273. // reset status code 重置状态码
  274. service.ResetStatusCode(newApiErr, statusCodeMappingStr)
  275. return newApiErr
  276. }
  277. if strings.HasPrefix(relayInfo.OriginModelName, "gpt-4o-audio") {
  278. service.PostAudioConsumeQuota(c, relayInfo, usage.(*dto.Usage), preConsumedQuota, userQuota, priceData, "")
  279. } else {
  280. postConsumeQuota(c, relayInfo, usage.(*dto.Usage), preConsumedQuota, userQuota, priceData, "")
  281. }
  282. return nil
  283. }
  284. func getPromptTokens(textRequest *dto.GeneralOpenAIRequest, info *relaycommon.RelayInfo) (int, error) {
  285. var promptTokens int
  286. var err error
  287. switch info.RelayMode {
  288. case relayconstant.RelayModeChatCompletions:
  289. promptTokens, err = service.CountTokenChatRequest(info, *textRequest)
  290. case relayconstant.RelayModeCompletions:
  291. promptTokens = service.CountTokenInput(textRequest.Prompt, textRequest.Model)
  292. case relayconstant.RelayModeModerations:
  293. promptTokens = service.CountTokenInput(textRequest.Input, textRequest.Model)
  294. case relayconstant.RelayModeEmbeddings:
  295. promptTokens = service.CountTokenInput(textRequest.Input, textRequest.Model)
  296. default:
  297. err = errors.New("unknown relay mode")
  298. promptTokens = 0
  299. }
  300. info.PromptTokens = promptTokens
  301. return promptTokens, err
  302. }
  303. func checkRequestSensitive(textRequest *dto.GeneralOpenAIRequest, info *relaycommon.RelayInfo) ([]string, error) {
  304. var err error
  305. var words []string
  306. switch info.RelayMode {
  307. case relayconstant.RelayModeChatCompletions:
  308. words, err = service.CheckSensitiveMessages(textRequest.Messages)
  309. case relayconstant.RelayModeCompletions:
  310. words, err = service.CheckSensitiveInput(textRequest.Prompt)
  311. case relayconstant.RelayModeModerations:
  312. words, err = service.CheckSensitiveInput(textRequest.Input)
  313. case relayconstant.RelayModeEmbeddings:
  314. words, err = service.CheckSensitiveInput(textRequest.Input)
  315. }
  316. return words, err
  317. }
  318. // 预扣费并返回用户剩余配额
  319. func preConsumeQuota(c *gin.Context, preConsumedQuota int, relayInfo *relaycommon.RelayInfo) (int, int, *types.NewAPIError) {
  320. userQuota, err := model.GetUserQuota(relayInfo.UserId, false)
  321. if err != nil {
  322. return 0, 0, types.NewError(err, types.ErrorCodeQueryDataError)
  323. }
  324. if userQuota <= 0 {
  325. return 0, 0, types.NewErrorWithStatusCode(errors.New("user quota is not enough"), types.ErrorCodeInsufficientUserQuota, http.StatusForbidden)
  326. }
  327. if userQuota-preConsumedQuota < 0 {
  328. return 0, 0, types.NewErrorWithStatusCode(fmt.Errorf("pre-consume quota failed, user quota: %s, need quota: %s", common.FormatQuota(userQuota), common.FormatQuota(preConsumedQuota)), types.ErrorCodeInsufficientUserQuota, http.StatusForbidden)
  329. }
  330. relayInfo.UserQuota = userQuota
  331. if userQuota > 100*preConsumedQuota {
  332. // 用户额度充足,判断令牌额度是否充足
  333. if !relayInfo.TokenUnlimited {
  334. // 非无限令牌,判断令牌额度是否充足
  335. tokenQuota := c.GetInt("token_quota")
  336. if tokenQuota > 100*preConsumedQuota {
  337. // 令牌额度充足,信任令牌
  338. preConsumedQuota = 0
  339. common.LogInfo(c, fmt.Sprintf("user %d quota %s and token %d quota %d are enough, trusted and no need to pre-consume", relayInfo.UserId, common.FormatQuota(userQuota), relayInfo.TokenId, tokenQuota))
  340. }
  341. } else {
  342. // in this case, we do not pre-consume quota
  343. // because the user has enough quota
  344. preConsumedQuota = 0
  345. common.LogInfo(c, fmt.Sprintf("user %d with unlimited token has enough quota %s, trusted and no need to pre-consume", relayInfo.UserId, common.FormatQuota(userQuota)))
  346. }
  347. }
  348. if preConsumedQuota > 0 {
  349. err := service.PreConsumeTokenQuota(relayInfo, preConsumedQuota)
  350. if err != nil {
  351. return 0, 0, types.NewErrorWithStatusCode(err, types.ErrorCodePreConsumeTokenQuotaFailed, http.StatusForbidden)
  352. }
  353. err = model.DecreaseUserQuota(relayInfo.UserId, preConsumedQuota)
  354. if err != nil {
  355. return 0, 0, types.NewError(err, types.ErrorCodeUpdateDataError)
  356. }
  357. }
  358. return preConsumedQuota, userQuota, nil
  359. }
  360. func returnPreConsumedQuota(c *gin.Context, relayInfo *relaycommon.RelayInfo, userQuota int, preConsumedQuota int) {
  361. if preConsumedQuota != 0 {
  362. gopool.Go(func() {
  363. relayInfoCopy := *relayInfo
  364. err := service.PostConsumeQuota(&relayInfoCopy, -preConsumedQuota, 0, false)
  365. if err != nil {
  366. common.SysError("error return pre-consumed quota: " + err.Error())
  367. }
  368. })
  369. }
  370. }
  371. func postConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo,
  372. usage *dto.Usage, preConsumedQuota int, userQuota int, priceData helper.PriceData, extraContent string) {
  373. if usage == nil {
  374. usage = &dto.Usage{
  375. PromptTokens: relayInfo.PromptTokens,
  376. CompletionTokens: 0,
  377. TotalTokens: relayInfo.PromptTokens,
  378. }
  379. extraContent += "(可能是请求出错)"
  380. }
  381. useTimeSeconds := time.Now().Unix() - relayInfo.StartTime.Unix()
  382. promptTokens := usage.PromptTokens
  383. cacheTokens := usage.PromptTokensDetails.CachedTokens
  384. imageTokens := usage.PromptTokensDetails.ImageTokens
  385. audioTokens := usage.PromptTokensDetails.AudioTokens
  386. completionTokens := usage.CompletionTokens
  387. modelName := relayInfo.OriginModelName
  388. tokenName := ctx.GetString("token_name")
  389. completionRatio := priceData.CompletionRatio
  390. cacheRatio := priceData.CacheRatio
  391. imageRatio := priceData.ImageRatio
  392. modelRatio := priceData.ModelRatio
  393. groupRatio := priceData.GroupRatioInfo.GroupRatio
  394. modelPrice := priceData.ModelPrice
  395. // Convert values to decimal for precise calculation
  396. dPromptTokens := decimal.NewFromInt(int64(promptTokens))
  397. dCacheTokens := decimal.NewFromInt(int64(cacheTokens))
  398. dImageTokens := decimal.NewFromInt(int64(imageTokens))
  399. dAudioTokens := decimal.NewFromInt(int64(audioTokens))
  400. dCompletionTokens := decimal.NewFromInt(int64(completionTokens))
  401. dCompletionRatio := decimal.NewFromFloat(completionRatio)
  402. dCacheRatio := decimal.NewFromFloat(cacheRatio)
  403. dImageRatio := decimal.NewFromFloat(imageRatio)
  404. dModelRatio := decimal.NewFromFloat(modelRatio)
  405. dGroupRatio := decimal.NewFromFloat(groupRatio)
  406. dModelPrice := decimal.NewFromFloat(modelPrice)
  407. dQuotaPerUnit := decimal.NewFromFloat(common.QuotaPerUnit)
  408. ratio := dModelRatio.Mul(dGroupRatio)
  409. // openai web search 工具计费
  410. var dWebSearchQuota decimal.Decimal
  411. var webSearchPrice float64
  412. // response api 格式工具计费
  413. if relayInfo.ResponsesUsageInfo != nil {
  414. if webSearchTool, exists := relayInfo.ResponsesUsageInfo.BuiltInTools[dto.BuildInToolWebSearchPreview]; exists && webSearchTool.CallCount > 0 {
  415. // 计算 web search 调用的配额 (配额 = 价格 * 调用次数 / 1000 * 分组倍率)
  416. webSearchPrice = operation_setting.GetWebSearchPricePerThousand(modelName, webSearchTool.SearchContextSize)
  417. dWebSearchQuota = decimal.NewFromFloat(webSearchPrice).
  418. Mul(decimal.NewFromInt(int64(webSearchTool.CallCount))).
  419. Div(decimal.NewFromInt(1000)).Mul(dGroupRatio).Mul(dQuotaPerUnit)
  420. extraContent += fmt.Sprintf("Web Search 调用 %d 次,上下文大小 %s,调用花费 %s",
  421. webSearchTool.CallCount, webSearchTool.SearchContextSize, dWebSearchQuota.String())
  422. }
  423. } else if strings.HasSuffix(modelName, "search-preview") {
  424. // search-preview 模型不支持 response api
  425. searchContextSize := ctx.GetString("chat_completion_web_search_context_size")
  426. if searchContextSize == "" {
  427. searchContextSize = "medium"
  428. }
  429. webSearchPrice = operation_setting.GetWebSearchPricePerThousand(modelName, searchContextSize)
  430. dWebSearchQuota = decimal.NewFromFloat(webSearchPrice).
  431. Div(decimal.NewFromInt(1000)).Mul(dGroupRatio).Mul(dQuotaPerUnit)
  432. extraContent += fmt.Sprintf("Web Search 调用 1 次,上下文大小 %s,调用花费 %s",
  433. searchContextSize, dWebSearchQuota.String())
  434. }
  435. // claude web search tool 计费
  436. var dClaudeWebSearchQuota decimal.Decimal
  437. var claudeWebSearchPrice float64
  438. claudeWebSearchCallCount := ctx.GetInt("claude_web_search_requests")
  439. if claudeWebSearchCallCount > 0 {
  440. claudeWebSearchPrice = operation_setting.GetClaudeWebSearchPricePerThousand()
  441. dClaudeWebSearchQuota = decimal.NewFromFloat(claudeWebSearchPrice).
  442. Div(decimal.NewFromInt(1000)).Mul(dGroupRatio).Mul(dQuotaPerUnit).Mul(decimal.NewFromInt(int64(claudeWebSearchCallCount)))
  443. extraContent += fmt.Sprintf("Claude Web Search 调用 %d 次,调用花费 %s",
  444. claudeWebSearchCallCount, dClaudeWebSearchQuota.String())
  445. }
  446. // file search tool 计费
  447. var dFileSearchQuota decimal.Decimal
  448. var fileSearchPrice float64
  449. if relayInfo.ResponsesUsageInfo != nil {
  450. if fileSearchTool, exists := relayInfo.ResponsesUsageInfo.BuiltInTools[dto.BuildInToolFileSearch]; exists && fileSearchTool.CallCount > 0 {
  451. fileSearchPrice = operation_setting.GetFileSearchPricePerThousand()
  452. dFileSearchQuota = decimal.NewFromFloat(fileSearchPrice).
  453. Mul(decimal.NewFromInt(int64(fileSearchTool.CallCount))).
  454. Div(decimal.NewFromInt(1000)).Mul(dGroupRatio).Mul(dQuotaPerUnit)
  455. extraContent += fmt.Sprintf("File Search 调用 %d 次,调用花费 %s",
  456. fileSearchTool.CallCount, dFileSearchQuota.String())
  457. }
  458. }
  459. var quotaCalculateDecimal decimal.Decimal
  460. var audioInputQuota decimal.Decimal
  461. var audioInputPrice float64
  462. if !priceData.UsePrice {
  463. baseTokens := dPromptTokens
  464. // 减去 cached tokens
  465. var cachedTokensWithRatio decimal.Decimal
  466. if !dCacheTokens.IsZero() {
  467. baseTokens = baseTokens.Sub(dCacheTokens)
  468. cachedTokensWithRatio = dCacheTokens.Mul(dCacheRatio)
  469. }
  470. // 减去 image tokens
  471. var imageTokensWithRatio decimal.Decimal
  472. if !dImageTokens.IsZero() {
  473. baseTokens = baseTokens.Sub(dImageTokens)
  474. imageTokensWithRatio = dImageTokens.Mul(dImageRatio)
  475. }
  476. // 减去 Gemini audio tokens
  477. if !dAudioTokens.IsZero() {
  478. audioInputPrice = operation_setting.GetGeminiInputAudioPricePerMillionTokens(modelName)
  479. if audioInputPrice > 0 {
  480. // 重新计算 base tokens
  481. baseTokens = baseTokens.Sub(dAudioTokens)
  482. audioInputQuota = decimal.NewFromFloat(audioInputPrice).Div(decimal.NewFromInt(1000000)).Mul(dAudioTokens).Mul(dGroupRatio).Mul(dQuotaPerUnit)
  483. extraContent += fmt.Sprintf("Audio Input 花费 %s", audioInputQuota.String())
  484. }
  485. }
  486. promptQuota := baseTokens.Add(cachedTokensWithRatio).Add(imageTokensWithRatio)
  487. completionQuota := dCompletionTokens.Mul(dCompletionRatio)
  488. quotaCalculateDecimal = promptQuota.Add(completionQuota).Mul(ratio)
  489. if !ratio.IsZero() && quotaCalculateDecimal.LessThanOrEqual(decimal.Zero) {
  490. quotaCalculateDecimal = decimal.NewFromInt(1)
  491. }
  492. } else {
  493. quotaCalculateDecimal = dModelPrice.Mul(dQuotaPerUnit).Mul(dGroupRatio)
  494. }
  495. // 添加 responses tools call 调用的配额
  496. quotaCalculateDecimal = quotaCalculateDecimal.Add(dWebSearchQuota)
  497. quotaCalculateDecimal = quotaCalculateDecimal.Add(dFileSearchQuota)
  498. // 添加 audio input 独立计费
  499. quotaCalculateDecimal = quotaCalculateDecimal.Add(audioInputQuota)
  500. quota := int(quotaCalculateDecimal.Round(0).IntPart())
  501. totalTokens := promptTokens + completionTokens
  502. var logContent string
  503. if !priceData.UsePrice {
  504. logContent = fmt.Sprintf("模型倍率 %.2f,补全倍率 %.2f,分组倍率 %.2f", modelRatio, completionRatio, groupRatio)
  505. } else {
  506. logContent = fmt.Sprintf("模型价格 %.2f,分组倍率 %.2f", modelPrice, groupRatio)
  507. }
  508. // record all the consume log even if quota is 0
  509. if totalTokens == 0 {
  510. // in this case, must be some error happened
  511. // we cannot just return, because we may have to return the pre-consumed quota
  512. quota = 0
  513. logContent += fmt.Sprintf("(可能是上游超时)")
  514. common.LogError(ctx, fmt.Sprintf("total tokens is 0, cannot consume quota, userId %d, channelId %d, "+
  515. "tokenId %d, model %s, pre-consumed quota %d", relayInfo.UserId, relayInfo.ChannelId, relayInfo.TokenId, modelName, preConsumedQuota))
  516. } else {
  517. model.UpdateUserUsedQuotaAndRequestCount(relayInfo.UserId, quota)
  518. model.UpdateChannelUsedQuota(relayInfo.ChannelId, quota)
  519. }
  520. quotaDelta := quota - preConsumedQuota
  521. if quotaDelta != 0 {
  522. err := service.PostConsumeQuota(relayInfo, quotaDelta, preConsumedQuota, true)
  523. if err != nil {
  524. common.LogError(ctx, "error consuming token remain quota: "+err.Error())
  525. }
  526. }
  527. logModel := modelName
  528. if strings.HasPrefix(logModel, "gpt-4-gizmo") {
  529. logModel = "gpt-4-gizmo-*"
  530. logContent += fmt.Sprintf(",模型 %s", modelName)
  531. }
  532. if strings.HasPrefix(logModel, "gpt-4o-gizmo") {
  533. logModel = "gpt-4o-gizmo-*"
  534. logContent += fmt.Sprintf(",模型 %s", modelName)
  535. }
  536. if extraContent != "" {
  537. logContent += ", " + extraContent
  538. }
  539. other := service.GenerateTextOtherInfo(ctx, relayInfo, modelRatio, groupRatio, completionRatio, cacheTokens, cacheRatio, modelPrice, priceData.GroupRatioInfo.GroupSpecialRatio)
  540. if imageTokens != 0 {
  541. other["image"] = true
  542. other["image_ratio"] = imageRatio
  543. other["image_output"] = imageTokens
  544. }
  545. if !dWebSearchQuota.IsZero() {
  546. if relayInfo.ResponsesUsageInfo != nil {
  547. if webSearchTool, exists := relayInfo.ResponsesUsageInfo.BuiltInTools[dto.BuildInToolWebSearchPreview]; exists {
  548. other["web_search"] = true
  549. other["web_search_call_count"] = webSearchTool.CallCount
  550. other["web_search_price"] = webSearchPrice
  551. }
  552. } else if strings.HasSuffix(modelName, "search-preview") {
  553. other["web_search"] = true
  554. other["web_search_call_count"] = 1
  555. other["web_search_price"] = webSearchPrice
  556. }
  557. } else if !dClaudeWebSearchQuota.IsZero() {
  558. other["web_search"] = true
  559. other["web_search_call_count"] = claudeWebSearchCallCount
  560. other["web_search_price"] = claudeWebSearchPrice
  561. }
  562. if !dFileSearchQuota.IsZero() && relayInfo.ResponsesUsageInfo != nil {
  563. if fileSearchTool, exists := relayInfo.ResponsesUsageInfo.BuiltInTools[dto.BuildInToolFileSearch]; exists {
  564. other["file_search"] = true
  565. other["file_search_call_count"] = fileSearchTool.CallCount
  566. other["file_search_price"] = fileSearchPrice
  567. }
  568. }
  569. if !audioInputQuota.IsZero() {
  570. other["audio_input_seperate_price"] = true
  571. other["audio_input_token_count"] = audioTokens
  572. other["audio_input_price"] = audioInputPrice
  573. }
  574. // 提取用户输入内容
  575. var userInput string
  576. if common.LogUserInputEnabled {
  577. if textRequestInterface, exists := ctx.Get("text_request"); exists {
  578. if textRequest, ok := textRequestInterface.(*dto.GeneralOpenAIRequest); ok && textRequest != nil {
  579. userInput = extractUserInputFromMessages(textRequest.Messages)
  580. }
  581. }
  582. }
  583. model.RecordConsumeLog(ctx, relayInfo.UserId, model.RecordConsumeLogParams{
  584. ChannelId: relayInfo.ChannelId,
  585. PromptTokens: promptTokens,
  586. CompletionTokens: completionTokens,
  587. ModelName: logModel,
  588. TokenName: tokenName,
  589. Quota: quota,
  590. Content: logContent,
  591. UserInput: userInput,
  592. TokenId: relayInfo.TokenId,
  593. UserQuota: userQuota,
  594. UseTimeSeconds: int(useTimeSeconds),
  595. IsStream: relayInfo.IsStream,
  596. Group: relayInfo.UsingGroup,
  597. Other: other,
  598. })
  599. }