adaptor.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package perplexity
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "github.com/QuantumNous/new-api/dto"
  8. "github.com/QuantumNous/new-api/relay/channel"
  9. "github.com/QuantumNous/new-api/relay/channel/openai"
  10. relaycommon "github.com/QuantumNous/new-api/relay/common"
  11. relayconstant "github.com/QuantumNous/new-api/relay/constant"
  12. "github.com/QuantumNous/new-api/types"
  13. "github.com/samber/lo"
  14. "github.com/gin-gonic/gin"
  15. )
  16. type Adaptor struct {
  17. }
  18. func (a *Adaptor) ConvertGeminiRequest(*gin.Context, *relaycommon.RelayInfo, *dto.GeminiChatRequest) (any, error) {
  19. //TODO implement me
  20. return nil, errors.New("not implemented")
  21. }
  22. func (a *Adaptor) ConvertClaudeRequest(c *gin.Context, info *relaycommon.RelayInfo, req *dto.ClaudeRequest) (any, error) {
  23. adaptor := openai.Adaptor{}
  24. return adaptor.ConvertClaudeRequest(c, info, req)
  25. }
  26. func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
  27. //TODO implement me
  28. return nil, errors.New("not implemented")
  29. }
  30. func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error) {
  31. //TODO implement me
  32. return nil, errors.New("not implemented")
  33. }
  34. func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
  35. }
  36. func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
  37. if info.RelayMode == relayconstant.RelayModeResponses {
  38. return fmt.Sprintf("%s/v1/responses", info.ChannelBaseUrl), nil
  39. }
  40. return fmt.Sprintf("%s/chat/completions", info.ChannelBaseUrl), nil
  41. }
  42. func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
  43. channel.SetupApiRequestHeader(info, c, req)
  44. req.Set("Authorization", "Bearer "+info.ApiKey)
  45. return nil
  46. }
  47. func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
  48. if request == nil {
  49. return nil, errors.New("request is nil")
  50. }
  51. if lo.FromPtrOr(request.TopP, 0) >= 1 {
  52. request.TopP = lo.ToPtr(0.99)
  53. }
  54. return requestOpenAI2Perplexity(*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. //TODO implement me
  61. return nil, errors.New("not implemented")
  62. }
  63. func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
  64. return request, nil
  65. }
  66. func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
  67. return channel.DoApiRequest(a, c, info, requestBody)
  68. }
  69. func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
  70. adaptor := openai.Adaptor{}
  71. usage, err = adaptor.DoResponse(c, resp, info)
  72. return
  73. }
  74. func (a *Adaptor) GetModelList() []string {
  75. return ModelList
  76. }
  77. func (a *Adaptor) GetChannelName() string {
  78. return ChannelName
  79. }