relay_info.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. setFirstResponse bool
  19. ApiType int
  20. IsStream bool
  21. RelayMode int
  22. UpstreamModelName string
  23. RequestURLPath string
  24. ApiVersion string
  25. PromptTokens int
  26. ApiKey string
  27. Organization string
  28. BaseUrl string
  29. SupportStreamOptions bool
  30. ShouldIncludeUsage bool
  31. }
  32. func GenRelayInfo(c *gin.Context) *RelayInfo {
  33. channelType := c.GetInt("channel")
  34. channelId := c.GetInt("channel_id")
  35. tokenId := c.GetInt("token_id")
  36. userId := c.GetInt("id")
  37. group := c.GetString("group")
  38. tokenUnlimited := c.GetBool("token_unlimited_quota")
  39. startTime := time.Now()
  40. // firstResponseTime = time.Now() - 1 second
  41. apiType, _ := constant.ChannelType2APIType(channelType)
  42. info := &RelayInfo{
  43. RelayMode: constant.Path2RelayMode(c.Request.URL.Path),
  44. BaseUrl: c.GetString("base_url"),
  45. RequestURLPath: c.Request.URL.String(),
  46. ChannelType: channelType,
  47. ChannelId: channelId,
  48. TokenId: tokenId,
  49. UserId: userId,
  50. Group: group,
  51. TokenUnlimited: tokenUnlimited,
  52. StartTime: startTime,
  53. FirstResponseTime: startTime.Add(-time.Second),
  54. ApiType: apiType,
  55. ApiVersion: c.GetString("api_version"),
  56. ApiKey: strings.TrimPrefix(c.Request.Header.Get("Authorization"), "Bearer "),
  57. Organization: c.GetString("channel_organization"),
  58. }
  59. if info.BaseUrl == "" {
  60. info.BaseUrl = common.ChannelBaseURLs[channelType]
  61. }
  62. if info.ChannelType == common.ChannelTypeAzure {
  63. info.ApiVersion = GetAPIVersion(c)
  64. }
  65. if info.ChannelType == common.ChannelTypeOpenAI || info.ChannelType == common.ChannelTypeAnthropic ||
  66. info.ChannelType == common.ChannelTypeAws || info.ChannelType == common.ChannelTypeGemini ||
  67. info.ChannelType == common.ChannelCloudflare {
  68. info.SupportStreamOptions = true
  69. }
  70. return info
  71. }
  72. func (info *RelayInfo) SetPromptTokens(promptTokens int) {
  73. info.PromptTokens = promptTokens
  74. }
  75. func (info *RelayInfo) SetIsStream(isStream bool) {
  76. info.IsStream = isStream
  77. }
  78. func (info *RelayInfo) SetFirstResponseTime() {
  79. if !info.setFirstResponse {
  80. info.FirstResponseTime = time.Now()
  81. info.setFirstResponse = true
  82. }
  83. }
  84. type TaskRelayInfo struct {
  85. ChannelType int
  86. ChannelId int
  87. TokenId int
  88. UserId int
  89. Group string
  90. StartTime time.Time
  91. ApiType int
  92. RelayMode int
  93. UpstreamModelName string
  94. RequestURLPath string
  95. ApiKey string
  96. BaseUrl string
  97. Action string
  98. OriginTaskID string
  99. ConsumeQuota bool
  100. }
  101. func GenTaskRelayInfo(c *gin.Context) *TaskRelayInfo {
  102. channelType := c.GetInt("channel")
  103. channelId := c.GetInt("channel_id")
  104. tokenId := c.GetInt("token_id")
  105. userId := c.GetInt("id")
  106. group := c.GetString("group")
  107. startTime := time.Now()
  108. apiType, _ := constant.ChannelType2APIType(channelType)
  109. info := &TaskRelayInfo{
  110. RelayMode: constant.Path2RelayMode(c.Request.URL.Path),
  111. BaseUrl: c.GetString("base_url"),
  112. RequestURLPath: c.Request.URL.String(),
  113. ChannelType: channelType,
  114. ChannelId: channelId,
  115. TokenId: tokenId,
  116. UserId: userId,
  117. Group: group,
  118. StartTime: startTime,
  119. ApiType: apiType,
  120. ApiKey: strings.TrimPrefix(c.Request.Header.Get("Authorization"), "Bearer "),
  121. }
  122. if info.BaseUrl == "" {
  123. info.BaseUrl = common.ChannelBaseURLs[channelType]
  124. }
  125. return info
  126. }