distributor.go 15 KB

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