relay_info.go 5.6 KB

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