fix: models api return models in deactivate channels

- Enhance logging functionality by adding context support and improving debugging options.
- Standardize function naming conventions across middleware to ensure consistency.
- Optimize data retrieval and handling in the model controller, including caching and error management.
- Simplify the bug report template to streamline the issue reporting process.
This commit is contained in:
Laisky.Cai
2025-02-26 11:22:03 +00:00
parent f5d4ff05dc
commit 5905a7f295
8 changed files with 119 additions and 61 deletions

View File

@@ -3,10 +3,11 @@ package controller
import (
"fmt"
"net/http"
"strings"
"sort"
"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common/ctxkey"
"github.com/songquanpeng/one-api/middleware"
"github.com/songquanpeng/one-api/model"
relay "github.com/songquanpeng/one-api/relay"
"github.com/songquanpeng/one-api/relay/adaptor/openai"
@@ -34,16 +35,20 @@ type OpenAIModelPermission struct {
}
type OpenAIModels struct {
Id string `json:"id"`
Object string `json:"object"`
Created int `json:"created"`
// Id model's name
//
// BUG: Different channels may have the same model name
Id string `json:"id"`
Object string `json:"object"`
Created int `json:"created"`
// OwnedBy is the channel's adaptor name
OwnedBy string `json:"owned_by"`
Permission []OpenAIModelPermission `json:"permission"`
Root string `json:"root"`
Parent *string `json:"parent"`
}
var models []OpenAIModels
var allModels []OpenAIModels
var modelsMap map[string]OpenAIModels
var channelId2Models map[int][]string
@@ -76,7 +81,7 @@ func init() {
channelName := adaptor.GetChannelName()
modelNames := adaptor.GetModelList()
for _, modelName := range modelNames {
models = append(models, OpenAIModels{
allModels = append(allModels, OpenAIModels{
Id: modelName,
Object: "model",
Created: 1626777600,
@@ -93,7 +98,7 @@ func init() {
}
channelName, channelModelList := openai.GetCompatibleChannelMeta(channelType)
for _, modelName := range channelModelList {
models = append(models, OpenAIModels{
allModels = append(allModels, OpenAIModels{
Id: modelName,
Object: "model",
Created: 1626777600,
@@ -105,7 +110,7 @@ func init() {
}
}
modelsMap = make(map[string]OpenAIModels)
for _, model := range models {
for _, model := range allModels {
modelsMap[model.Id] = model
}
channelId2Models = make(map[int][]string)
@@ -134,49 +139,56 @@ func DashboardListModels(c *gin.Context) {
func ListAllModels(c *gin.Context) {
c.JSON(200, gin.H{
"object": "list",
"data": models,
"data": allModels,
})
}
func ListModels(c *gin.Context) {
ctx := c.Request.Context()
var availableModels []string
if c.GetString(ctxkey.AvailableModels) != "" {
availableModels = strings.Split(c.GetString(ctxkey.AvailableModels), ",")
} else {
userId := c.GetInt(ctxkey.Id)
userGroup, _ := model.CacheGetUserGroup(userId)
availableModels, _ = model.CacheGetGroupModels(ctx, userGroup)
userId := c.GetInt(ctxkey.Id)
userGroup, err := model.CacheGetUserGroup(userId)
if err != nil {
middleware.AbortWithError(c, http.StatusBadRequest, err)
return
}
modelSet := make(map[string]bool)
for _, availableModel := range availableModels {
modelSet[availableModel] = true
// Get available models with their channel names
availableAbilities, err := model.GetGroupModelsV2(c.Request.Context(), userGroup)
if err != nil {
middleware.AbortWithError(c, http.StatusBadRequest, err)
return
}
// Create a map for quick lookup of enabled model+channel combinations
// Only store the exact model:channel combinations from abilities
abilityMap := make(map[string]bool)
for _, ability := range availableAbilities {
// Store as "modelName:channelName" for exact matching
adaptor := relay.GetAdaptor(channeltype.ToAPIType(ability.ChannelType))
key := ability.Model + ":" + adaptor.GetChannelName()
abilityMap[key] = true
}
// Filter models that match user's abilities with EXACT model+channel matches
availableOpenAIModels := make([]OpenAIModels, 0)
for _, model := range models {
if _, ok := modelSet[model.Id]; ok {
modelSet[model.Id] = false
// Only include models that have a matching model+channel combination
for _, model := range allModels {
key := model.Id + ":" + model.OwnedBy
if abilityMap[key] {
availableOpenAIModels = append(availableOpenAIModels, model)
}
}
for modelName, ok := range modelSet {
if ok {
availableOpenAIModels = append(availableOpenAIModels, OpenAIModels{
Id: modelName,
Object: "model",
Created: 1626777600,
OwnedBy: "custom",
Root: modelName,
Parent: nil,
})
}
}
// Sort models alphabetically for consistent presentation
sort.Slice(availableOpenAIModels, func(i, j int) bool {
return availableOpenAIModels[i].Id < availableOpenAIModels[j].Id
})
c.JSON(200, gin.H{
"object": "list",
"data": availableOpenAIModels,
})
}
func RetrieveModel(c *gin.Context) {
modelId := c.Param("model")
if model, ok := modelsMap[modelId]; ok {