relay_info.go 3.1 KB

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