relay_info.go 4.7 KB

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