adaptor.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package ali
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "io"
  7. "net/http"
  8. "one-api/dto"
  9. "one-api/relay/channel"
  10. "one-api/relay/channel/claude"
  11. "one-api/relay/channel/openai"
  12. relaycommon "one-api/relay/common"
  13. "one-api/relay/constant"
  14. "one-api/types"
  15. "strings"
  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.RelayModeCompletions:
  42. fullRequestURL = fmt.Sprintf("%s/compatible-mode/v1/completions", info.ChannelBaseUrl)
  43. default:
  44. fullRequestURL = fmt.Sprintf("%s/compatible-mode/v1/chat/completions", info.ChannelBaseUrl)
  45. }
  46. }
  47. return fullRequestURL, nil
  48. }
  49. func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
  50. channel.SetupApiRequestHeader(info, c, req)
  51. req.Set("Authorization", "Bearer "+info.ApiKey)
  52. if info.IsStream {
  53. req.Set("X-DashScope-SSE", "enable")
  54. }
  55. if c.GetString("plugin") != "" {
  56. req.Set("X-DashScope-Plugin", c.GetString("plugin"))
  57. }
  58. return nil
  59. }
  60. func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
  61. if request == nil {
  62. return nil, errors.New("request is nil")
  63. }
  64. // docs: https://bailian.console.aliyun.com/?tab=api#/api/?type=model&url=2712216
  65. // fix: InternalError.Algo.InvalidParameter: The value of the enable_thinking parameter is restricted to True.
  66. if strings.Contains(request.Model, "thinking") {
  67. request.EnableThinking = true
  68. request.Stream = true
  69. info.IsStream = true
  70. }
  71. // fix: ali parameter.enable_thinking must be set to false for non-streaming calls
  72. if !info.IsStream {
  73. request.EnableThinking = false
  74. }
  75. switch info.RelayMode {
  76. default:
  77. aliReq := requestOpenAI2Ali(*request)
  78. return aliReq, nil
  79. }
  80. }
  81. func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error) {
  82. aliRequest := oaiImage2Ali(request)
  83. return aliRequest, nil
  84. }
  85. func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
  86. return ConvertRerankRequest(request), nil
  87. }
  88. func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) {
  89. return request, nil
  90. }
  91. func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
  92. //TODO implement me
  93. return nil, errors.New("not implemented")
  94. }
  95. func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
  96. // TODO implement me
  97. return nil, errors.New("not implemented")
  98. }
  99. func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
  100. return channel.DoApiRequest(a, c, info, requestBody)
  101. }
  102. func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
  103. switch info.RelayFormat {
  104. case types.RelayFormatClaude:
  105. if info.IsStream {
  106. err, usage = claude.ClaudeStreamHandler(c, resp, info, claude.RequestModeMessage)
  107. } else {
  108. err, usage = claude.ClaudeHandler(c, resp, info, claude.RequestModeMessage)
  109. }
  110. default:
  111. adaptor := openai.Adaptor{}
  112. return adaptor.DoResponse(c, resp, info)
  113. }
  114. return
  115. }
  116. func (a *Adaptor) GetModelList() []string {
  117. return ModelList
  118. }
  119. func (a *Adaptor) GetChannelName() string {
  120. return ChannelName
  121. }