adaptor.go 3.2 KB

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