Cap Gemini max output tokens

This commit is contained in:
sususu98
2026-05-18 18:39:50 +08:00
parent cc0cb057b3
commit 1583cb4ef0
2 changed files with 113 additions and 0 deletions
@@ -13,6 +13,7 @@ import (
"strings"
"github.com/router-for-me/CLIProxyAPI/v7/internal/config"
"github.com/router-for-me/CLIProxyAPI/v7/internal/registry"
"github.com/router-for-me/CLIProxyAPI/v7/internal/runtime/executor/helps"
"github.com/router-for-me/CLIProxyAPI/v7/internal/thinking"
"github.com/router-for-me/CLIProxyAPI/v7/internal/util"
@@ -135,6 +136,7 @@ func (e *GeminiExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, r
requestPath := helps.PayloadRequestPath(opts)
body = helps.ApplyPayloadConfigWithRequest(e.cfg, baseModel, to.String(), from.String(), "", body, originalTranslated, requestedModel, requestPath, opts.Headers)
body, _ = sjson.SetBytes(body, "model", baseModel)
body = capGeminiMaxOutputTokens(body, baseModel)
action := "generateContent"
if req.Metadata != nil {
@@ -243,6 +245,7 @@ func (e *GeminiExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.A
requestPath := helps.PayloadRequestPath(opts)
body = helps.ApplyPayloadConfigWithRequest(e.cfg, baseModel, to.String(), from.String(), "", body, originalTranslated, requestedModel, requestPath, opts.Headers)
body, _ = sjson.SetBytes(body, "model", baseModel)
body = capGeminiMaxOutputTokens(body, baseModel)
baseURL := resolveGeminiBaseURL(auth)
url := fmt.Sprintf("%s/%s/models/%s:%s", baseURL, glAPIVersion, baseModel, "streamGenerateContent")
@@ -527,6 +530,26 @@ func applyGeminiHeaders(req *http.Request, auth *cliproxyauth.Auth) {
util.ApplyCustomHeadersFromAttrs(req, attrs)
}
func capGeminiMaxOutputTokens(body []byte, modelName string) []byte {
maxOut := gjson.GetBytes(body, "generationConfig.maxOutputTokens")
if !maxOut.Exists() || maxOut.Type != gjson.Number {
return body
}
modelInfo := registry.LookupModelInfo(modelName, "gemini")
if modelInfo == nil {
return body
}
limit := modelInfo.OutputTokenLimit
if limit <= 0 {
limit = modelInfo.MaxCompletionTokens
}
if limit <= 0 || maxOut.Int() <= int64(limit) {
return body
}
body, _ = sjson.SetBytes(body, "generationConfig.maxOutputTokens", limit)
return body
}
func fixGeminiImageAspectRatio(modelName string, rawJSON []byte) []byte {
if modelName == "gemini-2.5-flash-image-preview" {
aspectRatioResult := gjson.GetBytes(rawJSON, "generationConfig.imageConfig.aspectRatio")