relay_info.go 5.8 KB

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