| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- package middleware
- import (
- "fmt"
- "github.com/gin-gonic/gin"
- "net/http"
- "one-api/common"
- "one-api/model"
- "strconv"
- "strings"
- )
- type ModelRequest struct {
- Model string `json:"model"`
- }
- func Distribute() func(c *gin.Context) {
- return func(c *gin.Context) {
- userId := c.GetInt("id")
- userGroup, _ := model.CacheGetUserGroup(userId)
- c.Set("group", userGroup)
- var channel *model.Channel
- channelId, ok := c.Get("channelId")
- if ok {
- id, err := strconv.Atoi(channelId.(string))
- if err != nil {
- c.JSON(http.StatusBadRequest, gin.H{
- "error": gin.H{
- "message": "无效的渠道 ID",
- "type": "one_api_error",
- },
- })
- c.Abort()
- return
- }
- channel, err = model.GetChannelById(id, true)
- if err != nil {
- c.JSON(http.StatusBadRequest, gin.H{
- "error": gin.H{
- "message": "无效的渠道 ID",
- "type": "one_api_error",
- },
- })
- c.Abort()
- return
- }
- if channel.Status != common.ChannelStatusEnabled {
- c.JSON(http.StatusForbidden, gin.H{
- "error": gin.H{
- "message": "该渠道已被禁用",
- "type": "one_api_error",
- },
- })
- c.Abort()
- return
- }
- } else {
- // Select a channel for the user
- var modelRequest ModelRequest
- err := common.UnmarshalBodyReusable(c, &modelRequest)
- if err != nil {
- c.JSON(http.StatusBadRequest, gin.H{
- "error": gin.H{
- "message": "无效的请求",
- "type": "one_api_error",
- },
- })
- c.Abort()
- return
- }
- if strings.HasPrefix(c.Request.URL.Path, "/v1/moderations") {
- if modelRequest.Model == "" {
- modelRequest.Model = "text-moderation-stable"
- }
- }
- channel, err = model.CacheGetRandomSatisfiedChannel(userGroup, modelRequest.Model)
- if err != nil {
- c.JSON(http.StatusServiceUnavailable, gin.H{
- "error": gin.H{
- "message": "无可用渠道",
- "type": "one_api_error",
- },
- })
- c.Abort()
- return
- }
- }
- c.Set("channel", channel.Type)
- c.Set("channel_id", channel.Id)
- c.Set("channel_name", channel.Name)
- c.Request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", channel.Key))
- c.Set("base_url", channel.BaseURL)
- if channel.Type == common.ChannelTypeAzure {
- c.Set("api_version", channel.Other)
- }
- c.Next()
- }
- }
|