(c *gin.Context)
| 106 | } |
| 107 | |
| 108 | func GeminiHelper(c *gin.Context) (newAPIError *types.NewAPIError) { |
| 109 | req, err := getAndValidateGeminiRequest(c) |
| 110 | if err != nil { |
| 111 | common.LogError(c, fmt.Sprintf("getAndValidateGeminiRequest error: %s", err.Error())) |
| 112 | return types.NewError(err, types.ErrorCodeInvalidRequest) |
| 113 | } |
| 114 | |
| 115 | relayInfo := relaycommon.GenRelayInfoGemini(c) |
| 116 | |
| 117 | // 检查 Gemini 流式模式 |
| 118 | checkGeminiStreamMode(c, relayInfo) |
| 119 | |
| 120 | if setting.ShouldCheckPromptSensitive() { |
| 121 | sensitiveWords, err := checkGeminiInputSensitive(req) |
| 122 | if err != nil { |
| 123 | common.LogWarn(c, fmt.Sprintf("user sensitive words detected: %s", strings.Join(sensitiveWords, ", "))) |
| 124 | return types.NewError(err, types.ErrorCodeSensitiveWordsDetected) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // model mapped 模型映射 |
| 129 | err = helper.ModelMappedHelper(c, relayInfo, req) |
| 130 | if err != nil { |
| 131 | return types.NewError(err, types.ErrorCodeChannelModelMappedError) |
| 132 | } |
| 133 | |
| 134 | if value, exists := c.Get("prompt_tokens"); exists { |
| 135 | promptTokens := value.(int) |
| 136 | relayInfo.SetPromptTokens(promptTokens) |
| 137 | } else { |
| 138 | promptTokens := getGeminiInputTokens(req, relayInfo) |
| 139 | c.Set("prompt_tokens", promptTokens) |
| 140 | } |
| 141 | |
| 142 | if model_setting.GetGeminiSettings().ThinkingAdapterEnabled { |
| 143 | if isNoThinkingRequest(req) { |
| 144 | // check is thinking |
| 145 | if !strings.Contains(relayInfo.OriginModelName, "-nothinking") { |
| 146 | // try to get no thinking model price |
| 147 | noThinkingModelName := relayInfo.OriginModelName + "-nothinking" |
| 148 | containPrice := helper.ContainPriceOrRatio(noThinkingModelName) |
| 149 | if containPrice { |
| 150 | relayInfo.OriginModelName = noThinkingModelName |
| 151 | relayInfo.UpstreamModelName = noThinkingModelName |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | if req.GenerationConfig.ThinkingConfig == nil { |
| 156 | gemini.ThinkingAdaptor(req, relayInfo) |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | priceData, err := helper.ModelPriceHelper(c, relayInfo, relayInfo.PromptTokens, int(req.GenerationConfig.MaxOutputTokens)) |
| 161 | if err != nil { |
| 162 | return types.NewError(err, types.ErrorCodeModelPriceError) |
| 163 | } |
| 164 | |
| 165 | // pre consume quota |
no test coverage detected