relay_info.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. BaseUrl string
  26. }
  27. func GenRelayInfo(c *gin.Context) *RelayInfo {
  28. channelType := c.GetInt("channel")
  29. channelId := c.GetInt("channel_id")
  30. tokenId := c.GetInt("token_id")
  31. userId := c.GetInt("id")
  32. group := c.GetString("group")
  33. tokenUnlimited := c.GetBool("token_unlimited_quota")
  34. startTime := time.Now()
  35. apiType := constant.ChannelType2APIType(channelType)
  36. info := &RelayInfo{
  37. RelayMode: constant.Path2RelayMode(c.Request.URL.Path),
  38. BaseUrl: c.GetString("base_url"),
  39. RequestURLPath: c.Request.URL.String(),
  40. ChannelType: channelType,
  41. ChannelId: channelId,
  42. TokenId: tokenId,
  43. UserId: userId,
  44. Group: group,
  45. TokenUnlimited: tokenUnlimited,
  46. StartTime: startTime,
  47. ApiType: apiType,
  48. ApiVersion: c.GetString("api_version"),
  49. ApiKey: strings.TrimPrefix(c.Request.Header.Get("Authorization"), "Bearer "),
  50. }
  51. if info.BaseUrl == "" {
  52. info.BaseUrl = common.ChannelBaseURLs[channelType]
  53. }
  54. if info.ChannelType == common.ChannelTypeAzure {
  55. info.ApiVersion = GetAPIVersion(c)
  56. }
  57. return info
  58. }
  59. func (info *RelayInfo) SetPromptTokens(promptTokens int) {
  60. info.PromptTokens = promptTokens
  61. }
  62. func (info *RelayInfo) SetIsStream(isStream bool) {
  63. info.IsStream = isStream
  64. }