Skip to content

Fix/gemini-fetch-models - #1517

Merged
Calcium-Ion merged 3 commits into
QuantumNous:alphafrom
RedwindA:fix/gemini-fetch-models
Aug 7, 2025
Merged

Fix/gemini-fetch-models#1517
Calcium-Ion merged 3 commits into
QuantumNous:alphafrom
RedwindA:fix/gemini-fetch-models

Conversation

@RedwindA

@RedwindA RedwindA commented Aug 6, 2025

Copy link
Copy Markdown
Contributor

PR 类型

  • Bug 修复
  • 新功能
  • 文档更新
  • 其他

PR 是否包含破坏性更新?

PR 描述

Close #1515

  • Gemini的openai models端点目前返回与OpenAI一致的响应结构,所以移除了多余的解析尝试;
图片
  • 目前在URL中使用key无效,改为使用与文档一致的Authheader;

  • 适配了多key渠道的获取模型列表。使用第一个key向上游鉴权。未实现错误回退机制。

修复效果

图片

Summary by CodeRabbit

  • Refactor

    • Simplified model fetching by standardizing authorization header usage and removing special handling for Google-compatible responses.
  • Chores

    • Removed unused types and variables related to prior Google-compatible response parsing.

@coderabbitai

coderabbitai Bot commented Aug 6, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

This change removes special-case parsing for Google-compatible model responses in the FetchUpstreamModels function. It deletes the associated types and logic, standardizes the use of authorization headers for Gemini requests, and simplifies the model fetching process by directly unmarshalling into the standard OpenAI response structure.

Changes

Cohort / File(s) Change Summary
Google-compatible Parsing Removal
controller/channel.go
Deleted GoogleOpenAICompatibleModels and GoogleOpenAICompatibleResponse types; removed logic for parsing and converting Google-compatible responses; streamlined model fetching to use only the standard OpenAI response structure.
Gemini Authorization Handling
controller/channel.go
Modified Gemini URL construction to exclude API key from query parameters; now extracts and trims the key, passing it via the authorization header instead of as a query parameter. Also applies this header logic to other channel types.
General Cleanup
controller/channel.go
Removed unused variables and code related to the deleted parsing logic; simplified function internals.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant FetchUpstreamModels
    participant UpstreamAPI

    Client->>FetchUpstreamModels: Request model list
    FetchUpstreamModels->>UpstreamAPI: GET /models (with Authorization header)
    UpstreamAPI-->>FetchUpstreamModels: Standard OpenAI models response
    FetchUpstreamModels-->>Client: Forward parsed models response
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

Poem

A rabbit hopped through code so bright,
Sweeping old types out of sight.
No more Google quirks to parse,
Just headers sent, response unmarred.
Simpler now, the models gleam—
A streamlined hop, a coder’s dream! 🐇✨

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between aaed890 and ed95a9f.

📒 Files selected for processing (1)
  • controller/channel.go (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • controller/channel.go
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (2)
controller/channel.go (2)

187-187: Clarify the comment to reflect the actual change.

The comment is contradictory - it mentions "key in url" but then says "we need to use AuthHeader". Since the code is now using authorization headers instead of URL parameters, the comment should be updated to reflect this change accurately.

-		url = fmt.Sprintf("%s/v1beta/openai/models", baseURL) // Remember key in url since we need to use AuthHeader
+		url = fmt.Sprintf("%s/v1beta/openai/models", baseURL) // Use AuthHeader instead of key in URL

197-201: Simplify redundant conditional logic.

Both the Gemini and non-Gemini branches now execute identical code. The conditional check is no longer necessary and can be simplified.

-	key := strings.Split(channel.Key, "\n")[0]
-	if channel.Type == constant.ChannelTypeGemini {
-		body, err = GetResponseBody("GET", url, channel, GetAuthHeader(key)) // Use AuthHeader since Gemini now forces it
-	} else {
-		body, err = GetResponseBody("GET", url, channel, GetAuthHeader(key))
-	}
+	key := strings.Split(channel.Key, "\n")[0]
+	body, err = GetResponseBody("GET", url, channel, GetAuthHeader(key))
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0c0caad and 76d71a0.

📒 Files selected for processing (1)
  • controller/channel.go (2 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: 9Ninety
PR: QuantumNous/new-api#1273
File: relay/channel/gemini/relay-gemini.go:97-116
Timestamp: 2025-06-21T03:37:41.726Z
Learning: In relay/channel/gemini/relay-gemini.go, the thinking budget calculation logic (including the MaxOutputTokens multiplication) was introduced in PR #1247. PR #1273 focused specifically on decoupling the thoughts summary feature from thinking budget settings and did not modify the existing thinking budget behavior.
Learnt from: antecanis8
PR: QuantumNous/new-api#1500
File: relay/channel/gemini/adaptor.go:0-0
Timestamp: 2025-08-04T14:36:34.528Z
Learning: In the Gemini API batchEmbedContents endpoint, each request item in the "requests" array should include its own "model" field, as shown in the official documentation at https://ai.google.dev/api/embeddings?hl=zh-cn#method:-models.batchembedcontents. The model field is not a top-level parameter.
Learnt from: neotf
PR: QuantumNous/new-api#1511
File: setting/ratio_setting/model_ratio.go:118-123
Timestamp: 2025-08-05T17:14:17.246Z
Learning: Claude models handle "-thinking" variants differently from Gemini models. For Claude models, only the base model (without "-thinking") gets an entry in defaultModelRatio map. The "-thinking" variants rely on the Claude relay handler stripping the suffix using strings.TrimSuffix(textRequest.Model, "-thinking") before looking up the ratio, so they automatically use the base model's ratio.
Learnt from: antecanis8
PR: QuantumNous/new-api#1500
File: relay/channel/gemini/adaptor.go:0-0
Timestamp: 2025-08-04T14:36:34.528Z
Learning: All currently serviced Gemini embedding models support the outputDimensionality parameter, not just specific models like text-embedding-004.
📚 Learning: claude models handle "-thinking" variants differently from gemini models. for claude models, only th...
Learnt from: neotf
PR: QuantumNous/new-api#1511
File: setting/ratio_setting/model_ratio.go:118-123
Timestamp: 2025-08-05T17:14:17.246Z
Learning: Claude models handle "-thinking" variants differently from Gemini models. For Claude models, only the base model (without "-thinking") gets an entry in defaultModelRatio map. The "-thinking" variants rely on the Claude relay handler stripping the suffix using strings.TrimSuffix(textRequest.Model, "-thinking") before looking up the ratio, so they automatically use the base model's ratio.

Applied to files:

  • controller/channel.go
🔇 Additional comments (1)
controller/channel.go (1)

208-214: LGTM! Excellent simplification of response parsing.

The removal of special-case parsing for Google-compatible responses simplifies the code significantly. Since the Gemini endpoint now returns OpenAI-compatible responses directly, this change eliminates unnecessary complexity while maintaining functionality.

Comment thread controller/channel.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants