relay_info.go 5.5 KB

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