relay_info.go 5.3 KB

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