relay_info.go 5.8 KB

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