relay_info.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package common
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "one-api/common"
  5. "one-api/relay/constant"
  6. "strings"
  7. "time"
  8. )
  9. type RelayInfo struct {
  10. ChannelType int
  11. ChannelId int
  12. TokenId int
  13. UserId int
  14. Group string
  15. TokenUnlimited bool
  16. StartTime time.Time
  17. FirstResponseTime time.Time
  18. ApiType int
  19. IsStream bool
  20. RelayMode int
  21. UpstreamModelName string
  22. RequestURLPath string
  23. ApiVersion string
  24. PromptTokens int
  25. ApiKey string
  26. Organization string
  27. BaseUrl string
  28. }
  29. func GenRelayInfo(c *gin.Context) *RelayInfo {
  30. channelType := c.GetInt("channel")
  31. channelId := c.GetInt("channel_id")
  32. tokenId := c.GetInt("token_id")
  33. userId := c.GetInt("id")
  34. group := c.GetString("group")
  35. tokenUnlimited := c.GetBool("token_unlimited_quota")
  36. startTime := time.Now()
  37. // firstResponseTime = time.Now() - 1 second
  38. apiType, _ := constant.ChannelType2APIType(channelType)
  39. info := &RelayInfo{
  40. RelayMode: constant.Path2RelayMode(c.Request.URL.Path),
  41. BaseUrl: c.GetString("base_url"),
  42. RequestURLPath: c.Request.URL.String(),
  43. ChannelType: channelType,
  44. ChannelId: channelId,
  45. TokenId: tokenId,
  46. UserId: userId,
  47. Group: group,
  48. TokenUnlimited: tokenUnlimited,
  49. StartTime: startTime,
  50. FirstResponseTime: startTime.Add(-time.Second),
  51. ApiType: apiType,
  52. ApiVersion: c.GetString("api_version"),
  53. ApiKey: strings.TrimPrefix(c.Request.Header.Get("Authorization"), "Bearer "),
  54. Organization: c.GetString("channel_organization"),
  55. }
  56. if info.BaseUrl == "" {
  57. info.BaseUrl = common.ChannelBaseURLs[channelType]
  58. }
  59. if info.ChannelType == common.ChannelTypeAzure {
  60. info.ApiVersion = GetAPIVersion(c)
  61. }
  62. return info
  63. }
  64. func (info *RelayInfo) SetPromptTokens(promptTokens int) {
  65. info.PromptTokens = promptTokens
  66. }
  67. func (info *RelayInfo) SetIsStream(isStream bool) {
  68. info.IsStream = isStream
  69. }
  70. type TaskRelayInfo struct {
  71. ChannelType int
  72. ChannelId int
  73. TokenId int
  74. UserId int
  75. Group string
  76. StartTime time.Time
  77. ApiType int
  78. RelayMode int
  79. UpstreamModelName string
  80. RequestURLPath string
  81. ApiKey string
  82. BaseUrl string
  83. Action string
  84. OriginTaskID string
  85. ConsumeQuota bool
  86. }
  87. func GenTaskRelayInfo(c *gin.Context) *TaskRelayInfo {
  88. channelType := c.GetInt("channel")
  89. channelId := c.GetInt("channel_id")
  90. tokenId := c.GetInt("token_id")
  91. userId := c.GetInt("id")
  92. group := c.GetString("group")
  93. startTime := time.Now()
  94. apiType, _ := constant.ChannelType2APIType(channelType)
  95. info := &TaskRelayInfo{
  96. RelayMode: constant.Path2RelayMode(c.Request.URL.Path),
  97. BaseUrl: c.GetString("base_url"),
  98. RequestURLPath: c.Request.URL.String(),
  99. ChannelType: channelType,
  100. ChannelId: channelId,
  101. TokenId: tokenId,
  102. UserId: userId,
  103. Group: group,
  104. StartTime: startTime,
  105. ApiType: apiType,
  106. ApiKey: strings.TrimPrefix(c.Request.Header.Get("Authorization"), "Bearer "),
  107. }
  108. if info.BaseUrl == "" {
  109. info.BaseUrl = common.ChannelBaseURLs[channelType]
  110. }
  111. return info
  112. }