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:
model := googleai.Model("gemini-1.5-flash")
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:
text, err := ai.GenerateText(ctx, model, ai.WithTextPrompt("Tell me a joke."))
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 := ai.Embed(ctx, embeddingModel, ai.WithEmbedText(userInput))
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 := ai.Index(ctx, myIndexer, ai.WithIndexerDocs(docsToIndex...)); err != nil {
return err
}
retrieveRes, err := ai.Retrieve(ctx, myRetriever, ai.WithRetrieverText(userInput))
if err != nil {
return err
}
See Retrieval-augmented generation (RAG) for more information.