adaptor.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package ali
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "strings"
  8. "github.com/QuantumNous/new-api/dto"
  9. "github.com/QuantumNous/new-api/relay/channel"
  10. "github.com/QuantumNous/new-api/relay/channel/claude"
  11. "github.com/QuantumNous/new-api/relay/channel/openai"
  12. relaycommon "github.com/QuantumNous/new-api/relay/common"
  13. "github.com/QuantumNous/new-api/relay/constant"
  14. "github.com/QuantumNous/new-api/types"
  15. "github.com/gin-gonic/gin"
  16. )
  17. type Adaptor struct {
  18. }
  19. func (a *Adaptor) ConvertGeminiRequest(*gin.Context, *relaycommon.RelayInfo, *dto.GeminiChatRequest) (any, error) {
  20. //TODO implement me
  21. return nil, errors.New("not implemented")
  22. }
  23. func (a *Adaptor) ConvertClaudeRequest(c *gin.Context, info *relaycommon.RelayInfo, req *dto.ClaudeRequest) (any, error) {
  24. return req, nil
  25. }
  26. func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
  27. }
  28. func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
  29. var fullRequestURL string
  30. switch info.RelayFormat {
  31. case types.RelayFormatClaude:
  32. fullRequestURL = fmt.Sprintf("%s/api/v2/apps/claude-code-proxy/v1/messages", info.ChannelBaseUrl)
  33. default:
  34. switch info.RelayMode {
  35. case constant.RelayModeEmbeddings:
  36. fullRequestURL = fmt.Sprintf("%s/compatible-mode/v1/embeddings", info.ChannelBaseUrl)
  37. case constant.RelayModeRerank:
  38. fullRequestURL = fmt.Sprintf("%s/api/v1/services/rerank/text-rerank/text-rerank", info.ChannelBaseUrl)
  39. case constant.RelayModeImagesGenerations:
  40. fullRequestURL = fmt.Sprintf("%s/api/v1/services/aigc/text2image/image-synthesis", info.ChannelBaseUrl)
  41. case constant.RelayModeImagesEdits:
  42. if isWanModel(info.OriginModelName) {
  43. fullRequestURL = fmt.Sprintf("%s/api/v1/services/aigc/image2image/image-synthesis", info.ChannelBaseUrl)
  44. } else {
  45. fullRequestURL = fmt.Sprintf("%s/api/v1/services/aigc/multimodal-generation/generation", info.ChannelBaseUrl)
  46. }
  47. case constant.RelayModeCompletions:
  48. fullRequestURL = fmt.Sprintf("%s/compatible-mode/v1/completions", info.ChannelBaseUrl)
  49. default:
  50. fullRequestURL = fmt.Sprintf("%s/compatible-mode/v1/chat/completions", info.ChannelBaseUrl)
  51. }
  52. }
  53. return fullRequestURL, nil
  54. }
  55. func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
  56. channel.SetupApiRequestHeader(info, c, req)
  57. req.Set("Authorization", "Bearer "+info.ApiKey)
  58. if info.IsStream {
  59. req.Set("X-DashScope-SSE", "enable")
  60. }
  61. if c.GetString("plugin") != "" {
  62. req.Set("X-DashScope-Plugin", c.GetString("plugin"))
  63. }
  64. if info.RelayMode == constant.RelayModeImagesGenerations {
  65. req.Set("X-DashScope-Async", "enable")
  66. }
  67. if info.RelayMode == constant.RelayModeImagesEdits {
  68. if isWanModel(info.OriginModelName) {
  69. req.Set("X-DashScope-Async", "enable")
  70. }
  71. req.Set("Content-Type", "application/json")
  72. }
  73. return nil
  74. }
  75. func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
  76. if request == nil {
  77. return nil, errors.New("request is nil")
  78. }
  79. // docs: https://bailian.console.aliyun.com/?tab=api#/api/?type=model&url=2712216
  80. // fix: InternalError.Algo.InvalidParameter: The value of the enable_thinking parameter is restricted to True.
  81. //if strings.Contains(request.Model, "thinking") {
  82. // request.EnableThinking = true
  83. // request.Stream = true
  84. // info.IsStream = true
  85. //}
  86. //// fix: ali parameter.enable_thinking must be set to false for non-streaming calls
  87. //if !info.IsStream {
  88. // request.EnableThinking = false
  89. //}
  90. switch info.RelayMode {
  91. default:
  92. aliReq := requestOpenAI2Ali(*request)
  93. return aliReq, nil
  94. }
  95. }
  96. func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error) {
  97. if info.RelayMode == constant.RelayModeImagesGenerations {
  98. aliRequest, err := oaiImage2Ali(request)
  99. if err != nil {
  100. return nil, fmt.Errorf("convert image request failed: %w", err)
  101. }
  102. return aliRequest, nil
  103. } else if info.RelayMode == constant.RelayModeImagesEdits {
  104. if isWanModel(info.OriginModelName) {
  105. return oaiFormEdit2WanxImageEdit(c, info, request)
  106. }
  107. // ali image edit https://bailian.console.aliyun.com/?tab=api#/api/?type=model&url=2976416
  108. // 如果用户使用表单,则需要解析表单数据
  109. if strings.Contains(c.Request.Header.Get("Content-Type"), "multipart/form-data") {
  110. aliRequest, err := oaiFormEdit2AliImageEdit(c, info, request)
  111. if err != nil {
  112. return nil, fmt.Errorf("convert image edit form request failed: %w", err)
  113. }
  114. return aliRequest, nil
  115. } else {
  116. aliRequest, err := oaiImage2Ali(request)
  117. if err != nil {
  118. return nil, fmt.Errorf("convert image request failed: %w", err)
  119. }
  120. return aliRequest, nil
  121. }
  122. }
  123. return nil, fmt.Errorf("unsupported image relay mode: %d", info.RelayMode)
  124. }
  125. func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
  126. return ConvertRerankRequest(request), nil
  127. }
  128. func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) {
  129. return request, nil
  130. }
  131. func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
  132. //TODO implement me
  133. return nil, errors.New("not implemented")
  134. }
  135. func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
  136. // TODO implement me
  137. return nil, errors.New("not implemented")
  138. }
  139. func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
  140. return channel.DoApiRequest(a, c, info, requestBody)
  141. }
  142. func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
  143. switch info.RelayFormat {
  144. case types.RelayFormatClaude:
  145. if info.IsStream {
  146. return claude.ClaudeStreamHandler(c, resp, info, claude.RequestModeMessage)
  147. } else {
  148. return claude.ClaudeHandler(c, resp, info, claude.RequestModeMessage)
  149. }
  150. default:
  151. switch info.RelayMode {
  152. case constant.RelayModeImagesGenerations:
  153. err, usage = aliImageHandler(c, resp, info)
  154. case constant.RelayModeImagesEdits:
  155. if isWanModel(info.OriginModelName) {
  156. err, usage = aliImageHandler(c, resp, info)
  157. } else {
  158. err, usage = aliImageEditHandler(c, resp, info)
  159. }
  160. case constant.RelayModeRerank:
  161. err, usage = RerankHandler(c, resp, info)
  162. default:
  163. adaptor := openai.Adaptor{}
  164. usage, err = adaptor.DoResponse(c, resp, info)
  165. }
  166. return usage, err
  167. }
  168. }
  169. func (a *Adaptor) GetModelList() []string {
  170. return ModelList
  171. }
  172. func (a *Adaptor) GetChannelName() string {
  173. return ChannelName
  174. }