relay_info.go 3.0 KB

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