-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
做了较小的修正,使得Gemini完全支持多候选功能 #879
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Add support for OpenAI 'n' parameter to set candidate count in generationConfig.
Refactor response handling to support multiple candidates and improve structure.
Summary of ChangesHello @extremk, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! 此拉取请求旨在全面增强对Gemini多候选功能的兼容性。通过在请求转换时正确处理OpenAI的 Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
本次 PR 旨在为 Gemini 添加多候选(multi-candidate)功能,通过支持 OpenAI 的 n 参数来实现。您对请求和响应转换逻辑进行了相应的修改,以正确处理多个候选结果。
整体来看,代码修改是有效的,成功地实现了多候选功能。主要修改集中在 gemini_openai_response.go 文件中,通过循环处理 candidates 数组,为流式和非流式响应都生成了正确的 OpenAI 格式。请求转换部分也正确地将 n 参数映射到了 Gemini 的 candidateCount。
我提出了一些关于代码格式和参数初始化逻辑的建议,旨在提高代码的可读性和一致性。请查看具体的评论。
| // Candidate count (OpenAI 'n' parameter) | ||
| if n := gjson.GetBytes(rawJSON, "n"); n.Exists() && n.Type == gjson.Number { | ||
| if val := n.Int(); val > 1 { | ||
| out, _ = sjson.SetBytes(out, "request.generationConfig.candidateCount", val) | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
代码格式存在一些小问题:
- 第84行的注释
// Candidate count...有多余的缩进,应与下方的if语句对齐。 - 第90行是一个空行,但包含了多余的制表符(tab)。
建议修正这些格式问题以保持代码风格统一。
| // Candidate count (OpenAI 'n' parameter) | |
| if n := gjson.GetBytes(rawJSON, "n"); n.Exists() && n.Type == gjson.Number { | |
| if val := n.Int(); val > 1 { | |
| out, _ = sjson.SetBytes(out, "request.generationConfig.candidateCount", val) | |
| } | |
| } | |
| // Candidate count (OpenAI 'n' parameter) | |
| if n := gjson.GetBytes(rawJSON, "n"); n.Exists() && n.Type == gjson.Number { | |
| if val := n.Int(); val > 1 { | |
| out, _ = sjson.SetBytes(out, "request.generationConfig.candidateCount", val) | |
| } | |
| } | |
| // Candidate count (OpenAI 'n' parameter) | ||
| if n := gjson.GetBytes(rawJSON, "n"); n.Exists() && n.Type == gjson.Number { | ||
| if val := n.Int(); val > 1 { | ||
| out, _ = sjson.SetBytes(out, "generationConfig.candidateCount", val) | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
代码格式存在一些小问题:
- 第102行的注释
// Candidate count...有多余的缩进,应与下方的if语句对齐。 - 第108行是一个空行,但包含了多余的制表符(tab)。
建议修正这些格式问题以保持代码风格统一。
| // Candidate count (OpenAI 'n' parameter) | |
| if n := gjson.GetBytes(rawJSON, "n"); n.Exists() && n.Type == gjson.Number { | |
| if val := n.Int(); val > 1 { | |
| out, _ = sjson.SetBytes(out, "generationConfig.candidateCount", val) | |
| } | |
| } | |
| // Candidate count (OpenAI 'n' parameter) | |
| if n := gjson.GetBytes(rawJSON, "n"); n.Exists() && n.Type == gjson.Number { | |
| if val := n.Int(); val > 1 { | |
| out, _ = sjson.SetBytes(out, "generationConfig.candidateCount", val) | |
| } | |
| } | |
| if *param == nil { | ||
| *param = &convertGeminiResponseToOpenAIChatParams{ | ||
| UnixTimestamp: 0, | ||
| FunctionIndex: 0, | ||
| FunctionIndex: make(map[int]int), | ||
| } | ||
| } | ||
| // 确保 Map 已初始化 (针对旧的 param 可能的情况) | ||
| p := (*param).(*convertGeminiResponseToOpenAIChatParams) | ||
| if p.FunctionIndex == nil { | ||
| p.FunctionIndex = make(map[int]int) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这部分参数初始化的逻辑可以写得更简洁和符合 Go 的习惯用法。当前的实现中,对 p.FunctionIndex 的 nil 检查在 *param 初始化之后,显得有些分散。
建议将类型断言和初始化逻辑合并,这样代码更易读,也能一次性处理 *param 为 nil、类型不匹配或 FunctionIndex 未初始化的情况。
| if *param == nil { | |
| *param = &convertGeminiResponseToOpenAIChatParams{ | |
| UnixTimestamp: 0, | |
| FunctionIndex: 0, | |
| FunctionIndex: make(map[int]int), | |
| } | |
| } | |
| // 确保 Map 已初始化 (针对旧的 param 可能的情况) | |
| p := (*param).(*convertGeminiResponseToOpenAIChatParams) | |
| if p.FunctionIndex == nil { | |
| p.FunctionIndex = make(map[int]int) | |
| } | |
| p, ok := (*param).(*convertGeminiResponseToOpenAIChatParams) | |
| if !ok || p == nil { | |
| p = &convertGeminiResponseToOpenAIChatParams{ | |
| // UnixTimestamp 默认为 0 | |
| FunctionIndex: make(map[int]int), | |
| } | |
| *param = p | |
| } else if p.FunctionIndex == nil { | |
| // 处理 param 已存在但 map 未初始化的情况(为了兼容性) | |
| p.FunctionIndex = make(map[int]int) | |
| } |
…auto-update, request timeout, multi-candidate, auth persistence Applied features from upstream PRs: - PR router-for-me#878: Usage statistics persistence (SetPersistPath, LoadStatistics, SaveStatistics) - PR router-for-me#877: Codex plan type credential filename handling - PR router-for-me#868: Claude request cloaking utilities (obfuscation, fake user IDs) - PR router-for-me#715: Auto-update command for self-updating binary - PR router-for-me#860: Configurable request timeout with RequestTimeout config - PR router-for-me#879: Gemini multi-candidate support (n param -> candidateCount) - PR router-for-me#869: Auth token persistence for non-Google OAuth providers Fixed build issues: - Added applyPayloadConfig wrapper function - Added originalTranslated computation in claude/antigravity/codex executors - Fixed model registry hook methods - Removed duplicate SanitizeFunctionName - Fixed gemini_schema placeholder logic for nested object properties Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
Cherry-picked from upstream PR router-for-me#879 - Maps OpenAI 'n' parameter to generationConfig.candidateCount - Handles multiple candidates in streaming and non-streaming responses
|
请把代码中的所有中文注释修改成英文以后我再进行审核。 |
这次修改方案修改力度远低于以前方案,侵入性更低,原先逻辑保持得更好,且完全修正了多个渠道Gemini 不支持多候选功能,实测 Antigravity与Gemini CLI都得到修正
多候选功能 在某些场合非常有用,可以大幅度减少请求次数,节约Token与请求次数,提醒,此功能主要针对Gemini 2.5模型,Gemini 2.5系列支持到8,Qwen CLI渠道 支持到4,iFlow 渠道 Kimi支持5,其他模型大多数支持到1
所有的Gemini生图模型1,Gemini 3 预览版仅允许1,超过报错,但是 正式版可能会提高上限
Claude和百度不支持,会被忽略,视为1