relay_info.go 6.0 KB

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