Implementing Gemini in Node.js

This article can be read in about 10 minutes.
PR

The purpose 

Use Google’s generative AI, Gemini, with Node.js.

PR

Implementation

Generate API key

Go to the following page to get your API key.

Sign in - Google Accounts

Create progect

This project uses Node.js and Vite, based on the following article.

Next, execute the following command to install generative-ai. 

npm install @google/generative-ai

Create HTML

Create an index.html file in the Src folder.

Its contents are as follows:

<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title>Gemini</title>
 </head>
 <body>
    <script type="module" src="main.js"></script>
 </body>
</html>

Create JavaScript

create a main.js file that will be loaded by index.html in the src folder.

The contents are as follows: Replace “Your API key” with your acquired API key. Also, you shouldn’t expose your API key directly in the script.

This is hardcoded here for testing purposes only; it’s best practice to retrieve it from environment variables.

let api_key = "Your API key"

import { GoogleGenerativeAI } from "@google/generative-ai";

const genAI = new GoogleGenerativeAI(api_key);
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });

const prompt = "teach me about google";

const result = await model.generateContent(prompt);
console.log(result.response.text());
PR

Result

Execute the following, then run it in your browser.

npm run dev

The following content was logged to the console. (This will take a few to several tens of seconds.)


Google is a massive multinational technology company that offers a wide array of services and products.  It's best understood by breaking it down into several key areas:

**1. Search Engine:**  This is what most people associate with Google. It's a web search engine that uses sophisticated algorithms to index and rank web pages based on relevance to a user's query.  Its power lies in its massive scale, indexing billions of web pages and constantly refining its algorithms to provide the most accurate and relevant results. Key aspects of the search engine include:

* **Keywords:** The words or phrases users type into the search bar.
* **Algorithms:** Complex mathematical formulas that determine the ranking of search results. These are constantly updated to combat manipulation and improve accuracy.
* **Search Results Pages (SERPs):** The page displaying the results of a search query, including organic results (based on algorithms), ads, images, videos, and more.

**2. Advertising:** Google's primary revenue source is advertising.  It offers various advertising platforms:

* **Google Ads (formerly AdWords):**  A pay-per-click (PPC) advertising system where businesses bid on keywords to have their ads displayed on Google search results pages and other Google properties.
* **Google AdSense:** Allows website publishers to display ads on their websites and earn money based on clicks or impressions.

**3. Other Major Products and Services:**  Google's reach extends far beyond its search engine, including:

* **Android:** The world's most popular mobile operating system, powering billions of smartphones and tablets.
* **Chrome:**  A highly popular web browser known for its speed and security features.
* **Gmail:** A widely used free email service.
* **Google Maps:** A comprehensive mapping and navigation service with street view imagery, traffic data, and location-based services.
* **YouTube:** The world's largest video-sharing platform, owned by Google.
* **Google Drive:** Cloud storage and file sharing service.
* **Google Cloud Platform (GCP):** A suite of cloud computing services for businesses.
* **Google Workspace (formerly G Suite):**  A collection of productivity apps, including Gmail, Google Docs, Sheets, Slides, and Meet, for individual and business use.
* **Google Play Store:** An app store for Android devices.
* **Google Photos:** Cloud-based photo and video storage and sharing service.


**4. Google's Mission and Impact:**  Google's mission statement is "to organize the world's information and make it universally accessible and useful." While this is a broad statement, it reflects its impact on information access, communication, and technology development globally.

**5. Concerns and Criticisms:**  Google's size and influence have led to various concerns:

* **Monopoly Power:** Concerns exist regarding Google's dominant market share in several areas and its potential for anti-competitive practices.
* **Privacy:**  Google collects vast amounts of user data, raising privacy concerns.
* **Bias in Search Results:**  Allegations have been made about bias in Google's search algorithms.
* **Misinformation:**  The spread of misinformation and disinformation through Google's platforms is a significant challenge.

In short, Google is far more than just a search engine.  It's a vast ecosystem of interconnected products and services that shape how we interact with information, communicate, and navigate the digital world.  Understanding these different facets gives a much more complete picture of what Google is and does.

The GUI will be covered in the article below.

PR

Reference

Gemini API クイックスタート  |  Google AI for Developers
Gemini API のスタートガイド

comment

Copied title and URL