relay_info.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package common
  2. import (
  3. "one-api/common"
  4. "one-api/constant"
  5. "one-api/dto"
  6. relayconstant "one-api/relay/constant"
  7. "strings"
  8. "time"
  9. "github.com/gin-gonic/gin"
  10. "github.com/gorilla/websocket"
  11. )
  12. type RelayInfo struct {
  13. ChannelType int
  14. ChannelId int
  15. TokenId int
  16. TokenKey string
  17. UserId int
  18. Group string
  19. TokenUnlimited bool
  20. StartTime time.Time
  21. FirstResponseTime time.Time
  22. IsFirstResponse bool
  23. SendLastReasoningResponse bool
  24. ApiType int
  25. IsStream bool
  26. IsPlayground bool
  27. UsePrice bool
  28. RelayMode int
  29. UpstreamModelName string
  30. OriginModelName string
  31. //RecodeModelName string
  32. RequestURLPath string
  33. ApiVersion string
  34. PromptTokens int
  35. ApiKey string
  36. Organization string
  37. BaseUrl string
  38. SupportStreamOptions bool
  39. ShouldIncludeUsage bool
  40. IsModelMapped bool
  41. ClientWs *websocket.Conn
  42. TargetWs *websocket.Conn
  43. InputAudioFormat string
  44. OutputAudioFormat string
  45. RealtimeTools []dto.RealTimeTool
  46. IsFirstRequest bool
  47. AudioUsage bool
  48. ReasoningEffort string
  49. ChannelSetting map[string]interface{}
  50. UserSetting map[string]interface{}
  51. UserEmail string
  52. UserQuota int
  53. }
  54. // 定义支持流式选项的通道类型
  55. var streamSupportedChannels = map[int]bool{
  56. common.ChannelTypeOpenAI: true,
  57. common.ChannelTypeAnthropic: true,
  58. common.ChannelTypeAws: true,
  59. common.ChannelTypeGemini: true,
  60. common.ChannelCloudflare: true,
  61. common.ChannelTypeAzure: true,
  62. common.ChannelTypeVolcEngine: true,
  63. common.ChannelTypeOllama: true,
  64. }
  65. func GenRelayInfoWs(c *gin.Context, ws *websocket.Conn) *RelayInfo {
  66. info := GenRelayInfo(c)
  67. info.ClientWs = ws
  68. info.InputAudioFormat = "pcm16"
  69. info.OutputAudioFormat = "pcm16"
  70. info.IsFirstRequest = true
  71. return info
  72. }
  73. func GenRelayInfo(c *gin.Context) *RelayInfo {
  74. channelType := c.GetInt("channel_type")
  75. channelId := c.GetInt("channel_id")
  76. channelSetting := c.GetStringMap("channel_setting")
  77. tokenId := c.GetInt("token_id")
  78. tokenKey := c.GetString("token_key")
  79. userId := c.GetInt("id")
  80. group := c.GetString("group")
  81. tokenUnlimited := c.GetBool("token_unlimited_quota")
  82. startTime := c.GetTime(constant.ContextKeyRequestStartTime)
  83. // firstResponseTime = time.Now() - 1 second
  84. apiType, _ := relayconstant.ChannelType2APIType(channelType)
  85. info := &RelayInfo{
  86. UserQuota: c.GetInt(constant.ContextKeyUserQuota),
  87. UserSetting: c.GetStringMap(constant.ContextKeyUserSetting),
  88. UserEmail: c.GetString(constant.ContextKeyUserEmail),
  89. IsFirstResponse: true,
  90. RelayMode: relayconstant.Path2RelayMode(c.Request.URL.Path),
  91. BaseUrl: c.GetString("base_url"),
  92. RequestURLPath: c.Request.URL.String(),
  93. ChannelType: channelType,
  94. ChannelId: channelId,
  95. TokenId: tokenId,
  96. TokenKey: tokenKey,
  97. UserId: userId,
  98. Group: group,
  99. TokenUnlimited: tokenUnlimited,
  100. StartTime: startTime,
  101. FirstResponseTime: startTime.Add(-time.Second),
  102. OriginModelName: c.GetString("original_model"),
  103. UpstreamModelName: c.GetString("original_model"),
  104. //RecodeModelName: c.GetString("original_model"),
  105. IsModelMapped: false,
  106. ApiType: apiType,
  107. ApiVersion: c.GetString("api_version"),
  108. ApiKey: strings.TrimPrefix(c.Request.Header.Get("Authorization"), "Bearer "),
  109. Organization: c.GetString("channel_organization"),
  110. ChannelSetting: channelSetting,
  111. }
  112. if strings.HasPrefix(c.Request.URL.Path, "/pg") {
  113. info.IsPlayground = true
  114. info.RequestURLPath = strings.TrimPrefix(info.RequestURLPath, "/pg")
  115. info.RequestURLPath = "/v1" + info.RequestURLPath
  116. }
  117. if info.BaseUrl == "" {
  118. info.BaseUrl = common.ChannelBaseURLs[channelType]
  119. }
  120. if info.ChannelType == common.ChannelTypeAzure {
  121. info.ApiVersion = GetAPIVersion(c)
  122. }
  123. if info.ChannelType == common.ChannelTypeVertexAi {
  124. info.ApiVersion = c.GetString("region")
  125. }
  126. if streamSupportedChannels[info.ChannelType] {
  127. info.SupportStreamOptions = true
  128. }
  129. return info
  130. }
  131. func (info *RelayInfo) SetPromptTokens(promptTokens int) {
  132. info.PromptTokens = promptTokens
  133. }
  134. func (info *RelayInfo) SetIsStream(isStream bool) {
  135. info.IsStream = isStream
  136. }
  137. func (info *RelayInfo) SetFirstResponseTime() {
  138. if info.IsFirstResponse {
  139. info.FirstResponseTime = time.Now()
  140. info.IsFirstResponse = false
  141. }
  142. }
  143. type TaskRelayInfo struct {
  144. *RelayInfo
  145. Action string
  146. OriginTaskID string
  147. ConsumeQuota bool
  148. }
  149. func GenTaskRelayInfo(c *gin.Context) *TaskRelayInfo {
  150. info := &TaskRelayInfo{
  151. RelayInfo: GenRelayInfo(c),
  152. }
  153. return info
  154. }