(c *gin.Context, channel *model.Channel, modelName string)
| 251 | } |
| 252 | |
| 253 | func SetupContextForSelectedChannel(c *gin.Context, channel *model.Channel, modelName string) *types.NewAPIError { |
| 254 | c.Set("original_model", modelName) // for retry |
| 255 | if channel == nil { |
| 256 | return types.NewError(errors.New("channel is nil"), types.ErrorCodeGetChannelFailed) |
| 257 | } |
| 258 | common.SetContextKey(c, constant.ContextKeyChannelId, channel.Id) |
| 259 | common.SetContextKey(c, constant.ContextKeyChannelName, channel.Name) |
| 260 | common.SetContextKey(c, constant.ContextKeyChannelType, channel.Type) |
| 261 | common.SetContextKey(c, constant.ContextKeyChannelCreateTime, channel.CreatedTime) |
| 262 | common.SetContextKey(c, constant.ContextKeyChannelSetting, channel.GetSetting()) |
| 263 | common.SetContextKey(c, constant.ContextKeyChannelParamOverride, channel.GetParamOverride()) |
| 264 | if nil != channel.OpenAIOrganization && *channel.OpenAIOrganization != "" { |
| 265 | common.SetContextKey(c, constant.ContextKeyChannelOrganization, *channel.OpenAIOrganization) |
| 266 | } |
| 267 | common.SetContextKey(c, constant.ContextKeyChannelAutoBan, channel.GetAutoBan()) |
| 268 | common.SetContextKey(c, constant.ContextKeyChannelModelMapping, channel.GetModelMapping()) |
| 269 | common.SetContextKey(c, constant.ContextKeyChannelStatusCodeMapping, channel.GetStatusCodeMapping()) |
| 270 | |
| 271 | key, index, newAPIError := channel.GetNextEnabledKey() |
| 272 | if newAPIError != nil { |
| 273 | return newAPIError |
| 274 | } |
| 275 | if channel.ChannelInfo.IsMultiKey { |
| 276 | common.SetContextKey(c, constant.ContextKeyChannelIsMultiKey, true) |
| 277 | common.SetContextKey(c, constant.ContextKeyChannelMultiKeyIndex, index) |
| 278 | } |
| 279 | // c.Request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", key)) |
| 280 | common.SetContextKey(c, constant.ContextKeyChannelKey, key) |
| 281 | common.SetContextKey(c, constant.ContextKeyChannelBaseUrl, channel.GetBaseURL()) |
| 282 | |
| 283 | // TODO: api_version统一 |
| 284 | switch channel.Type { |
| 285 | case constant.ChannelTypeAzure: |
| 286 | c.Set("api_version", channel.Other) |
| 287 | case constant.ChannelTypeVertexAi: |
| 288 | c.Set("region", channel.Other) |
| 289 | case constant.ChannelTypeXunfei: |
| 290 | c.Set("api_version", channel.Other) |
| 291 | case constant.ChannelTypeGemini: |
| 292 | c.Set("api_version", channel.Other) |
| 293 | case constant.ChannelTypeAli: |
| 294 | c.Set("plugin", channel.Other) |
| 295 | case constant.ChannelCloudflare: |
| 296 | c.Set("api_version", channel.Other) |
| 297 | case constant.ChannelTypeMokaAI: |
| 298 | c.Set("api_version", channel.Other) |
| 299 | case constant.ChannelTypeCoze: |
| 300 | c.Set("bot_id", channel.Other) |
| 301 | } |
| 302 | return nil |
| 303 | } |
| 304 | |
| 305 | // extractModelNameFromGeminiPath 从 Gemini API URL 路径中提取模型名 |
| 306 | // 输入格式: /v1beta/models/gemini-2.0-flash:generateContent |
no test coverage detected