distributor.go 8.8 KB

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