distributor.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. package middleware
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. "slices"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/QuantumNous/new-api/common"
  11. "github.com/QuantumNous/new-api/constant"
  12. "github.com/QuantumNous/new-api/dto"
  13. "github.com/QuantumNous/new-api/i18n"
  14. "github.com/QuantumNous/new-api/model"
  15. relayconstant "github.com/QuantumNous/new-api/relay/constant"
  16. "github.com/QuantumNous/new-api/service"
  17. "github.com/QuantumNous/new-api/setting/ratio_setting"
  18. "github.com/QuantumNous/new-api/types"
  19. "github.com/gin-gonic/gin"
  20. )
  21. type ModelRequest struct {
  22. Model string `json:"model"`
  23. Group string `json:"group,omitempty"`
  24. }
  25. func Distribute() func(c *gin.Context) {
  26. return func(c *gin.Context) {
  27. var channel *model.Channel
  28. channelId, ok := common.GetContextKey(c, constant.ContextKeyTokenSpecificChannelId)
  29. modelRequest, shouldSelectChannel, err := getModelRequest(c)
  30. if err != nil {
  31. abortWithOpenAiMessage(c, http.StatusBadRequest, i18n.T(c, i18n.MsgDistributorInvalidRequest, map[string]any{"Error": err.Error()}))
  32. return
  33. }
  34. if ok {
  35. id, err := strconv.Atoi(channelId.(string))
  36. if err != nil {
  37. abortWithOpenAiMessage(c, http.StatusBadRequest, i18n.T(c, i18n.MsgDistributorInvalidChannelId))
  38. return
  39. }
  40. channel, err = model.GetChannelById(id, true)
  41. if err != nil {
  42. abortWithOpenAiMessage(c, http.StatusBadRequest, i18n.T(c, i18n.MsgDistributorInvalidChannelId))
  43. return
  44. }
  45. if channel.Status != common.ChannelStatusEnabled {
  46. abortWithOpenAiMessage(c, http.StatusForbidden, i18n.T(c, i18n.MsgDistributorChannelDisabled))
  47. return
  48. }
  49. } else {
  50. // Select a channel for the user
  51. // check token model mapping
  52. modelLimitEnable := common.GetContextKeyBool(c, constant.ContextKeyTokenModelLimitEnabled)
  53. if modelLimitEnable {
  54. s, ok := common.GetContextKey(c, constant.ContextKeyTokenModelLimit)
  55. if !ok {
  56. // token model limit is empty, all models are not allowed
  57. abortWithOpenAiMessage(c, http.StatusForbidden, i18n.T(c, i18n.MsgDistributorTokenNoModelAccess))
  58. return
  59. }
  60. var tokenModelLimit map[string]bool
  61. tokenModelLimit, ok = s.(map[string]bool)
  62. if !ok {
  63. tokenModelLimit = map[string]bool{}
  64. }
  65. matchName := ratio_setting.FormatMatchingModelName(modelRequest.Model) // match gpts & thinking-*
  66. if _, ok := tokenModelLimit[matchName]; !ok {
  67. abortWithOpenAiMessage(c, http.StatusForbidden, i18n.T(c, i18n.MsgDistributorTokenModelForbidden, map[string]any{"Model": modelRequest.Model}))
  68. return
  69. }
  70. }
  71. if shouldSelectChannel {
  72. if modelRequest.Model == "" {
  73. abortWithOpenAiMessage(c, http.StatusBadRequest, i18n.T(c, i18n.MsgDistributorModelNameRequired))
  74. return
  75. }
  76. var selectGroup string
  77. usingGroup := common.GetContextKeyString(c, constant.ContextKeyUsingGroup)
  78. // check path is /pg/chat/completions
  79. if strings.HasPrefix(c.Request.URL.Path, "/pg/chat/completions") {
  80. playgroundRequest := &dto.PlayGroundRequest{}
  81. err = common.UnmarshalBodyReusable(c, playgroundRequest)
  82. if err != nil {
  83. abortWithOpenAiMessage(c, http.StatusBadRequest, i18n.T(c, i18n.MsgDistributorInvalidPlayground, map[string]any{"Error": err.Error()}))
  84. return
  85. }
  86. if playgroundRequest.Group != "" {
  87. if !service.GroupInUserUsableGroups(usingGroup, playgroundRequest.Group) && playgroundRequest.Group != usingGroup {
  88. abortWithOpenAiMessage(c, http.StatusForbidden, i18n.T(c, i18n.MsgDistributorGroupAccessDenied))
  89. return
  90. }
  91. usingGroup = playgroundRequest.Group
  92. common.SetContextKey(c, constant.ContextKeyUsingGroup, usingGroup)
  93. }
  94. }
  95. if preferredChannelID, found := service.GetPreferredChannelByAffinity(c, modelRequest.Model, usingGroup); found {
  96. preferred, err := model.CacheGetChannel(preferredChannelID)
  97. if err == nil && preferred != nil {
  98. if preferred.Status != common.ChannelStatusEnabled {
  99. if service.ShouldSkipRetryAfterChannelAffinityFailure(c) {
  100. abortWithOpenAiMessage(c, http.StatusForbidden, i18n.T(c, i18n.MsgDistributorChannelDisabled))
  101. return
  102. }
  103. } else if usingGroup == "auto" {
  104. userGroup := common.GetContextKeyString(c, constant.ContextKeyUserGroup)
  105. autoGroups := service.GetUserAutoGroup(userGroup)
  106. for _, g := range autoGroups {
  107. if model.IsChannelEnabledForGroupModel(g, modelRequest.Model, preferred.Id) {
  108. selectGroup = g
  109. common.SetContextKey(c, constant.ContextKeyAutoGroup, g)
  110. channel = preferred
  111. service.MarkChannelAffinityUsed(c, g, preferred.Id)
  112. break
  113. }
  114. }
  115. } else if model.IsChannelEnabledForGroupModel(usingGroup, modelRequest.Model, preferred.Id) {
  116. channel = preferred
  117. selectGroup = usingGroup
  118. service.MarkChannelAffinityUsed(c, usingGroup, preferred.Id)
  119. }
  120. }
  121. }
  122. if channel == nil {
  123. channel, selectGroup, err = service.CacheGetRandomSatisfiedChannel(&service.RetryParam{
  124. Ctx: c,
  125. ModelName: modelRequest.Model,
  126. TokenGroup: usingGroup,
  127. Retry: common.GetPointer(0),
  128. })
  129. if err != nil {
  130. showGroup := usingGroup
  131. if usingGroup == "auto" {
  132. showGroup = fmt.Sprintf("auto(%s)", selectGroup)
  133. }
  134. message := i18n.T(c, i18n.MsgDistributorGetChannelFailed, map[string]any{"Group": showGroup, "Model": modelRequest.Model, "Error": err.Error()})
  135. // 如果错误,但是渠道不为空,说明是数据库一致性问题
  136. //if channel != nil {
  137. // common.SysError(fmt.Sprintf("渠道不存在:%d", channel.Id))
  138. // message = "数据库一致性已被破坏,请联系管理员"
  139. //}
  140. abortWithOpenAiMessage(c, http.StatusServiceUnavailable, message, types.ErrorCodeModelNotFound)
  141. return
  142. }
  143. if channel == nil {
  144. abortWithOpenAiMessage(c, http.StatusServiceUnavailable, i18n.T(c, i18n.MsgDistributorNoAvailableChannel, map[string]any{"Group": usingGroup, "Model": modelRequest.Model}), types.ErrorCodeModelNotFound)
  145. return
  146. }
  147. }
  148. }
  149. }
  150. common.SetContextKey(c, constant.ContextKeyRequestStartTime, time.Now())
  151. SetupContextForSelectedChannel(c, channel, modelRequest.Model)
  152. c.Next()
  153. if channel != nil && c.Writer != nil && c.Writer.Status() < http.StatusBadRequest {
  154. service.RecordChannelAffinity(c, channel.Id)
  155. }
  156. }
  157. }
  158. // getModelFromRequest 从请求中读取模型信息
  159. // 根据 Content-Type 自动处理:
  160. // - application/json
  161. // - application/x-www-form-urlencoded
  162. // - multipart/form-data
  163. func getModelFromRequest(c *gin.Context) (*ModelRequest, error) {
  164. var modelRequest ModelRequest
  165. err := common.UnmarshalBodyReusable(c, &modelRequest)
  166. if err != nil {
  167. return nil, errors.New(i18n.T(c, i18n.MsgDistributorInvalidRequest, map[string]any{"Error": err.Error()}))
  168. }
  169. return &modelRequest, nil
  170. }
  171. func getModelRequest(c *gin.Context) (*ModelRequest, bool, error) {
  172. var modelRequest ModelRequest
  173. shouldSelectChannel := true
  174. var err error
  175. if strings.Contains(c.Request.URL.Path, "/mj/") {
  176. relayMode := relayconstant.Path2RelayModeMidjourney(c.Request.URL.Path)
  177. if relayMode == relayconstant.RelayModeMidjourneyTaskFetch ||
  178. relayMode == relayconstant.RelayModeMidjourneyTaskFetchByCondition ||
  179. relayMode == relayconstant.RelayModeMidjourneyNotify ||
  180. relayMode == relayconstant.RelayModeMidjourneyTaskImageSeed {
  181. shouldSelectChannel = false
  182. } else {
  183. midjourneyRequest := dto.MidjourneyRequest{}
  184. err = common.UnmarshalBodyReusable(c, &midjourneyRequest)
  185. if err != nil {
  186. return nil, false, errors.New(i18n.T(c, i18n.MsgDistributorInvalidMidjourney, map[string]any{"Error": err.Error()}))
  187. }
  188. midjourneyModel, mjErr, success := service.GetMjRequestModel(relayMode, &midjourneyRequest)
  189. if mjErr != nil {
  190. return nil, false, fmt.Errorf("%s", mjErr.Description)
  191. }
  192. if midjourneyModel == "" {
  193. if !success {
  194. return nil, false, fmt.Errorf("%s", i18n.T(c, i18n.MsgDistributorInvalidParseModel))
  195. } else {
  196. // task fetch, task fetch by condition, notify
  197. shouldSelectChannel = false
  198. }
  199. }
  200. modelRequest.Model = midjourneyModel
  201. }
  202. c.Set("relay_mode", relayMode)
  203. } else if strings.Contains(c.Request.URL.Path, "/suno/") {
  204. relayMode := relayconstant.Path2RelaySuno(c.Request.Method, c.Request.URL.Path)
  205. if relayMode == relayconstant.RelayModeSunoFetch ||
  206. relayMode == relayconstant.RelayModeSunoFetchByID {
  207. shouldSelectChannel = false
  208. } else {
  209. modelName := service.CoverTaskActionToModelName(constant.TaskPlatformSuno, c.Param("action"))
  210. modelRequest.Model = modelName
  211. }
  212. c.Set("platform", string(constant.TaskPlatformSuno))
  213. c.Set("relay_mode", relayMode)
  214. } else if strings.Contains(c.Request.URL.Path, "/v1/videos/") && strings.HasSuffix(c.Request.URL.Path, "/remix") {
  215. relayMode := relayconstant.RelayModeVideoSubmit
  216. c.Set("relay_mode", relayMode)
  217. shouldSelectChannel = false
  218. } else if strings.Contains(c.Request.URL.Path, "/v1/videos") {
  219. //curl https://api.openai.com/v1/videos \
  220. // -H "Authorization: Bearer $OPENAI_API_KEY" \
  221. // -F "model=sora-2" \
  222. // -F "prompt=A calico cat playing a piano on stage"
  223. // -F input_reference="@image.jpg"
  224. relayMode := relayconstant.RelayModeUnknown
  225. if c.Request.Method == http.MethodPost {
  226. relayMode = relayconstant.RelayModeVideoSubmit
  227. req, err := getModelFromRequest(c)
  228. if err != nil {
  229. return nil, false, err
  230. }
  231. if req != nil {
  232. modelRequest.Model = req.Model
  233. }
  234. } else if c.Request.Method == http.MethodGet {
  235. relayMode = relayconstant.RelayModeVideoFetchByID
  236. shouldSelectChannel = false
  237. }
  238. c.Set("relay_mode", relayMode)
  239. } else if strings.Contains(c.Request.URL.Path, "/v1/video/generations") {
  240. relayMode := relayconstant.RelayModeUnknown
  241. if c.Request.Method == http.MethodPost {
  242. req, err := getModelFromRequest(c)
  243. if err != nil {
  244. return nil, false, err
  245. }
  246. modelRequest.Model = req.Model
  247. relayMode = relayconstant.RelayModeVideoSubmit
  248. } else if c.Request.Method == http.MethodGet {
  249. relayMode = relayconstant.RelayModeVideoFetchByID
  250. shouldSelectChannel = false
  251. }
  252. if _, ok := c.Get("relay_mode"); !ok {
  253. c.Set("relay_mode", relayMode)
  254. }
  255. } else if strings.HasPrefix(c.Request.URL.Path, "/v1beta/models/") || strings.HasPrefix(c.Request.URL.Path, "/v1/models/") {
  256. // Gemini API 路径处理: /v1beta/models/gemini-2.0-flash:generateContent
  257. relayMode := relayconstant.RelayModeGemini
  258. modelName := extractModelNameFromGeminiPath(c.Request.URL.Path)
  259. if modelName != "" {
  260. modelRequest.Model = modelName
  261. }
  262. c.Set("relay_mode", relayMode)
  263. } else if !strings.HasPrefix(c.Request.URL.Path, "/v1/audio/transcriptions") && !strings.Contains(c.Request.Header.Get("Content-Type"), "multipart/form-data") {
  264. req, err := getModelFromRequest(c)
  265. if err != nil {
  266. return nil, false, err
  267. }
  268. modelRequest.Model = req.Model
  269. }
  270. if strings.HasPrefix(c.Request.URL.Path, "/v1/realtime") {
  271. //wss://api.openai.com/v1/realtime?model=gpt-4o-realtime-preview-2024-10-01
  272. modelRequest.Model = c.Query("model")
  273. }
  274. if strings.HasPrefix(c.Request.URL.Path, "/v1/moderations") {
  275. if modelRequest.Model == "" {
  276. modelRequest.Model = "text-moderation-stable"
  277. }
  278. }
  279. if strings.HasSuffix(c.Request.URL.Path, "embeddings") {
  280. if modelRequest.Model == "" {
  281. modelRequest.Model = c.Param("model")
  282. }
  283. }
  284. if strings.HasPrefix(c.Request.URL.Path, "/v1/images/generations") {
  285. modelRequest.Model = common.GetStringIfEmpty(modelRequest.Model, "dall-e")
  286. } else if strings.HasPrefix(c.Request.URL.Path, "/v1/images/edits") {
  287. //modelRequest.Model = common.GetStringIfEmpty(c.PostForm("model"), "gpt-image-1")
  288. contentType := c.ContentType()
  289. if slices.Contains([]string{gin.MIMEPOSTForm, gin.MIMEMultipartPOSTForm}, contentType) {
  290. req, err := getModelFromRequest(c)
  291. if err == nil && req.Model != "" {
  292. modelRequest.Model = req.Model
  293. }
  294. }
  295. }
  296. if strings.HasPrefix(c.Request.URL.Path, "/v1/audio") {
  297. relayMode := relayconstant.RelayModeAudioSpeech
  298. if strings.HasPrefix(c.Request.URL.Path, "/v1/audio/speech") {
  299. modelRequest.Model = common.GetStringIfEmpty(modelRequest.Model, "tts-1")
  300. } else if strings.HasPrefix(c.Request.URL.Path, "/v1/audio/translations") {
  301. // 先尝试从请求读取
  302. if req, err := getModelFromRequest(c); err == nil && req.Model != "" {
  303. modelRequest.Model = req.Model
  304. }
  305. modelRequest.Model = common.GetStringIfEmpty(modelRequest.Model, "whisper-1")
  306. relayMode = relayconstant.RelayModeAudioTranslation
  307. } else if strings.HasPrefix(c.Request.URL.Path, "/v1/audio/transcriptions") {
  308. // 先尝试从请求读取
  309. if req, err := getModelFromRequest(c); err == nil && req.Model != "" {
  310. modelRequest.Model = req.Model
  311. }
  312. modelRequest.Model = common.GetStringIfEmpty(modelRequest.Model, "whisper-1")
  313. relayMode = relayconstant.RelayModeAudioTranscription
  314. }
  315. c.Set("relay_mode", relayMode)
  316. }
  317. if strings.HasPrefix(c.Request.URL.Path, "/pg/chat/completions") {
  318. // playground chat completions
  319. req, err := getModelFromRequest(c)
  320. if err != nil {
  321. return nil, false, err
  322. }
  323. modelRequest.Model = req.Model
  324. modelRequest.Group = req.Group
  325. common.SetContextKey(c, constant.ContextKeyTokenGroup, modelRequest.Group)
  326. }
  327. if strings.HasPrefix(c.Request.URL.Path, "/v1/responses/compact") && modelRequest.Model != "" {
  328. modelRequest.Model = ratio_setting.WithCompactModelSuffix(modelRequest.Model)
  329. }
  330. return &modelRequest, shouldSelectChannel, nil
  331. }
  332. func SetupContextForSelectedChannel(c *gin.Context, channel *model.Channel, modelName string) *types.NewAPIError {
  333. c.Set("original_model", modelName) // for retry
  334. if channel == nil {
  335. return types.NewError(errors.New("channel is nil"), types.ErrorCodeGetChannelFailed, types.ErrOptionWithSkipRetry())
  336. }
  337. common.SetContextKey(c, constant.ContextKeyChannelId, channel.Id)
  338. common.SetContextKey(c, constant.ContextKeyChannelName, channel.Name)
  339. common.SetContextKey(c, constant.ContextKeyChannelType, channel.Type)
  340. common.SetContextKey(c, constant.ContextKeyChannelCreateTime, channel.CreatedTime)
  341. common.SetContextKey(c, constant.ContextKeyChannelSetting, channel.GetSetting())
  342. common.SetContextKey(c, constant.ContextKeyChannelOtherSetting, channel.GetOtherSettings())
  343. paramOverride := channel.GetParamOverride()
  344. headerOverride := channel.GetHeaderOverride()
  345. if mergedParam, applied := service.ApplyChannelAffinityOverrideTemplate(c, paramOverride); applied {
  346. paramOverride = mergedParam
  347. }
  348. common.SetContextKey(c, constant.ContextKeyChannelParamOverride, paramOverride)
  349. common.SetContextKey(c, constant.ContextKeyChannelHeaderOverride, headerOverride)
  350. if nil != channel.OpenAIOrganization && *channel.OpenAIOrganization != "" {
  351. common.SetContextKey(c, constant.ContextKeyChannelOrganization, *channel.OpenAIOrganization)
  352. }
  353. common.SetContextKey(c, constant.ContextKeyChannelAutoBan, channel.GetAutoBan())
  354. common.SetContextKey(c, constant.ContextKeyChannelModelMapping, channel.GetModelMapping())
  355. common.SetContextKey(c, constant.ContextKeyChannelStatusCodeMapping, channel.GetStatusCodeMapping())
  356. key, index, newAPIError := channel.GetNextEnabledKey()
  357. if newAPIError != nil {
  358. return newAPIError
  359. }
  360. if channel.ChannelInfo.IsMultiKey {
  361. common.SetContextKey(c, constant.ContextKeyChannelIsMultiKey, true)
  362. common.SetContextKey(c, constant.ContextKeyChannelMultiKeyIndex, index)
  363. } else {
  364. // 必须设置为 false,否则在重试到单个 key 的时候会导致日志显示错误
  365. common.SetContextKey(c, constant.ContextKeyChannelIsMultiKey, false)
  366. }
  367. // c.Request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", key))
  368. common.SetContextKey(c, constant.ContextKeyChannelKey, key)
  369. common.SetContextKey(c, constant.ContextKeyChannelBaseUrl, channel.GetBaseURL())
  370. common.SetContextKey(c, constant.ContextKeySystemPromptOverride, false)
  371. // TODO: api_version统一
  372. switch channel.Type {
  373. case constant.ChannelTypeAzure:
  374. c.Set("api_version", channel.Other)
  375. case constant.ChannelTypeVertexAi:
  376. c.Set("region", channel.Other)
  377. case constant.ChannelTypeXunfei:
  378. c.Set("api_version", channel.Other)
  379. case constant.ChannelTypeGemini:
  380. c.Set("api_version", channel.Other)
  381. case constant.ChannelTypeAli:
  382. c.Set("plugin", channel.Other)
  383. case constant.ChannelCloudflare:
  384. c.Set("api_version", channel.Other)
  385. case constant.ChannelTypeMokaAI:
  386. c.Set("api_version", channel.Other)
  387. case constant.ChannelTypeCoze:
  388. c.Set("bot_id", channel.Other)
  389. }
  390. return nil
  391. }
  392. // extractModelNameFromGeminiPath 从 Gemini API URL 路径中提取模型名
  393. // 输入格式: /v1beta/models/gemini-2.0-flash:generateContent
  394. // 输出: gemini-2.0-flash
  395. func extractModelNameFromGeminiPath(path string) string {
  396. // 查找 "/models/" 的位置
  397. modelsPrefix := "/models/"
  398. modelsIndex := strings.Index(path, modelsPrefix)
  399. if modelsIndex == -1 {
  400. return ""
  401. }
  402. // 从 "/models/" 之后开始提取
  403. startIndex := modelsIndex + len(modelsPrefix)
  404. if startIndex >= len(path) {
  405. return ""
  406. }
  407. // 查找 ":" 的位置,模型名在 ":" 之前
  408. colonIndex := strings.Index(path[startIndex:], ":")
  409. if colonIndex == -1 {
  410. // 如果没有找到 ":",返回从 "/models/" 到路径结尾的部分
  411. return path[startIndex:]
  412. }
  413. // 返回模型名部分
  414. return path[startIndex : startIndex+colonIndex]
  415. }