distributor.go 10 KB

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