Facebook
From sajid, 1 Month ago, written in JavaScript.
Embed
Download Paste or View Raw
Hits: 160
  1. const { GoogleGenerativeAI } = require("@google/generative-ai");
  2. const fs = require("fs");
  3.  
  4. // Access your API key as an environment variable (see "Set up your API key" above)
  5. const genAI = new GoogleGenerativeAI(process.env.API_KEY);
  6.  
  7. // Converts local file information to a GoogleGenerativeAI.Part object.
  8. function fileToGenerativePart(path, mimeType) {
  9.   return {
  10.     inline[removed]
  11.       mimeType
  12.     },
  13.   };
  14. }
  15.  
  16. async function run() {
  17.   // For text-and-image input (multimodal), use the gemini-pro-vision model
  18.   const model = genAI.getGenerativeModel({ model: "gemini-pro-vision" });
  19.  
  20.   const prompt = "What's different between these pictures?";
  21.  
  22.   const imageParts = [
  23.     fileToGenerativePart("image1.png", "image/png"),
  24.     fileToGenerativePart("image2.jpeg", "image/jpeg"),
  25.   ];
  26.  
  27.   const result = await model.generateContent([prompt, ...imageParts]);
  28.   const response = await result.response;
  29.   const text = response.text();
  30.   console.log(text);
  31. }
  32.  
  33. run();