I have integrated Gemini API in SAPUI5 app and the results are coming as well. I am able to get the response JSON when the API key is inserted and can see what models are available. But not able to send the prompt to the model and get the response. I am not sure how to use async and await in UI5, since UI5 by default calls functions in async
This is the code, which I am trying just a basic prompt. askGemini() is the function I am calling to fetch the result
<code>checkGoogleConnection: function() {
const vBox = this.getView().byId("vb1")
const APIBtn = this.getView().byId("checkGoogleConn");
const APIField = this.getView().byId("checkGoogleKey");
vBox.removeItem(APIBtn);
vBox.removeItem(APIField);
const gemKey = this.getView().byId("checkGoogleKey").getValue();
console.log(gemKey);
this.GOOGLE_API_KEY = gemKey;
console.log(this.GOOGLE_API_KEY);
// call Gemini AI API to check models.
const gen_ai = new GenAI.GoogleGenerativeAI(gemKey);
console.log(gen_ai);
const models = gen_ai.getGenerativeModel({ model: "gemini-1.5-flash"});
console.log(models);
var labelText = "";
if (models){
console.log("pass");
labelText = "available models: " + models.model;
}else{
console.log("fail");
labelText = "no models available right now!!";
}
const geminiConnSuccessInfoLabel = new InfoLabel("gcsil1", {
text: labelText,
colorScheme: 5
})
vBox.addItem(geminiConnSuccessInfoLabel);
},
// call Gemini and get resolution.
askGemini: function() {
sap.m.MessageToast.show("Fetching from Gemini!!");
const gen_ai2 = new GenAI.GoogleGenerativeAI(this.GOOGLE_API_KEY);
console.log(gen_ai2);
const models2 = gen_ai2.getGenerativeModel({ model: "gemini-1.5-flash"});
console.log(models2);
const prompt = "Write a story about an AI and magic"
const result = models2.generateContent(prompt);
const response = result.response;
//const text = response.text();
console.log(response);
//console.log(text);
},
</code>
<code>checkGoogleConnection: function() {
const vBox = this.getView().byId("vb1")
const APIBtn = this.getView().byId("checkGoogleConn");
const APIField = this.getView().byId("checkGoogleKey");
vBox.removeItem(APIBtn);
vBox.removeItem(APIField);
const gemKey = this.getView().byId("checkGoogleKey").getValue();
console.log(gemKey);
this.GOOGLE_API_KEY = gemKey;
console.log(this.GOOGLE_API_KEY);
// call Gemini AI API to check models.
const gen_ai = new GenAI.GoogleGenerativeAI(gemKey);
console.log(gen_ai);
const models = gen_ai.getGenerativeModel({ model: "gemini-1.5-flash"});
console.log(models);
var labelText = "";
if (models){
console.log("pass");
labelText = "available models: " + models.model;
}else{
console.log("fail");
labelText = "no models available right now!!";
}
const geminiConnSuccessInfoLabel = new InfoLabel("gcsil1", {
text: labelText,
colorScheme: 5
})
vBox.addItem(geminiConnSuccessInfoLabel);
},
// call Gemini and get resolution.
askGemini: function() {
sap.m.MessageToast.show("Fetching from Gemini!!");
const gen_ai2 = new GenAI.GoogleGenerativeAI(this.GOOGLE_API_KEY);
console.log(gen_ai2);
const models2 = gen_ai2.getGenerativeModel({ model: "gemini-1.5-flash"});
console.log(models2);
const prompt = "Write a story about an AI and magic"
const result = models2.generateContent(prompt);
const response = result.response;
//const text = response.text();
console.log(response);
//console.log(text);
},
</code>
checkGoogleConnection: function() {
const vBox = this.getView().byId("vb1")
const APIBtn = this.getView().byId("checkGoogleConn");
const APIField = this.getView().byId("checkGoogleKey");
vBox.removeItem(APIBtn);
vBox.removeItem(APIField);
const gemKey = this.getView().byId("checkGoogleKey").getValue();
console.log(gemKey);
this.GOOGLE_API_KEY = gemKey;
console.log(this.GOOGLE_API_KEY);
// call Gemini AI API to check models.
const gen_ai = new GenAI.GoogleGenerativeAI(gemKey);
console.log(gen_ai);
const models = gen_ai.getGenerativeModel({ model: "gemini-1.5-flash"});
console.log(models);
var labelText = "";
if (models){
console.log("pass");
labelText = "available models: " + models.model;
}else{
console.log("fail");
labelText = "no models available right now!!";
}
const geminiConnSuccessInfoLabel = new InfoLabel("gcsil1", {
text: labelText,
colorScheme: 5
})
vBox.addItem(geminiConnSuccessInfoLabel);
},
// call Gemini and get resolution.
askGemini: function() {
sap.m.MessageToast.show("Fetching from Gemini!!");
const gen_ai2 = new GenAI.GoogleGenerativeAI(this.GOOGLE_API_KEY);
console.log(gen_ai2);
const models2 = gen_ai2.getGenerativeModel({ model: "gemini-1.5-flash"});
console.log(models2);
const prompt = "Write a story about an AI and magic"
const result = models2.generateContent(prompt);
const response = result.response;
//const text = response.text();
console.log(response);
//console.log(text);
},
What is done wrong here? If I use async and await, errors are shown in console.
this is the app. The Ask button calls the function above.