relay_info.go 5.9 KB

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