adaptor.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package deepseek
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "strings"
  8. "github.com/QuantumNous/new-api/common"
  9. "github.com/QuantumNous/new-api/dto"
  10. "github.com/QuantumNous/new-api/relay/channel"
  11. "github.com/QuantumNous/new-api/relay/channel/claude"
  12. "github.com/QuantumNous/new-api/relay/channel/openai"
  13. relaycommon "github.com/QuantumNous/new-api/relay/common"
  14. "github.com/QuantumNous/new-api/relay/constant"
  15. "github.com/QuantumNous/new-api/setting/reasoning"
  16. "github.com/QuantumNous/new-api/types"
  17. "github.com/gin-gonic/gin"
  18. )
  19. type Adaptor struct {
  20. }
  21. func (a *Adaptor) ConvertGeminiRequest(*gin.Context, *relaycommon.RelayInfo, *dto.GeminiChatRequest) (any, error) {
  22. //TODO implement me
  23. return nil, errors.New("not implemented")
  24. }
  25. func (a *Adaptor) ConvertClaudeRequest(c *gin.Context, info *relaycommon.RelayInfo, req *dto.ClaudeRequest) (any, error) {
  26. adaptor := claude.Adaptor{}
  27. convertedRequest, err := adaptor.ConvertClaudeRequest(c, info, req)
  28. if err != nil {
  29. return nil, err
  30. }
  31. claudeRequest, ok := convertedRequest.(*dto.ClaudeRequest)
  32. if !ok {
  33. return convertedRequest, nil
  34. }
  35. if err := applyDeepSeekV4ClaudeThinkingSuffix(info, claudeRequest); err != nil {
  36. return nil, err
  37. }
  38. return claudeRequest, nil
  39. }
  40. func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
  41. //TODO implement me
  42. return nil, errors.New("not implemented")
  43. }
  44. func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error) {
  45. //TODO implement me
  46. return nil, errors.New("not implemented")
  47. }
  48. func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
  49. }
  50. func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
  51. fimBaseUrl := info.ChannelBaseUrl
  52. switch info.RelayFormat {
  53. case types.RelayFormatClaude:
  54. return fmt.Sprintf("%s/anthropic/v1/messages", info.ChannelBaseUrl), nil
  55. default:
  56. if !strings.HasSuffix(info.ChannelBaseUrl, "/beta") {
  57. fimBaseUrl += "/beta"
  58. }
  59. switch info.RelayMode {
  60. case constant.RelayModeCompletions:
  61. return fmt.Sprintf("%s/completions", fimBaseUrl), nil
  62. default:
  63. return fmt.Sprintf("%s/v1/chat/completions", info.ChannelBaseUrl), nil
  64. }
  65. }
  66. }
  67. func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
  68. channel.SetupApiRequestHeader(info, c, req)
  69. req.Set("Authorization", "Bearer "+info.ApiKey)
  70. return nil
  71. }
  72. func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
  73. if request == nil {
  74. return nil, errors.New("request is nil")
  75. }
  76. if err := applyDeepSeekV4OpenAIThinkingSuffix(info, request); err != nil {
  77. return nil, err
  78. }
  79. return request, nil
  80. }
  81. func applyDeepSeekV4OpenAIThinkingSuffix(info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) error {
  82. modelName := request.Model
  83. if info != nil && info.ChannelMeta != nil && info.UpstreamModelName != "" {
  84. modelName = info.UpstreamModelName
  85. }
  86. baseModel, thinkingType, effort, ok := reasoning.ParseDeepSeekV4ThinkingSuffix(modelName)
  87. if !ok {
  88. return nil
  89. }
  90. thinking, err := common.Marshal(map[string]string{
  91. "type": thinkingType,
  92. })
  93. if err != nil {
  94. return fmt.Errorf("error marshalling thinking: %w", err)
  95. }
  96. request.Model = baseModel
  97. request.THINKING = thinking
  98. request.ReasoningEffort = effort
  99. if info != nil {
  100. if info.ChannelMeta != nil {
  101. info.UpstreamModelName = baseModel
  102. }
  103. info.ReasoningEffort = effort
  104. }
  105. return nil
  106. }
  107. func applyDeepSeekV4ClaudeThinkingSuffix(info *relaycommon.RelayInfo, request *dto.ClaudeRequest) error {
  108. modelName := request.Model
  109. if info != nil && info.ChannelMeta != nil && info.UpstreamModelName != "" {
  110. modelName = info.UpstreamModelName
  111. }
  112. baseModel, thinkingType, effort, ok := reasoning.ParseDeepSeekV4ThinkingSuffix(modelName)
  113. if !ok {
  114. return nil
  115. }
  116. request.Model = baseModel
  117. request.Thinking = &dto.Thinking{Type: thinkingType}
  118. if effort == "" {
  119. request.OutputConfig = nil
  120. } else {
  121. outputConfig, err := common.Marshal(map[string]string{
  122. "effort": effort,
  123. })
  124. if err != nil {
  125. return fmt.Errorf("error marshalling output_config: %w", err)
  126. }
  127. request.OutputConfig = outputConfig
  128. }
  129. if info != nil {
  130. if info.ChannelMeta != nil {
  131. info.UpstreamModelName = baseModel
  132. }
  133. info.ReasoningEffort = effort
  134. }
  135. return nil
  136. }
  137. func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
  138. return nil, nil
  139. }
  140. func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) {
  141. //TODO implement me
  142. return nil, errors.New("not implemented")
  143. }
  144. func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
  145. // TODO implement me
  146. return nil, errors.New("not implemented")
  147. }
  148. func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
  149. return channel.DoApiRequest(a, c, info, requestBody)
  150. }
  151. func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
  152. switch info.RelayFormat {
  153. case types.RelayFormatClaude:
  154. adaptor := claude.Adaptor{}
  155. return adaptor.DoResponse(c, resp, info)
  156. default:
  157. adaptor := openai.Adaptor{}
  158. return adaptor.DoResponse(c, resp, info)
  159. }
  160. }
  161. func (a *Adaptor) GetModelList() []string {
  162. return ModelList
  163. }
  164. func (a *Adaptor) GetChannelName() string {
  165. return ChannelName
  166. }