adaptor.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package baidu_v2
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "strings"
  8. "github.com/QuantumNous/new-api/dto"
  9. "github.com/QuantumNous/new-api/relay/channel"
  10. "github.com/QuantumNous/new-api/relay/channel/openai"
  11. relaycommon "github.com/QuantumNous/new-api/relay/common"
  12. "github.com/QuantumNous/new-api/relay/constant"
  13. "github.com/QuantumNous/new-api/types"
  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. switch info.RelayMode {
  38. case constant.RelayModeChatCompletions:
  39. return fmt.Sprintf("%s/v2/chat/completions", info.ChannelBaseUrl), nil
  40. case constant.RelayModeEmbeddings:
  41. return fmt.Sprintf("%s/v2/embeddings", info.ChannelBaseUrl), nil
  42. case constant.RelayModeImagesGenerations:
  43. return fmt.Sprintf("%s/v2/images/generations", info.ChannelBaseUrl), nil
  44. case constant.RelayModeImagesEdits:
  45. return fmt.Sprintf("%s/v2/images/edits", info.ChannelBaseUrl), nil
  46. case constant.RelayModeRerank:
  47. return fmt.Sprintf("%s/v2/rerank", info.ChannelBaseUrl), nil
  48. default:
  49. }
  50. return "", fmt.Errorf("unsupported relay mode: %d", info.RelayMode)
  51. }
  52. func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
  53. channel.SetupApiRequestHeader(info, c, req)
  54. keyParts := strings.Split(info.ApiKey, "|")
  55. if len(keyParts) == 0 || keyParts[0] == "" {
  56. return errors.New("invalid API key: authorization token is required")
  57. }
  58. if len(keyParts) > 1 {
  59. if keyParts[1] != "" {
  60. req.Set("appid", keyParts[1])
  61. }
  62. }
  63. req.Set("Authorization", "Bearer "+keyParts[0])
  64. return nil
  65. }
  66. func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
  67. if request == nil {
  68. return nil, errors.New("request is nil")
  69. }
  70. if strings.HasSuffix(info.UpstreamModelName, "-search") {
  71. info.UpstreamModelName = strings.TrimSuffix(info.UpstreamModelName, "-search")
  72. request.Model = info.UpstreamModelName
  73. if len(request.WebSearch) == 0 {
  74. toMap := request.ToMap()
  75. toMap["web_search"] = map[string]any{
  76. "enable": true,
  77. "enable_citation": true,
  78. "enable_trace": true,
  79. "enable_status": false,
  80. }
  81. return toMap, nil
  82. }
  83. return request, nil
  84. }
  85. return request, nil
  86. }
  87. func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
  88. return nil, errors.New("not implemented")
  89. }
  90. func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) {
  91. //TODO implement me
  92. return nil, errors.New("not implemented")
  93. }
  94. func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
  95. // TODO implement me
  96. return nil, errors.New("not implemented")
  97. }
  98. func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
  99. return channel.DoApiRequest(a, c, info, requestBody)
  100. }
  101. func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
  102. adaptor := openai.Adaptor{}
  103. usage, err = adaptor.DoResponse(c, resp, info)
  104. return
  105. }
  106. func (a *Adaptor) GetModelList() []string {
  107. return ModelList
  108. }
  109. func (a *Adaptor) GetChannelName() string {
  110. return ChannelName
  111. }