adaptor.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package siliconflow
  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/openai"
  11. relaycommon "one-api/relay/common"
  12. "one-api/relay/constant"
  13. )
  14. type Adaptor struct {
  15. }
  16. func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
  17. //TODO implement me
  18. return nil, errors.New("not implemented")
  19. }
  20. func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error) {
  21. //TODO implement me
  22. return nil, errors.New("not implemented")
  23. }
  24. func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
  25. }
  26. func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
  27. if info.RelayMode == constant.RelayModeRerank {
  28. return fmt.Sprintf("%s/v1/rerank", info.BaseUrl), nil
  29. } else if info.RelayMode == constant.RelayModeEmbeddings {
  30. return fmt.Sprintf("%s/v1/embeddings", info.BaseUrl), nil
  31. } else if info.RelayMode == constant.RelayModeChatCompletions {
  32. return fmt.Sprintf("%s/v1/chat/completions", info.BaseUrl), nil
  33. }
  34. return "", errors.New("invalid relay mode")
  35. }
  36. func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
  37. channel.SetupApiRequestHeader(info, c, req)
  38. req.Set("Authorization", fmt.Sprintf("Bearer %s", info.ApiKey))
  39. return nil
  40. }
  41. func (a *Adaptor) ConvertRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
  42. return request, nil
  43. }
  44. func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
  45. return channel.DoApiRequest(a, c, info, requestBody)
  46. }
  47. func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
  48. return request, nil
  49. }
  50. func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) {
  51. return request, nil
  52. }
  53. func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *dto.OpenAIErrorWithStatusCode) {
  54. switch info.RelayMode {
  55. case constant.RelayModeRerank:
  56. err, usage = siliconflowRerankHandler(c, resp)
  57. case constant.RelayModeChatCompletions:
  58. if info.IsStream {
  59. err, usage = openai.OaiStreamHandler(c, resp, info)
  60. } else {
  61. err, usage = openai.OpenaiHandler(c, resp, info.PromptTokens, info.UpstreamModelName)
  62. }
  63. case constant.RelayModeEmbeddings:
  64. err, usage = openai.OpenaiHandler(c, resp, info.PromptTokens, info.UpstreamModelName)
  65. }
  66. return
  67. }
  68. func (a *Adaptor) GetModelList() []string {
  69. return ModelList
  70. }
  71. func (a *Adaptor) GetChannelName() string {
  72. return ChannelName
  73. }