adaptor.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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/gin-gonic/gin"
  14. )
  15. type Adaptor struct {
  16. }
  17. func (a *Adaptor) ConvertGeminiRequest(*gin.Context, *relaycommon.RelayInfo, *dto.GeminiChatRequest) (any, error) {
  18. //TODO implement me
  19. return nil, errors.New("not implemented")
  20. }
  21. func (a *Adaptor) ConvertClaudeRequest(c *gin.Context, info *relaycommon.RelayInfo, req *dto.ClaudeRequest) (any, error) {
  22. adaptor := openai.Adaptor{}
  23. return adaptor.ConvertClaudeRequest(c, info, req)
  24. }
  25. func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
  26. //TODO implement me
  27. return nil, errors.New("not implemented")
  28. }
  29. func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error) {
  30. //TODO implement me
  31. return nil, errors.New("not implemented")
  32. }
  33. func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
  34. }
  35. func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
  36. if info.RelayMode == relayconstant.RelayModeResponses {
  37. return fmt.Sprintf("%s/v1/responses", info.ChannelBaseUrl), nil
  38. }
  39. return fmt.Sprintf("%s/chat/completions", info.ChannelBaseUrl), nil
  40. }
  41. func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
  42. channel.SetupApiRequestHeader(info, c, req)
  43. req.Set("Authorization", "Bearer "+info.ApiKey)
  44. return nil
  45. }
  46. func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
  47. if request == nil {
  48. return nil, errors.New("request is nil")
  49. }
  50. if request.TopP >= 1 {
  51. request.TopP = 0.99
  52. }
  53. return requestOpenAI2Perplexity(*request), nil
  54. }
  55. func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
  56. return nil, nil
  57. }
  58. func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) {
  59. //TODO implement me
  60. return nil, errors.New("not implemented")
  61. }
  62. func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
  63. return request, nil
  64. }
  65. func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
  66. return channel.DoApiRequest(a, c, info, requestBody)
  67. }
  68. func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
  69. adaptor := openai.Adaptor{}
  70. usage, err = adaptor.DoResponse(c, resp, info)
  71. return
  72. }
  73. func (a *Adaptor) GetModelList() []string {
  74. return ModelList
  75. }
  76. func (a *Adaptor) GetChannelName() string {
  77. return ChannelName
  78. }