distributor.go 4.3 KB

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