This repository is a comprehensive learning course called "Generative AI for Beginners with JavaScript". It teaches developers how to integrate Generative AI into JavaScript applications through a time-traveling narrative where learners interact with historical figures.
Key Technologies:
- JavaScript/Node.js (ES Modules)
- TypeScript (for some lessons)
- OpenAI SDK
- Model Context Protocol (MCP)
- Express.js (for web applications)
- GitHub Models (free AI model access)
- Azure OpenAI (optional alternative)
Architecture:
- Multi-package learning repository (not a monorepo, but lesson-based structure)
- Each lesson in
/lessons/contains standalone sample applications - Main companion app in
/app/directory for character interactions - Video content and demos in
/videos/ - Documentation in
/docs/
/
├── app/ # Main companion app for interacting with historical characters
├── lessons/ # 8 lesson directories, each with sample code and solutions
│ ├── 01-intro-to-genai/
│ ├── 02-first-ai-app/
│ ├── 03-prompt-engineering/
│ ├── 04-structured-output/
│ ├── 05-rag/
│ ├── 06-tool-calling/
│ ├── 07-mcp/
│ └── 08-mcp-advanced/
├── docs/ # Setup guides and additional resources
├── videos/ # Video session content and demos
└── .devcontainer/ # GitHub Codespaces configuration
Prerequisites:
- Node.js 20 or higher
- GitHub account (for GitHub Models access)
- Optional: Personal Access Token (PAT) if running locally
Initial Setup:
# Clone the repository
git clone https://github.com/microsoft/generative-ai-with-javascript.git
cd generative-ai-with-javascript
# Install root dependencies
npm install
# For GitHub Codespaces users (recommended):
# The environment is pre-configured - no additional setup needed
# Use built-in GITHUB_TOKEN environment variable
# For local development:
# 1. Create a GitHub Personal Access Token (PAT) at https://github.com/settings/tokens
# 2. Set it as an environment variable: export GITHUB_TOKEN=your_token_hereRunning the Main Companion App:
cd app
npm install
npm start
# Access at http://localhost:3000 or use "Open in Browser" in CodespacesWorking with Individual Lessons:
Each lesson has its own package.json. Navigate to the specific lesson directory:
cd lessons/02-first-ai-app/sample-app
npm install
npm start- Fork the repository
- Create a Codespace from your fork
- Pre-configured environment includes:
- Node.js 20
- VSCode extensions (EditorConfig, Code Runner, REST Client)
- Ollama with phi3 and all-minilm models
GITHUB_TOKENautomatically available
- Navigate to any lesson or app directory and run
npm install && npm start
- Ensure Node.js 20+ is installed
- Set
GITHUB_TOKENenvironment variable - Navigate to the directory you want to work with
- Run
npm installfollowed bynpm start
GITHUB_TOKEN- Required for GitHub Models API access- Automatically available in GitHub Codespaces
- Must be set manually for local development
- No scopes/permissions needed for the token when using GitHub Models
Each lesson follows a consistent structure:
README.md- Main lesson content with narrative and technical instructionssample-app/orcode/- Starting code for exercisessolution/- Complete solution codetranslations/- Lesson translations in various languages
To work on a lesson:
cd lessons/<lesson-number>-<lesson-name>/sample-app
npm install
npm startFor TypeScript-based lessons (MCP lessons 07-08):
cd lessons/07-mcp/solution
npm install
npm run build # Compiles TypeScript to build/ directory
npm run client # Runs the MCP client
npm run inspect # Runs MCP inspector toolCurrent Testing Setup:
This is primarily an educational repository focused on tutorial content. There are no automated unit or integration tests for the code samples.
Manual Testing Approach:
-
Validate lesson code by running it:
cd lessons/<lesson-name>/sample-app npm install npm start
-
Test the companion app:
cd app npm install npm start # Interact with characters through the web interface
-
For MCP lessons, use the inspector tool:
cd lessons/07-mcp/solution npm run build npm run inspect # Test individual tools: npx @modelcontextprotocol/inspector --cli node build/index.js --method tools/call --tool-name add --tool-arg a=1 --tool-arg b=3
-
Verify GitHub Models integration:
- Ensure
GITHUB_TOKENis set - Run any lesson sample that calls OpenAI APIs
- Verify responses are generated successfully
- Ensure
JavaScript/TypeScript Conventions:
- Use ES Modules (
type: "module"in package.json) - Use
async/awaitfor asynchronous operations - Prefer
constoverlet, avoidvar - Use template literals for string interpolation
- Follow existing code patterns in each lesson
File Organization:
- Keep lesson code self-contained within lesson directories
- Place shared resources in
/docs/or/assets/ - Character data stored in
app/public/characters.json - TypeScript source in
src/, compiled output inbuild/
TypeScript (where applicable):
- Target: ES2022
- Module: Node16
- Strict mode enabled
- Output to
build/directory
Import/Export Patterns:
// ES Module imports
import { OpenAI } from 'openai';
import express from 'express';
// Default exports for apps
export default app;Naming Conventions:
- Use camelCase for variables and functions
- Use PascalCase for classes and constructors
- Prefix directories with numbers for lessons (e.g.,
01-intro-to-genai) - Use kebab-case for directory names
Building TypeScript Lessons:
cd lessons/07-mcp/solution
npm run build
# Output: build/ directory with compiled JavaScriptDeployment:
The repository uses GitHub Pages for documentation hosting:
npm run build # Builds documentation site
# Deployment handled by .github/workflows/deploy.ymlGitHub Actions Workflows:
deploy.yml- Deploys to GitHub Pageslinks.yml- Checks for broken links (weekly schedule)profanity.yml- Filters inappropriate content in PRsspelling.yml- Spell checks documentation
Primary AI Service:
This course uses GitHub Models for free access to AI capabilities. All code examples are configured to work with GitHub Models by default.
API Configuration:
import { OpenAI } from 'openai';
const openai = new OpenAI({
baseURL: "https://models.inference.ai.azure.com",
apiKey: process.env.GITHUB_TOKEN,
});Available Models:
gpt-4o- Primary model used in examplesgpt-4o-mini- Lightweight alternative- Access via GitHub Models marketplace: https://github.com/marketplace/models
While not a true monorepo, this repository contains multiple independent packages:
Finding Lesson Packages:
# List all lesson directories
ls lessons/
# Find all package.json files
find . -name "package.json" -not -path "*/node_modules/*"
# Jump to a specific lesson
cd lessons/03-prompt-engineering/sample-appPackage Locations:
- Root:
/package.json(minimal dependencies: openai, tsx) - Main App:
/app/package.json(Express app with OpenAI) - Each Lesson:
lessons/<lesson>/sample-app/package.jsonorcode/package.json - Solutions:
lessons/<lesson>/solution/package.json
Working Across Packages:
Each package is independent - install dependencies in each directory you work with:
cd lessons/05-rag/sample-app && npm install
cd ../../06-tool-calling/solution && npm installAdding a New Lesson Sample:
- Create lesson directory:
lessons/XX-lesson-name/ - Add
README.mdwith lesson content - Create
sample-app/with starter code - Create
solution/with complete code - Add package.json with required dependencies
- Update main README.md and
_sidebar.md
Running Spell Check:
# Triggered via GitHub Actions workflow
# Checks: README.md and lessons/01-intro-to-genai/README.mdTesting Links:
# Runs automatically weekly via GitHub Actions
# Or trigger manually via workflow_dispatchThe companion app (/app/) allows interaction with historical AI characters.
Key Files:
app.js- Express server and OpenAI integrationpublic/characters.json- Character definitions and system promptspublic/index.html- Frontend interfaceviews/- EJS templates
Character Configuration:
Edit public/characters.json to:
- Add new characters
- Modify character descriptions (used as system prompts)
- Change character personalities
Adding Background Audio (Optional):
- Download royalty-free music
- Place in
public/audio/as<character-name>.mp3 - Uncomment audio code in
index.html
"Cannot find module" errors:
# Ensure you're in the correct directory with package.json
cd lessons/<lesson-name>/sample-app
npm installGitHub Models authentication issues:
# Verify token is set
echo $GITHUB_TOKEN
# For Codespaces: Token is automatically available
# For local: Create PAT at https://github.com/settings/tokensTypeScript compilation errors:
cd lessons/07-mcp/solution
# Check tsconfig.json exists
npm install
npm run buildPort already in use:
# Kill process on port 3000
lsof -ti:3000 | xargs kill -9
# Or use a different port in your app configurationResponsible AI:
This repository contains fictional AI-generated content. Historical characters generate responses via AI based on training data, not actual historical views. Content is for educational and entertainment purposes only.
Educational Focus:
- Each lesson builds progressively on previous concepts
- Narrative-driven learning with time-travel story
- Hands-on coding exercises with solutions provided
- Video content supplements written lessons
Contributing:
- See
.github/CONTRIBUTING.mdfor contribution guidelines - Follow existing code patterns and structure
- Add translations in
lessons/<lesson>/translations/ - Use issue templates in
.github/ISSUE_TEMPLATE/
Community Resources:
- Discord: https://discord.gg/kzRShWzttr
- Azure AI Foundry Forum: https://aka.ms/foundry/forum
- Related courses linked in main README.md
Performance Considerations:
- GitHub Codespaces requires 16GB RAM (configured in devcontainer.json)
- Ollama models (phi3, all-minilm) pre-installed in Codespaces
- Keep lesson samples lightweight and focused
- Use streaming for better UX in production apps (see lesson 10)