relay_info.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 || info.ChannelType == common.ChannelTypeAws {
  65. info.SupportStreamOptions = true
  66. }
  67. return info
  68. }
  69. func (info *RelayInfo) SetPromptTokens(promptTokens int) {
  70. info.PromptTokens = promptTokens
  71. }
  72. func (info *RelayInfo) SetIsStream(isStream bool) {
  73. info.IsStream = isStream
  74. }
  75. type TaskRelayInfo struct {
  76. ChannelType int
  77. ChannelId int
  78. TokenId int
  79. UserId int
  80. Group string
  81. StartTime time.Time
  82. ApiType int
  83. RelayMode int
  84. UpstreamModelName string
  85. RequestURLPath string
  86. ApiKey string
  87. BaseUrl string
  88. Action string
  89. OriginTaskID string
  90. ConsumeQuota bool
  91. }
  92. func GenTaskRelayInfo(c *gin.Context) *TaskRelayInfo {
  93. channelType := c.GetInt("channel")
  94. channelId := c.GetInt("channel_id")
  95. tokenId := c.GetInt("token_id")
  96. userId := c.GetInt("id")
  97. group := c.GetString("group")
  98. startTime := time.Now()
  99. apiType, _ := constant.ChannelType2APIType(channelType)
  100. info := &TaskRelayInfo{
  101. RelayMode: constant.Path2RelayMode(c.Request.URL.Path),
  102. BaseUrl: c.GetString("base_url"),
  103. RequestURLPath: c.Request.URL.String(),
  104. ChannelType: channelType,
  105. ChannelId: channelId,
  106. TokenId: tokenId,
  107. UserId: userId,
  108. Group: group,
  109. StartTime: startTime,
  110. ApiType: apiType,
  111. ApiKey: strings.TrimPrefix(c.Request.Header.Get("Authorization"), "Bearer "),
  112. }
  113. if info.BaseUrl == "" {
  114. info.BaseUrl = common.ChannelBaseURLs[channelType]
  115. }
  116. return info
  117. }