Adding Gemini to the hobby project

Visakh Vijayan
3 min readMar 8, 2024

--

Gemini has been the buzzword out there. And with the APIs becoming available now, it has become even more exciting. You can get started on it by visiting the Google AI Studio.

What we are doing

Today we are going to leverage Gemini to generate a random quote for us so that we can add that to our hobby project of QuotesGen. Currently, APINinja drives the quotes, so we will add Gemini as an alternative. The idea is to pick a random vendor amongst APINinja or Gemini. You can find the source code over here.

  1. Visit the AI Studio and Create an API Key. This will authenticate your requests. We are allowed 60 requests per minute for free.
  2. AI is all about prompts (inputs) given to a model. You have the option to choose between a chat, freeform, or structured prompt to create. We will use the freeform one since it is more flexible and fulfills our needs.
  3. We will keep it simple and choose the Gemini 1.0 Pro model itself since we need only text responses. For images, we can use the vision model.
  4. What we need is a motivational quote and that is exactly what we type into the prompt. Once done Run it and check if you are getting the response in the desired output. We want the output as a JSON response so that our project can use it easily.
Give me a motivational quote in the form of json
{"quote":"<quote>","author":"<author>"}

5. On the top right you will be able to get the code in your desired language as well. It doesn’t get any better than this. Right?

Once done, we will open up our project and add this beauty to it. The code looks something like this —

import { QuoteResponse } from "../types/common";

const { GoogleGenerativeAI, HarmCategory, HarmBlockThreshold } = require("@google/generative-ai");

const MODEL_NAME = 'gemini-1.0-pro';
const API_KEY = process.env.REACT_APP_GEMINI_API_KEY;

export const getQuoteFromGemini = async (): Promise<QuoteResponse> => {
const genAI = new GoogleGenerativeAI(API_KEY);
const model = genAI.getGenerativeModel({ model: MODEL_NAME });

const generationConfig = {
temperature: 0.9,
topK: 1,
topP: 1,
maxOutputTokens: 2048,
};

const safetySettings = [
{
category: HarmCategory.HARM_CATEGORY_HARASSMENT,
threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
},
{
category: HarmCategory.HARM_CATEGORY_HATE_SPEECH,
threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
},
{
category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
},
{
category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
},
];

const parts = [
{text: "Give me a motivational quote in the form of json\n {\"quote\":\"<quote>\",\"author\":\"<author>\"}"},
];

const result = await model.generateContent({
contents: [{ role: "user", parts }],
generationConfig,
safetySettings,
});

const response = result.response;
return JSON.parse(response.text()) as QuoteResponse;
}

The const parts is where our prompt resides. Once the code runs it will generate a random motivational quote. We went ahead and added a little Powered By Gemini label so that we know it is working. Yippeee!

The things you can do with Gemini are only limited by your imagination. Go crazy! Go code!

--

--

Visakh Vijayan
Visakh Vijayan

Written by Visakh Vijayan

Techie from Kerala, India. Days are for coding, nights for weaving tales of tech, travel, and finance. Join me in exploring this multifaceted journey

No responses yet