2023-04-23 10:24:11 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-01-28 11:38:58 +00:00
|
|
|
"github.com/songquanpeng/one-api/common"
|
|
|
|
"github.com/songquanpeng/one-api/common/logger"
|
|
|
|
"github.com/songquanpeng/one-api/model"
|
2023-04-23 10:24:11 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2023-06-11 01:37:36 +00:00
|
|
|
"strings"
|
2023-07-15 04:03:23 +00:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-04-23 10:24:11 +00:00
|
|
|
)
|
|
|
|
|
2023-06-07 15:26:00 +00:00
|
|
|
type ModelRequest struct {
|
|
|
|
Model string `json:"model"`
|
|
|
|
}
|
|
|
|
|
2023-04-23 10:24:11 +00:00
|
|
|
func Distribute() func(c *gin.Context) {
|
|
|
|
return func(c *gin.Context) {
|
2023-06-11 03:08:16 +00:00
|
|
|
userId := c.GetInt("id")
|
2023-06-20 11:09:49 +00:00
|
|
|
userGroup, _ := model.CacheGetUserGroup(userId)
|
2023-06-11 03:08:16 +00:00
|
|
|
c.Set("group", userGroup)
|
2024-02-25 11:01:49 +00:00
|
|
|
var requestModel string
|
2023-04-23 10:24:11 +00:00
|
|
|
var channel *model.Channel
|
2024-02-25 11:01:49 +00:00
|
|
|
channelId, ok := c.Get("specific_channel_id")
|
2023-04-23 10:24:11 +00:00
|
|
|
if ok {
|
|
|
|
id, err := strconv.Atoi(channelId.(string))
|
|
|
|
if err != nil {
|
2023-10-03 06:19:03 +00:00
|
|
|
abortWithMessage(c, http.StatusBadRequest, "无效的渠道 Id")
|
2023-04-23 10:24:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
channel, err = model.GetChannelById(id, true)
|
|
|
|
if err != nil {
|
2023-10-03 06:19:03 +00:00
|
|
|
abortWithMessage(c, http.StatusBadRequest, "无效的渠道 Id")
|
2023-04-23 10:24:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if channel.Status != common.ChannelStatusEnabled {
|
2023-09-17 07:39:46 +00:00
|
|
|
abortWithMessage(c, http.StatusForbidden, "该渠道已被禁用")
|
2023-04-23 10:24:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Select a channel for the user
|
2023-06-07 15:26:00 +00:00
|
|
|
var modelRequest ModelRequest
|
2023-11-17 13:18:51 +00:00
|
|
|
err := common.UnmarshalBodyReusable(c, &modelRequest)
|
2023-06-07 15:26:00 +00:00
|
|
|
if err != nil {
|
2023-09-17 07:39:46 +00:00
|
|
|
abortWithMessage(c, http.StatusBadRequest, "无效的请求")
|
2023-06-07 15:26:00 +00:00
|
|
|
return
|
|
|
|
}
|
2023-06-11 01:37:36 +00:00
|
|
|
if strings.HasPrefix(c.Request.URL.Path, "/v1/moderations") {
|
|
|
|
if modelRequest.Model == "" {
|
|
|
|
modelRequest.Model = "text-moderation-stable"
|
|
|
|
}
|
|
|
|
}
|
2023-07-15 04:03:23 +00:00
|
|
|
if strings.HasSuffix(c.Request.URL.Path, "embeddings") {
|
|
|
|
if modelRequest.Model == "" {
|
|
|
|
modelRequest.Model = c.Param("model")
|
|
|
|
}
|
|
|
|
}
|
2023-07-15 04:30:06 +00:00
|
|
|
if strings.HasPrefix(c.Request.URL.Path, "/v1/images/generations") {
|
|
|
|
if modelRequest.Model == "" {
|
2023-11-17 13:18:51 +00:00
|
|
|
modelRequest.Model = "dall-e-2"
|
2023-07-15 04:30:06 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-17 13:18:51 +00:00
|
|
|
if strings.HasPrefix(c.Request.URL.Path, "/v1/audio/transcriptions") || strings.HasPrefix(c.Request.URL.Path, "/v1/audio/translations") {
|
2023-08-27 07:28:23 +00:00
|
|
|
if modelRequest.Model == "" {
|
|
|
|
modelRequest.Model = "whisper-1"
|
|
|
|
}
|
|
|
|
}
|
2024-02-25 11:01:49 +00:00
|
|
|
requestModel = modelRequest.Model
|
2023-06-20 11:09:49 +00:00
|
|
|
channel, err = model.CacheGetRandomSatisfiedChannel(userGroup, modelRequest.Model)
|
2023-04-23 10:24:11 +00:00
|
|
|
if err != nil {
|
2023-07-23 01:49:46 +00:00
|
|
|
message := fmt.Sprintf("当前分组 %s 下对于模型 %s 无可用渠道", userGroup, modelRequest.Model)
|
2023-06-29 03:27:34 +00:00
|
|
|
if channel != nil {
|
2024-01-21 15:21:42 +00:00
|
|
|
logger.SysError(fmt.Sprintf("渠道不存在:%d", channel.Id))
|
2023-06-29 03:27:34 +00:00
|
|
|
message = "数据库一致性已被破坏,请联系管理员"
|
|
|
|
}
|
2023-09-17 07:39:46 +00:00
|
|
|
abortWithMessage(c, http.StatusServiceUnavailable, message)
|
2023-04-23 10:24:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2024-02-25 11:01:49 +00:00
|
|
|
SetupContextForSelectedChannel(c, channel, requestModel)
|
2023-04-23 10:24:11 +00:00
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
}
|
2024-02-25 11:01:49 +00:00
|
|
|
|
|
|
|
func SetupContextForSelectedChannel(c *gin.Context, channel *model.Channel, modelName string) {
|
|
|
|
c.Set("channel", channel.Type)
|
|
|
|
c.Set("channel_id", channel.Id)
|
|
|
|
c.Set("channel_name", channel.Name)
|
|
|
|
c.Set("model_mapping", channel.GetModelMapping())
|
|
|
|
c.Set("original_model", modelName) // for retry
|
|
|
|
c.Request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", channel.Key))
|
|
|
|
c.Set("base_url", channel.GetBaseURL())
|
|
|
|
// this is for backward compatibility
|
|
|
|
switch channel.Type {
|
|
|
|
case common.ChannelTypeAzure:
|
|
|
|
c.Set(common.ConfigKeyAPIVersion, channel.Other)
|
|
|
|
case common.ChannelTypeXunfei:
|
|
|
|
c.Set(common.ConfigKeyAPIVersion, channel.Other)
|
|
|
|
case common.ChannelTypeGemini:
|
|
|
|
c.Set(common.ConfigKeyAPIVersion, channel.Other)
|
|
|
|
case common.ChannelTypeAIProxyLibrary:
|
|
|
|
c.Set(common.ConfigKeyLibraryID, channel.Other)
|
|
|
|
case common.ChannelTypeAli:
|
|
|
|
c.Set(common.ConfigKeyPlugin, channel.Other)
|
|
|
|
}
|
|
|
|
cfg, _ := channel.LoadConfig()
|
|
|
|
for k, v := range cfg {
|
|
|
|
c.Set(common.ConfigKeyPrefix+k, v)
|
|
|
|
}
|
|
|
|
}
|