mirror of
https://github.com/songquanpeng/one-api.git
synced 2026-01-01 12:35:58 +08:00
✨ Model list return by group
This commit is contained in:
@@ -39,6 +39,22 @@ func GetRandomSatisfiedChannel(group string, model string) (*Channel, error) {
|
||||
return &channel, err
|
||||
}
|
||||
|
||||
func GetGroupModels(group string) ([]string, error) {
|
||||
var models []string
|
||||
groupCol := "`group`"
|
||||
trueVal := "1"
|
||||
if common.UsingPostgreSQL {
|
||||
groupCol = `"group"`
|
||||
trueVal = "true"
|
||||
}
|
||||
|
||||
err := DB.Model(&Ability{}).Where(groupCol+" = ? and enabled = ? ", group, trueVal).Distinct("model").Pluck("model", &models).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return models, nil
|
||||
}
|
||||
|
||||
func (channel *Channel) AddAbilities() error {
|
||||
models_ := strings.Split(channel.Models, ",")
|
||||
groups_ := strings.Split(channel.Group, ",")
|
||||
|
||||
@@ -213,3 +213,22 @@ func CacheGetRandomSatisfiedChannel(group string, model string) (*Channel, error
|
||||
idx := rand.Intn(endIdx)
|
||||
return channels[idx], nil
|
||||
}
|
||||
|
||||
func CacheGetGroupModels(group string) ([]string, error) {
|
||||
if !common.MemoryCacheEnabled {
|
||||
return GetGroupModels(group)
|
||||
}
|
||||
channelSyncLock.RLock()
|
||||
defer channelSyncLock.RUnlock()
|
||||
|
||||
groupModels := group2model2channels[group]
|
||||
if groupModels == nil {
|
||||
return nil, errors.New("group not found")
|
||||
}
|
||||
|
||||
models := make([]string, 0)
|
||||
for model := range groupModels {
|
||||
models = append(models, model)
|
||||
}
|
||||
return models, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user