adaptor.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package siliconflow
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "github.com/QuantumNous/new-api/common"
  8. "github.com/QuantumNous/new-api/dto"
  9. "github.com/QuantumNous/new-api/relay/channel"
  10. "github.com/QuantumNous/new-api/relay/channel/openai"
  11. relaycommon "github.com/QuantumNous/new-api/relay/common"
  12. "github.com/QuantumNous/new-api/relay/constant"
  13. "github.com/QuantumNous/new-api/types"
  14. "github.com/gin-gonic/gin"
  15. )
  16. type Adaptor struct {
  17. }
  18. func (a *Adaptor) ConvertGeminiRequest(*gin.Context, *relaycommon.RelayInfo, *dto.GeminiChatRequest) (any, error) {
  19. //TODO implement me
  20. return nil, errors.New("not implemented")
  21. }
  22. func (a *Adaptor) ConvertClaudeRequest(c *gin.Context, info *relaycommon.RelayInfo, req *dto.ClaudeRequest) (any, error) {
  23. adaptor := openai.Adaptor{}
  24. return adaptor.ConvertClaudeRequest(c, info, req)
  25. }
  26. func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
  27. //TODO implement me
  28. return nil, errors.New("not supported")
  29. }
  30. func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error) {
  31. // 解析extra到SFImageRequest里,以填入SiliconFlow特殊字段。若失败重建一个空的。
  32. sfRequest := &SFImageRequest{}
  33. extra, err := common.Marshal(request.Extra)
  34. if err == nil {
  35. err = common.Unmarshal(extra, sfRequest)
  36. if err != nil {
  37. sfRequest = &SFImageRequest{}
  38. }
  39. }
  40. sfRequest.Model = request.Model
  41. sfRequest.Prompt = request.Prompt
  42. // 优先使用image_size/batch_size,否则使用OpenAI标准的size/n
  43. if sfRequest.ImageSize == "" {
  44. sfRequest.ImageSize = request.Size
  45. }
  46. if sfRequest.BatchSize == 0 {
  47. sfRequest.BatchSize = request.N
  48. }
  49. return sfRequest, nil
  50. }
  51. func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
  52. }
  53. func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
  54. if info.RelayMode == constant.RelayModeRerank {
  55. return fmt.Sprintf("%s/v1/rerank", info.ChannelBaseUrl), nil
  56. } else if info.RelayMode == constant.RelayModeEmbeddings {
  57. return fmt.Sprintf("%s/v1/embeddings", info.ChannelBaseUrl), nil
  58. } else if info.RelayMode == constant.RelayModeChatCompletions {
  59. return fmt.Sprintf("%s/v1/chat/completions", info.ChannelBaseUrl), nil
  60. } else if info.RelayMode == constant.RelayModeCompletions {
  61. return fmt.Sprintf("%s/v1/completions", info.ChannelBaseUrl), nil
  62. } else if info.RelayMode == constant.RelayModeImagesGenerations {
  63. return fmt.Sprintf("%s/v1/images/generations", info.ChannelBaseUrl), nil
  64. }
  65. return fmt.Sprintf("%s/v1/chat/completions", info.ChannelBaseUrl), nil
  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", fmt.Sprintf("Bearer %s", info.ApiKey))
  70. return nil
  71. }
  72. func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
  73. // SiliconFlow requires messages array for FIM requests, even if client doesn't send it
  74. if (request.Prefix != nil || request.Suffix != nil) && len(request.Messages) == 0 {
  75. // Add an empty user message to satisfy SiliconFlow's requirement
  76. request.Messages = []dto.Message{
  77. {
  78. Role: "user",
  79. Content: "",
  80. },
  81. }
  82. }
  83. return request, nil
  84. }
  85. func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
  86. // TODO implement me
  87. return nil, errors.New("not implemented")
  88. }
  89. func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
  90. return channel.DoApiRequest(a, c, info, requestBody)
  91. }
  92. func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
  93. return request, nil
  94. }
  95. func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) {
  96. return request, nil
  97. }
  98. func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
  99. switch info.RelayMode {
  100. case constant.RelayModeRerank:
  101. usage, err = siliconflowRerankHandler(c, info, resp)
  102. case constant.RelayModeEmbeddings:
  103. usage, err = openai.OpenaiHandler(c, info, resp)
  104. case constant.RelayModeCompletions:
  105. fallthrough
  106. case constant.RelayModeChatCompletions:
  107. fallthrough
  108. case constant.RelayModeImagesGenerations:
  109. fallthrough
  110. default:
  111. if info.IsStream {
  112. usage, err = openai.OaiStreamHandler(c, info, resp)
  113. } else {
  114. usage, err = openai.OpenaiHandler(c, info, resp)
  115. }
  116. }
  117. return
  118. }
  119. func (a *Adaptor) GetModelList() []string {
  120. return ModelList
  121. }
  122. func (a *Adaptor) GetChannelName() string {
  123. return ChannelName
  124. }