adaptor.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package tencent
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "io"
  7. "net/http"
  8. "one-api/common"
  9. "one-api/dto"
  10. "one-api/relay/channel"
  11. relaycommon "one-api/relay/common"
  12. "one-api/service"
  13. "strconv"
  14. "strings"
  15. )
  16. type Adaptor struct {
  17. Sign string
  18. Action string
  19. Version string
  20. Timestamp int64
  21. }
  22. func (a *Adaptor) InitRerank(info *relaycommon.RelayInfo, request dto.RerankRequest) {
  23. //TODO implement me
  24. }
  25. func (a *Adaptor) Init(info *relaycommon.RelayInfo, request dto.GeneralOpenAIRequest) {
  26. a.Action = "ChatCompletions"
  27. a.Version = "2023-09-01"
  28. a.Timestamp = common.GetTimestamp()
  29. }
  30. func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
  31. return fmt.Sprintf("%s/hyllm/v1/chat/completions", info.BaseUrl), nil
  32. }
  33. func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, info *relaycommon.RelayInfo) error {
  34. channel.SetupApiRequestHeader(info, c, req)
  35. req.Header.Set("Authorization", a.Sign)
  36. req.Header.Set("X-TC-Action", a.Action)
  37. req.Header.Set("X-TC-Version", a.Version)
  38. req.Header.Set("X-TC-Timestamp", strconv.FormatInt(a.Timestamp, 10))
  39. return nil
  40. }
  41. func (a *Adaptor) ConvertRequest(c *gin.Context, relayMode int, request *dto.GeneralOpenAIRequest) (any, error) {
  42. if request == nil {
  43. return nil, errors.New("request is nil")
  44. }
  45. apiKey := c.Request.Header.Get("Authorization")
  46. apiKey = strings.TrimPrefix(apiKey, "Bearer ")
  47. _, secretId, secretKey, err := parseTencentConfig(apiKey)
  48. if err != nil {
  49. return nil, err
  50. }
  51. tencentRequest := requestOpenAI2Tencent(*request)
  52. // we have to calculate the sign here
  53. a.Sign = getTencentSign(*tencentRequest, a, secretId, secretKey)
  54. return tencentRequest, nil
  55. }
  56. func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
  57. return nil, nil
  58. }
  59. func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (*http.Response, error) {
  60. return channel.DoApiRequest(a, c, info, requestBody)
  61. }
  62. func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage *dto.Usage, err *dto.OpenAIErrorWithStatusCode) {
  63. if info.IsStream {
  64. var responseText string
  65. err, responseText = tencentStreamHandler(c, resp)
  66. usage, _ = service.ResponseText2Usage(responseText, info.UpstreamModelName, info.PromptTokens)
  67. } else {
  68. err, usage = tencentHandler(c, resp)
  69. }
  70. return
  71. }
  72. func (a *Adaptor) GetModelList() []string {
  73. return ModelList
  74. }
  75. func (a *Adaptor) GetChannelName() string {
  76. return ChannelName
  77. }