Gemini API Services
Gemini is Googleโs most advanced family of multimodal AI models, capable of understanding and reasoning across text, code, images, and video. This guide focuses on how to deploy and leverage Gemini API services in production environments.
Key Features
- Multimodal Capabilities: Process and generate content across different modalities (text, code, images, video)
- Reasoning: More advanced reasoning capabilities compared to previous models
- Security & Privacy: Enterprise-grade security and privacy controls
- API Integration: Simple REST and gRPC interfaces
- Models: Multiple model variants including Gemini Ultra, Gemini Pro, and Gemini Nano
Getting Started with Gemini API
Enabling the API with Terraform
resource "google_project_service" "gemini_api" {
service = "generativelanguage.googleapis.com"
disable_on_destroy = false
}
# Set up service account for Gemini API access
resource "google_service_account" "gemini_service_account" {
account_id = "gemini-api-sa"
display_name = "Gemini API Service Account"
depends_on = [google_project_service.gemini_api]
}
# Grant necessary permissions
resource "google_project_iam_member" "gemini_sa_role" {
project = var.project_id
role = "roles/aiplatform.user"
member = "serviceAccount:${google_service_account.gemini_service_account.email}"
}
# Create API key (for development only - use service accounts for production)
resource "google_api_keys_key" "gemini_api_key" {
name = "gemini-api-key"
display_name = "Gemini API Key"
restrictions {
api_targets {
service = "generativelanguage.googleapis.com"
}
}
}
Using gcloud CLI to Set Up Gemini API
# Enable the Gemini API
gcloud services enable generativelanguage.googleapis.com
# Create service account
gcloud iam service-accounts create gemini-api-sa \
--display-name="Gemini API Service Account"
# Grant permissions
gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \
--member="serviceAccount:gemini-api-sa@YOUR_PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/aiplatform.user"
# Create service account key (for application use)
gcloud iam service-accounts keys create gemini-sa-key.json \
--iam-account=gemini-api-sa@YOUR_PROJECT_ID.iam.gserviceaccount.com
Integration Examples
Python Application Integration
Create a file named gemini_client.py:
from google.cloud import aiplatform
from vertexai.preview.generative_models import GenerativeModel
import vertexai
def initialize_gemini_api(project_id, location):
"""Initialize Vertex AI with the specified project and region."""
vertexai.init(project=project_id, location=location)
def generate_content(prompt):
"""Generate content using the Gemini Pro model."""
model = GenerativeModel("gemini-pro")
response = model.generate_content(prompt)
return response.text
if __name__ == "__main__":
# Replace with your project ID and location
initialize_gemini_api("your-project-id", "us-central1")
# Example prompt
result = generate_content("Explain the architecture of a modern microservice application")
print(result)
Docker Containerized Application
Create a Dockerfile:
FROM python:3.10-slim
WORKDIR /app
# Copy requirements
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy the application code
COPY . .
# Set environment variable for authentication
ENV GOOGLE_APPLICATION_CREDENTIALS="/app/keys/gemini-sa-key.json"
# Create directory for service account key
RUN mkdir -p /app/keys
# Run the application
CMD ["python", "gemini_client.py"]
Create a requirements.txt file:
google-cloud-aiplatform>=1.36.0
vertexai>=0.0.1
Terraform Configuration for GKE Deployment
resource "google_container_cluster" "gemini_cluster" {
name = "gemini-app-cluster"
location = "us-central1"
# Remove the default node pool
remove_default_node_pool = true
initial_node_count = 1
}
resource "google_container_node_pool" "primary_preemptible_nodes" {
name = "gemini-node-pool"
location = "us-central1"
cluster = google_container_cluster.gemini_cluster.name
node_count = 3
node_config {
preemptible = true
machine_type = "e2-standard-4"
service_account = google_service_account.gemini_service_account.email
oauth_scopes = [
"https://www.googleapis.com/auth/cloud-platform"
]
}
}
resource "kubernetes_secret" "gemini_sa_key" {
metadata {
name = "gemini-sa-key"
}
data = {
"key.json" = base64decode(google_service_account_key.gemini_sa_key.private_key)
}
type = "Opaque"
depends_on = [google_container_node_pool.primary_preemptible_nodes]
}
resource "google_service_account_key" "gemini_sa_key" {
service_account_id = google_service_account.gemini_service_account.name
}
resource "kubernetes_deployment" "gemini_app" {
metadata {
name = "gemini-app"
labels = {
app = "gemini-app"
}
}
spec {
replicas = 2
selector {
match_labels = {
app = "gemini-app"
}
}
template {
metadata {
labels = {
app = "gemini-app"
}
}
spec {
container {
image = "gcr.io/${var.project_id}/gemini-app:latest"
name = "gemini-app"
volume_mount {
name = "gemini-sa-key-volume"
mount_path = "/app/keys"
read_only = true
}
}
volume {
name = "gemini-sa-key-volume"
secret {
secret_name = kubernetes_secret.gemini_sa_key.metadata[0].name
}
}
}
}
}
depends_on = [kubernetes_secret.gemini_sa_key]
}
Real-World Use Case: Customer Support Bot
This example showcases how to build a customer support bot using Gemini API and deploy it on Google Cloud Run:
Step 1: Create the Application
# app.py
import os
from flask import Flask, request, jsonify
from vertexai.preview.generative_models import GenerativeModel
import vertexai
import logging
app = Flask(__name__)
logging.basicConfig(level=logging.INFO)
# Initialize Vertex AI
vertexai.init(project=os.environ.get("GOOGLE_CLOUD_PROJECT"), location="us-central1")
model = GenerativeModel("gemini-pro")
@app.route('/api/support', methods=['POST'])
def support():
try:
data = request.get_json()
if not data or 'query' not in data:
return jsonify({"error": "Missing query parameter"}), 400
# Get product knowledge base context
product_context = get_product_knowledge(data.get('product', 'general'))
# Format prompt with context
prompt = f"""
You are a helpful customer support agent. Use the following product information to answer the customer's question.
Product information: {product_context}
Customer question: {data['query']}
Provide a concise, accurate and helpful response.
"""
# Generate response from Gemini
response = model.generate_content(prompt)
return jsonify({"response": response.text})
except Exception as e:
logging.error(f"Error generating response: {str(e)}")
return jsonify({"error": str(e)}), 500
def get_product_knowledge(product):
# In a real application, this would fetch knowledge from a database or CMS
knowledge_base = {
"cloud_storage": "Google Cloud Storage is a RESTful online file storage web service for storing and accessing data on Google Cloud Platform infrastructure.",
"compute_engine": "Google Compute Engine is a service that provides virtual machines running in Google's data centers.",
"general": "Google Cloud offers over 200 products spanning compute, storage, networking, databases, AI/ML, and more."
}
return knowledge_base.get(product, knowledge_base["general"])
if __name__ == '__main__':
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 8080)))
Step 2: Create a Dockerfile
FROM python:3.10-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
ENV PORT 8080
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 app:app
Step 3: Deploy with Terraform to Cloud Run
resource "google_project_service" "required_services" {
for_each = toset([
"artifactregistry.googleapis.com",
"run.googleapis.com",
"generativelanguage.googleapis.com"
])
service = each.key
disable_dependent_services = true
}
resource "google_service_account" "gemini_support_bot" {
account_id = "gemini-support-bot"
display_name = "Gemini Support Bot Service Account"
}
resource "google_project_iam_member" "gemini_bot_role" {
project = var.project_id
role = "roles/aiplatform.user"
member = "serviceAccount:${google_service_account.gemini_support_bot.email}"
}
resource "google_artifact_registry_repository" "gemini_repo" {
location = "us-central1"
repository_id = "gemini-support-bot"
description = "Docker repository for Gemini Support Bot"
format = "DOCKER"
depends_on = [google_project_service.required_services]
}
resource "google_cloud_run_v2_service" "gemini_support_service" {
name = "gemini-support-bot"
location = "us-central1"
template {
containers {
image = "us-central1-docker.pkg.dev/${var.project_id}/gemini-support-bot/support-bot:latest"
env {
name = "GOOGLE_CLOUD_PROJECT"
value = var.project_id
}
}
service_account = google_service_account.gemini_support_bot.email
}
depends_on = [
google_artifact_registry_repository.gemini_repo,
google_project_iam_member.gemini_bot_role
]
}
resource "google_cloud_run_service_iam_binding" "public_access" {
location = google_cloud_run_v2_service.gemini_support_service.location
service = google_cloud_run_v2_service.gemini_support_service.name
role = "roles/run.invoker"
members = ["allUsers"]
}
Best Practices
- Cost Management
- Use the appropriate model sizes for your use case (Ultra, Pro, Nano)
- Implement caching for common queries
- Monitor and set usage quotas
- Performance Optimization
- Use streaming responses for better user experience
- Optimize prompts to reduce token usage
- Use model compression for edge deployments
- Security Best Practices
- Use service accounts rather than API keys in production
- Implement rate limiting to prevent abuse
- Apply Data Loss Prevention (DLP) to filter sensitive information
- Responsible AI
- Implement human review for critical content generation
- Use AI Platformโs explainability features to understand model outputs
- Monitor for harmful outputs and implement safeguards
Troubleshooting
API Rate Limits
- Implement exponential backoff in your client code
- Request quota increases for production workloads
- Use batch processing for large workloads
Model Behavior
- Provide better context in your prompts
- Try different model parameters like temperature and top_k
- Use the Safety settings to adjust response filters
Authentication Issues
- Check service account permissions
- Verify environment variables are properly set
- Ensure API is enabled in your project