feat(admin): 重构系统设置页面并新增渠道管理功能
- 将系统设置页面改为公开/私有双标签页结构 - 新增渠道协议、权重配置和加权随机选择算法 - 替换自定义模型为自定义渠道配置选项 - 实现渠道模型远程获取和在线测试功能 - 添加批量测试和抽屉式编辑界面 - 更新文档说明和数据表结构定义
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math/rand"
|
||||
"strings"
|
||||
|
||||
"github.com/basketikun/infinite-canvas/model"
|
||||
"github.com/basketikun/infinite-canvas/repository"
|
||||
)
|
||||
@@ -37,9 +41,54 @@ func normalizePrivateSetting(setting model.PrivateSetting) model.PrivateSetting
|
||||
setting.Channels = []model.ModelChannel{}
|
||||
}
|
||||
for i := range setting.Channels {
|
||||
if setting.Channels[i].Protocol == "" {
|
||||
setting.Channels[i].Protocol = "openai"
|
||||
}
|
||||
if setting.Channels[i].Models == nil {
|
||||
setting.Channels[i].Models = []string{}
|
||||
}
|
||||
if setting.Channels[i].Weight <= 0 {
|
||||
setting.Channels[i].Weight = 1
|
||||
}
|
||||
}
|
||||
return setting
|
||||
}
|
||||
|
||||
func SelectModelChannel(modelName string) (model.ModelChannel, error) {
|
||||
settings, err := repository.GetSettings()
|
||||
if err != nil {
|
||||
return model.ModelChannel{}, err
|
||||
}
|
||||
channels := modelChannelsForModel(normalizePrivateSetting(settings.Private).Channels, modelName)
|
||||
if len(channels) == 0 {
|
||||
return model.ModelChannel{}, errors.New("没有可用模型渠道")
|
||||
}
|
||||
total := 0
|
||||
for _, channel := range channels {
|
||||
total += channel.Weight
|
||||
}
|
||||
hit := rand.Intn(total)
|
||||
for _, channel := range channels {
|
||||
hit -= channel.Weight
|
||||
if hit < 0 {
|
||||
return channel, nil
|
||||
}
|
||||
}
|
||||
return channels[0], nil
|
||||
}
|
||||
|
||||
func modelChannelsForModel(channels []model.ModelChannel, modelName string) []model.ModelChannel {
|
||||
result := []model.ModelChannel{}
|
||||
for _, channel := range channels {
|
||||
if !channel.Enabled || channel.BaseURL == "" || channel.APIKey == "" {
|
||||
continue
|
||||
}
|
||||
for _, item := range channel.Models {
|
||||
if strings.TrimSpace(item) == modelName {
|
||||
result = append(result, channel)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user