distributor.go 14 KB

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