聖誕特惠:結帳時使用優惠碼CHRISTMAS立享 20% 折扣!
Gemini API Best Practices: Building High-Performance AI Applications
返回部落格
Development

Gemini API Best Practices: Building High-Performance AI Applications

BananaImg Team
September 25, 2025
10 分鐘閱讀

Gemini API Best Practices: Building High-Performance AI Applications

Google Gemini API provides developers with powerful AI capabilities. This article shares practical experiences we've accumulated while developing the BananaImg platform.

Gemini API Overview

Model Selection Strategy

ModelUse CaseCostSpeed
Gemini ProComplex tasks, high-quality outputHighMedium
Gemini Pro VisionImage understanding and generationHighMedium
Gemini FlashFast response, simple tasksLowFast

Performance Optimization Tips

1. Request Batching

// Not recommended: Serial requests
for (const prompt of prompts) {
  const result = await generateImage(prompt);
  results.push(result);
}
 
// Recommended: Parallel requests
const results = await Promise.all(
  prompts.map(prompt => generateImage(prompt))
);

2. Smart Caching Strategy

class GeminiCache {
  constructor(ttl = 3600) {
    this.cache = new Map();
    this.ttl = ttl * 1000;
  }
 
  async get(key, generator) {
    const cached = this.cache.get(key);
 
    if (cached && Date.now() - cached.time < this.ttl) {
      return cached.value;
    }
 
    const value = await generator();
    this.cache.set(key, { value, time: Date.now() });
    return value;
  }
}

3. Stream Response Handling

async function* streamGeneration(prompt) {
  const stream = await gemini.generateContentStream(prompt);
 
  for await (const chunk of stream) {
    yield chunk.text();
  }
}
 
// Using stream response
for await (const text of streamGeneration(prompt)) {
  updateUI(text); // Real-time UI update
}

Cost Optimization Strategies

1. Token Usage Optimization

Reduce Input Tokens:

// Before optimization: Verbose prompt
const prompt = `
  Please generate an image of a cat.
  The cat should be sitting.
  The cat should be orange.
  The background should be a garden.
`;
 
// After optimization: Concise prompt
const prompt = "Orange cat sitting in garden";

2. Model Downgrade Strategy

async function intelligentGenerate(prompt, complexity) {
  // Select model based on task complexity
  const model = complexity > 0.7
    ? 'gemini-pro'
    : 'gemini-flash';
 
  return await gemini[model].generate(prompt);
}

3. Result Reuse

// Generate variations instead of regenerating
async function generateVariations(baseResult) {
  const variations = [];
  const seeds = [1, 2, 3, 4];
 
  for (const seed of seeds) {
    variations.push(
      modifyResult(baseResult, { seed })
    );
  }
 
  return variations;
}

Error Handling Best Practices

1. Retry Mechanism

async function robustGenerate(prompt, maxRetries = 3) {
  let lastError;
 
  for (let i = 0; i < maxRetries; i++) {
    try {
      return await gemini.generate(prompt);
    } catch (error) {
      lastError = error;
 
      if (error.code === 'RATE_LIMIT') {
        await sleep(Math.pow(2, i) * 1000); // Exponential backoff
      } else if (error.code === 'INVALID_PROMPT') {
        throw error; // Non-retryable error
      }
    }
  }
 
  throw lastError;
}

2. Fallback Strategy

async function generateWithFallback(prompt) {
  try {
    // Try primary model
    return await gemini.pro.generate(prompt);
  } catch (error) {
    console.warn('Primary model failed, using fallback');
 
    try {
      // Downgrade to backup model
      return await gemini.flash.generate(prompt);
    } catch (fallbackError) {
      // Return default response
      return getDefaultResponse();
    }
  }
}

Security Considerations

1. Content Filtering

class ContentFilter {
  constructor() {
    this.bannedWords = new Set([...]);
    this.sensitivePatterns = [...];
  }
 
  validate(prompt) {
    // Check banned words
    for (const word of this.bannedWords) {
      if (prompt.toLowerCase().includes(word)) {
        throw new Error('Inappropriate content detected');
      }
    }
 
    // Check sensitive patterns
    for (const pattern of this.sensitivePatterns) {
      if (pattern.test(prompt)) {
        return this.sanitize(prompt);
      }
    }
 
    return prompt;
  }
}

2. Rate Limiting

class RateLimiter {
  constructor(maxRequests = 60, window = 60000) {
    this.requests = [];
    this.maxRequests = maxRequests;
    this.window = window;
  }
 
  async acquire() {
    const now = Date.now();
 
    // Clean expired requests
    this.requests = this.requests.filter(
      time => now - time < this.window
    );
 
    if (this.requests.length >= this.maxRequests) {
      const oldestRequest = this.requests[0];
      const waitTime = this.window - (now - oldestRequest);
      await sleep(waitTime);
      return this.acquire();
    }
 
    this.requests.push(now);
  }
}

Monitoring and Debugging

1. Performance Tracking

class PerformanceMonitor {
  async track(operation, fn) {
    const start = performance.now();
    const result = await fn();
    const duration = performance.now() - start;
 
    this.log({
      operation,
      duration,
      timestamp: new Date(),
      success: true
    });
 
    return result;
  }
 
  getStatistics() {
    return {
      avgResponseTime: this.calculateAverage(),
      p95ResponseTime: this.calculatePercentile(95),
      successRate: this.calculateSuccessRate()
    };
  }
}

2. Logging

class GeminiLogger {
  log(level, message, metadata = {}) {
    const logEntry = {
      timestamp: new Date().toISOString(),
      level,
      message,
      ...metadata,
      environment: process.env.NODE_ENV
    };
 
    if (level === 'error') {
      this.sendToMonitoring(logEntry);
    }
 
    console.log(JSON.stringify(logEntry));
  }
}

User Experience Optimization

1. Progress Feedback

async function generateWithProgress(prompt, onProgress) {
  onProgress({ stage: 'validating', progress: 0 });
  await validatePrompt(prompt);
 
  onProgress({ stage: 'generating', progress: 30 });
  const result = await gemini.generate(prompt);
 
  onProgress({ stage: 'processing', progress: 70 });
  const processed = await postProcess(result);
 
  onProgress({ stage: 'complete', progress: 100 });
  return processed;
}

2. Predictive Loading

class PredictiveLoader {
  async preload(userBehavior) {
    const likelyPrompts = this.predictNextPrompts(userBehavior);
 
    // Warm up cache
    for (const prompt of likelyPrompts) {
      this.cache.warm(prompt);
    }
  }
}

Integration Best Practices

1. Environment Configuration

// config/gemini.js
export const geminiConfig = {
  development: {
    apiKey: process.env.GEMINI_DEV_KEY,
    model: 'gemini-flash',
    maxRetries: 5,
    timeout: 30000
  },
  production: {
    apiKey: process.env.GEMINI_PROD_KEY,
    model: 'gemini-pro',
    maxRetries: 3,
    timeout: 15000
  }
};

2. Dependency Injection

class GeminiService {
  constructor(config, cache, logger) {
    this.config = config;
    this.cache = cache;
    this.logger = logger;
    this.client = new GeminiClient(config);
  }
 
  async generate(prompt, options = {}) {
    const cacheKey = this.getCacheKey(prompt, options);
 
    return await this.cache.get(cacheKey, async () => {
      this.logger.log('info', 'Generating content', { prompt });
      return await this.client.generate(prompt, options);
    });
  }
}

Testing Strategies

1. Unit Testing

describe('GeminiService', () => {
  it('should cache repeated requests', async () => {
    const service = new GeminiService(mockConfig);
 
    const result1 = await service.generate('test prompt');
    const result2 = await service.generate('test prompt');
 
    expect(result1).toBe(result2);
    expect(mockClient.generate).toHaveBeenCalledTimes(1);
  });
});

2. Integration Testing

describe('Gemini Integration', () => {
  it('should handle rate limits gracefully', async () => {
    const promises = Array(100).fill(null).map(() =>
      service.generate('test')
    );
 
    const results = await Promise.allSettled(promises);
    const successful = results.filter(r => r.status === 'fulfilled');
 
    expect(successful.length).toBeGreaterThan(0);
  });
});

Summary

Mastering Gemini API best practices not only improves application performance but also significantly reduces operational costs. The key points are:

  1. Smart model selection
  2. Efficient caching strategies
  3. Robust error handling
  4. Detailed performance monitoring
  5. Excellent user experience

At BananaImg, we integrate these best practices into every corner of the platform, providing users with fast, stable, and high-quality AI image generation services.

Keep following our blog for more AI development practical experiences!

分享這篇文章

相關文章

Gemini API Best Practices: Building High-Performance AI Applications - BananaImg AI Blog | Nano Banana