distributor.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. if modelRequest.Model == "" {
  60. modelRequest.Model = "midjourney"
  61. }
  62. } else {
  63. err := common.UnmarshalBodyReusable(c, &modelRequest)
  64. if err != nil {
  65. log.Println(err)
  66. c.JSON(http.StatusBadRequest, gin.H{
  67. "error": gin.H{
  68. "message": "无效的请求",
  69. "type": "one_api_error",
  70. },
  71. })
  72. c.Abort()
  73. return
  74. }
  75. }
  76. if strings.HasPrefix(c.Request.URL.Path, "/v1/moderations") {
  77. if modelRequest.Model == "" {
  78. modelRequest.Model = "text-moderation-stable"
  79. }
  80. }
  81. if strings.HasSuffix(c.Request.URL.Path, "embeddings") {
  82. if modelRequest.Model == "" {
  83. modelRequest.Model = c.Param("model")
  84. }
  85. }
  86. if strings.HasPrefix(c.Request.URL.Path, "/v1/images/generations") {
  87. if modelRequest.Model == "" {
  88. modelRequest.Model = "dall-e"
  89. }
  90. }
  91. if strings.HasPrefix(c.Request.URL.Path, "/v1/audio") {
  92. if modelRequest.Model == "" {
  93. modelRequest.Model = "whisper-1"
  94. }
  95. }
  96. channel, err := model.CacheGetRandomSatisfiedChannel(userGroup, modelRequest.Model)
  97. if err != nil {
  98. message := fmt.Sprintf("当前分组 %s 下对于模型 %s 无可用渠道", userGroup, modelRequest.Model)
  99. if channel != nil {
  100. common.SysError(fmt.Sprintf("渠道不存在:%d", channel.Id))
  101. message = "数据库一致性已被破坏,请联系管理员"
  102. }
  103. c.JSON(http.StatusServiceUnavailable, gin.H{
  104. "error": gin.H{
  105. "message": message,
  106. "type": "one_api_error",
  107. },
  108. })
  109. c.Abort()
  110. return
  111. }
  112. }
  113. c.Set("channel", channel.Type)
  114. c.Set("channel_id", channel.Id)
  115. c.Set("channel_name", channel.Name)
  116. c.Set("model_mapping", channel.ModelMapping)
  117. c.Request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", channel.Key))
  118. c.Set("base_url", channel.BaseURL)
  119. switch channel.Type {
  120. case common.ChannelTypeAzure:
  121. c.Set("api_version", channel.Other)
  122. case common.ChannelTypeXunfei:
  123. c.Set("api_version", channel.Other)
  124. case common.ChannelTypeAIProxyLibrary:
  125. c.Set("library_id", channel.Other)
  126. }
  127. c.Next()
  128. }
  129. }