relay_info.go 5.5 KB

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