distributor.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package middleware
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "one-api/common"
  7. "one-api/model"
  8. "strconv"
  9. "strings"
  10. "github.com/gin-gonic/gin"
  11. )
  12. type ModelRequest struct {
  13. Model string `json:"model"`
  14. }
  15. func Distribute() func(c *gin.Context) {
  16. return func(c *gin.Context) {
  17. userId := c.GetInt("id")
  18. userGroup, _ := model.CacheGetUserGroup(userId)
  19. c.Set("group", userGroup)
  20. var channel *model.Channel
  21. var err error
  22. channelId, ok := c.Get("channelId")
  23. if ok {
  24. id, err := strconv.Atoi(channelId.(string))
  25. if err != nil {
  26. c.JSON(http.StatusBadRequest, gin.H{
  27. "error": gin.H{
  28. "message": "无效的渠道 ID",
  29. "type": "one_api_error",
  30. },
  31. })
  32. c.Abort()
  33. return
  34. }
  35. channel, err = model.GetChannelById(id, true)
  36. if err != nil {
  37. c.JSON(http.StatusBadRequest, gin.H{
  38. "error": gin.H{
  39. "message": "无效的渠道 ID",
  40. "type": "one_api_error",
  41. },
  42. })
  43. c.Abort()
  44. return
  45. }
  46. if channel.Status != common.ChannelStatusEnabled {
  47. c.JSON(http.StatusForbidden, gin.H{
  48. "error": gin.H{
  49. "message": "该渠道已被禁用",
  50. "type": "one_api_error",
  51. },
  52. })
  53. c.Abort()
  54. return
  55. }
  56. } else {
  57. // Select a channel for the user
  58. var modelRequest ModelRequest
  59. if strings.HasPrefix(c.Request.URL.Path, "/mj") {
  60. if modelRequest.Model == "" {
  61. modelRequest.Model = "midjourney"
  62. }
  63. } else {
  64. err := common.UnmarshalBodyReusable(c, &modelRequest)
  65. if err != nil {
  66. log.Println(err)
  67. c.JSON(http.StatusBadRequest, gin.H{
  68. "error": gin.H{
  69. "message": "无效的请求",
  70. "type": "one_api_error",
  71. },
  72. })
  73. c.Abort()
  74. return
  75. }
  76. }
  77. if strings.HasPrefix(c.Request.URL.Path, "/v1/moderations") {
  78. if modelRequest.Model == "" {
  79. modelRequest.Model = "text-moderation-stable"
  80. }
  81. }
  82. if strings.HasSuffix(c.Request.URL.Path, "embeddings") {
  83. if modelRequest.Model == "" {
  84. modelRequest.Model = c.Param("model")
  85. }
  86. }
  87. if strings.HasPrefix(c.Request.URL.Path, "/v1/images/generations") {
  88. if modelRequest.Model == "" {
  89. modelRequest.Model = "dall-e"
  90. }
  91. }
  92. isStable := false
  93. channel, err = model.CacheGetRandomSatisfiedChannel(userGroup, modelRequest.Model)
  94. c.Set("stable", false)
  95. if err != nil {
  96. message := fmt.Sprintf("当前分组 %s 下对于模型 %s 无可用渠道", userGroup, modelRequest.Model)
  97. if strings.HasPrefix(modelRequest.Model, "gpt-4") {
  98. common.SysLog("GPT-4低价渠道宕机,正在尝试转换")
  99. nowUser, err := model.GetUserById(userId, false)
  100. if err == nil {
  101. if nowUser.StableMode {
  102. userGroup = "svip"
  103. //stableRatio = (common.StablePrice / common.BasePrice) * modelRatio
  104. userMaxPrice, _ := strconv.ParseFloat(nowUser.MaxPrice, 64)
  105. if userMaxPrice < common.StablePrice {
  106. message = "当前低价通道不可用,稳定渠道价格为" + strconv.FormatFloat(common.StablePrice, 'f', -1, 64) + "R/刀"
  107. } else {
  108. //common.SysLog(fmt.Sprintf("用户 %s 使用稳定渠道", nowUser.Username))
  109. channel, err = model.CacheGetRandomSatisfiedChannel(userGroup, modelRequest.Model)
  110. if err != nil {
  111. message = "稳定渠道已经宕机,请联系管理员"
  112. }
  113. isStable = true
  114. common.SysLog(fmt.Sprintf("用户 %s 使用稳定渠道 %v", nowUser.Username, channel))
  115. c.Set("stable", true)
  116. }
  117. } else {
  118. message = "当前低价通道不可用,请稍后再试,或者在后台开启稳定渠道模式"
  119. }
  120. }
  121. }
  122. //if channel == nil {
  123. // common.SysError(fmt.Sprintf("渠道不存在:%d", channel.Id))
  124. // message = "数据库一致性已被破坏,请联系管理员"
  125. //}
  126. if !isStable {
  127. c.JSON(http.StatusInternalServerError, gin.H{
  128. "error": gin.H{
  129. "message": message,
  130. "type": "one_api_error",
  131. },
  132. })
  133. c.Abort()
  134. return
  135. }
  136. }
  137. }
  138. c.Set("channel", channel.Type)
  139. c.Set("channel_id", channel.Id)
  140. c.Set("channel_name", channel.Name)
  141. c.Set("model_mapping", channel.ModelMapping)
  142. c.Request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", channel.Key))
  143. c.Set("base_url", channel.BaseURL)
  144. if channel.Type == common.ChannelTypeAzure || channel.Type == common.ChannelTypeXunfei {
  145. c.Set("api_version", channel.Other)
  146. }
  147. c.Next()
  148. }
  149. }