Introduction to Generative AI JS
The Generative AI JS project introduces a Google AI JavaScript SDK designed to facilitate JavaScript developers in using the Gemini API. This API provides access to Gemini models developed by Google DeepMind, which are specifically crafted to be multimodal, allowing seamless reasoning across text, images, and code.
Getting Started with the Gemini API
To begin working with the Gemini API, developers should follow these steps:
- Visit the Google AI Studio.
- Log in using a Google account.
- Create an API key. It is important to note that the free tier is not available in Europe.
- Explore the Node.js quickstart guide for a comprehensive introduction.
Usage Example
For code examples and detailed instructions, reference the Node.js quickstart. Here is a brief overview of how to use the SDK:
-
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());
Exploring a Sample App
The repository provides sample Node and web apps demonstrating the SDK’s capabilities with the Gemini model for different use cases. To test a sample Node app, follow these instructions:
-
Clone the repository:
git clone https://github.com/google/generative-ai-js
-
Obtain an API key from Makersuite for use with Google AI SDKs.
-
Navigate to the
samples
folder and install dependencies:cd samples npm install
-
Assign the API key to an environment variable:
export API_KEY=MY_API_KEY
-
Open and edit the sample file of interest, like
text_generation.js
, commenting out samples you do not wish to run. -
Execute the sample file:
node text_generation.js
Documentation and Contributing
Comprehensive documentation can be found in the Gemini API Cookbook or at ai.google.dev. For information on contributing to the project, see the Contributing Guide.
License
This project's contents are licensed under the Apache License, version 2.0.