relay_info.go 3.6 KB

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