relay_info.go 4.0 KB

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