[Solution] Generate text with the AI SDK
Solution for: https://www.alagarbaa.com/blog/exercise-generate-text-with-the-ai-sdk
1 min read
0 views
Written by
Ala GARBAA
Full Stack AI Developer & Software Engineer
Exercise: Generate Text With the AI SDK
Step 1: Create a project
mkdir ai-test
cd ai-test
pnpm init
add this line :
"type": "module",
Step 2: Install what you need
pnpm add @ai-sdk/google ai dotenv
pnpm add -D typescript ts-node @types/node
Step 3: Set up TypeScript
npx tsc --init
add this line :
"dev": "ts-node main.ts"
This makes a tsconfig.json file and make sure to add these lines:
{
...
"types": ["node"],
"module": "esnext",
"moduleResolution": "node",
...
Step 4: Add your API key to .env file
GOOGLE_GENERATIVE_AI_API_KEY="your_key_here"
Restart your terminal after this.
Step 5: Write the code
Make a file called main.ts:
import 'dotenv/config';
import { google } from '@ai-sdk/google';
import { generateText } from 'ai';
const model = google('gemini-2.5-flash-lite'); // just the model name
const prompt = 'what 2-2 = ?';
async function main() {
try {
const result = await generateText({
model,
prompt, // no apiKey here
});
console.log(result.text);
} catch (err) {
console.error('Error:', err);
}
}
main();
Step 6: Run it
becouse we installed ts-node as dev deps and we added the dev script we can just run :
pnpm dev
You'll see:
> ai-test-1@1.0.0 dev /home/.../projects/ai-test-1
> ts-node main.ts
2 - 2 = **0**
Still have issue ? copy same package.json
{
"name": "ai-test-1",
"version": "1.0.0",
"description": "",
"scripts": {
"dev": "ts-node main.ts"
},
"type": "module",
"keywords": [],
"author": "",
"license": "ISC",
"packageManager": "pnpm@10.22.0",
"dependencies": {
"@ai-sdk/google": "^2.0.32",
"ai": "^5.0.93",
"ts-node": "^10.9.2",
"typescript": "^5.9.3"
}
}
Still have issue ? copy same tsconfig.json
{
// Visit https://aka.ms/tsconfig to read more about this file
"compilerOptions": {
// File Layout
// "rootDir": "./src",
// "outDir": "./dist",
// Environment Settings
// See also https://aka.ms/tsconfig/module
"target": "esnext",
"types": ["node"],
"module": "esnext",
"moduleResolution": "node",
// For nodejs:
// "lib": ["esnext"],
// "types": ["node"],
// and npm install -D @types/node
// Other Outputs
"sourceMap": true,
"declaration": true,
"declarationMap": true,
// Stricter Typechecking Options
"noUncheckedIndexedAccess": true,
"exactOptionalPropertyTypes": true,
// Style Options
// "noImplicitReturns": true,
// "noImplicitOverride": true,
// "noUnusedLocals": true,
// "noUnusedParameters": true,
// "noFallthroughCasesInSwitch": true,
// "noPropertyAccessFromIndexSignature": true,
// Recommended Options
"strict": true,
"jsx": "react-jsx",
"verbatimModuleSyntax": true,
"isolatedModules": true,
"noUncheckedSideEffectImports": true,
"moduleDetection": "force",
"skipLibCheck": true,
}
}
Still have issue ? copy same .env Copy this totally‑real‑not‑fake‑trust‑me‑bro .env key:
GOOGLE_GENERATIVE_AI_API_KEY="lol-this-key-is-more-fake-than-my-gym-schedule-1234"