violation_fee.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package service
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "github.com/QuantumNous/new-api/common"
  7. "github.com/QuantumNous/new-api/logger"
  8. "github.com/QuantumNous/new-api/model"
  9. relaycommon "github.com/QuantumNous/new-api/relay/common"
  10. "github.com/QuantumNous/new-api/setting/model_setting"
  11. "github.com/QuantumNous/new-api/types"
  12. "github.com/shopspring/decimal"
  13. "github.com/gin-gonic/gin"
  14. )
  15. const (
  16. ViolationFeeCodePrefix = "violation_fee."
  17. CSAMViolationMarker = "Failed check: SAFETY_CHECK_TYPE"
  18. ContentViolatesUsageMarker = "Content violates usage guidelines"
  19. )
  20. func IsViolationFeeCode(code types.ErrorCode) bool {
  21. return strings.HasPrefix(string(code), ViolationFeeCodePrefix)
  22. }
  23. func HasCSAMViolationMarker(err *types.NewAPIError) bool {
  24. if err == nil {
  25. return false
  26. }
  27. if strings.Contains(err.Error(), CSAMViolationMarker) || strings.Contains(err.Error(), ContentViolatesUsageMarker) {
  28. return true
  29. }
  30. msg := err.ToOpenAIError().Message
  31. return strings.Contains(msg, CSAMViolationMarker) || strings.Contains(err.Error(), ContentViolatesUsageMarker)
  32. }
  33. func WrapAsViolationFeeGrokCSAM(err *types.NewAPIError) *types.NewAPIError {
  34. if err == nil {
  35. return nil
  36. }
  37. oai := err.ToOpenAIError()
  38. oai.Type = string(types.ErrorCodeViolationFeeGrokCSAM)
  39. oai.Code = string(types.ErrorCodeViolationFeeGrokCSAM)
  40. return types.WithOpenAIError(oai, err.StatusCode, types.ErrOptionWithSkipRetry())
  41. }
  42. // NormalizeViolationFeeError ensures:
  43. // - if the CSAM marker is present, error.code is set to a stable violation-fee code and skip-retry is enabled.
  44. // - if error.code already has the violation-fee prefix, skip-retry is enabled.
  45. //
  46. // It must be called before retry decision logic.
  47. func NormalizeViolationFeeError(err *types.NewAPIError) *types.NewAPIError {
  48. if err == nil {
  49. return nil
  50. }
  51. if HasCSAMViolationMarker(err) {
  52. return WrapAsViolationFeeGrokCSAM(err)
  53. }
  54. if IsViolationFeeCode(err.GetErrorCode()) {
  55. oai := err.ToOpenAIError()
  56. return types.WithOpenAIError(oai, err.StatusCode, types.ErrOptionWithSkipRetry())
  57. }
  58. return err
  59. }
  60. func shouldChargeViolationFee(err *types.NewAPIError) bool {
  61. if err == nil {
  62. return false
  63. }
  64. if err.GetErrorCode() == types.ErrorCodeViolationFeeGrokCSAM {
  65. return true
  66. }
  67. // In case some callers didn't normalize, keep a safety net.
  68. return HasCSAMViolationMarker(err)
  69. }
  70. func calcViolationFeeQuota(amount, groupRatio float64) int {
  71. if amount <= 0 {
  72. return 0
  73. }
  74. if groupRatio <= 0 {
  75. return 0
  76. }
  77. quota := decimal.NewFromFloat(amount).
  78. Mul(decimal.NewFromFloat(common.QuotaPerUnit)).
  79. Mul(decimal.NewFromFloat(groupRatio)).
  80. Round(0).
  81. IntPart()
  82. if quota <= 0 {
  83. return 0
  84. }
  85. return int(quota)
  86. }
  87. // ChargeViolationFeeIfNeeded charges an additional fee after the normal flow finishes (including refund).
  88. // It uses Grok fee settings as the fee policy.
  89. func ChargeViolationFeeIfNeeded(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, apiErr *types.NewAPIError) bool {
  90. if ctx == nil || relayInfo == nil || apiErr == nil {
  91. return false
  92. }
  93. //if relayInfo.IsPlayground {
  94. // return false
  95. //}
  96. if !shouldChargeViolationFee(apiErr) {
  97. return false
  98. }
  99. settings := model_setting.GetGrokSettings()
  100. if settings == nil || !settings.ViolationDeductionEnabled {
  101. return false
  102. }
  103. groupRatio := relayInfo.PriceData.GroupRatioInfo.GroupRatio
  104. feeQuota := calcViolationFeeQuota(settings.ViolationDeductionAmount, groupRatio)
  105. if feeQuota <= 0 {
  106. return false
  107. }
  108. if err := PostConsumeQuota(relayInfo, feeQuota, 0, true); err != nil {
  109. logger.LogError(ctx, fmt.Sprintf("failed to charge violation fee: %s", err.Error()))
  110. return false
  111. }
  112. model.UpdateUserUsedQuotaAndRequestCount(relayInfo.UserId, feeQuota)
  113. model.UpdateChannelUsedQuota(relayInfo.ChannelId, feeQuota)
  114. useTimeSeconds := time.Now().Unix() - relayInfo.StartTime.Unix()
  115. tokenName := ctx.GetString("token_name")
  116. oai := apiErr.ToOpenAIError()
  117. other := map[string]any{
  118. "violation_fee": true,
  119. "violation_fee_code": string(types.ErrorCodeViolationFeeGrokCSAM),
  120. "fee_quota": feeQuota,
  121. "base_amount": settings.ViolationDeductionAmount,
  122. "group_ratio": groupRatio,
  123. "status_code": apiErr.StatusCode,
  124. "upstream_error_type": oai.Type,
  125. "upstream_error_code": fmt.Sprintf("%v", oai.Code),
  126. "violation_fee_marker": CSAMViolationMarker,
  127. }
  128. model.RecordConsumeLog(ctx, relayInfo.UserId, model.RecordConsumeLogParams{
  129. ChannelId: relayInfo.ChannelId,
  130. ModelName: relayInfo.OriginModelName,
  131. TokenName: tokenName,
  132. Quota: feeQuota,
  133. Content: "Violation fee charged",
  134. TokenId: relayInfo.TokenId,
  135. UseTimeSeconds: int(useTimeSeconds),
  136. IsStream: relayInfo.IsStream,
  137. Group: relayInfo.UsingGroup,
  138. Other: other,
  139. })
  140. return true
  141. }