adaptor.go 3.2 KB

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