distributor.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. channelId, ok := c.Get("channelId")
  22. if ok {
  23. id, err := strconv.Atoi(channelId.(string))
  24. if err != nil {
  25. c.JSON(http.StatusBadRequest, gin.H{
  26. "error": gin.H{
  27. "message": "无效的渠道 ID",
  28. "type": "one_api_error",
  29. },
  30. })
  31. c.Abort()
  32. return
  33. }
  34. channel, err = model.GetChannelById(id, true)
  35. if err != nil {
  36. c.JSON(http.StatusBadRequest, gin.H{
  37. "error": gin.H{
  38. "message": "无效的渠道 ID",
  39. "type": "one_api_error",
  40. },
  41. })
  42. c.Abort()
  43. return
  44. }
  45. if channel.Status != common.ChannelStatusEnabled {
  46. c.JSON(http.StatusForbidden, gin.H{
  47. "error": gin.H{
  48. "message": "该渠道已被禁用",
  49. "type": "one_api_error",
  50. },
  51. })
  52. c.Abort()
  53. return
  54. }
  55. } else {
  56. // Select a channel for the user
  57. var modelRequest ModelRequest
  58. if strings.HasPrefix(c.Request.URL.Path, "/mj") {
  59. // Midjourney
  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. if strings.HasPrefix(c.Request.URL.Path, "/v1/audio") {
  93. if modelRequest.Model == "" {
  94. modelRequest.Model = "whisper-1"
  95. }
  96. }
  97. var err error
  98. channel, err = model.CacheGetRandomSatisfiedChannel(userGroup, modelRequest.Model)
  99. if err != nil {
  100. message := fmt.Sprintf("当前分组 %s 下对于模型 %s 无可用渠道", userGroup, modelRequest.Model)
  101. if channel != nil {
  102. common.SysError(fmt.Sprintf("渠道不存在:%d", channel.Id))
  103. message = "数据库一致性已被破坏,请联系管理员"
  104. }
  105. c.JSON(http.StatusServiceUnavailable, gin.H{
  106. "error": gin.H{
  107. "message": message,
  108. "type": "one_api_error",
  109. },
  110. })
  111. c.Abort()
  112. return
  113. }
  114. }
  115. //log.Printf("Using channel %v", channel)
  116. c.Set("channel", channel.Type)
  117. c.Set("channel_id", channel.Id)
  118. c.Set("channel_name", channel.Name)
  119. c.Set("model_mapping", channel.ModelMapping)
  120. c.Request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", channel.Key))
  121. c.Set("base_url", channel.BaseURL)
  122. switch channel.Type {
  123. case common.ChannelTypeAzure:
  124. c.Set("api_version", channel.Other)
  125. case common.ChannelTypeXunfei:
  126. c.Set("api_version", channel.Other)
  127. case common.ChannelTypeAIProxyLibrary:
  128. c.Set("library_id", channel.Other)
  129. }
  130. c.Next()
  131. }
  132. }