relay_info.go 5.3 KB

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