relay_info.go 3.4 KB

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