MCPcopy
hub / github.com/QuantumNous/new-api / SetupContextForSelectedChannel

Function SetupContextForSelectedChannel

middleware/distributor.go:443–505  ·  view source on GitHub ↗
(c *gin.Context, channel *model.Channel, modelName string)

Source from the content-addressed store, hash-verified

441}
442
443func SetupContextForSelectedChannel(c *gin.Context, channel *model.Channel, modelName string) *types.NewAPIError {
444 c.Set("original_model", modelName) // for retry
445 if channel == nil {
446 return types.NewError(errors.New("channel is nil"), types.ErrorCodeGetChannelFailed, types.ErrOptionWithSkipRetry())
447 }
448 common.SetContextKey(c, constant.ContextKeyChannelId, channel.Id)
449 common.SetContextKey(c, constant.ContextKeyChannelName, channel.Name)
450 common.SetContextKey(c, constant.ContextKeyChannelType, channel.Type)
451 common.SetContextKey(c, constant.ContextKeyChannelCreateTime, channel.CreatedTime)
452 common.SetContextKey(c, constant.ContextKeyChannelSetting, channel.GetSetting())
453 common.SetContextKey(c, constant.ContextKeyChannelOtherSetting, channel.GetOtherSettings())
454 paramOverride := channel.GetParamOverride()
455 headerOverride := channel.GetHeaderOverride()
456 if mergedParam, applied := service.ApplyChannelAffinityOverrideTemplate(c, paramOverride); applied {
457 paramOverride = mergedParam
458 }
459 common.SetContextKey(c, constant.ContextKeyChannelParamOverride, paramOverride)
460 common.SetContextKey(c, constant.ContextKeyChannelHeaderOverride, headerOverride)
461 if nil != channel.OpenAIOrganization && *channel.OpenAIOrganization != "" {
462 common.SetContextKey(c, constant.ContextKeyChannelOrganization, *channel.OpenAIOrganization)
463 }
464 common.SetContextKey(c, constant.ContextKeyChannelAutoBan, channel.GetAutoBan())
465 common.SetContextKey(c, constant.ContextKeyChannelModelMapping, channel.GetModelMapping())
466 common.SetContextKey(c, constant.ContextKeyChannelStatusCodeMapping, channel.GetStatusCodeMapping())
467
468 key, index, newAPIError := channel.GetNextEnabledKey()
469 if newAPIError != nil {
470 return newAPIError
471 }
472 if channel.ChannelInfo.IsMultiKey {
473 common.SetContextKey(c, constant.ContextKeyChannelIsMultiKey, true)
474 common.SetContextKey(c, constant.ContextKeyChannelMultiKeyIndex, index)
475 } else {
476 // 必须设置为 false,否则在重试到单个 key 的时候会导致日志显示错误
477 common.SetContextKey(c, constant.ContextKeyChannelIsMultiKey, false)
478 }
479 // c.Request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", key))
480 common.SetContextKey(c, constant.ContextKeyChannelKey, key)
481 common.SetContextKey(c, constant.ContextKeyChannelBaseUrl, channel.GetBaseURL())
482
483 common.SetContextKey(c, constant.ContextKeySystemPromptOverride, false)
484
485 // TODO: api_version统一
486 switch channel.Type {
487 case constant.ChannelTypeAzure:
488 c.Set("api_version", channel.Other)
489 case constant.ChannelTypeVertexAi:
490 c.Set("region", channel.Other)
491 case constant.ChannelTypeXunfei:
492 c.Set("api_version", channel.Other)
493 case constant.ChannelTypeGemini:
494 c.Set("api_version", channel.Other)
495 case constant.ChannelTypeAli:
496 c.Set("plugin", channel.Other)
497 case constant.ChannelCloudflare:
498 c.Set("api_version", channel.Other)
499 case constant.ChannelTypeMokaAI:
500 c.Set("api_version", channel.Other)

Callers 4

testChannelFunction · 0.92
getChannelFunction · 0.92
RelayTaskFunction · 0.92
DistributeFunction · 0.85

Calls 14

NewErrorFunction · 0.92
ErrOptionWithSkipRetryFunction · 0.92
SetContextKeyFunction · 0.92
SetMethod · 0.80
GetOtherSettingsMethod · 0.80
GetParamOverrideMethod · 0.80
GetHeaderOverrideMethod · 0.80
GetAutoBanMethod · 0.80
GetModelMappingMethod · 0.80
GetStatusCodeMappingMethod · 0.80
GetNextEnabledKeyMethod · 0.80

Tested by

no test coverage detected