Gemini API Exercises
Fill in the blanks to test your knowledge.
1
Initialize the Google Generative AI client
import { } from "@google/generative-ai";
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY!);
2
Get a model instance from the client
const model = genAI.({
model: "gemini-1.5-flash"
});
3
Generate text content from a prompt
const result = await model.("Explain closures in JavaScript.");
4
Extract the text string from a Gemini response
const result = await model.generateContent(prompt);
const text = result.response.();
5
Add a system instruction to the model config
const model = genAI.getGenerativeModel({
model: "gemini-1.5-pro",
: "You are a concise coding assistant."
});
6
Start a multi-turn chat session
const chat = model.();
const response = await chat.sendMessage("Hello!");
7
Force structured JSON output via generationConfig
const model = genAI.getGenerativeModel({
model: "gemini-1.5-pro",
generationConfig: { : "application/json" }
});
8
Count tokens before sending a large prompt
const { } = await model.countTokens(largePrompt);
if (totalTokens > 900_000) throw new Error("Too large!");