adaptor.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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) ConvertClaudeRequest(c *gin.Context, info *relaycommon.RelayInfo, req *dto.ClaudeRequest) (any, error) {
  18. adaptor := openai.Adaptor{}
  19. return adaptor.ConvertClaudeRequest(c, info, req)
  20. }
  21. func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
  22. //TODO implement me
  23. return nil, errors.New("not supported")
  24. }
  25. func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error) {
  26. adaptor := openai.Adaptor{}
  27. return adaptor.ConvertImageRequest(c, info, request)
  28. }
  29. func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
  30. }
  31. func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
  32. if info.RelayMode == constant.RelayModeRerank {
  33. return fmt.Sprintf("%s/v1/rerank", info.BaseUrl), nil
  34. } else if info.RelayMode == constant.RelayModeEmbeddings {
  35. return fmt.Sprintf("%s/v1/embeddings", info.BaseUrl), nil
  36. } else if info.RelayMode == constant.RelayModeChatCompletions {
  37. return fmt.Sprintf("%s/v1/chat/completions", info.BaseUrl), nil
  38. } else if info.RelayMode == constant.RelayModeCompletions {
  39. return fmt.Sprintf("%s/v1/completions", info.BaseUrl), nil
  40. }
  41. return fmt.Sprintf("%s/v1/chat/completions", info.BaseUrl), nil
  42. }
  43. func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
  44. channel.SetupApiRequestHeader(info, c, req)
  45. req.Set("Authorization", fmt.Sprintf("Bearer %s", info.ApiKey))
  46. return nil
  47. }
  48. func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
  49. return request, nil
  50. }
  51. func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
  52. // TODO implement me
  53. return nil, errors.New("not implemented")
  54. }
  55. func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
  56. return channel.DoApiRequest(a, c, info, requestBody)
  57. }
  58. func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
  59. return request, nil
  60. }
  61. func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) {
  62. return request, nil
  63. }
  64. func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
  65. switch info.RelayMode {
  66. case constant.RelayModeRerank:
  67. usage, err = siliconflowRerankHandler(c, info, resp)
  68. case constant.RelayModeEmbeddings:
  69. usage, err = openai.OpenaiHandler(c, info, resp)
  70. case constant.RelayModeCompletions:
  71. fallthrough
  72. case constant.RelayModeChatCompletions:
  73. fallthrough
  74. default:
  75. if info.IsStream {
  76. usage, err = openai.OaiStreamHandler(c, info, resp)
  77. } else {
  78. usage, err = openai.OpenaiHandler(c, info, resp)
  79. }
  80. }
  81. return
  82. }
  83. func (a *Adaptor) GetModelList() []string {
  84. return ModelList
  85. }
  86. func (a *Adaptor) GetChannelName() string {
  87. return ChannelName
  88. }