relay_info.go 5.2 KB

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