adaptor.go 3.2 KB

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