distributor.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. userGroup, _ := model.CacheGetUserGroup(userId)
  18. c.Set("group", userGroup)
  19. var channel *model.Channel
  20. channelId, ok := c.Get("channelId")
  21. if ok {
  22. id, err := strconv.Atoi(channelId.(string))
  23. if err != nil {
  24. c.JSON(http.StatusBadRequest, gin.H{
  25. "error": gin.H{
  26. "message": "无效的渠道 ID",
  27. "type": "one_api_error",
  28. },
  29. })
  30. c.Abort()
  31. return
  32. }
  33. channel, err = model.GetChannelById(id, true)
  34. if err != nil {
  35. c.JSON(http.StatusBadRequest, gin.H{
  36. "error": gin.H{
  37. "message": "无效的渠道 ID",
  38. "type": "one_api_error",
  39. },
  40. })
  41. c.Abort()
  42. return
  43. }
  44. if channel.Status != common.ChannelStatusEnabled {
  45. c.JSON(http.StatusForbidden, gin.H{
  46. "error": gin.H{
  47. "message": "该渠道已被禁用",
  48. "type": "one_api_error",
  49. },
  50. })
  51. c.Abort()
  52. return
  53. }
  54. } else {
  55. // Select a channel for the user
  56. var modelRequest ModelRequest
  57. if strings.HasPrefix(c.Request.URL.Path, "/mj") {
  58. if modelRequest.Model == "" {
  59. modelRequest.Model = "midjourney"
  60. }
  61. } else {
  62. err := common.UnmarshalBodyReusable(c, &modelRequest)
  63. if err != nil {
  64. log.Println(err)
  65. c.JSON(http.StatusBadRequest, gin.H{
  66. "error": gin.H{
  67. "message": "无效的请求",
  68. "type": "one_api_error",
  69. },
  70. })
  71. c.Abort()
  72. return
  73. }
  74. }
  75. if strings.HasPrefix(c.Request.URL.Path, "/v1/moderations") {
  76. if modelRequest.Model == "" {
  77. modelRequest.Model = "text-moderation-stable"
  78. }
  79. }
  80. if strings.HasSuffix(c.Request.URL.Path, "embeddings") {
  81. if modelRequest.Model == "" {
  82. modelRequest.Model = c.Param("model")
  83. }
  84. }
  85. if strings.HasPrefix(c.Request.URL.Path, "/v1/images/generations") {
  86. if modelRequest.Model == "" {
  87. modelRequest.Model = "dall-e"
  88. }
  89. }
  90. if strings.HasPrefix(c.Request.URL.Path, "/v1/audio") {
  91. if modelRequest.Model == "" {
  92. modelRequest.Model = "whisper-1"
  93. }
  94. }
  95. channel, err = model.CacheGetRandomSatisfiedChannel(userGroup, modelRequest.Model)
  96. if err != nil {
  97. message := fmt.Sprintf("当前分组 %s 下对于模型 %s 无可用渠道", userGroup, modelRequest.Model)
  98. if channel != nil {
  99. common.SysError(fmt.Sprintf("渠道不存在:%d", channel.Id))
  100. message = "数据库一致性已被破坏,请联系管理员"
  101. }
  102. c.JSON(http.StatusServiceUnavailable, gin.H{
  103. "error": gin.H{
  104. "message": message,
  105. "type": "one_api_error",
  106. },
  107. })
  108. c.Abort()
  109. return
  110. }
  111. }
  112. c.Set("channel", channel.Type)
  113. c.Set("channel_id", channel.Id)
  114. c.Set("channel_name", channel.Name)
  115. c.Set("model_mapping", channel.ModelMapping)
  116. c.Request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", channel.Key))
  117. c.Set("base_url", channel.BaseURL)
  118. switch channel.Type {
  119. case common.ChannelTypeAzure:
  120. c.Set("api_version", channel.Other)
  121. case common.ChannelTypeXunfei:
  122. c.Set("api_version", channel.Other)
  123. case common.ChannelTypeAIProxyLibrary:
  124. c.Set("library_id", channel.Other)
  125. }
  126. c.Next()
  127. }
  128. }