fix: /v1/models support custom channel

closes #39
This commit is contained in:
Laisky.Cai 2025-03-12 02:11:04 +00:00
parent a0ea7268fe
commit 864c53e3e9

View File

@ -143,6 +143,7 @@ func ListAllModels(c *gin.Context) {
}) })
} }
// ListModels lists all models available to the user.
func ListModels(c *gin.Context) { func ListModels(c *gin.Context) {
userId := c.GetInt(ctxkey.Id) userId := c.GetInt(ctxkey.Id)
userGroup, err := model.CacheGetUserGroup(userId) userGroup, err := model.CacheGetUserGroup(userId)
@ -166,27 +167,37 @@ func ListModels(c *gin.Context) {
adaptor := relay.GetAdaptor(channeltype.ToAPIType(ability.ChannelType)) adaptor := relay.GetAdaptor(channeltype.ToAPIType(ability.ChannelType))
key := ability.Model + ":" + adaptor.GetChannelName() key := ability.Model + ":" + adaptor.GetChannelName()
abilityMap[key] = true abilityMap[key] = true
// for custom channels, store the model name only
if ability.ChannelType == channeltype.Custom {
abilityMap[ability.Model] = true
}
} }
// Filter models that match user's abilities with EXACT model+channel matches // Filter models that match user's abilities with EXACT model+channel matches
availableOpenAIModels := make([]OpenAIModels, 0) userAvailableModels := make([]OpenAIModels, 0)
fmt.Println(allModels)
// Only include models that have a matching model+channel combination // Only include models that have a matching model+channel combination
for _, model := range allModels { for _, model := range allModels {
key := model.Id + ":" + model.OwnedBy key := model.Id + ":" + model.OwnedBy
if abilityMap[key] { if abilityMap[key] {
availableOpenAIModels = append(availableOpenAIModels, model) userAvailableModels = append(userAvailableModels, model)
} else if abilityMap[model.Id] {
// for custom channels, store the model name only
userAvailableModels = append(userAvailableModels, model)
} }
} }
// Sort models alphabetically for consistent presentation // Sort models alphabetically for consistent presentation
sort.Slice(availableOpenAIModels, func(i, j int) bool { sort.Slice(userAvailableModels, func(i, j int) bool {
return availableOpenAIModels[i].Id < availableOpenAIModels[j].Id return userAvailableModels[i].Id < userAvailableModels[j].Id
}) })
c.JSON(200, gin.H{ c.JSON(200, gin.H{
"object": "list", "object": "list",
"data": availableOpenAIModels, "data": userAvailableModels,
}) })
} }
func RetrieveModel(c *gin.Context) { func RetrieveModel(c *gin.Context) {