feat: Add support for Vertex AI and Vertex express mode (#380)

This commit is contained in:
sasha-gitg 2025-05-16 08:06:43 -07:00 committed by GitHub
parent 7d818b46bc
commit 609757f911
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 64 additions and 19 deletions

View File

@ -8,19 +8,46 @@ For more comprehensive documentation, please see the [full documentation here](.
## Setup
1. **Get a Gemini API Key:** Obtain your API key from Google AI Studio: [https://aistudio.google.com/app/apikey](https://aistudio.google.com/app/apikey)
2. **Set Environment Variable:** Set the `GEMINI_API_KEY` environment variable to your obtained key. You can do this temporarily in your current shell session:
```bash
export GEMINI_API_KEY="YOUR_API_KEY"
```
Or add it to your shell's configuration file (like `~/.bashrc`, `~/.zshrc`, or `~/.profile`) for persistence:
```bash
echo 'export GEMINI_API_KEY="YOUR_API_KEY"' >> ~/.bashrc # Or your preferred shell config file
source ~/.bashrc # Reload the config
```
Replace `"YOUR_API_KEY"` with your actual key.
3. **Install the Gemini CLI:**
_(Instructions for installing the CLI will be added here once packaging is finalized. For now, if you have access to the source code, you can run it directly after building the project as described below.)_
The Gemini CLI supports several ways to authenticate with Google's AI services. You'll need to configure **one** of the following methods:
1. **Gemini API Key:**
- Obtain your API key from Google AI Studio: [https://aistudio.google.com/app/apikey](https://aistudio.google.com/app/apikey)
- Set the `GEMINI_API_KEY` environment variable. You can do this temporarily in your current shell session:
```bash
export GEMINI_API_KEY="YOUR_GEMINI_API_KEY"
```
Or add it to your shell's configuration file (like `~/.bashrc`, `~/.zshrc`, or `~/.profile`) for persistence:
```bash
echo 'export GEMINI_API_KEY="YOUR_GEMINI_API_KEY"' >> ~/.bashrc # Or your preferred shell config file
source ~/.bashrc # Reload the config
```
Replace `"YOUR_GEMINI_API_KEY"` with your actual key.
2. **Google API Key (Vertex AI Express Mode):**
- This key can be a general Google Cloud API key enabled for the Gemini API or Vertex AI.
- Set the `GOOGLE_API_KEY` and `GOOGLE_GENAI_USE_VERTEXAI` environment variables:
```bash
export GOOGLE_API_KEY="YOUR_GOOGLE_API_KEY"
export GOOGLE_GENAI_USE_VERTEXAI=true
```
3. **Vertex AI (Project and Location):**
- Ensure you have a Google Cloud Project and have enabled the Vertex AI API.
- Set up Application Default Credentials (ADC). For more details, refer to the [official Google Cloud ADC documentation](https://cloud.google.com/docs/authentication/provide-credentials-adc):
```bash
gcloud auth application-default login
```
- Set the `GOOGLE_CLOUD_PROJECT`, `GOOGLE_CLOUD_LOCATION`, and `GOOGLE_GENAI_USE_VERTEXAI` environment variables:
```bash
export GOOGLE_CLOUD_PROJECT="YOUR_PROJECT_ID"
export GOOGLE_CLOUD_LOCATION="YOUR_PROJECT_LOCATION" # e.g., us-central1
export GOOGLE_GENAI_USE_VERTEXAI=true
```
**Install the Gemini CLI:**
_(Instructions for installing the CLI will be added here once packaging is finalized. For now, if you have access to the source code, you can run it directly after building the project as described elsewhere.)_
## Building (for contributors)

View File

@ -354,11 +354,25 @@ export async function loadCliConfig(settings: Settings): Promise<Config> {
// Load .env file using logic from server package
loadEnvironment();
// Check API key (CLI responsibility)
if (!process.env.GEMINI_API_KEY) {
const geminiApiKey = process.env.GEMINI_API_KEY;
const googleApiKey = process.env.GOOGLE_API_KEY;
const googleCloudProject = process.env.GOOGLE_CLOUD_PROJECT;
const googleCloudLocation = process.env.GOOGLE_CLOUD_LOCATION;
const hasGeminiApiKey = !!geminiApiKey;
const hasGoogleApiKey = !!googleApiKey;
const hasVertexProjectLocationConfig =
!!googleCloudProject && !!googleCloudLocation;
if (!hasGeminiApiKey && !hasGoogleApiKey && !hasVertexProjectLocationConfig) {
logger.error(
'GEMINI_API_KEY is not set. See https://ai.google.dev/gemini-api/docs/api-key to obtain one. ' +
'Please set it in your .env file or as an environment variable.',
'No valid API authentication configuration found. Please set ONE of the following combinations in your environment variables or .env file:\n' +
'1. GEMINI_API_KEY (for Gemini API access).\n' +
'2. GOOGLE_API_KEY (for Gemini API or Vertex AI Express Mode access).\n' +
'3. GOOGLE_CLOUD_PROJECT and GOOGLE_CLOUD_LOCATION (for Vertex AI access).\n\n' +
'For Gemini API keys, visit: https://ai.google.dev/gemini-api/docs/api-key\n' +
'For Vertex AI authentication, visit: https://cloud.google.com/vertex-ai/docs/start/authentication\n' +
'The GOOGLE_GENAI_USE_VERTEXAI environment variable can also be set to true/false to influence service selection when ambiguity exists.',
);
process.exit(1);
}
@ -373,9 +387,12 @@ export async function loadCliConfig(settings: Settings): Promise<Config> {
const userAgent = await createUserAgent();
// Gemini Developer API or GCP Express or Vertex AI
const apiKeyForServer = geminiApiKey || googleApiKey || '';
// Create config using factory from server package
return createServerConfig(
process.env.GEMINI_API_KEY,
apiKeyForServer,
argv.model || DEFAULT_GEMINI_MODEL,
argv.sandbox ?? settings.sandbox ?? false,
process.cwd(),

View File

@ -35,8 +35,9 @@ export class GeminiClient {
constructor(private config: Config) {
const userAgent = config.getUserAgent();
const apiKeyFromConfig = config.getApiKey();
this.client = new GoogleGenAI({
apiKey: config.getApiKey(),
apiKey: apiKeyFromConfig === '' ? undefined : apiKeyFromConfig,
httpOptions: {
headers: {
'User-Agent': userAgent,