Add example scripts and README for Gemini API usage with ADC authentication
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
# Gemini API Python Examples (Gemini Enterprise Agent Platform (Vertex AI) / ADC Authentication)
|
||||
|
||||
This directory contains examples for interacting with the Gemini API using the **`google-genai`** Python SDK and the **`gemini-3.5-flash`** model.
|
||||
|
||||
To comply with enterprise security policies that disable static API keys, these examples use the **Gemini Enterprise Agent Platform** (Vertex AI) authenticated via secure **Application Default Credentials (ADC)**.
|
||||
|
||||
## Core Features
|
||||
|
||||
* **Gemini 3.5 Flash**: Offers frontier-level intelligence, advanced reasoning, and pro-level coding capability inside an ultra-fast, cost-efficient Flash tier.
|
||||
* **Agentic Capabilities**: Optimized out of the box for handling complex, multi-step agentic workflows and sustained reasoning tasks.
|
||||
* **Gemini Enterprise Agent Platform (Vertex AI) Integration**: Uses native Google Cloud IAM authorization instead of long-lived API keys.
|
||||
|
||||
---
|
||||
|
||||
## Prerequisites
|
||||
|
||||
1. **Google Cloud Project**: You need an active Google Cloud Project.
|
||||
2. **Gemini Enterprise Agent Platform (Vertex AI) API Enabled**: Make sure the API (`aiplatform.googleapis.com`) is enabled in your project.
|
||||
3. **IAM Permissions**: Your Google account (or service account) must have the **Gemini Enterprise Agent Platform User** (Vertex AI User) (`roles/aiplatform.user`) role.
|
||||
4. **Google Cloud SDK (`gcloud`)**: Installed and configured on your machine.
|
||||
|
||||
---
|
||||
|
||||
## Setup Guide
|
||||
|
||||
### 1. Install Dependencies
|
||||
Create a virtual environment (optional but recommended) and install the required dependencies:
|
||||
|
||||
```bash
|
||||
# Create and activate a virtual environment
|
||||
python3 -m venv venv
|
||||
source venv/bin/activate
|
||||
|
||||
# Install the modern google-genai library
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
### 2. Authenticate locally with Application Default Credentials (ADC)
|
||||
Generate local credentials that the SDK will automatically detect:
|
||||
|
||||
```bash
|
||||
gcloud auth application-default login
|
||||
```
|
||||
|
||||
### 3. Set Environment Variables
|
||||
Specify your Google Cloud project and region:
|
||||
|
||||
```bash
|
||||
# Set your GCP Project ID (Required)
|
||||
export GOOGLE_CLOUD_PROJECT="your-gcp-project-id"
|
||||
|
||||
# Set your preferred location (Optional, defaults to us-central1)
|
||||
export GOOGLE_CLOUD_LOCATION="us-central1"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Running the Examples
|
||||
|
||||
Run the script to verify the connection and see both standard content generation and config-based content generation in action:
|
||||
|
||||
```bash
|
||||
python call_gemini.py
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### "Gemini Enterprise Agent Platform (Vertex AI) API has not been used in project..."
|
||||
Enable the API on your Google Cloud Console or use the `gcloud` CLI:
|
||||
```bash
|
||||
gcloud services enable aiplatform.googleapis.com --project="your-gcp-project-id"
|
||||
```
|
||||
|
||||
### "Permission denied..." or "Lack of IAM permissions..."
|
||||
Ensure your authenticated user or active service account has the **Gemini Enterprise Agent Platform User** (Vertex AI User) role on your project:
|
||||
```bash
|
||||
gcloud projects add-iam-policy-binding your-gcp-project-id \
|
||||
--member="user:your-email@example.com" \
|
||||
--role="roles/aiplatform.user"
|
||||
```
|
||||
Reference in New Issue
Block a user