relay_info.go 3.5 KB

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