The Google Generative AI plugin provides interfaces to Google's Gemini models through the Gemini API.
Installation
npm i --save @genkit-ai/googleai
Configuration
To use this plugin, specify it when you call configureGenkit()
:
import { googleAI } from '@genkit-ai/googleai';
export default configureGenkit({
plugins: [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
GOOGLE_GENAI_API_KEY
environment variable to your API key.Specify the API key when you initialize the plugin:
googleAI({ apiKey: yourKey });
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.
Some models (like Gemini 1.5 Pro) are in preview and only aviable via the
v1beta
API. You can specify the apiVersion
to get access to those models:
configureGenkit({
plugins: [googleAI({ apiVersion: 'v1beta' })],
});
or you can specify multiple versions if you'd like to use different versions of models at the same time.
configureGenkit({
plugins: [googleAI({ apiVersion: ['v1', 'v1beta'] })],
});
Usage
This plugin statically exports references to its supported models:
import {
gemini15Flash,
gemini15Pro,
textEmbeddingGecko001,
} from '@genkit-ai/googleai';
You can use these references to specify which model generate()
uses:
const llmResponse = await generate({
model: gemini15Flash,
prompt: 'Tell me a joke.',
});
or use embedders (ex. textEmbeddingGecko001
) with embed
or retrievers:
const embedding = await embed({
embedder: textEmbeddingGecko001,
content: input,
});
Gemini Files API
You can use files uploaded to the Gemini Files API with Genkit:
import { GoogleAIFileManager } from '@google/generative-ai/server';
const fileManager = new GoogleAIFileManager(process.env.GOOGLE_GENAI_API_KEY);
const uploadResult = await fileManager.uploadFile(
'path/to/file.jpg',
{
mimeType: 'image/jpeg',
displayName: 'Your Image',
}
);
const response = await generate({
model: gemini15Flash,
prompt: [
{text: 'Describe this image:'},
{media: {contentType: uploadResult.file.mimeType, url: uploadResult.file.uri}}
]
});
Fine-tuned models
You can use models fine-tuned with the Google Gemini API. Follow the instructions from the Gemini API or fine-tune a model using AI Studio.
The tuning process uses a base model—for example, Gemini 1.5 Flash—and your provided examples to create a new tuned model. Remember the base model you used, and copy the new model's ID.
When calling the tuned model in Genkit, use the base model as the model
parameter, and pass the tuned model's ID as part of the config
block. For example, if you used Gemini 1.5 Flash as the base model, and got the model ID tunedModels/my-example-model-apbm8oqbvuv2
you can call it with a block like the following:
const llmResponse = await generate({
prompt: `Suggest an item for the menu of fish themed restruant`,
model: gemini15Flash,
config: {
version: "tunedModels/my-example-model-apbm8oqbvuv2",
},
});