Google AI JavaScript SDK
npm install generative-ai
const model = genAI.getGenerativeModel({ model: "gemini-pro-vision" });
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());
`
For web:
`
const model = genAI.getGenerativeModel({ model: "gemini-pro-vision" });
const prompt = "Does this look store-bought or homemade?";
const image = {
inlineData: {
data: base64EncodedImage / see JavaScript quickstart for details /,
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
1. Obtain an API key to use with the Google AI SDKs.
1. cd into the samples/node folder and run npm install.
1. Assign your API key to an environment variable: export API_KEY=MY_API_KEY.
1. Run the sample file you're interested in. Example: node simple-text.js.
To try out the sample web app, follow these steps:
1. Check out this repository.\
git clone https://github.com/google/generative-ai-js
1. Obtain an API key to use with the Google AI SDKs.
1. cd into the samples/web folder and run npm install.
1. Assign your API key to an environment variable: export API_KEY=MY_API_KEY.
1. Serve your web app by running: npm run http-server. Open the displayed URL in a browser.
Installation and usage
- For Node.js (or web projects using NPM), run npm install @google/generative-ai.
- For web, add import { GoogleGenerativeAI } from "https://esm.run/@google/generative-ai"`.