Skip to content

Google Generative AI plugin

The Google Generative AI plugin provides interfaces to Google’s Gemini models through the Gemini API.

To use this plugin, import the googlegenai package and pass googlegenai.GoogleAI to WithPlugins() in the Genkit initializer:

import "github.com/firebase/genkit/go/plugins/googlegenai"
g := genkit.Init(context.Background(), genkit.WithPlugins(&googlegenai.GoogleAI{}))

The plugin requires an API key for the Gemini API, which you can get from Google AI Studio.

Configure the plugin to use your API key by doing one of the following:

  • Set the GEMINI_API_KEY environment variable to your API key.

  • Specify the API key when you initialize the plugin:

genkit.WithPlugins(&googlegenai.GoogleAI{APIKey: "YOUR_API_KEY"})

However, don’t embed your API key directly in code! Use this feature only in conjunction with a service like Cloud Secret Manager or similar.

Gemini 3+ Series - Latest models with state-of-the-art reasoning:

  • gemini-3.5-flash - Fast and efficient for most use cases
  • gemini-3.1-flash-image - Fast and efficient image generation
  • gemini-3.1-flash-image-preview - Preview of the flash image model
  • gemini-3.1-flash-lite-preview - Preview of the lightweight model
  • gemini-3-pro-image - Supports image generation outputs

Gemini 2.5 Series - Stable models with advanced reasoning and multimodal capabilities:

  • gemini-2.5-pro - Most advanced stable model for complex tasks, with deep reasoning and coding
  • gemini-2.5-flash - Fast and efficient for most use cases
  • gemini-2.5-flash-lite - Lightweight version for simple tasks

Gemini 2.0 Series:

  • gemini-2.0-flash - Fast and efficient
  • gemini-2.0-flash-exp - Experimental Flash model

To get a reference to a supported model, specify its identifier to googlegenai.GoogleAIModel:

model := googlegenai.GoogleAIModel(g, "gemini-flash-latest")

Alternatively, you may create a ModelRef which pairs the model name with its config:

modelRef := googlegenai.GoogleAIModelRef("gemini-flash-latest", &genai.GenerateContentConfig{
Temperature: genai.Ptr[float32](0.5),
MaxOutputTokens: genai.Ptr[int32](500),
// Other configuration...
})

Model references have a Generate() method that calls the Google API:

resp, err := genkit.Generate(ctx, g, ai.WithModel(modelRef), ai.WithPrompt("Tell me a joke."))
if err != nil {
return err
}
log.Println(resp.Text())

See Generating content with AI models for more information.

  • gemini-embedding-2 - Latest embedding model with 3072 dimensions; supports multimodal input (text, images, video).
  • text-embedding-004 - Stable text embedding model with 768 dimensions.
  • embedding-001 - Legacy text embedding model with 768 dimensions.

To get a reference to a supported embedding model, specify its identifier to googlegenai.GoogleAIEmbedder:

embeddingModel := googlegenai.GoogleAIEmbedder(g, "gemini-embedding-2")

Embedder references have an Embed() method that calls the Google AI API:

resp, err := genkit.Embed(ctx, g, ai.WithEmbedder(embeddingModel), ai.WithTextDocs(userInput))
if err != nil {
return err
}

See Retrieval-augmented generation (RAG) for more information.