relay_info.go 4.9 KB

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