claude_handler.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package relay
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "strings"
  9. "github.com/QuantumNous/new-api/common"
  10. "github.com/QuantumNous/new-api/constant"
  11. "github.com/QuantumNous/new-api/dto"
  12. relaycommon "github.com/QuantumNous/new-api/relay/common"
  13. "github.com/QuantumNous/new-api/relay/helper"
  14. "github.com/QuantumNous/new-api/service"
  15. "github.com/QuantumNous/new-api/setting/model_setting"
  16. "github.com/QuantumNous/new-api/setting/reasoning"
  17. "github.com/QuantumNous/new-api/types"
  18. "github.com/gin-gonic/gin"
  19. )
  20. func ClaudeHelper(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError *types.NewAPIError) {
  21. info.InitChannelMeta(c)
  22. claudeReq, ok := info.Request.(*dto.ClaudeRequest)
  23. if !ok {
  24. return types.NewErrorWithStatusCode(fmt.Errorf("invalid request type, expected *dto.ClaudeRequest, got %T", info.Request), types.ErrorCodeInvalidRequest, http.StatusBadRequest, types.ErrOptionWithSkipRetry())
  25. }
  26. request, err := common.DeepCopy(claudeReq)
  27. if err != nil {
  28. return types.NewError(fmt.Errorf("failed to copy request to ClaudeRequest: %w", err), types.ErrorCodeInvalidRequest, types.ErrOptionWithSkipRetry())
  29. }
  30. err = helper.ModelMappedHelper(c, info, request)
  31. if err != nil {
  32. return types.NewError(err, types.ErrorCodeChannelModelMappedError, types.ErrOptionWithSkipRetry())
  33. }
  34. adaptor := GetAdaptor(info.ApiType)
  35. if adaptor == nil {
  36. return types.NewError(fmt.Errorf("invalid api type: %d", info.ApiType), types.ErrorCodeInvalidApiType, types.ErrOptionWithSkipRetry())
  37. }
  38. adaptor.Init(info)
  39. if request.MaxTokens == nil || *request.MaxTokens == 0 {
  40. defaultMaxTokens := uint(model_setting.GetClaudeSettings().GetDefaultMaxTokens(request.Model))
  41. request.MaxTokens = &defaultMaxTokens
  42. }
  43. if baseModel, effortLevel, ok := reasoning.TrimEffortSuffix(request.Model); ok && effortLevel != "" &&
  44. (strings.HasPrefix(request.Model, "claude-opus-4-6") || strings.HasPrefix(request.Model, "claude-opus-4-7")) {
  45. request.Model = baseModel
  46. request.Thinking = &dto.Thinking{
  47. Type: "adaptive",
  48. }
  49. request.OutputConfig = json.RawMessage(fmt.Sprintf(`{"effort":"%s"}`, effortLevel))
  50. if strings.HasPrefix(request.Model, "claude-opus-4-7") {
  51. // Opus 4.7 rejects non-default temperature/top_p/top_k with 400
  52. // and defaults display to "omitted"; restore the 4.6 visible summary.
  53. request.Thinking.Display = "summarized"
  54. request.Temperature = nil
  55. request.TopP = nil
  56. request.TopK = nil
  57. } else {
  58. request.Temperature = common.GetPointer[float64](1.0)
  59. }
  60. info.UpstreamModelName = request.Model
  61. } else if model_setting.GetClaudeSettings().ThinkingAdapterEnabled &&
  62. strings.HasSuffix(request.Model, "-thinking") {
  63. if request.Thinking == nil {
  64. baseModel := strings.TrimSuffix(request.Model, "-thinking")
  65. if strings.HasPrefix(baseModel, "claude-opus-4-7") {
  66. // Opus 4.7 rejects thinking.type="enabled"; use adaptive at high effort.
  67. request.Thinking = &dto.Thinking{Type: "adaptive", Display: "summarized"}
  68. request.OutputConfig = json.RawMessage(`{"effort":"high"}`)
  69. request.Temperature = nil
  70. request.TopP = nil
  71. request.TopK = nil
  72. } else {
  73. // 因为BudgetTokens 必须大于1024
  74. if request.MaxTokens == nil || *request.MaxTokens < 1280 {
  75. request.MaxTokens = common.GetPointer[uint](1280)
  76. }
  77. // BudgetTokens 为 max_tokens 的 80%
  78. request.Thinking = &dto.Thinking{
  79. Type: "enabled",
  80. BudgetTokens: common.GetPointer[int](int(float64(*request.MaxTokens) * model_setting.GetClaudeSettings().ThinkingAdapterBudgetTokensPercentage)),
  81. }
  82. // TODO: 临时处理
  83. // https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking#important-considerations-when-using-extended-thinking
  84. request.Temperature = common.GetPointer[float64](1.0)
  85. }
  86. }
  87. if !model_setting.ShouldPreserveThinkingSuffix(info.OriginModelName) {
  88. request.Model = strings.TrimSuffix(request.Model, "-thinking")
  89. }
  90. info.UpstreamModelName = request.Model
  91. }
  92. if info.ChannelSetting.SystemPrompt != "" {
  93. if request.System == nil {
  94. request.SetStringSystem(info.ChannelSetting.SystemPrompt)
  95. } else if info.ChannelSetting.SystemPromptOverride {
  96. common.SetContextKey(c, constant.ContextKeySystemPromptOverride, true)
  97. if request.IsStringSystem() {
  98. existing := strings.TrimSpace(request.GetStringSystem())
  99. if existing == "" {
  100. request.SetStringSystem(info.ChannelSetting.SystemPrompt)
  101. } else {
  102. request.SetStringSystem(info.ChannelSetting.SystemPrompt + "\n" + existing)
  103. }
  104. } else {
  105. systemContents := request.ParseSystem()
  106. newSystem := dto.ClaudeMediaMessage{Type: dto.ContentTypeText}
  107. newSystem.SetText(info.ChannelSetting.SystemPrompt)
  108. if len(systemContents) == 0 {
  109. request.System = []dto.ClaudeMediaMessage{newSystem}
  110. } else {
  111. request.System = append([]dto.ClaudeMediaMessage{newSystem}, systemContents...)
  112. }
  113. }
  114. }
  115. }
  116. if !model_setting.GetGlobalSettings().PassThroughRequestEnabled &&
  117. !info.ChannelSetting.PassThroughBodyEnabled &&
  118. service.ShouldChatCompletionsUseResponsesGlobal(info.ChannelId, info.ChannelType, info.OriginModelName) {
  119. openAIRequest, convErr := service.ClaudeToOpenAIRequest(*request, info)
  120. if convErr != nil {
  121. return types.NewError(convErr, types.ErrorCodeConvertRequestFailed, types.ErrOptionWithSkipRetry())
  122. }
  123. usage, newApiErr := chatCompletionsViaResponses(c, info, adaptor, openAIRequest)
  124. if newApiErr != nil {
  125. return newApiErr
  126. }
  127. service.PostTextConsumeQuota(c, info, usage, nil)
  128. return nil
  129. }
  130. var requestBody io.Reader
  131. if model_setting.GetGlobalSettings().PassThroughRequestEnabled || info.ChannelSetting.PassThroughBodyEnabled {
  132. storage, err := common.GetBodyStorage(c)
  133. if err != nil {
  134. return types.NewErrorWithStatusCode(err, types.ErrorCodeReadRequestBodyFailed, http.StatusBadRequest, types.ErrOptionWithSkipRetry())
  135. }
  136. requestBody = common.ReaderOnly(storage)
  137. } else {
  138. convertedRequest, err := adaptor.ConvertClaudeRequest(c, info, request)
  139. if err != nil {
  140. return types.NewError(err, types.ErrorCodeConvertRequestFailed, types.ErrOptionWithSkipRetry())
  141. }
  142. relaycommon.AppendRequestConversionFromRequest(info, convertedRequest)
  143. jsonData, err := common.Marshal(convertedRequest)
  144. if err != nil {
  145. return types.NewError(err, types.ErrorCodeConvertRequestFailed, types.ErrOptionWithSkipRetry())
  146. }
  147. // remove disabled fields for Claude API
  148. jsonData, err = relaycommon.RemoveDisabledFields(jsonData, info.ChannelOtherSettings, info.ChannelSetting.PassThroughBodyEnabled)
  149. if err != nil {
  150. return types.NewError(err, types.ErrorCodeConvertRequestFailed, types.ErrOptionWithSkipRetry())
  151. }
  152. // apply param override
  153. if len(info.ParamOverride) > 0 {
  154. jsonData, err = relaycommon.ApplyParamOverrideWithRelayInfo(jsonData, info)
  155. if err != nil {
  156. return newAPIErrorFromParamOverride(err)
  157. }
  158. }
  159. if common.DebugEnabled {
  160. println("requestBody: ", string(jsonData))
  161. }
  162. requestBody = bytes.NewBuffer(jsonData)
  163. }
  164. statusCodeMappingStr := c.GetString("status_code_mapping")
  165. var httpResp *http.Response
  166. resp, err := adaptor.DoRequest(c, info, requestBody)
  167. if err != nil {
  168. return types.NewOpenAIError(err, types.ErrorCodeDoRequestFailed, http.StatusInternalServerError)
  169. }
  170. if resp != nil {
  171. httpResp = resp.(*http.Response)
  172. info.IsStream = info.IsStream || strings.HasPrefix(httpResp.Header.Get("Content-Type"), "text/event-stream")
  173. if httpResp.StatusCode != http.StatusOK {
  174. newAPIError = service.RelayErrorHandler(c.Request.Context(), httpResp, false)
  175. // reset status code 重置状态码
  176. service.ResetStatusCode(newAPIError, statusCodeMappingStr)
  177. return newAPIError
  178. }
  179. }
  180. usage, newAPIError := adaptor.DoResponse(c, httpResp, info)
  181. //log.Printf("usage: %v", usage)
  182. if newAPIError != nil {
  183. // reset status code 重置状态码
  184. service.ResetStatusCode(newAPIError, statusCodeMappingStr)
  185. return newAPIError
  186. }
  187. service.PostTextConsumeQuota(c, info, usage.(*dto.Usage), nil)
  188. return nil
  189. }