relay_info.go 6.2 KB

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