Exercise: Generate Text With the AI SDK

Written by
Ala GARBAA
Full Stack AI Developer & Software Engineer
Exercise: Generate Text With the AI SDK
In this exercise, you’ll write a small script that sends a prompt to a Google Gemini model using the AI SDK. You already have the project structure, the packages, and the setup. Now let’s focus on using the SDK itself.
You need two things to generate text:
- a model
- a prompt
Inside your main.ts file you'll see a couple of TODOs.
These TODOs tell you exactly what needs to be replaced.
Your starter code (with TODOs)
// Load environment variables
import 'dotenv/config';
// Import the necessary functions
import { google } from '@ai-sdk/google';
import { generateText } from 'ai';
// TODO: Choose a model. I recommend using:
// gemini-2.5-flash-lite
const model = /* TODO: call google(...) */;
// TODO: Write a simple prompt
const prompt = /* TODO: add your prompt here */;
async function main() {
try {
// TODO: Call generateText, passing in the model and the prompt.
// Remember to await the result.
const result = /* TODO */;
// Log the model output
console.log(result.text);
} catch (err) {
console.error('Error:', err);
}
}
main();
What you need to do
1. Choose a model
Replace the first TODO with something like:
const model = google('gemini-2.5-flash-lite');
This creates the model instance you will pass to the SDK.
2. Add a prompt
Replace the second TODO with any short text prompt, for example:
const prompt = "What is 2 - 2?";
Or choose your own.
3. Call generateText
Replace the third TODO with:
const result = await generateText({
model,
prompt,
});
You now have everything you need to actually run the code.
What you should see
When you run:
pnpm dev
You should see the AI’s answer printed in the terminal. For example:
2 - 2 = 0
If you see an error like TODO is not defined, that just means you left a TODO somewhere. Replace it with real code.
Steps To Complete
- Replace the model TODO with a Google model instance
- Replace the prompt TODO with a simple question
- Replace the
generateTextTODO with an awaited function call - Run the script and verify that the answer prints to the terminal
- If you get stuck, check the finished code in the solution above
Good luck, and test your output carefully.