MCPcopy Index your code
hub / github.com/aiprodcoder/MIXAPI / getRandomSatisfiedChannelWithTag

Function getRandomSatisfiedChannelWithTag

model/channel_cache.go:128–245  ·  view source on GitHub ↗

新增带标签过滤的渠道选择函数

(group string, model string, retry int, channelTag *string)

Source from the content-addressed store, hash-verified

126
127// 新增带标签过滤的渠道选择函数
128func getRandomSatisfiedChannelWithTag(group string, model string, retry int, channelTag *string) (*Channel, error) {
129 if strings.HasPrefix(model, "gpt-4-gizmo") {
130 model = "gpt-4-gizmo-*"
131 }
132 if strings.HasPrefix(model, "gpt-4o-gizmo") {
133 model = "gpt-4o-gizmo-*"
134 }
135
136 // if memory cache is disabled, get channel directly from database
137 if !common.MemoryCacheEnabled {
138 return GetRandomSatisfiedChannel(group, model, retry, channelTag)
139 }
140
141 channelSyncLock.RLock()
142 defer channelSyncLock.RUnlock()
143 channels := group2model2channels[group][model]
144
145 if len(channels) == 0 {
146 return nil, errors.New("channel not found")
147 }
148
149 // 过滤符合标签要求的渠道
150 var filteredChannels []int
151 for _, channelId := range channels {
152 if channel, ok := channelsIDM[channelId]; ok {
153 // 如果没有指定标签要求,则所有渠道都符合
154 if channelTag == nil || *channelTag == "" {
155 filteredChannels = append(filteredChannels, channelId)
156 } else {
157 // 如果指定了标签要求,则只选择匹配标签的渠道
158 channelTagStr := channel.GetTag()
159 if channelTagStr == *channelTag {
160 filteredChannels = append(filteredChannels, channelId)
161 }
162 }
163 }
164 }
165
166 // 如果没有符合标签要求的渠道,返回错误
167 if len(filteredChannels) == 0 {
168 if channelTag != nil && *channelTag != "" {
169 return nil, fmt.Errorf("没有找到标签为 '%s' 的可用渠道", *channelTag)
170 }
171 return nil, errors.New("channel not found")
172 }
173
174 if len(filteredChannels) == 1 {
175 if channel, ok := channelsIDM[filteredChannels[0]]; ok {
176 return channel, nil
177 }
178 return nil, fmt.Errorf("数据库一致性错误,渠道# %d 不存在,请联系管理员修复", filteredChannels[0])
179 }
180
181 uniquePriorities := make(map[int]bool)
182 for _, channelId := range filteredChannels {
183 if channel, ok := channelsIDM[channelId]; ok {
184 uniquePriorities[int(channel.GetPriority())] = true
185 } else {

Callers 1

Calls 5

GetRandomIntFunction · 0.92
GetTagMethod · 0.80
GetPriorityMethod · 0.80
GetWeightMethod · 0.80

Tested by

no test coverage detected