I followed the steps in the Genkit docs and deployed a Cloud function using the Gemini 1.5 Pro model. When I try to hit the function from a Flutter app however, I get this error:
Unhandled Exception: [firebase_functions/internal] [GoogleGenerativeAI Error]: Error fetching from https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:generateContent: [400 Bad Request] API key not valid. Please pass a valid API key. [{"@type":"type.googleapis.com/google.rpc.ErrorInfo","reason":"API_KEY_INVALID","domain":"googleapis.com","metadata":{"service":"generativelanguage.googleapis.com"}}]
I tried several ways to set the API key env variable.
I added it as an environment variable before deploying the cloud function:
export GOOGLE_GENAI_API_KEY=<your API key>
I also added my API key in the GCP Secret Manager and set it in a .env
file in my functions folder. Nothing seems to fix this error.
Here's my function:
import { generate } from "@genkit-ai/ai";
import { configureGenkit } from "@genkit-ai/core";
import { noAuth, onFlow } from "@genkit-ai/firebase/functions";
import { gemini15Pro } from "@genkit-ai/googleai";
import * as z from "zod";
import { firebase } from "@genkit-ai/firebase";
import { googleAI } from "@genkit-ai/googleai";
import { defineSecret } from "firebase-functions/params";
defineSecret("GOOGLE_GENAI_API_KEY");
configureGenkit({
plugins: [firebase(), googleAI({ apiVersion: "v1beta" })],
logLevel: "debug",
enableTracingAndMetrics: true,
});
const InventoryItem = z.object({
name: z.string().describe("the name of the item"),
description: z.string().describe("a brief description of the item"),
value: z.number().describe("the monetary value of the item in bottlecaps"),
funFact: z.string().optional().describe("a fun fact about the item"),
});
export const identifyItemFromUrl = onFlow(
{
name: "identifyItemFromUrl",
inputSchema: z.string().url(),
outputSchema: InventoryItem,
authPolicy: noAuth(),
},
async (subject) => {
const prompt = "Identify this item.";
const llmResponse = await generate({
model: gemini15Pro,
prompt: [
{ text: prompt },
{
media: {
url: "https://plus.unsplash.com/premium_photo-1699467558340-e04dd429a314?q=80&w=3870&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D",
contentType: "image/jpeg",
},
},
],
output: { schema: InventoryItem },
});
return llmResponse.output()!;
}
);