Repository navigation
feat(Gemini): implement thinking budget control in model name - #1231
Conversation
WalkthroughThe updates introduce support for Gemini model names that include a thinking budget parameter (e.g., "-thinking-128"), enhancing normalization and processing logic to recognize and handle these new naming conventions. Associated configuration, pricing ratio logic, and UI text are updated to reflect and support these changes, including new localization entries. Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant UI
participant Backend
participant GeminiAdaptor
participant GeminiRelay
User->>UI: Selects Gemini model with "-thinking-<budget>" suffix
UI->>Backend: Sends model selection and config
Backend->>GeminiAdaptor: Normalizes model name (handles "-thinking-<budget>")
GeminiAdaptor->>GeminiRelay: Passes normalized name and config
GeminiRelay->>GeminiRelay: Parses budget from name, validates, sets config
GeminiRelay->>Backend: Returns processed config
Backend->>UI: Updates with new settings/ratios
UI->>User: Displays updated settings and descriptions
Assessment against linked issues
Assessment against linked issues: Out-of-scope changesNo out-of-scope changes detected. Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
✨ Finishing Touches
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (3)
relay/channel/gemini/adaptor.go (1)
72-84: Edge-cases in-thinking-stripping logic
strings.Split(info.UpstreamModelName, "-thinking-")will always allocate the whole slice, yet you only need the prefix.- When
info.OriginModelNamedoes contain “-thinking-” butinfo.UpstreamModelNamedoesn’t (e.g. they were already normalised upstream),partswill have length 1 → you silently keep the original value; that’s fine but the extra allocation is wasted.- A simpler (& allocation-free) pattern is:
- if strings.Contains(info.OriginModelName, "-thinking-") { - parts := strings.Split(info.UpstreamModelName, "-thinking-") - info.UpstreamModelName = parts[0] + if idx := strings.Index(info.UpstreamModelName, "-thinking-"); idx != -1 { + info.UpstreamModelName = info.UpstreamModelName[:idx] }This avoids the intermediate slice and guards directly on the real string you mutate.
Minor, but worth it in a hot path that runs on every request.
relay/channel/gemini/relay-gemini.go (1)
67-101: Parsing logic works but is brittle; consider a tiny refactor‐
strings.SplitN()plusstrconv.Atoisucceeds only when the token right after “-thinking-” is a pure decimal string. Any trailing text (e.g. version suffix) silently disables thinking support becauseAtoifails.
‐ TheisNew25Procheck is duplicated three times in this file; extracting it into a small helper would cut repetition and future-proof the range rules.Optional compact refactor:
+var new25ProRegexp = regexp.MustCompile(`^gemini-2\.5-pro(?!-preview-(05-06|03-25))`) + +func parseThinkingBudget(model string) (int, bool) { + m := thinkingBudgetRegexp.FindStringSubmatch(model) // e.g. []{"-thinking-128","128"} + if len(m) != 2 { return 0, false } + budget, err := strconv.Atoi(m[1]) + if err != nil { return 0, false } + if new25ProRegexp.MatchString(model) { + return clamp(budget, pro25MinBudget, pro25MaxBudget), true + } + return clamp(budget, 0, flash25MaxBudget), true +}This reduces edge-case fragility and centralises the clamping logic.
🧰 Tools
🪛 Gitleaks (8.26.0)
81-81: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.
(generic-api-key)
82-82: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.
(generic-api-key)
83-83: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.
(generic-api-key)
setting/operation_setting/model-ratio.go (1)
353-361: Inline closure mutates outer variable – readability concern
handleThinkingBudgetModelcloses over and mutatesname. While correct, it costs a mental round-trip for readers (captured-by-reference semantics). A straightifchain is clearer and avoids needless reassignments under a read lock:- handleThinkingBudgetModel := func(prefix, wildcard string) { - if strings.HasPrefix(name, prefix) && strings.Contains(name, "-thinking-") { - name = wildcard - } - } - handleThinkingBudgetModel("gemini-2.5-flash", "gemini-2.5-flash-thinking-*") - handleThinkingBudgetModel("gemini-2.5-pro", "gemini-2.5-pro-thinking-*") + if strings.HasPrefix(name, "gemini-2.5-flash") && strings.Contains(name, "-thinking-") { + name = "gemini-2.5-flash-thinking-*" + } else if strings.HasPrefix(name, "gemini-2.5-pro") && strings.Contains(name, "-thinking-") { + name = "gemini-2.5-pro-thinking-*" + }Purely stylistic, but it improves traceability.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (5)
relay/channel/gemini/adaptor.go(1 hunks)relay/channel/gemini/relay-gemini.go(3 hunks)setting/operation_setting/model-ratio.go(3 hunks)web/src/i18n/locales/en.json(1 hunks)web/src/pages/Setting/Model/SettingGeminiModel.js(3 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
relay/channel/gemini/relay-gemini.go (2)
relay/channel/gemini/dto.go (1)
GeminiThinkingConfig(13-16)common/utils.go (1)
GetPointer(217-219)
🪛 Gitleaks (8.26.0)
relay/channel/gemini/relay-gemini.go
81-81: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.
(generic-api-key)
82-82: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.
(generic-api-key)
83-83: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.
(generic-api-key)
🔇 Additional comments (7)
web/src/pages/Setting/Model/SettingGeminiModel.js (3)
174-178: Missing i18n entry for the updated long descriptionThe Chinese literal passed to
t()has changed, but no corresponding key/value was added toen.json.
Result: English UI will now fall back to Chinese for this paragraph.Add the new sentence (full Chinese string) to
web/src/i18n/locales/en.jsonto keep localisation complete.
186-188: 👍 Extra helper text clearly explains supported suffixesThe new
extraTextprecisely lists all accepted patterns, improving UX.
208-215: Label & validator are in syncThe label change to “思考预算占比” together with the 0.1–1 range keeps validation coherent with backend expectations.
web/src/i18n/locales/en.json (1)
1375-1378: New translations look consistentKeys follow the existing “Chinese-key → English-value” convention and wording matches UI labels.
relay/channel/gemini/relay-gemini.go (1)
40-45: Constants clarify magic numbers—👍Introducing named constants for the various budget limits removes “magic numbers” that previously appeared multiple times in the code. This is a clear readability / maintainability win.
setting/operation_setting/model-ratio.go (2)
145-149: Good forward-compat defaultsAdding wildcard keys for thinking variants will stop pricing gaps as new budgets appear. Looks solid.
485-493: Ratio rule generalisation is clearRelaxing the preview-only check ensures official (non-preview) 2.5 models inherit the intended ratio—nice catch.
…in-name feat(Gemini): implement thinking budget control in model name
…ugh-worktree Support bulk editing for OpenAI passthrough
…in-name feat(Gemini): implement thinking budget control in model name
…in-name feat(Gemini): implement thinking budget control in model name
close #1183
gemini-2.5-pro-preview-06-05-thinking-128这种模型名格式控制思考预算,旧的比例规则仍然有效;Summary by CodeRabbit