distributor.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. channel, err = model.CacheGetRandomSatisfiedChannel(userGroup, modelRequest.Model)
  93. if err != nil {
  94. message := fmt.Sprintf("当前分组 %s 下对于模型 %s 无可用渠道", userGroup, modelRequest.Model)
  95. if channel != nil {
  96. common.SysError(fmt.Sprintf("渠道不存在:%d", channel.Id))
  97. message = "数据库一致性已被破坏,请联系管理员"
  98. }
  99. c.JSON(http.StatusServiceUnavailable, gin.H{
  100. "error": gin.H{
  101. "message": message,
  102. "type": "one_api_error",
  103. },
  104. })
  105. c.Abort()
  106. return
  107. }
  108. }
  109. c.Set("channel", channel.Type)
  110. c.Set("channel_id", channel.Id)
  111. c.Set("channel_name", channel.Name)
  112. c.Set("model_mapping", channel.ModelMapping)
  113. c.Request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", channel.Key))
  114. c.Set("base_url", channel.BaseURL)
  115. if channel.Type == common.ChannelTypeAzure || channel.Type == common.ChannelTypeXunfei {
  116. c.Set("api_version", channel.Other)
  117. }
  118. c.Next()
  119. }
  120. }