adaptor.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package cloudflare
  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. relaycommon "one-api/relay/common"
  11. "one-api/relay/constant"
  12. )
  13. type Adaptor struct {
  14. }
  15. func (a *Adaptor) InitRerank(info *relaycommon.RelayInfo, request dto.RerankRequest) {
  16. }
  17. func (a *Adaptor) Init(info *relaycommon.RelayInfo, request dto.GeneralOpenAIRequest) {
  18. }
  19. func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
  20. switch info.RelayMode {
  21. case constant.RelayModeChatCompletions:
  22. return fmt.Sprintf("%s/client/v4/accounts/%s/ai/v1/chat/completions", info.BaseUrl, info.ApiVersion), nil
  23. case constant.RelayModeEmbeddings:
  24. return fmt.Sprintf("%s/client/v4/accounts/%s/ai/v1/embeddings", info.BaseUrl, info.ApiVersion), nil
  25. default:
  26. return fmt.Sprintf("%s/client/v4/accounts/%s/ai/run/%s", info.BaseUrl, info.ApiVersion, info.UpstreamModelName), nil
  27. }
  28. }
  29. func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, info *relaycommon.RelayInfo) error {
  30. channel.SetupApiRequestHeader(info, c, req)
  31. req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", info.ApiKey))
  32. return nil
  33. }
  34. func (a *Adaptor) ConvertRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
  35. if request == nil {
  36. return nil, errors.New("request is nil")
  37. }
  38. switch info.RelayMode {
  39. case constant.RelayModeCompletions:
  40. return convertCf2CompletionsRequest(*request), nil
  41. default:
  42. return request, nil
  43. }
  44. }
  45. func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (*http.Response, error) {
  46. return channel.DoApiRequest(a, c, info, requestBody)
  47. }
  48. func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
  49. return request, nil
  50. }
  51. func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage *dto.Usage, err *dto.OpenAIErrorWithStatusCode) {
  52. if info.IsStream {
  53. err, usage = cfStreamHandler(c, resp, info)
  54. } else {
  55. err, usage = cfHandler(c, resp, info)
  56. }
  57. return
  58. }
  59. func (a *Adaptor) GetModelList() []string {
  60. return ModelList
  61. }
  62. func (a *Adaptor) GetChannelName() string {
  63. return ChannelName
  64. }