Google Generative AI plugin

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

Configuration

To use this plugin, import the googleai package and call googleai.Init():

import "github.com/firebase/genkit/go/plugins/googleai"
if err := googleai.Init(ctx, nil); err != nil {
    return err
}

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 GOOGLE_GENAI_API_KEY environment variable to your API key.

  • Specify the API key when you initialize the plugin:

    if err := googleai.Init(ctx, &googleai.Config{APIKey: yourKey}); err != nil {
      return err
    }
    

    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.

Usage

Generative models

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

langModel := googleai.Model("gemini-1.5-pro")

The following models are supported: gemini-1.0-pro, gemini-1.5-pro, and gemini-1.5-flash.

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

genRes, err := langModel.Generate(ctx, ai.NewGenerateRequest(
    nil, ai.NewUserTextMessage("Tell me a joke.")), nil)
if err != nil {
    return err
}

See Generating content for more information.

Embedding models

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

embeddingModel := googleai.Embedder("text-embedding-004")

The following models are supported: text-embedding-004 and embedding-001.

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

embedRes, err := embeddingModel.Embed(ctx, &ai.EmbedRequest{
    Documents: []*ai.Document{ai.DocumentFromText(userInput, nil)},
})
if err != nil {
    return err
}

You can also pass an Embedder to an indexer's Index() method and a retriever's Retrieve() method:

if err := myIndexer.Index(ctx, &ai.IndexerRequest{Documents: docsToIndex}); err != nil {
    return err
}
retrieveRes, err := myRetriever.Retrieve(ctx, &ai.RetrieverRequest{
    Document: ai.DocumentFromText(userInput, nil),
})
if err != nil {
    return err
}

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