Skip to content

feat: add configurable Gemini provider with VertexAI and GeminiAPI support#240

Merged
appleboy merged 1 commit intomainfrom
gemini
May 14, 2025
Merged

feat: add configurable Gemini provider with VertexAI and GeminiAPI support#240
appleboy merged 1 commit intomainfrom
gemini

Conversation

@appleboy
Copy link
Owner

@appleboy appleboy commented May 14, 2025

  • Add support for configuring Gemini provider with project, location, backend, and API key options
  • Register new Gemini-related flags in the config set command and bind them to viper
  • Update Gemini provider initialization to use new config values and allow fallback to OpenAI API key
  • Enhance Gemini client creation to support both VertexAI and GeminiAPI backends, including project and location when needed
  • Extend Gemini provider options with WithProject, WithLocation, and WithBackend functions
  • Improve config validation to require project and location for VertexAI backend

Summary by CodeRabbit

  • New Features

    • Added new configuration options for the Gemini provider: project, location, backend type, and API key.
    • Introduced command-line flags to set Gemini-specific configuration values.
  • Enhancements

    • Enabled support for multiple Gemini backend types with dynamic client configuration.
    • Improved validation to require project and location for VertexAI backend or API key for Gemini API backend.
    • Masked Gemini API key values in configuration listings for enhanced security.
  • Bug Fixes

    • Prioritized use of Gemini-specific API key with fallback to existing key when unset.

@coderabbitai
Copy link

coderabbitai bot commented May 14, 2025

Caution

Review failed

The pull request is closed.

"""

Walkthrough

This update introduces comprehensive support for Gemini provider configuration, adding new keys and command-line flags for project, location, backend type, and API key. The Gemini client initialization logic is updated to use these settings, supporting both Gemini API and VertexAI backends with appropriate validation and option setters throughout the provider code.

Changes

File(s) Change Summary
cmd/config_list.go Added four new Gemini-related keys (gemini.project, gemini.location, gemini.backend, gemini.api_key) with descriptions; masked gemini.api_key in config listing.
cmd/config_set.go Added new command-line flags and Viper bindings for Gemini provider configuration options: project, location, backend, and API key.
cmd/provider.go Updated NewGemini to read API key first from gemini.api_key with fallback to openai.api_key; passed backend, project, and location options to Gemini client constructor.
provider/gemini/gemini.go Modified client creation to support multiple backends (BackendGeminiAPI, BackendVertexAI); constructed client config dynamically based on backend type.
provider/gemini/options.go Extended config struct with project, location, and backend fields; added backend-specific validation requiring project/location or API key; introduced new option setters (WithBackend, WithProject, WithLocation); set default backend to BackendGeminiAPI.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CLI
    participant Viper
    participant Provider
    participant GeminiClient

    User->>CLI: Set Gemini config via flags
    CLI->>Viper: Bind flags to config keys
    User->>Provider: Request Gemini client
    Provider->>Viper: Read Gemini config (api_key, backend, project, location)
    Provider->>GeminiClient: Initialize with config options
    GeminiClient-->>Provider: Client instance (validated)
    Provider-->>User: Ready-to-use Gemini client
Loading

Possibly related PRs

Poem

In the warren of configs, new keys appear,
Gemini’s secrets now crystal clear.
Project and location, backend too,
API keys ready for what bunnies do!
With options expanded, we hop with delight—
Gemini’s journey is now set right.
🐰✨
"""

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (1.64.8)

Error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2
Failed executing command with error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2

Tip

⚡️ Faster reviews with caching
  • CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.

Enjoy the performance boost—your workflow just got faster.


📜 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 1ef006f and a2fa90a.

📒 Files selected for processing (5)
  • cmd/config_list.go (2 hunks)
  • cmd/config_set.go (2 hunks)
  • cmd/provider.go (1 hunks)
  • provider/gemini/gemini.go (1 hunks)
  • provider/gemini/options.go (2 hunks)
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 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.
    • @coderabbitai modularize this function.
  • 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.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need 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)

  • @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 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.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🧹 Nitpick comments (2)
cmd/config_set.go (2)

36-40: Consider standardizing flag definition style

The new Gemini flags are defined using String() while most other flags in this file use StringP() which provides short-form aliases. Also, the naming uses dot notation directly in flag names, unlike other flags which use underscores and rely on viper bindings for the namespace.

Consider updating for consistency:

- configSetCmd.Flags().String("gemini.project", "", availableKeys["gemini.project"])
- configSetCmd.Flags().String("gemini.location", "", availableKeys["gemini.location"])
- configSetCmd.Flags().String("gemini.backend", "BackendGeminiAPI", availableKeys["gemini.backend"])
- configSetCmd.Flags().String("gemini.api_key", "", availableKeys["gemini.api_key"])
+ configSetCmd.Flags().StringP("gemini_project", "", "", availableKeys["gemini.project"])
+ configSetCmd.Flags().StringP("gemini_location", "", "", availableKeys["gemini.location"])
+ configSetCmd.Flags().StringP("gemini_backend", "", "BackendGeminiAPI", availableKeys["gemini.backend"])
+ configSetCmd.Flags().StringP("gemini_api_key", "", "", availableKeys["gemini.api_key"])

Then update the bindings accordingly.


39-39: Consider extracting "BackendGeminiAPI" as a constant

The string literal "BackendGeminiAPI" appears to be an enum value or constant. For better maintainability, consider referencing a defined constant.

- configSetCmd.Flags().String("gemini.backend", "BackendGeminiAPI", availableKeys["gemini.backend"])
+ configSetCmd.Flags().String("gemini.backend", provider.BackendGeminiAPI, availableKeys["gemini.backend"])
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 2d74b54 and 613b931.

📒 Files selected for processing (5)
  • cmd/config_list.go (1 hunks)
  • cmd/config_set.go (2 hunks)
  • cmd/provider.go (1 hunks)
  • provider/gemini/gemini.go (1 hunks)
  • provider/gemini/options.go (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
cmd/provider.go (2)
provider/gemini/gemini.go (1)
  • New (143-194)
provider/gemini/options.go (8)
  • WithToken (40-44)
  • WithModel (47-51)
  • WithMaxTokens (56-63)
  • WithTemperature (69-76)
  • WithTopP (79-83)
  • WithBackend (118-129)
  • WithProject (131-135)
  • WithLocation (112-116)
🪛 GitHub Check: ubuntu-latest @ Go stable
provider/gemini/gemini.go

[failure] 163-163:
missing cases in switch of type genai.Backend: genai.BackendUnspecified (exhaustive)

🪛 GitHub Actions: Lint and Testing
provider/gemini/gemini.go

[error] 163-163: golangci-lint: missing cases in switch of type genai.Backend: genai.BackendUnspecified (exhaustive)

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Analyze (go)
🔇 Additional comments (6)
cmd/provider.go (2)

38-41: Good implementation of fallback mechanism for API keys

The code now properly checks for a Gemini-specific API key first before falling back to the OpenAI key, which provides better isolation between provider configurations.


44-52: Backend configuration options added appropriately

The configuration now correctly passes Gemini-specific options including backend type, project, and location to support both VertexAI and GeminiAPI backends.

provider/gemini/options.go (3)

91-94: Backend configuration fields added appropriately

The configuration struct now properly includes fields for project, location, and backend to support the new configuration options.


97-105: Backend-specific validation logic looks good

The validation correctly implements different requirements based on the selected backend, requiring project and location for VertexAI and API token for GeminiAPI.


144-144: Default backend selection looks good

Setting the default backend to BackendGeminiAPI is appropriate for backward compatibility.

cmd/config_set.go (1)

61-64: Bindings for new Gemini configuration look good

The bindings correctly map the flag values to the corresponding Viper configuration keys, which aligns with the PR objective of enhancing Gemini provider configuration.

Comment on lines +118 to +131
func WithBackend(val string) Option {
return optionFunc(func(c *config) {
switch val {
case "BackendVertexAI":
c.backend = genai.BackendVertexAI
case "BackendGeminiAPI":
c.backend = genai.BackendGeminiAPI
default:
c.backend = genai.BackendGeminiAPI
}
})
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add handling for BackendUnspecified in WithBackend

Similar to the issue in gemini.go, the WithBackend function should handle the BackendUnspecified value to maintain consistency with the enumeration type.

func WithBackend(val string) Option {
	return optionFunc(func(c *config) {
		switch val {
		case "BackendVertexAI":
			c.backend = genai.BackendVertexAI
		case "BackendGeminiAPI":
			c.backend = genai.BackendGeminiAPI
+		case "BackendUnspecified":
+			c.backend = genai.BackendUnspecified
		default:
			c.backend = genai.BackendGeminiAPI
		}
	})
}

Additionally, consider using string constants for these backend identifiers to avoid typos and improve maintainability.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func WithBackend(val string) Option {
return optionFunc(func(c *config) {
switch val {
case "BackendVertexAI":
c.backend = genai.BackendVertexAI
case "BackendGeminiAPI":
c.backend = genai.BackendGeminiAPI
default:
c.backend = genai.BackendGeminiAPI
}
})
}
func WithBackend(val string) Option {
return optionFunc(func(c *config) {
switch val {
case "BackendVertexAI":
c.backend = genai.BackendVertexAI
case "BackendGeminiAPI":
c.backend = genai.BackendGeminiAPI
case "BackendUnspecified":
c.backend = genai.BackendUnspecified
default:
c.backend = genai.BackendGeminiAPI
}
})
}

…pport

- Add support for Gemini provider configuration, including project, location, backend, and API key options
- Ensure gemini.api_key is treated as sensitive and hidden in config listing
- Allow Gemini client to fall back to openai.api_key if gemini.api_key is not set
- Pass Gemini backend, project, and location settings to the Gemini client
- Refactor Gemini client initialization to support both VertexAI and GeminiAPI backends with appropriate configuration
- Add validation for required project and location fields when using the VertexAI backend
- Introduce new option functions: WithProject, WithLocation, and WithBackend for flexible Gemini configuration

Signed-off-by: Bo-Yi Wu <[email protected]>
@appleboy appleboy merged commit 39afdb6 into main May 14, 2025
14 checks passed
@appleboy appleboy deleted the gemini branch May 14, 2025 06:59
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.

1 participant