Gemini

Overview

Gemini integration is implemented using the Google Generative AI SDK, specifically using the 'gemini-pro' model. The implementation provides chat functionality without function calling support.

Implementation

Provider Setup

import { GoogleGenerativeAI, GenerativeModel } from '@google/generative-ai';

class GeminiProvider implements LLMProvider {
  private model: GenerativeModel;

  constructor() {
    const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY!);
    this.model = genAI.getGenerativeModel({ model: 'gemini-pro' });
  }
}

Message Generation

async generateResponse(
  messages: Array<{ role: string; content: string }>, 
  cleanContent: string
): Promise<LLMResponse> {
  const chat = this.model.startChat({
    history: messages.slice(0, -1).map(msg => ({
      role: msg.role === 'assistant' ? 'model' : 'user',
      parts: [{ text: msg.content }],
    })),
  });

  const result = await chat.sendMessage(
    messages[messages.length - 1].content
  );
  const response = await result.response;

  return {
    content: response.text(),
    totalTokens: undefined,
    toolCalls: undefined
  };
}

Features

Supported Capabilities

  • Chat history management

  • Role-based messaging

  • Text generation

  • Error handling

Limitations

  • No function calling support

  • No token counting

  • No streaming support

Error Handling

Implementation

try {
  // Message generation logic
} catch (error) {
  console.error('Gemini Error:', error);
  throw new Error('Failed to generate Gemini response');
}

Common Errors

  • API key issues

  • Rate limiting

  • Invalid requests

  • Model availability

Best Practices

Configuration

  • Secure API key management

  • Model version selection

  • Error logging

  • Response validation

Message Handling

  • Proper role mapping

  • History management

  • Content validation

  • Error recovery

Last updated