2
0

adaptor.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package siliconflow
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "one-api/dto"
  8. "one-api/relay/channel"
  9. "one-api/relay/channel/openai"
  10. relaycommon "one-api/relay/common"
  11. "one-api/relay/constant"
  12. "one-api/types"
  13. "github.com/gin-gonic/gin"
  14. )
  15. type Adaptor struct {
  16. }
  17. func (a *Adaptor) ConvertGeminiRequest(*gin.Context, *relaycommon.RelayInfo, *dto.GeminiChatRequest) (any, error) {
  18. //TODO implement me
  19. return nil, errors.New("not implemented")
  20. }
  21. func (a *Adaptor) ConvertClaudeRequest(c *gin.Context, info *relaycommon.RelayInfo, req *dto.ClaudeRequest) (any, error) {
  22. adaptor := openai.Adaptor{}
  23. return adaptor.ConvertClaudeRequest(c, info, req)
  24. }
  25. func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
  26. //TODO implement me
  27. return nil, errors.New("not supported")
  28. }
  29. func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error) {
  30. adaptor := openai.Adaptor{}
  31. return adaptor.ConvertImageRequest(c, info, request)
  32. }
  33. func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
  34. }
  35. func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
  36. if info.RelayMode == constant.RelayModeRerank {
  37. return fmt.Sprintf("%s/v1/rerank", info.ChannelBaseUrl), nil
  38. } else if info.RelayMode == constant.RelayModeEmbeddings {
  39. return fmt.Sprintf("%s/v1/embeddings", info.ChannelBaseUrl), nil
  40. } else if info.RelayMode == constant.RelayModeChatCompletions {
  41. return fmt.Sprintf("%s/v1/chat/completions", info.ChannelBaseUrl), nil
  42. } else if info.RelayMode == constant.RelayModeCompletions {
  43. return fmt.Sprintf("%s/v1/completions", info.ChannelBaseUrl), nil
  44. }
  45. return fmt.Sprintf("%s/v1/chat/completions", info.ChannelBaseUrl), nil
  46. }
  47. func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
  48. channel.SetupApiRequestHeader(info, c, req)
  49. req.Set("Authorization", fmt.Sprintf("Bearer %s", info.ApiKey))
  50. return nil
  51. }
  52. func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
  53. // SiliconFlow requires messages array for FIM requests, even if client doesn't send it
  54. if (request.Prefix != nil || request.Suffix != nil) && len(request.Messages) == 0 {
  55. // Add an empty user message to satisfy SiliconFlow's requirement
  56. request.Messages = []dto.Message{
  57. {
  58. Role: "user",
  59. Content: "",
  60. },
  61. }
  62. }
  63. return request, nil
  64. }
  65. func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
  66. // TODO implement me
  67. return nil, errors.New("not implemented")
  68. }
  69. func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
  70. return channel.DoApiRequest(a, c, info, requestBody)
  71. }
  72. func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
  73. return request, nil
  74. }
  75. func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) {
  76. return request, nil
  77. }
  78. func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
  79. switch info.RelayMode {
  80. case constant.RelayModeRerank:
  81. usage, err = siliconflowRerankHandler(c, info, resp)
  82. case constant.RelayModeEmbeddings:
  83. usage, err = openai.OpenaiHandler(c, info, resp)
  84. case constant.RelayModeCompletions:
  85. fallthrough
  86. case constant.RelayModeChatCompletions:
  87. fallthrough
  88. default:
  89. if info.IsStream {
  90. usage, err = openai.OaiStreamHandler(c, info, resp)
  91. } else {
  92. usage, err = openai.OpenaiHandler(c, info, resp)
  93. }
  94. }
  95. return
  96. }
  97. func (a *Adaptor) GetModelList() []string {
  98. return ModelList
  99. }
  100. func (a *Adaptor) GetChannelName() string {
  101. return ChannelName
  102. }