adaptor.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. fullRequestURL = fmt.Sprintf("%s/api/v1/services/aigc/multimodal-generation/generation", info.ChannelBaseUrl)
  43. case constant.RelayModeCompletions:
  44. fullRequestURL = fmt.Sprintf("%s/compatible-mode/v1/completions", info.ChannelBaseUrl)
  45. default:
  46. fullRequestURL = fmt.Sprintf("%s/compatible-mode/v1/chat/completions", info.ChannelBaseUrl)
  47. }
  48. }
  49. return fullRequestURL, nil
  50. }
  51. func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
  52. channel.SetupApiRequestHeader(info, c, req)
  53. req.Set("Authorization", "Bearer "+info.ApiKey)
  54. if info.IsStream {
  55. req.Set("X-DashScope-SSE", "enable")
  56. }
  57. if c.GetString("plugin") != "" {
  58. req.Set("X-DashScope-Plugin", c.GetString("plugin"))
  59. }
  60. if info.RelayMode == constant.RelayModeImagesGenerations {
  61. req.Set("X-DashScope-Async", "enable")
  62. }
  63. if info.RelayMode == constant.RelayModeImagesEdits {
  64. req.Set("Content-Type", "application/json")
  65. }
  66. return nil
  67. }
  68. func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
  69. if request == nil {
  70. return nil, errors.New("request is nil")
  71. }
  72. // docs: https://bailian.console.aliyun.com/?tab=api#/api/?type=model&url=2712216
  73. // fix: InternalError.Algo.InvalidParameter: The value of the enable_thinking parameter is restricted to True.
  74. if strings.Contains(request.Model, "thinking") {
  75. request.EnableThinking = true
  76. request.Stream = true
  77. info.IsStream = true
  78. }
  79. // fix: ali parameter.enable_thinking must be set to false for non-streaming calls
  80. if !info.IsStream {
  81. request.EnableThinking = false
  82. }
  83. switch info.RelayMode {
  84. default:
  85. aliReq := requestOpenAI2Ali(*request)
  86. return aliReq, nil
  87. }
  88. }
  89. func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error) {
  90. if info.RelayMode == constant.RelayModeImagesGenerations {
  91. aliRequest, err := oaiImage2Ali(request)
  92. if err != nil {
  93. return nil, fmt.Errorf("convert image request failed: %w", err)
  94. }
  95. return aliRequest, nil
  96. } else if info.RelayMode == constant.RelayModeImagesEdits {
  97. // ali image edit https://bailian.console.aliyun.com/?tab=api#/api/?type=model&url=2976416
  98. // 如果用户使用表单,则需要解析表单数据
  99. if strings.Contains(c.Request.Header.Get("Content-Type"), "multipart/form-data") {
  100. aliRequest, err := oaiFormEdit2AliImageEdit(c, info, request)
  101. if err != nil {
  102. return nil, fmt.Errorf("convert image edit form request failed: %w", err)
  103. }
  104. return aliRequest, nil
  105. } else {
  106. aliRequest, err := oaiImage2Ali(request)
  107. if err != nil {
  108. return nil, fmt.Errorf("convert image request failed: %w", err)
  109. }
  110. return aliRequest, nil
  111. }
  112. }
  113. return nil, fmt.Errorf("unsupported image relay mode: %d", info.RelayMode)
  114. }
  115. func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
  116. return ConvertRerankRequest(request), nil
  117. }
  118. func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) {
  119. return request, nil
  120. }
  121. func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
  122. //TODO implement me
  123. return nil, errors.New("not implemented")
  124. }
  125. func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
  126. // TODO implement me
  127. return nil, errors.New("not implemented")
  128. }
  129. func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
  130. return channel.DoApiRequest(a, c, info, requestBody)
  131. }
  132. func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
  133. switch info.RelayFormat {
  134. case types.RelayFormatClaude:
  135. if info.IsStream {
  136. return claude.ClaudeStreamHandler(c, resp, info, claude.RequestModeMessage)
  137. } else {
  138. return claude.ClaudeHandler(c, resp, info, claude.RequestModeMessage)
  139. }
  140. default:
  141. switch info.RelayMode {
  142. case constant.RelayModeImagesGenerations:
  143. err, usage = aliImageHandler(c, resp, info)
  144. case constant.RelayModeImagesEdits:
  145. err, usage = aliImageEditHandler(c, resp, info)
  146. case constant.RelayModeRerank:
  147. err, usage = RerankHandler(c, resp, info)
  148. default:
  149. adaptor := openai.Adaptor{}
  150. usage, err = adaptor.DoResponse(c, resp, info)
  151. }
  152. return usage, err
  153. }
  154. }
  155. func (a *Adaptor) GetModelList() []string {
  156. return ModelList
  157. }
  158. func (a *Adaptor) GetChannelName() string {
  159. return ChannelName
  160. }