Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@google/generative-ai

Package Overview
Dependencies
Maintainers
4
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@google/generative-ai

Google AI JavaScript SDK

  • 0.21.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
232K
decreased by-16.18%
Maintainers
4
Weekly downloads
 
Created
Source

Google AI SDK for JavaScript

The Google AI JavaScript SDK is the easiest way for JavaScript developers to build with the Gemini API. The Gemini API gives you access to Gemini models created by Google DeepMind. Gemini models are built from the ground up to be multimodal, so you can reason seamlessly across text, images, and code.

[!CAUTION] Using the Google AI SDK for JavaScript directly from a client-side app is recommended for prototyping only. If you plan to enable billing, we strongly recommend that you call the Google AI Gemini API only server-side to keep your API key safe. You risk potentially exposing your API key to malicious actors if you embed your API key directly in your JavaScript app or fetch it remotely at runtime.

Get started with the Gemini API

  1. Go to Google AI Studio.
  2. Login with your Google account.
  3. Create an API key. Note that in Europe the free tier is not available.
  4. Try the Node.js quickstart

Usage example

See the Node.js quickstart for complete code.

  1. Install the SDK package
npm install @google/generative-ai
  1. Initialize the model
const { GoogleGenerativeAI } = require("@google/generative-ai");

const genAI = new GoogleGenerativeAI(process.env.API_KEY);

const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });
  1. Run a prompt
const prompt = "Does this look store-bought or homemade?";
const image = {
  inlineData: {
    data: Buffer.from(fs.readFileSync("cookie.png")).toString("base64"),
    mimeType: "image/png",
  },
};

const result = await model.generateContent([prompt, image]);
console.log(result.response.text());

Try out a sample app

This repository contains sample Node and web apps demonstrating how the SDK can access and utilize the Gemini model for various use cases.

To try out the sample Node app, follow these steps:

  1. Check out this repository.
    git clone https://github.com/google/generative-ai-js

  2. Obtain an API key to use with the Google AI SDKs.

  3. cd into the samples folder and run npm install.

  4. Assign your API key to an environment variable: export API_KEY=MY_API_KEY.

  5. Open the sample file you're interested in. Example: text_generation.js. In the runAll() function, comment out any samples you don't want to run.

  6. Run the sample file. Example: node text_generation.js.

Documentation

See the Gemini API Cookbook or ai.google.dev for complete documentation.

Contributing

See Contributing for more information on contributing to the Google AI JavaScript SDK.

License

The contents of this repository are licensed under the Apache License, version 2.0.

FAQs

Package last updated on 02 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc