deprecated-generative-ai-js
This SDK is now deprecated, use the new unified Google GenAI SDK.
Top Related Projects
Official JavaScript / TypeScript library for the OpenAI API
Utilities to use the Hugging Face Hub API
Quick Overview
The google-gemini/generative-ai-js repository is a JavaScript library for interacting with Google's Gemini AI models. It provides a simple interface for developers to integrate Gemini's powerful language models into their web applications, enabling tasks such as text generation, summarization, and more.
Pros
- Easy integration with web applications using JavaScript
- Supports multiple Gemini models and capabilities
- Provides both streaming and non-streaming API options
- Well-documented with clear examples and TypeScript support
Cons
- Limited to Google's Gemini models, not a general-purpose AI library
- Requires API key and potential usage costs
- May have rate limits or usage restrictions
- Dependency on Google's services and infrastructure
Code Examples
- Basic text generation:
import { GoogleGenerativeAI } from "@google/generative-ai";
const genAI = new GoogleGenerativeAI(API_KEY);
const model = genAI.getGenerativeModel({ model: "gemini-pro" });
const result = await model.generateContent("Write a haiku about coding");
console.log(result.response.text());
- Streaming text generation:
const model = genAI.getGenerativeModel({ model: "gemini-pro" });
const stream = await model.generateContentStream("Explain quantum computing");
for await (const chunk of stream.stream) {
console.log(chunk.text());
}
- Multi-modal input (text and image):
const model = genAI.getGenerativeModel({ model: "gemini-pro-vision" });
const imageParts = [
fileToGenerativePart(imageFile),
{ text: "What's in this image?" },
];
const result = await model.generateContent(imageParts);
console.log(result.response.text());
Getting Started
To get started with google-gemini/generative-ai-js:
-
Install the library:
npm install @google/generative-ai
-
Import and initialize the library:
import { GoogleGenerativeAI } from "@google/generative-ai"; const API_KEY = "YOUR_API_KEY"; const genAI = new GoogleGenerativeAI(API_KEY);
-
Use the library to interact with Gemini models:
const model = genAI.getGenerativeModel({ model: "gemini-pro" }); const result = await model.generateContent("Hello, Gemini!"); console.log(result.response.text());
Remember to replace "YOUR_API_KEY"
with your actual Google API key for Gemini access.
Competitor Comparisons
Official JavaScript / TypeScript library for the OpenAI API
Pros of openai-node
- More mature and widely adopted, with a larger community and ecosystem
- Comprehensive documentation and extensive examples
- Supports a broader range of OpenAI models and features
Cons of openai-node
- Limited to OpenAI's services, less flexibility for multi-provider scenarios
- Potentially higher costs due to OpenAI's pricing structure
- More complex setup and configuration compared to generative-ai-js
Code Comparison
generative-ai-js:
import { GoogleGenerativeAI } from "@google/generative-ai";
const genAI = new GoogleGenerativeAI(API_KEY);
const model = genAI.getGenerativeModel({ model: "gemini-pro" });
const result = await model.generateContent(prompt);
console.log(result.response.text());
openai-node:
import OpenAI from "openai";
const openai = new OpenAI({ apiKey: API_KEY });
const completion = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages: [{ role: "user", content: prompt }],
});
console.log(completion.choices[0].message.content);
Both libraries offer straightforward APIs for generating content, but generative-ai-js has a slightly simpler setup process. openai-node provides more granular control over model parameters and supports a wider range of OpenAI-specific features.
Utilities to use the Hugging Face Hub API
Pros of huggingface.js
- Broader model support: Access to a wide range of AI models beyond text generation
- Community-driven: Benefits from contributions and updates from the open-source community
- Extensive documentation: Comprehensive guides and examples for various use cases
Cons of huggingface.js
- Steeper learning curve: May require more setup and configuration for specific tasks
- Potentially slower inference: Performance might vary depending on the chosen model and setup
Code Comparison
huggingface.js:
import { HfInference } from '@huggingface/inference';
const hf = new HfInference(HF_TOKEN);
const result = await hf.textGeneration({
model: 'gpt2',
inputs: 'The quick brown fox',
});
generative-ai-js:
import { GoogleGenerativeAI } from '@google/generative-ai';
const genAI = new GoogleGenerativeAI(API_KEY);
const model = genAI.getGenerativeModel({ model: 'gemini-pro' });
const result = await model.generateContent('The quick brown fox');
Both libraries offer straightforward ways to generate text, but huggingface.js provides more flexibility in model selection, while generative-ai-js focuses on Google's Gemini models with a simpler API. huggingface.js may be preferred for diverse AI tasks, while generative-ai-js excels in ease of use for Google's specific offerings.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
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
- Go to Google AI Studio.
- Login with your Google account.
- Create an API key. Note that in Europe the free tier is not available.
- Try the Node.js quickstart
Usage example
See the Node.js quickstart for complete code.
- Install the SDK package
npm install @google/generative-ai
- 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" });
- 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:
-
Check out this repository.
git clone https://github.com/google/generative-ai-js
-
Obtain an API key to use with the Google AI SDKs.
-
cd into the
samples
folder and runnpm install
. -
Assign your API key to an environment variable:
export API_KEY=MY_API_KEY
. -
Open the sample file you're interested in. Example:
text_generation.js
. In therunAll()
function, comment out any samples you don't want to run. -
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.
Top Related Projects
Official JavaScript / TypeScript library for the OpenAI API
Utilities to use the Hugging Face Hub API
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot