relay_info.go 5.5 KB

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