adaptor.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package volcengine
  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. "strings"
  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. switch info.RelayMode {
  34. case constant.RelayModeChatCompletions:
  35. if strings.HasPrefix(info.UpstreamModelName, "bot") {
  36. return fmt.Sprintf("%s/api/v3/bots/chat/completions", info.BaseUrl), nil
  37. }
  38. return fmt.Sprintf("%s/api/v3/chat/completions", info.BaseUrl), nil
  39. case constant.RelayModeEmbeddings:
  40. return fmt.Sprintf("%s/api/v3/embeddings", info.BaseUrl), nil
  41. default:
  42. }
  43. return "", fmt.Errorf("unsupported relay mode: %d", info.RelayMode)
  44. }
  45. func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
  46. channel.SetupApiRequestHeader(info, c, req)
  47. req.Set("Authorization", "Bearer "+info.ApiKey)
  48. return nil
  49. }
  50. func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
  51. if request == nil {
  52. return nil, errors.New("request is nil")
  53. }
  54. return request, nil
  55. }
  56. func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
  57. return nil, nil
  58. }
  59. func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) {
  60. return request, nil
  61. }
  62. func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
  63. return channel.DoApiRequest(a, c, info, requestBody)
  64. }
  65. func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *dto.OpenAIErrorWithStatusCode) {
  66. switch info.RelayMode {
  67. case constant.RelayModeChatCompletions:
  68. if info.IsStream {
  69. err, usage = openai.OaiStreamHandler(c, resp, info)
  70. } else {
  71. err, usage = openai.OpenaiHandler(c, resp, info)
  72. }
  73. case constant.RelayModeEmbeddings:
  74. err, usage = openai.OpenaiHandler(c, resp, info)
  75. }
  76. return
  77. }
  78. func (a *Adaptor) GetModelList() []string {
  79. return ModelList
  80. }
  81. func (a *Adaptor) GetChannelName() string {
  82. return ChannelName
  83. }