relay_info.go 5.2 KB

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