distributor.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. package middleware
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. "one-api/common"
  7. "one-api/constant"
  8. "one-api/dto"
  9. "one-api/model"
  10. relayconstant "one-api/relay/constant"
  11. "one-api/service"
  12. "one-api/setting"
  13. "one-api/setting/ratio_setting"
  14. "one-api/types"
  15. "strconv"
  16. "strings"
  17. "time"
  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. allowIpsMap := common.GetContextKeyStringMap(c, constant.ContextKeyTokenAllowIps)
  27. if len(allowIpsMap) != 0 {
  28. clientIp := c.ClientIP()
  29. if _, ok := allowIpsMap[clientIp]; !ok {
  30. abortWithOpenAiMessage(c, http.StatusForbidden, "您的 IP 不在令牌允许访问的列表中")
  31. return
  32. }
  33. }
  34. var channel *model.Channel
  35. channelId, ok := common.GetContextKey(c, constant.ContextKeyTokenSpecificChannelId)
  36. modelRequest, shouldSelectChannel, err := getModelRequest(c)
  37. if err != nil {
  38. abortWithOpenAiMessage(c, http.StatusBadRequest, "Invalid request, "+err.Error())
  39. return
  40. }
  41. userGroup := common.GetContextKeyString(c, constant.ContextKeyUserGroup)
  42. tokenGroup := common.GetContextKeyString(c, constant.ContextKeyTokenGroup)
  43. if tokenGroup != "" {
  44. // check common.UserUsableGroups[userGroup]
  45. if _, ok := setting.GetUserUsableGroups(userGroup)[tokenGroup]; !ok {
  46. abortWithOpenAiMessage(c, http.StatusForbidden, fmt.Sprintf("令牌分组 %s 已被禁用", tokenGroup))
  47. return
  48. }
  49. // check group in common.GroupRatio
  50. if !ratio_setting.ContainsGroupRatio(tokenGroup) {
  51. if tokenGroup != "auto" {
  52. abortWithOpenAiMessage(c, http.StatusForbidden, fmt.Sprintf("分组 %s 已被弃用", tokenGroup))
  53. return
  54. }
  55. }
  56. userGroup = tokenGroup
  57. }
  58. common.SetContextKey(c, constant.ContextKeyUsingGroup, userGroup)
  59. if ok {
  60. id, err := strconv.Atoi(channelId.(string))
  61. if err != nil {
  62. abortWithOpenAiMessage(c, http.StatusBadRequest, "无效的渠道 Id")
  63. return
  64. }
  65. channel, err = model.GetChannelById(id, true)
  66. if err != nil {
  67. abortWithOpenAiMessage(c, http.StatusBadRequest, "无效的渠道 Id")
  68. return
  69. }
  70. if channel.Status != common.ChannelStatusEnabled {
  71. abortWithOpenAiMessage(c, http.StatusForbidden, "该渠道已被禁用")
  72. return
  73. }
  74. } else {
  75. // Select a channel for the user
  76. // check token model mapping
  77. modelLimitEnable := common.GetContextKeyBool(c, constant.ContextKeyTokenModelLimitEnabled)
  78. if modelLimitEnable {
  79. s, ok := common.GetContextKey(c, constant.ContextKeyTokenModelLimit)
  80. var tokenModelLimit map[string]bool
  81. if ok {
  82. tokenModelLimit = s.(map[string]bool)
  83. } else {
  84. tokenModelLimit = map[string]bool{}
  85. }
  86. if tokenModelLimit != nil {
  87. if _, ok := tokenModelLimit[modelRequest.Model]; !ok {
  88. abortWithOpenAiMessage(c, http.StatusForbidden, "该令牌无权访问模型 "+modelRequest.Model)
  89. return
  90. }
  91. } else {
  92. // token model limit is empty, all models are not allowed
  93. abortWithOpenAiMessage(c, http.StatusForbidden, "该令牌无权访问任何模型")
  94. return
  95. }
  96. }
  97. if shouldSelectChannel {
  98. var selectGroup string
  99. channel, selectGroup, err = model.CacheGetRandomSatisfiedChannel(c, userGroup, modelRequest.Model, 0)
  100. if err != nil {
  101. showGroup := userGroup
  102. if userGroup == "auto" {
  103. showGroup = fmt.Sprintf("auto(%s)", selectGroup)
  104. }
  105. message := fmt.Sprintf("当前分组 %s 下对于模型 %s 无可用渠道", showGroup, modelRequest.Model)
  106. // 如果错误,但是渠道不为空,说明是数据库一致性问题
  107. if channel != nil {
  108. common.SysError(fmt.Sprintf("渠道不存在:%d", channel.Id))
  109. message = "数据库一致性已被破坏,请联系管理员"
  110. }
  111. // 如果错误,而且渠道为空,说明是没有可用渠道
  112. abortWithOpenAiMessage(c, http.StatusServiceUnavailable, message)
  113. return
  114. }
  115. if channel == nil {
  116. abortWithOpenAiMessage(c, http.StatusServiceUnavailable, fmt.Sprintf("当前分组 %s 下对于模型 %s 无可用渠道(数据库一致性已被破坏)", userGroup, modelRequest.Model))
  117. return
  118. }
  119. }
  120. }
  121. common.SetContextKey(c, constant.ContextKeyRequestStartTime, time.Now())
  122. SetupContextForSelectedChannel(c, channel, modelRequest.Model)
  123. c.Next()
  124. }
  125. }
  126. func getModelRequest(c *gin.Context) (*ModelRequest, bool, error) {
  127. var modelRequest ModelRequest
  128. shouldSelectChannel := true
  129. var err error
  130. if strings.Contains(c.Request.URL.Path, "/mj/") {
  131. relayMode := relayconstant.Path2RelayModeMidjourney(c.Request.URL.Path)
  132. if relayMode == relayconstant.RelayModeMidjourneyTaskFetch ||
  133. relayMode == relayconstant.RelayModeMidjourneyTaskFetchByCondition ||
  134. relayMode == relayconstant.RelayModeMidjourneyNotify ||
  135. relayMode == relayconstant.RelayModeMidjourneyTaskImageSeed {
  136. shouldSelectChannel = false
  137. } else {
  138. midjourneyRequest := dto.MidjourneyRequest{}
  139. err = common.UnmarshalBodyReusable(c, &midjourneyRequest)
  140. if err != nil {
  141. return nil, false, err
  142. }
  143. midjourneyModel, mjErr, success := service.GetMjRequestModel(relayMode, &midjourneyRequest)
  144. if mjErr != nil {
  145. return nil, false, fmt.Errorf(mjErr.Description)
  146. }
  147. if midjourneyModel == "" {
  148. if !success {
  149. return nil, false, fmt.Errorf("无效的请求, 无法解析模型")
  150. } else {
  151. // task fetch, task fetch by condition, notify
  152. shouldSelectChannel = false
  153. }
  154. }
  155. modelRequest.Model = midjourneyModel
  156. }
  157. c.Set("relay_mode", relayMode)
  158. } else if strings.Contains(c.Request.URL.Path, "/suno/") {
  159. relayMode := relayconstant.Path2RelaySuno(c.Request.Method, c.Request.URL.Path)
  160. if relayMode == relayconstant.RelayModeSunoFetch ||
  161. relayMode == relayconstant.RelayModeSunoFetchByID {
  162. shouldSelectChannel = false
  163. } else {
  164. modelName := service.CoverTaskActionToModelName(constant.TaskPlatformSuno, c.Param("action"))
  165. modelRequest.Model = modelName
  166. }
  167. c.Set("platform", string(constant.TaskPlatformSuno))
  168. c.Set("relay_mode", relayMode)
  169. } else if strings.Contains(c.Request.URL.Path, "/v1/video/generations") {
  170. err = common.UnmarshalBodyReusable(c, &modelRequest)
  171. var platform string
  172. var relayMode int
  173. if strings.HasPrefix(modelRequest.Model, "jimeng") {
  174. platform = string(constant.TaskPlatformJimeng)
  175. relayMode = relayconstant.Path2RelayJimeng(c.Request.Method, c.Request.URL.Path)
  176. if relayMode == relayconstant.RelayModeJimengFetchByID {
  177. shouldSelectChannel = false
  178. }
  179. } else {
  180. platform = string(constant.TaskPlatformKling)
  181. relayMode = relayconstant.Path2RelayKling(c.Request.Method, c.Request.URL.Path)
  182. if relayMode == relayconstant.RelayModeKlingFetchByID {
  183. shouldSelectChannel = false
  184. }
  185. }
  186. c.Set("platform", platform)
  187. c.Set("relay_mode", relayMode)
  188. } else if strings.HasPrefix(c.Request.URL.Path, "/v1beta/models/") || strings.HasPrefix(c.Request.URL.Path, "/v1/models/") {
  189. // Gemini API 路径处理: /v1beta/models/gemini-2.0-flash:generateContent
  190. relayMode := relayconstant.RelayModeGemini
  191. modelName := extractModelNameFromGeminiPath(c.Request.URL.Path)
  192. if modelName != "" {
  193. modelRequest.Model = modelName
  194. }
  195. c.Set("relay_mode", relayMode)
  196. } else if !strings.HasPrefix(c.Request.URL.Path, "/v1/audio/transcriptions") && !strings.HasPrefix(c.Request.URL.Path, "/v1/images/edits") {
  197. err = common.UnmarshalBodyReusable(c, &modelRequest)
  198. }
  199. if err != nil {
  200. return nil, false, errors.New("无效的请求, " + err.Error())
  201. }
  202. if strings.HasPrefix(c.Request.URL.Path, "/v1/realtime") {
  203. //wss://api.openai.com/v1/realtime?model=gpt-4o-realtime-preview-2024-10-01
  204. modelRequest.Model = c.Query("model")
  205. }
  206. if strings.HasPrefix(c.Request.URL.Path, "/v1/moderations") {
  207. if modelRequest.Model == "" {
  208. modelRequest.Model = "text-moderation-stable"
  209. }
  210. }
  211. if strings.HasSuffix(c.Request.URL.Path, "embeddings") {
  212. if modelRequest.Model == "" {
  213. modelRequest.Model = c.Param("model")
  214. }
  215. }
  216. if strings.HasPrefix(c.Request.URL.Path, "/v1/images/generations") {
  217. modelRequest.Model = common.GetStringIfEmpty(modelRequest.Model, "dall-e")
  218. } else if strings.HasPrefix(c.Request.URL.Path, "/v1/images/edits") {
  219. modelRequest.Model = common.GetStringIfEmpty(c.PostForm("model"), "gpt-image-1")
  220. }
  221. if strings.HasPrefix(c.Request.URL.Path, "/v1/audio") {
  222. relayMode := relayconstant.RelayModeAudioSpeech
  223. if strings.HasPrefix(c.Request.URL.Path, "/v1/audio/speech") {
  224. modelRequest.Model = common.GetStringIfEmpty(modelRequest.Model, "tts-1")
  225. } else if strings.HasPrefix(c.Request.URL.Path, "/v1/audio/translations") {
  226. modelRequest.Model = common.GetStringIfEmpty(modelRequest.Model, c.PostForm("model"))
  227. modelRequest.Model = common.GetStringIfEmpty(modelRequest.Model, "whisper-1")
  228. relayMode = relayconstant.RelayModeAudioTranslation
  229. } else if strings.HasPrefix(c.Request.URL.Path, "/v1/audio/transcriptions") {
  230. modelRequest.Model = common.GetStringIfEmpty(modelRequest.Model, c.PostForm("model"))
  231. modelRequest.Model = common.GetStringIfEmpty(modelRequest.Model, "whisper-1")
  232. relayMode = relayconstant.RelayModeAudioTranscription
  233. }
  234. c.Set("relay_mode", relayMode)
  235. }
  236. if strings.HasPrefix(c.Request.URL.Path, "/pg/chat/completions") {
  237. // playground chat completions
  238. err = common.UnmarshalBodyReusable(c, &modelRequest)
  239. if err != nil {
  240. return nil, false, errors.New("无效的请求, " + err.Error())
  241. }
  242. common.SetContextKey(c, constant.ContextKeyTokenGroup, modelRequest.Group)
  243. }
  244. return &modelRequest, shouldSelectChannel, nil
  245. }
  246. func SetupContextForSelectedChannel(c *gin.Context, channel *model.Channel, modelName string) *types.NewAPIError {
  247. c.Set("original_model", modelName) // for retry
  248. if channel == nil {
  249. return types.NewError(errors.New("channel is nil"), types.ErrorCodeGetChannelFailed)
  250. }
  251. common.SetContextKey(c, constant.ContextKeyChannelId, channel.Id)
  252. common.SetContextKey(c, constant.ContextKeyChannelName, channel.Name)
  253. common.SetContextKey(c, constant.ContextKeyChannelType, channel.Type)
  254. common.SetContextKey(c, constant.ContextKeyChannelCreateTime, channel.CreatedTime)
  255. common.SetContextKey(c, constant.ContextKeyChannelSetting, channel.GetSetting())
  256. common.SetContextKey(c, constant.ContextKeyChannelParamOverride, channel.GetParamOverride())
  257. if nil != channel.OpenAIOrganization && *channel.OpenAIOrganization != "" {
  258. common.SetContextKey(c, constant.ContextKeyChannelOrganization, *channel.OpenAIOrganization)
  259. }
  260. common.SetContextKey(c, constant.ContextKeyChannelAutoBan, channel.GetAutoBan())
  261. common.SetContextKey(c, constant.ContextKeyChannelModelMapping, channel.GetModelMapping())
  262. common.SetContextKey(c, constant.ContextKeyChannelStatusCodeMapping, channel.GetStatusCodeMapping())
  263. key, index, newAPIError := channel.GetNextEnabledKey()
  264. if newAPIError != nil {
  265. return newAPIError
  266. }
  267. if channel.ChannelInfo.IsMultiKey {
  268. common.SetContextKey(c, constant.ContextKeyChannelIsMultiKey, true)
  269. common.SetContextKey(c, constant.ContextKeyChannelMultiKeyIndex, index)
  270. }
  271. // c.Request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", key))
  272. common.SetContextKey(c, constant.ContextKeyChannelKey, key)
  273. common.SetContextKey(c, constant.ContextKeyChannelBaseUrl, channel.GetBaseURL())
  274. // TODO: api_version统一
  275. switch channel.Type {
  276. case constant.ChannelTypeAzure:
  277. c.Set("api_version", channel.Other)
  278. case constant.ChannelTypeVertexAi:
  279. c.Set("region", channel.Other)
  280. case constant.ChannelTypeXunfei:
  281. c.Set("api_version", channel.Other)
  282. case constant.ChannelTypeGemini:
  283. c.Set("api_version", channel.Other)
  284. case constant.ChannelTypeAli:
  285. c.Set("plugin", channel.Other)
  286. case constant.ChannelCloudflare:
  287. c.Set("api_version", channel.Other)
  288. case constant.ChannelTypeMokaAI:
  289. c.Set("api_version", channel.Other)
  290. case constant.ChannelTypeCoze:
  291. c.Set("bot_id", channel.Other)
  292. }
  293. return nil
  294. }
  295. // extractModelNameFromGeminiPath 从 Gemini API URL 路径中提取模型名
  296. // 输入格式: /v1beta/models/gemini-2.0-flash:generateContent
  297. // 输出: gemini-2.0-flash
  298. func extractModelNameFromGeminiPath(path string) string {
  299. // 查找 "/models/" 的位置
  300. modelsPrefix := "/models/"
  301. modelsIndex := strings.Index(path, modelsPrefix)
  302. if modelsIndex == -1 {
  303. return ""
  304. }
  305. // 从 "/models/" 之后开始提取
  306. startIndex := modelsIndex + len(modelsPrefix)
  307. if startIndex >= len(path) {
  308. return ""
  309. }
  310. // 查找 ":" 的位置,模型名在 ":" 之前
  311. colonIndex := strings.Index(path[startIndex:], ":")
  312. if colonIndex == -1 {
  313. // 如果没有找到 ":",返回从 "/models/" 到路径结尾的部分
  314. return path[startIndex:]
  315. }
  316. // 返回模型名部分
  317. return path[startIndex : startIndex+colonIndex]
  318. }