distributor.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. abortWithMessage(c, http.StatusBadRequest, "无效的渠道 ID")
  25. return
  26. }
  27. channel, err = model.GetChannelById(id, true)
  28. if err != nil {
  29. abortWithMessage(c, http.StatusBadRequest, "无效的渠道 ID")
  30. return
  31. }
  32. if channel.Status != common.ChannelStatusEnabled {
  33. abortWithMessage(c, http.StatusForbidden, "该渠道已被禁用")
  34. return
  35. }
  36. } else {
  37. // Select a channel for the user
  38. var modelRequest ModelRequest
  39. var err error
  40. if !strings.HasPrefix(c.Request.URL.Path, "/v1/audio") {
  41. err = common.UnmarshalBodyReusable(c, &modelRequest)
  42. }
  43. if err != nil {
  44. abortWithMessage(c, http.StatusBadRequest, "无效的请求")
  45. return
  46. }
  47. if strings.HasPrefix(c.Request.URL.Path, "/v1/moderations") {
  48. if modelRequest.Model == "" {
  49. modelRequest.Model = "text-moderation-stable"
  50. }
  51. }
  52. if strings.HasSuffix(c.Request.URL.Path, "embeddings") {
  53. if modelRequest.Model == "" {
  54. modelRequest.Model = c.Param("model")
  55. }
  56. }
  57. if strings.HasPrefix(c.Request.URL.Path, "/v1/images/generations") {
  58. if modelRequest.Model == "" {
  59. modelRequest.Model = "dall-e"
  60. }
  61. }
  62. if strings.HasPrefix(c.Request.URL.Path, "/v1/audio") {
  63. if modelRequest.Model == "" {
  64. modelRequest.Model = "whisper-1"
  65. }
  66. }
  67. channel, err = model.CacheGetRandomSatisfiedChannel(userGroup, modelRequest.Model)
  68. if err != nil {
  69. message := fmt.Sprintf("当前分组 %s 下对于模型 %s 无可用渠道", userGroup, modelRequest.Model)
  70. if channel != nil {
  71. common.SysError(fmt.Sprintf("渠道不存在:%d", channel.Id))
  72. message = "数据库一致性已被破坏,请联系管理员"
  73. }
  74. abortWithMessage(c, http.StatusServiceUnavailable, message)
  75. return
  76. }
  77. }
  78. c.Set("channel", channel.Type)
  79. c.Set("channel_id", channel.Id)
  80. c.Set("channel_name", channel.Name)
  81. c.Set("model_mapping", channel.ModelMapping)
  82. c.Request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", channel.Key))
  83. c.Set("base_url", channel.BaseURL)
  84. switch channel.Type {
  85. case common.ChannelTypeAzure:
  86. c.Set("api_version", channel.Other)
  87. case common.ChannelTypeXunfei:
  88. c.Set("api_version", channel.Other)
  89. case common.ChannelTypeAIProxyLibrary:
  90. c.Set("library_id", channel.Other)
  91. }
  92. c.Next()
  93. }
  94. }