adaptor.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package tencent
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "strconv"
  8. "strings"
  9. "github.com/QuantumNous/new-api/common"
  10. "github.com/QuantumNous/new-api/constant"
  11. "github.com/QuantumNous/new-api/dto"
  12. "github.com/QuantumNous/new-api/relay/channel"
  13. relaycommon "github.com/QuantumNous/new-api/relay/common"
  14. "github.com/QuantumNous/new-api/types"
  15. "github.com/gin-gonic/gin"
  16. )
  17. type Adaptor struct {
  18. Sign string
  19. AppID int64
  20. Action string
  21. Version string
  22. Timestamp int64
  23. }
  24. func (a *Adaptor) ConvertGeminiRequest(*gin.Context, *relaycommon.RelayInfo, *dto.GeminiChatRequest) (any, error) {
  25. //TODO implement me
  26. return nil, errors.New("not implemented")
  27. }
  28. func (a *Adaptor) ConvertClaudeRequest(*gin.Context, *relaycommon.RelayInfo, *dto.ClaudeRequest) (any, error) {
  29. //TODO implement me
  30. panic("implement me")
  31. return nil, nil
  32. }
  33. func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
  34. //TODO implement me
  35. return nil, errors.New("not implemented")
  36. }
  37. func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error) {
  38. //TODO implement me
  39. return nil, errors.New("not implemented")
  40. }
  41. func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
  42. a.Action = "ChatCompletions"
  43. a.Version = "2023-09-01"
  44. a.Timestamp = common.GetTimestamp()
  45. }
  46. func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
  47. return fmt.Sprintf("%s/", info.ChannelBaseUrl), nil
  48. }
  49. func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
  50. channel.SetupApiRequestHeader(info, c, req)
  51. req.Set("Authorization", a.Sign)
  52. req.Set("X-TC-Action", a.Action)
  53. req.Set("X-TC-Version", a.Version)
  54. req.Set("X-TC-Timestamp", strconv.FormatInt(a.Timestamp, 10))
  55. return nil
  56. }
  57. func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
  58. if request == nil {
  59. return nil, errors.New("request is nil")
  60. }
  61. apiKey := common.GetContextKeyString(c, constant.ContextKeyChannelKey)
  62. apiKey = strings.TrimPrefix(apiKey, "Bearer ")
  63. appId, secretId, secretKey, err := parseTencentConfig(apiKey)
  64. a.AppID = appId
  65. if err != nil {
  66. return nil, err
  67. }
  68. tencentRequest := requestOpenAI2Tencent(a, *request)
  69. // we have to calculate the sign here
  70. a.Sign = getTencentSign(*tencentRequest, a, secretId, secretKey)
  71. return tencentRequest, nil
  72. }
  73. func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
  74. return nil, nil
  75. }
  76. func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) {
  77. //TODO implement me
  78. return nil, errors.New("not implemented")
  79. }
  80. func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
  81. // TODO implement me
  82. return nil, errors.New("not implemented")
  83. }
  84. func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
  85. return channel.DoApiRequest(a, c, info, requestBody)
  86. }
  87. func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
  88. if info.IsStream {
  89. usage, err = tencentStreamHandler(c, info, resp)
  90. } else {
  91. usage, err = tencentHandler(c, info, resp)
  92. }
  93. return
  94. }
  95. func (a *Adaptor) GetModelList() []string {
  96. return ModelList
  97. }
  98. func (a *Adaptor) GetChannelName() string {
  99. return ChannelName
  100. }