When trying to use the Google Gemini GenAI API, instead of actually creating text that responds to the code, I instead get passed code from the package itself.
(For context, I am using this in a Chrome extension using Plasmo).
This is the response I get:
()=>{
if (response.candidates && response.candidates.length > 0) {
if (response.candidates.length > 1) console.warn(`This response had ${response.candidates.length} ` + `candidates. Returning text from the first candidate only. ` + `Access response.candidates directly to use the other candidates.`);
if (hadBadFinishReason(response.candidates[0])) throw new GoogleGenerativeAIResponseError(`${formatBlockErrorMessage(response)}`, response);
return getText(response);
} else if (response.promptFeedback) throw new GoogleGenerativeAIResponseError(`Text not available. ${formatBlockErrorMessage(response)}`, response);
return "";
And this is the full class that makes use of the API.
require('dotenv').config();
const {GoogleGenerativeAI } = require("@google/generative-ai");
class AdviceLogic {
async getAdvice(pageText, question, mainPageUrl){
console.log("AL GA"+question+mainPageUrl+pageText);
if (pageText === undefined || pageText === null || pageText === "" || mainPageUrl === "" ){
return false;
}
else{
try{
const prompt = "Give the user 1-3 (max, if only 1 is necessary that's fine) CLICKABLE links EXCLUSIVELY from ones that are present on the current page and that are relevant to their request."
+ "with a short of what the link does based on their request and the page body. " +
"DO NOT, under any circumstance, include ANY links that are not present on the page. If there are no relevant links, please let the user know that there are no relevant links on the page. If there are no links at all, please let the user know that there are no links on the page."+
"If the user's request is not clear, please let the user know that the request is not clear. If the page content is not clear, please let the user know that the page content is not clear. If the page content is not relevant to the user's request, please let the user know that the page content is not relevant to the user's request."+
"Links will be automatically embedded if they follow the format of [Text](Link). Use 10 words at MOST for describing why to click on a link.";
pageText = pageText.replace(/href="/([^"]*)"/g, `href="${mainPageUrl}/$1"`);
console.log("1");
var queryPrompt = prompt + "This is the user's question: " + question + " This is the page content: " + pageText + "Give a clear and concise answer.";
console.log("2");
const result = await this.QueryAI(queryPrompt);
console.log("3");
console.log("response"+result.toString());
console.log("4");
return result;
}
catch(err){
console.log(err);
return false;
}
}
}
async QueryAI(question){
const genAI = new GoogleGenerativeAI(process.env.PLASMO_PUBLIC_GEMINI_API_KEY.toString());
// console.log("APIKEY"+process.env.PLASMO_PUBLIC_GEMINI_API_KEY);
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });
const result = await model.generateContent(question);
console.log("result"+result.response.text);
return result.response.text;
}
}
export default AdviceLogic;
I based my implementation based on the offficial documentation found here: https://ai.google.dev/gemini-api/docs#node.js
If someone could help me with this it’d be greatly appreciated!