relay_info.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. setFirstResponse bool
  23. ApiType int
  24. IsStream bool
  25. IsPlayground bool
  26. UsePrice bool
  27. RelayMode int
  28. UpstreamModelName string
  29. OriginModelName string
  30. RecodeModelName string
  31. RequestURLPath string
  32. ApiVersion string
  33. PromptTokens int
  34. ApiKey string
  35. Organization string
  36. BaseUrl string
  37. SupportStreamOptions bool
  38. ShouldIncludeUsage bool
  39. ClientWs *websocket.Conn
  40. TargetWs *websocket.Conn
  41. InputAudioFormat string
  42. OutputAudioFormat string
  43. RealtimeTools []dto.RealTimeTool
  44. IsFirstRequest bool
  45. AudioUsage bool
  46. ReasoningEffort string
  47. ChannelSetting map[string]interface{}
  48. }
  49. func GenRelayInfoWs(c *gin.Context, ws *websocket.Conn) *RelayInfo {
  50. info := GenRelayInfo(c)
  51. info.ClientWs = ws
  52. info.InputAudioFormat = "pcm16"
  53. info.OutputAudioFormat = "pcm16"
  54. info.IsFirstRequest = true
  55. return info
  56. }
  57. func GenRelayInfo(c *gin.Context) *RelayInfo {
  58. channelType := c.GetInt("channel_type")
  59. channelId := c.GetInt("channel_id")
  60. channelSetting := c.GetStringMap("channel_setting")
  61. tokenId := c.GetInt("token_id")
  62. tokenKey := c.GetString("token_key")
  63. userId := c.GetInt("id")
  64. group := c.GetString("group")
  65. tokenUnlimited := c.GetBool("token_unlimited_quota")
  66. startTime := c.GetTime(constant.ContextKeyRequestStartTime)
  67. // firstResponseTime = time.Now() - 1 second
  68. apiType, _ := relayconstant.ChannelType2APIType(channelType)
  69. info := &RelayInfo{
  70. RelayMode: relayconstant.Path2RelayMode(c.Request.URL.Path),
  71. BaseUrl: c.GetString("base_url"),
  72. RequestURLPath: c.Request.URL.String(),
  73. ChannelType: channelType,
  74. ChannelId: channelId,
  75. TokenId: tokenId,
  76. TokenKey: tokenKey,
  77. UserId: userId,
  78. Group: group,
  79. TokenUnlimited: tokenUnlimited,
  80. StartTime: startTime,
  81. FirstResponseTime: startTime.Add(-time.Second),
  82. OriginModelName: c.GetString("original_model"),
  83. UpstreamModelName: c.GetString("original_model"),
  84. RecodeModelName: c.GetString("recode_model"),
  85. ApiType: apiType,
  86. ApiVersion: c.GetString("api_version"),
  87. ApiKey: strings.TrimPrefix(c.Request.Header.Get("Authorization"), "Bearer "),
  88. Organization: c.GetString("channel_organization"),
  89. ChannelSetting: channelSetting,
  90. }
  91. if strings.HasPrefix(c.Request.URL.Path, "/pg") {
  92. info.IsPlayground = true
  93. info.RequestURLPath = strings.TrimPrefix(info.RequestURLPath, "/pg")
  94. info.RequestURLPath = "/v1" + info.RequestURLPath
  95. }
  96. if info.BaseUrl == "" {
  97. info.BaseUrl = common.ChannelBaseURLs[channelType]
  98. }
  99. if info.ChannelType == common.ChannelTypeAzure {
  100. info.ApiVersion = GetAPIVersion(c)
  101. }
  102. if info.ChannelType == common.ChannelTypeVertexAi {
  103. info.ApiVersion = c.GetString("region")
  104. }
  105. if info.ChannelType == common.ChannelTypeOpenAI || info.ChannelType == common.ChannelTypeAnthropic ||
  106. info.ChannelType == common.ChannelTypeAws || info.ChannelType == common.ChannelTypeGemini ||
  107. info.ChannelType == common.ChannelCloudflare || info.ChannelType == common.ChannelTypeAzure {
  108. info.SupportStreamOptions = true
  109. }
  110. return info
  111. }
  112. func (info *RelayInfo) SetPromptTokens(promptTokens int) {
  113. info.PromptTokens = promptTokens
  114. }
  115. func (info *RelayInfo) SetIsStream(isStream bool) {
  116. info.IsStream = isStream
  117. }
  118. func (info *RelayInfo) SetFirstResponseTime() {
  119. if !info.setFirstResponse {
  120. info.FirstResponseTime = time.Now()
  121. info.setFirstResponse = true
  122. }
  123. }
  124. type TaskRelayInfo struct {
  125. ChannelType int
  126. ChannelId int
  127. TokenId int
  128. UserId int
  129. Group string
  130. StartTime time.Time
  131. ApiType int
  132. RelayMode int
  133. UpstreamModelName string
  134. RequestURLPath string
  135. ApiKey string
  136. BaseUrl string
  137. Action string
  138. OriginTaskID string
  139. ConsumeQuota bool
  140. }
  141. func GenTaskRelayInfo(c *gin.Context) *TaskRelayInfo {
  142. channelType := c.GetInt("channel_type")
  143. channelId := c.GetInt("channel_id")
  144. tokenId := c.GetInt("token_id")
  145. userId := c.GetInt("id")
  146. group := c.GetString("group")
  147. startTime := time.Now()
  148. apiType, _ := relayconstant.ChannelType2APIType(channelType)
  149. info := &TaskRelayInfo{
  150. RelayMode: relayconstant.Path2RelayMode(c.Request.URL.Path),
  151. BaseUrl: c.GetString("base_url"),
  152. RequestURLPath: c.Request.URL.String(),
  153. ChannelType: channelType,
  154. ChannelId: channelId,
  155. TokenId: tokenId,
  156. UserId: userId,
  157. Group: group,
  158. StartTime: startTime,
  159. ApiType: apiType,
  160. ApiKey: strings.TrimPrefix(c.Request.Header.Get("Authorization"), "Bearer "),
  161. }
  162. if info.BaseUrl == "" {
  163. info.BaseUrl = common.ChannelBaseURLs[channelType]
  164. }
  165. return info
  166. }
  167. func (info *TaskRelayInfo) ToRelayInfo() *RelayInfo {
  168. return &RelayInfo{
  169. ChannelType: info.ChannelType,
  170. ChannelId: info.ChannelId,
  171. TokenId: info.TokenId,
  172. UserId: info.UserId,
  173. Group: info.Group,
  174. StartTime: info.StartTime,
  175. ApiType: info.ApiType,
  176. RelayMode: info.RelayMode,
  177. UpstreamModelName: info.UpstreamModelName,
  178. RequestURLPath: info.RequestURLPath,
  179. ApiKey: info.ApiKey,
  180. BaseUrl: info.BaseUrl,
  181. }
  182. }