relay_info.go 5.3 KB

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