// Make sure to include these imports: // import { GoogleGenerativeAI } from…
- User
// Make sure to include these imports: // import { GoogleGenerativeAI } from "@google/generative-ai"; const genAI = new GoogleGenerativeAI(process.env.API_KEY); const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });
function fileToGenerativePart(path, mimeType) { return { inlineData: { data: Buffer.from(fs.readFileSync(path)).toString("base64"), mimeType, }, }; }
const prompt = "Describe how this product might be manufactured."; // Note: The only accepted mime types are some image types, image/*. const imagePart = fileToGenerativePart(
${mediaPath}/jetpack.jpg, "image/jpeg", );const result = await model.generateContentStream([prompt, imagePart]);
// Print text as it comes in. for await (const chunk of result.stream) { const chunkText = chunk.text(); process.stdout.write(chunkText); }
###解釋一下
- Assistant
這段程式碼使用了 Google 的生成式 AI 來根據提供的提示和圖片生成內容。下面是詳細的解釋:
-
引入必要的模組
javascript// 確保包含以下引入: // import { GoogleGenerativeAI } from "@google/generative-ai"; const genAI = new GoogleGenerativeAI(process.env.API_KEY);
-