distributor.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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/ratio_setting"
  13. "one-api/types"
  14. "strconv"
  15. "strings"
  16. "time"
  17. "github.com/gin-gonic/gin"
  18. )
  19. type ModelRequest struct {
  20. Model string `json:"model"`
  21. Group string `json:"group,omitempty"`
  22. }
  23. func Distribute() func(c *gin.Context) {
  24. return func(c *gin.Context) {
  25. var channel *model.Channel
  26. channelId, ok := common.GetContextKey(c, constant.ContextKeyTokenSpecificChannelId)
  27. modelRequest, shouldSelectChannel, err := getModelRequest(c)
  28. if err != nil {
  29. abortWithOpenAiMessage(c, http.StatusBadRequest, "Invalid request, "+err.Error())
  30. return
  31. }
  32. if ok {
  33. id, err := strconv.Atoi(channelId.(string))
  34. if err != nil {
  35. abortWithOpenAiMessage(c, http.StatusBadRequest, "无效的渠道 Id")
  36. return
  37. }
  38. channel, err = model.GetChannelById(id, true)
  39. if err != nil {
  40. abortWithOpenAiMessage(c, http.StatusBadRequest, "无效的渠道 Id")
  41. return
  42. }
  43. if channel.Status != common.ChannelStatusEnabled {
  44. abortWithOpenAiMessage(c, http.StatusForbidden, "该渠道已被禁用")
  45. return
  46. }
  47. } else {
  48. // Select a channel for the user
  49. // check token model mapping
  50. modelLimitEnable := common.GetContextKeyBool(c, constant.ContextKeyTokenModelLimitEnabled)
  51. if modelLimitEnable {
  52. s, ok := common.GetContextKey(c, constant.ContextKeyTokenModelLimit)
  53. if !ok {
  54. // token model limit is empty, all models are not allowed
  55. abortWithOpenAiMessage(c, http.StatusForbidden, "该令牌无权访问任何模型")
  56. return
  57. }
  58. var tokenModelLimit map[string]bool
  59. tokenModelLimit, ok = s.(map[string]bool)
  60. if !ok {
  61. tokenModelLimit = map[string]bool{}
  62. }
  63. matchName := ratio_setting.FormatMatchingModelName(modelRequest.Model) // match gpts & thinking-*
  64. if _, ok := tokenModelLimit[matchName]; !ok {
  65. abortWithOpenAiMessage(c, http.StatusForbidden, "该令牌无权访问模型 "+modelRequest.Model)
  66. return
  67. }
  68. }
  69. if shouldSelectChannel {
  70. if modelRequest.Model == "" {
  71. abortWithOpenAiMessage(c, http.StatusBadRequest, "未指定模型名称,模型名称不能为空")
  72. return
  73. }
  74. var selectGroup string
  75. userGroup := common.GetContextKeyString(c, constant.ContextKeyUsingGroup)
  76. channel, selectGroup, err = model.CacheGetRandomSatisfiedChannel(c, userGroup, modelRequest.Model, 0)
  77. if err != nil {
  78. showGroup := userGroup
  79. if userGroup == "auto" {
  80. showGroup = fmt.Sprintf("auto(%s)", selectGroup)
  81. }
  82. message := fmt.Sprintf("获取分组 %s 下模型 %s 的可用渠道失败(数据库一致性已被破坏,distributor): %s", showGroup, modelRequest.Model, err.Error())
  83. // 如果错误,但是渠道不为空,说明是数据库一致性问题
  84. //if channel != nil {
  85. // common.SysError(fmt.Sprintf("渠道不存在:%d", channel.Id))
  86. // message = "数据库一致性已被破坏,请联系管理员"
  87. //}
  88. abortWithOpenAiMessage(c, http.StatusServiceUnavailable, message)
  89. return
  90. }
  91. if channel == nil {
  92. abortWithOpenAiMessage(c, http.StatusServiceUnavailable, fmt.Sprintf("分组 %s 下模型 %s 无可用渠道(distributor)", userGroup, modelRequest.Model))
  93. return
  94. }
  95. }
  96. }
  97. common.SetContextKey(c, constant.ContextKeyRequestStartTime, time.Now())
  98. SetupContextForSelectedChannel(c, channel, modelRequest.Model)
  99. c.Next()
  100. }
  101. }
  102. func getModelRequest(c *gin.Context) (*ModelRequest, bool, error) {
  103. var modelRequest ModelRequest
  104. shouldSelectChannel := true
  105. var err error
  106. if strings.Contains(c.Request.URL.Path, "/mj/") {
  107. relayMode := relayconstant.Path2RelayModeMidjourney(c.Request.URL.Path)
  108. if relayMode == relayconstant.RelayModeMidjourneyTaskFetch ||
  109. relayMode == relayconstant.RelayModeMidjourneyTaskFetchByCondition ||
  110. relayMode == relayconstant.RelayModeMidjourneyNotify ||
  111. relayMode == relayconstant.RelayModeMidjourneyTaskImageSeed {
  112. shouldSelectChannel = false
  113. } else {
  114. midjourneyRequest := dto.MidjourneyRequest{}
  115. err = common.UnmarshalBodyReusable(c, &midjourneyRequest)
  116. if err != nil {
  117. return nil, false, err
  118. }
  119. midjourneyModel, mjErr, success := service.GetMjRequestModel(relayMode, &midjourneyRequest)
  120. if mjErr != nil {
  121. return nil, false, fmt.Errorf(mjErr.Description)
  122. }
  123. if midjourneyModel == "" {
  124. if !success {
  125. return nil, false, fmt.Errorf("无效的请求, 无法解析模型")
  126. } else {
  127. // task fetch, task fetch by condition, notify
  128. shouldSelectChannel = false
  129. }
  130. }
  131. modelRequest.Model = midjourneyModel
  132. }
  133. c.Set("relay_mode", relayMode)
  134. } else if strings.Contains(c.Request.URL.Path, "/suno/") {
  135. relayMode := relayconstant.Path2RelaySuno(c.Request.Method, c.Request.URL.Path)
  136. if relayMode == relayconstant.RelayModeSunoFetch ||
  137. relayMode == relayconstant.RelayModeSunoFetchByID {
  138. shouldSelectChannel = false
  139. } else {
  140. modelName := service.CoverTaskActionToModelName(constant.TaskPlatformSuno, c.Param("action"))
  141. modelRequest.Model = modelName
  142. }
  143. c.Set("platform", string(constant.TaskPlatformSuno))
  144. c.Set("relay_mode", relayMode)
  145. } else if strings.Contains(c.Request.URL.Path, "/v1/video/generations") {
  146. err = common.UnmarshalBodyReusable(c, &modelRequest)
  147. relayMode := relayconstant.RelayModeUnknown
  148. if c.Request.Method == http.MethodPost {
  149. relayMode = relayconstant.RelayModeVideoSubmit
  150. } else if c.Request.Method == http.MethodGet {
  151. relayMode = relayconstant.RelayModeVideoFetchByID
  152. shouldSelectChannel = false
  153. }
  154. c.Set("relay_mode", relayMode)
  155. } else if strings.HasPrefix(c.Request.URL.Path, "/v1beta/models/") || strings.HasPrefix(c.Request.URL.Path, "/v1/models/") {
  156. // Gemini API 路径处理: /v1beta/models/gemini-2.0-flash:generateContent
  157. relayMode := relayconstant.RelayModeGemini
  158. modelName := extractModelNameFromGeminiPath(c.Request.URL.Path)
  159. if modelName != "" {
  160. modelRequest.Model = modelName
  161. }
  162. c.Set("relay_mode", relayMode)
  163. } else if !strings.HasPrefix(c.Request.URL.Path, "/v1/audio/transcriptions") && !strings.HasPrefix(c.Request.URL.Path, "/v1/images/edits") {
  164. err = common.UnmarshalBodyReusable(c, &modelRequest)
  165. }
  166. if err != nil {
  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. } else if strings.HasPrefix(c.Request.URL.Path, "/v1/images/edits") {
  186. modelRequest.Model = common.GetStringIfEmpty(c.PostForm("model"), "gpt-image-1")
  187. }
  188. if strings.HasPrefix(c.Request.URL.Path, "/v1/audio") {
  189. relayMode := relayconstant.RelayModeAudioSpeech
  190. if strings.HasPrefix(c.Request.URL.Path, "/v1/audio/speech") {
  191. modelRequest.Model = common.GetStringIfEmpty(modelRequest.Model, "tts-1")
  192. } else if strings.HasPrefix(c.Request.URL.Path, "/v1/audio/translations") {
  193. modelRequest.Model = common.GetStringIfEmpty(modelRequest.Model, c.PostForm("model"))
  194. modelRequest.Model = common.GetStringIfEmpty(modelRequest.Model, "whisper-1")
  195. relayMode = relayconstant.RelayModeAudioTranslation
  196. } else if strings.HasPrefix(c.Request.URL.Path, "/v1/audio/transcriptions") {
  197. modelRequest.Model = common.GetStringIfEmpty(modelRequest.Model, c.PostForm("model"))
  198. modelRequest.Model = common.GetStringIfEmpty(modelRequest.Model, "whisper-1")
  199. relayMode = relayconstant.RelayModeAudioTranscription
  200. }
  201. c.Set("relay_mode", relayMode)
  202. }
  203. if strings.HasPrefix(c.Request.URL.Path, "/pg/chat/completions") {
  204. // playground chat completions
  205. err = common.UnmarshalBodyReusable(c, &modelRequest)
  206. if err != nil {
  207. return nil, false, errors.New("无效的请求, " + err.Error())
  208. }
  209. common.SetContextKey(c, constant.ContextKeyTokenGroup, modelRequest.Group)
  210. }
  211. return &modelRequest, shouldSelectChannel, nil
  212. }
  213. func SetupContextForSelectedChannel(c *gin.Context, channel *model.Channel, modelName string) *types.NewAPIError {
  214. c.Set("original_model", modelName) // for retry
  215. if channel == nil {
  216. return types.NewError(errors.New("channel is nil"), types.ErrorCodeGetChannelFailed, types.ErrOptionWithSkipRetry())
  217. }
  218. common.SetContextKey(c, constant.ContextKeyChannelId, channel.Id)
  219. common.SetContextKey(c, constant.ContextKeyChannelName, channel.Name)
  220. common.SetContextKey(c, constant.ContextKeyChannelType, channel.Type)
  221. common.SetContextKey(c, constant.ContextKeyChannelCreateTime, channel.CreatedTime)
  222. common.SetContextKey(c, constant.ContextKeyChannelSetting, channel.GetSetting())
  223. common.SetContextKey(c, constant.ContextKeyChannelParamOverride, channel.GetParamOverride())
  224. if nil != channel.OpenAIOrganization && *channel.OpenAIOrganization != "" {
  225. common.SetContextKey(c, constant.ContextKeyChannelOrganization, *channel.OpenAIOrganization)
  226. }
  227. common.SetContextKey(c, constant.ContextKeyChannelAutoBan, channel.GetAutoBan())
  228. common.SetContextKey(c, constant.ContextKeyChannelModelMapping, channel.GetModelMapping())
  229. common.SetContextKey(c, constant.ContextKeyChannelStatusCodeMapping, channel.GetStatusCodeMapping())
  230. key, index, newAPIError := channel.GetNextEnabledKey()
  231. if newAPIError != nil {
  232. return newAPIError
  233. }
  234. if channel.ChannelInfo.IsMultiKey {
  235. common.SetContextKey(c, constant.ContextKeyChannelIsMultiKey, true)
  236. common.SetContextKey(c, constant.ContextKeyChannelMultiKeyIndex, index)
  237. } else {
  238. // 必须设置为 false,否则在重试到单个 key 的时候会导致日志显示错误
  239. common.SetContextKey(c, constant.ContextKeyChannelIsMultiKey, false)
  240. }
  241. // c.Request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", key))
  242. common.SetContextKey(c, constant.ContextKeyChannelKey, key)
  243. common.SetContextKey(c, constant.ContextKeyChannelBaseUrl, channel.GetBaseURL())
  244. // TODO: api_version统一
  245. switch channel.Type {
  246. case constant.ChannelTypeAzure:
  247. c.Set("api_version", channel.Other)
  248. case constant.ChannelTypeVertexAi:
  249. c.Set("region", channel.Other)
  250. case constant.ChannelTypeXunfei:
  251. c.Set("api_version", channel.Other)
  252. case constant.ChannelTypeGemini:
  253. c.Set("api_version", channel.Other)
  254. case constant.ChannelTypeAli:
  255. c.Set("plugin", channel.Other)
  256. case constant.ChannelCloudflare:
  257. c.Set("api_version", channel.Other)
  258. case constant.ChannelTypeMokaAI:
  259. c.Set("api_version", channel.Other)
  260. case constant.ChannelTypeCoze:
  261. c.Set("bot_id", channel.Other)
  262. }
  263. return nil
  264. }
  265. // extractModelNameFromGeminiPath 从 Gemini API URL 路径中提取模型名
  266. // 输入格式: /v1beta/models/gemini-2.0-flash:generateContent
  267. // 输出: gemini-2.0-flash
  268. func extractModelNameFromGeminiPath(path string) string {
  269. // 查找 "/models/" 的位置
  270. modelsPrefix := "/models/"
  271. modelsIndex := strings.Index(path, modelsPrefix)
  272. if modelsIndex == -1 {
  273. return ""
  274. }
  275. // 从 "/models/" 之后开始提取
  276. startIndex := modelsIndex + len(modelsPrefix)
  277. if startIndex >= len(path) {
  278. return ""
  279. }
  280. // 查找 ":" 的位置,模型名在 ":" 之前
  281. colonIndex := strings.Index(path[startIndex:], ":")
  282. if colonIndex == -1 {
  283. // 如果没有找到 ":",返回从 "/models/" 到路径结尾的部分
  284. return path[startIndex:]
  285. }
  286. // 返回模型名部分
  287. return path[startIndex : startIndex+colonIndex]
  288. }