Incorporating Google Gemini into a project using an API

Hi everyone, I am hoping to incorporate Google Gemini using a personal API. I am trying to follow the steps listed here, but I am not sure where to apply the steps or which specific language steps I should follow. Thanks!

Ive looked at this and the rest api is the simplest method. You would use ajax in appstudio to access the api.

Good question!

@GaryGorsline’s answer looks good. We’ll do a bit more research into this.

Here is some sample code. You will need to get your own API key from Google to make it work.

When you generate the API Key, you will see warnings from Google that you need to keep it private. That can be a problem if you are putting this code on a publicly accessible website.

// Sample inspired by this doc:
https://ai.google.dev/tutorials/rest_quickstart

// cURL command converted to fetch format using this utility
https://www.scrapingbee.com/curl-converter/javascript-fetch/

// Get API key here: https://ai.google.dev/tutorials/setup
const GOOGLE_API = '...';

butAsk.onclick = async function () {
  labAnswer.text = "Retrieving...";
  res = await fetch(
    `https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=${GOOGLE_API}`,
    {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      // body: '{"contents": [{"parts":[{"text": "Write a story about a magic backpack."}]}]}',
      body: JSON.stringify({
        contents: [
          {
            parts: [
              {
                text: "Write a story about a magic backpack.",
              },
            ],
          },
        ],
      }),
    }
  );

  data = await res.json();
  console.log(data);
  labAnswer.text = data.candidates[0].content.parts[0].text;
};

Exactly. It’s pretty slick.

Thanks for noting the public privateized key. Not really sure how to hid it, unless you write your own cover api on another server to call the google api.

What if I want to have a continuous chat with Gemini? For example I want it to ask me quiz questions based on terms and definitions I give it. Then, I would reply with an answer. I would like to access its response to my answer and see if my answer was correct. Then I would like it to ask me another question and do the same process again.

You’ll need to look at the docs a bit more for that. I think this is the place…

Multi-turn conversations (chat)

(We’re not experts on Google Gemini)

Specifically, the streamGenerateContent endpoint is intended for chat sessions. It is way quicker than the generateContent endpoint in producing results, so it enhances the overall user experience.

Or are you considering a different style of exchange between party/parties and AI?

I am attempting to follow this tutorial: https://youtu.be/Z8F6FvMrN4o?si=LRpSOqt_gkHA7-6A&t=766. But I need to adjust it to work with AppStudio and I plan on using a button to call the function and getting input for the function from an input control. Where do I import the module from the library?

In that tutorial, he uses Node.js, which is a different way of working with AppStudio:
https://wiki.appstudio.dev/Using_Node_and_Electron_to_build_Desktop_Apps

Before you go to that, you might see if you can do what you need using the REST API, as we do in the sample.