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
[Deprecated] Google AI JavaScript SDK for the Gemini API
With Gemini 2.0, we took the chance to create a single unified SDK for all developers who want to use Google's GenAI models (Gemini, Veo, Imagen, etc). As part of that process, we took all of the feedback from this SDK and what developers like about other SDKs in the ecosystem to create the Google Gen AI SDK.
The full migration guide from the old SDK to new SDK is available in the Gemini API docs.
The Gemini API docs are fully updated to show examples of the new Google Gen AI SDK. We know how disruptive an SDK change can be and don't take this change lightly, but our goal is to create an extremely simple and clear path for developers to build with our models so it felt necessary to make this change.
Thank you for building with Gemini and let us know if you need any help!
Please be advised that this repository is now considered legacy. For the latest features, performance improvements, and active development, we strongly recommend migrating to the official Google Generative AI SDK for JavaScript.
Support Plan for this Repository:
- Limited Maintenance: Development is now restricted to critical bug fixes only. No new features will be added.
- Purpose: This limited support aims to provide stability for users while they transition to the new SDK.
- End-of-Life Date: All support for this repository (including bug fixes) will permanently end on August 31st, 2025.
We encourage all users to begin planning their migration to the Google Generative AI SDK to ensure continued access to the latest capabilities and support.
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