I’ve uploaded a Firebase function (code below) which uses Google Gemini API to generate workouts. Then, I use Dart to call it but, even though I’ve allowed public access to call it, it throws this error:
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: [firebase_functions/internal] INTERNAL
#0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:648:7)
#1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:334:18)
<asynchronous suspension>
#2 MethodChannelHttpsCallable.call (package:cloud_functions_platform_interface/src/method_channel/method_channel_https_callable.dart:22:24)
<asynchronous suspension>
#3 HttpsCallable.call (package:cloud_functions/src/https_callable.dart:49:37)
<asynchronous suspension>
#4 new GenerateWorkoutBloc.<anonymous closure> (package:energi/logic/blocs/generate_workout/generate_workout_bloc.dart:50:26)
<asynchronous suspension>
#5 Bloc.on.<anonymous closure>.handleEvent (package:bloc/src/bloc.dart:229:13)
I’ve allowed public access otherwise it showed a Firebase UNAUTHENITCATED error. Now I don’t know what INTERNAL stands for, so I would love it if you could explain it to me.
Here is the function:
const functions = require("firebase-functions");
const { GoogleGenerativeAI } = require("@google/generative-ai");
const apiKey = "key";
const genAI = new GoogleGenerativeAI(apiKey);
const model = genAI.getGenerativeModel({
model: "gemini-1.5-pro",
});
const generationConfig = {
temperature: 1.7,
topP: 0.95,
topK: 64,
maxOutputTokens: 8192,
responseMimeType: "application/json",
};
exports.generateWorkout = functions.https.onRequest(async (req, res) => {
try {
console.log('starting');
const chatSession = model.generateContent({
generationConfig,
history: [
{
role: "user",
parts: [
{text: "You are a professional...."},
],
},
{
role: "model",
parts: [
{text: "```json....`"},
],
},
],
});
const result = (await chatSession).response;
const responseText = await result.response.text();
console.log(responseText);
res.status(200).send(responseText);
} catch (error) {
console.error("Error generating workout:", error);
res.status(500).send("Error generating workout");
}
});
And here is how I call it:
try {
HttpsCallable callable =
FirebaseFunctions.instance.httpsCallable('generateWorkout');
final response = await callable.call();
print(response);
} on FirebaseFunctionsException catch (error) {
print(error.toString());
} catch (e) {
print(e.toString());
}