distributor.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package middleware
  2. import (
  3. "fmt"
  4. "net/http"
  5. "one-api/common"
  6. "one-api/model"
  7. "strconv"
  8. "strings"
  9. "github.com/gin-gonic/gin"
  10. )
  11. type ModelRequest struct {
  12. Model string `json:"model"`
  13. }
  14. func Distribute() func(c *gin.Context) {
  15. return func(c *gin.Context) {
  16. userId := c.GetInt("id")
  17. userGroup, _ := model.CacheGetUserGroup(userId)
  18. c.Set("group", userGroup)
  19. var channel *model.Channel
  20. channelId, ok := c.Get("channelId")
  21. if ok {
  22. id, err := strconv.Atoi(channelId.(string))
  23. if err != nil {
  24. abortWithMessage(c, http.StatusBadRequest, "无效的渠道 Id")
  25. return
  26. }
  27. channel, err = model.GetChannelById(id, true)
  28. if err != nil {
  29. abortWithMessage(c, http.StatusBadRequest, "无效的渠道 Id")
  30. return
  31. }
  32. if channel.Status != common.ChannelStatusEnabled {
  33. abortWithMessage(c, http.StatusForbidden, "该渠道已被禁用")
  34. return
  35. }
  36. } else {
  37. // Select a channel for the user
  38. var modelRequest ModelRequest
  39. var err error
  40. if strings.HasPrefix(c.Request.URL.Path, "/mj") {
  41. // Midjourney
  42. if modelRequest.Model == "" {
  43. modelRequest.Model = "midjourney"
  44. }
  45. } else if !strings.HasPrefix(c.Request.URL.Path, "/v1/audio/transcriptions") {
  46. err = common.UnmarshalBodyReusable(c, &modelRequest)
  47. }
  48. if err != nil {
  49. abortWithMessage(c, http.StatusBadRequest, "无效的请求: "+err.Error())
  50. return
  51. }
  52. if strings.HasPrefix(c.Request.URL.Path, "/v1/moderations") {
  53. if modelRequest.Model == "" {
  54. modelRequest.Model = "text-moderation-stable"
  55. }
  56. }
  57. if strings.HasSuffix(c.Request.URL.Path, "embeddings") {
  58. if modelRequest.Model == "" {
  59. modelRequest.Model = c.Param("model")
  60. }
  61. }
  62. if strings.HasPrefix(c.Request.URL.Path, "/v1/images/generations") {
  63. if modelRequest.Model == "" {
  64. modelRequest.Model = "dall-e"
  65. }
  66. }
  67. if strings.HasPrefix(c.Request.URL.Path, "/v1/audio") {
  68. if modelRequest.Model == "" {
  69. if strings.HasPrefix(c.Request.URL.Path, "/v1/audio/speech") {
  70. modelRequest.Model = "tts-1"
  71. } else {
  72. modelRequest.Model = "whisper-1"
  73. }
  74. }
  75. }
  76. // check token model mapping
  77. modelLimitEnable := c.GetBool("token_model_limit_enabled")
  78. if modelLimitEnable {
  79. s, ok := c.Get("token_model_limit")
  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. abortWithMessage(c, http.StatusForbidden, "该令牌无权访问模型 "+modelRequest.Model)
  89. return
  90. }
  91. } else {
  92. // token model limit is empty, all models are not allowed
  93. abortWithMessage(c, http.StatusForbidden, "该令牌无权访问任何模型")
  94. return
  95. }
  96. }
  97. channel, err = model.CacheGetRandomSatisfiedChannel(userGroup, modelRequest.Model)
  98. if err != nil {
  99. message := fmt.Sprintf("当前分组 %s 下对于模型 %s 无可用渠道", userGroup, modelRequest.Model)
  100. if channel != nil {
  101. common.SysError(fmt.Sprintf("渠道不存在:%d", channel.Id))
  102. message = "数据库一致性已被破坏,请联系管理员"
  103. }
  104. abortWithMessage(c, http.StatusServiceUnavailable, message)
  105. return
  106. }
  107. }
  108. c.Set("channel", channel.Type)
  109. c.Set("channel_id", channel.Id)
  110. c.Set("channel_name", channel.Name)
  111. ban := true
  112. // parse *int to bool
  113. if channel.AutoBan != nil && *channel.AutoBan == 0 {
  114. ban = false
  115. }
  116. c.Set("auto_ban", ban)
  117. c.Set("model_mapping", channel.GetModelMapping())
  118. c.Request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", channel.Key))
  119. c.Set("base_url", channel.GetBaseURL())
  120. switch channel.Type {
  121. case common.ChannelTypeAzure:
  122. c.Set("api_version", channel.Other)
  123. case common.ChannelTypeXunfei:
  124. c.Set("api_version", channel.Other)
  125. case common.ChannelTypeAIProxyLibrary:
  126. c.Set("library_id", channel.Other)
  127. case common.ChannelTypeGemini:
  128. c.Set("api_version", channel.Other)
  129. }
  130. c.Next()
  131. }
  132. }