relay_info.go 6.0 KB

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