relay_info.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. }