Skip to content

Commit b768631

Browse files
committed
Enhance workshop chapters with teaching notes and collapsible code
Added to all CHAPTER-*.md files: - Branch checkout instructions at the top - Teaching Notes for Presenters section with: - React Parallels table (maps AI SDK concepts to familiar React patterns) - Key Talking Points for live presentation - Live Demo Tips for demonstrating features - Common Questions with answers - Collapsible code snippets using <details><summary> HTML - React Parallel callout boxes explaining concepts Target audience: Mid-level React developers learning AI/LLM concepts
1 parent 55d445d commit b768631

File tree

6 files changed

+342
-0
lines changed

6 files changed

+342
-0
lines changed

CHAPTER-0.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,49 @@
22

33
Welcome to the AI Chatbot Workshop! In this chapter, we'll explore the foundation of our application - a basic chat interface powered by Next.js and the AI SDK.
44

5+
> **Branch**: `workshop/chapter-00-starting-point`
6+
> ```bash
7+
> git checkout workshop/chapter-00-starting-point
8+
> ```
9+
10+
---
11+
12+
## Teaching Notes for Presenters
13+
14+
### React Parallels
15+
16+
| AI SDK Concept | React Equivalent | Key Insight |
17+
|----------------|------------------|-------------|
18+
| `useChat` hook | `useState` + `useEffect` | Manages message state + side effects in one hook |
19+
| `streamText` | Server Action with streaming | Similar to `generateStaticParams` but for runtime AI |
20+
| Message history | Component state array | Just like managing a list of items in React state |
21+
| Data streaming | React Suspense boundaries | Progressive loading, but for tokens instead of components |
22+
23+
### Key Talking Points
24+
25+
1. **"The AI SDK is just React patterns applied to AI"**
26+
- `useChat` is essentially `useSWR` or `useQuery` specialized for chat
27+
- Streaming is like Progressive Hydration but for text generation
28+
- The chat API route is a standard Next.js Route Handler
29+
30+
2. **"Tokens, not characters"**
31+
- LLMs generate tokens (word pieces), not individual characters
32+
- This is why text appears in chunks, not letter-by-letter
33+
- Cost is measured in tokens (~4 chars/token for English)
34+
35+
3. **"Messages are immutable"**
36+
- Just like React state, we never mutate message arrays
37+
- Each message gets a unique ID (like React keys)
38+
- History grows by adding, never modifying
39+
40+
### Common Questions
41+
42+
- **"Why streaming?"** - UX feels faster (first token in ~200ms vs 5s for complete response)
43+
- **"What's the system prompt?"** - Like a component's defaultProps, but for AI behavior
44+
- **"Can I use a different AI?"** - Yes! Just change the provider in `lib/ai/providers.ts`
45+
46+
---
47+
548
## Learning Objectives
649
750
By the end of this chapter, you'll understand:
@@ -41,6 +84,9 @@ The heart of the application is `/app/(chat)/api/chat/route.ts`. This is where m
4184

4285
### Basic Chat Flow
4386

87+
<details>
88+
<summary>📄 <strong>Code: Basic Chat API Route</strong> (click to expand)</summary>
89+
4490
```typescript
4591
// app/(chat)/api/chat/route.ts (simplified)
4692
import { streamText } from "ai";
@@ -61,6 +107,8 @@ export async function POST(request: Request) {
61107
}
62108
```
63109

110+
</details>
111+
64112
### Key Concepts
65113

66114
1. **`streamText`**: The AI SDK function that sends messages to the model and streams the response token by token.
@@ -92,6 +140,9 @@ When you send a message:
92140

93141
The frontend uses `useChat` from the AI SDK React package:
94142

143+
<details>
144+
<summary>📄 <strong>Code: useChat Hook Usage</strong> (click to expand)</summary>
145+
95146
```typescript
96147
// Simplified usage in a chat component
97148
import { useChat } from "@ai-sdk/react";
@@ -115,6 +166,10 @@ export function Chat() {
115166
}
116167
```
117168

169+
</details>
170+
171+
> 💡 **React Parallel**: `useChat` is like combining `useState` for messages, `useReducer` for state transitions, and `useSWR` for the API call - all in one hook.
172+
118173
The `useChat` hook handles:
119174
- Managing message history
120175
- Sending messages to the API
@@ -139,6 +194,9 @@ type Message = {
139194

140195
The system prompt shapes the AI's personality and behavior:
141196

197+
<details>
198+
<summary>📄 <strong>Code: System Prompt</strong> (click to expand)</summary>
199+
142200
```typescript
143201
// lib/ai/prompts.ts
144202
export const systemPrompt = () => `
@@ -147,6 +205,10 @@ Today's date is ${new Date().toLocaleDateString()}.
147205
`;
148206
```
149207

208+
</details>
209+
210+
> 💡 **React Parallel**: Think of the system prompt as `defaultProps` or the initial context value - it sets the baseline behavior that all messages inherit.
211+
150212
## Exercise: Trace a Message
151213

152214
1. Start the development server: `npm run dev`

CHAPTER-1.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,55 @@
22

33
In this chapter, we'll give the AI the ability to **do things** beyond just responding with text. We'll add a weather tool that demonstrates how AI can call functions to retrieve real-time information.
44

5+
> **Branch**: `workshop/chapter-01-first-tool`
6+
> ```bash
7+
> git checkout workshop/chapter-01-first-tool
8+
> ```
9+
10+
---
11+
12+
## Teaching Notes for Presenters
13+
14+
### React Parallels
15+
16+
| AI SDK Concept | React Equivalent | Key Insight |
17+
|----------------|------------------|-------------|
18+
| `tool()` function | Custom hook definition | Encapsulates reusable logic with a defined interface |
19+
| `inputSchema` (Zod) | PropTypes / TypeScript props | Validates inputs before execution |
20+
| `execute` function | Event handler callback | Runs when the AI decides to use the tool |
21+
| Tool description | JSDoc / component documentation | Helps the AI "read" when to use it |
22+
23+
### Key Talking Points
24+
25+
1. **"Tools are just functions with extra metadata"**
26+
- The AI reads the `description` to decide when to call
27+
- Zod schemas ensure type-safe inputs (like TypeScript for AI)
28+
- `execute` is async - can fetch APIs, query DBs, anything
29+
30+
2. **"The AI decides, not you"**
31+
- You define WHAT the tool does, the AI decides WHEN
32+
- Good descriptions = accurate tool selection
33+
- Bad descriptions = confused AI, wrong tool calls
34+
35+
3. **"Tools don't break the chat"**
36+
- Tool results flow back into the conversation
37+
- The AI synthesizes results into natural language
38+
- Users see a seamless experience
39+
40+
### Live Demo Tips
41+
42+
- Show the Network tab to see tool calls as separate events
43+
- Intentionally ask something the tool can't handle ("What's the weather on Mars?")
44+
- Compare with/without tools: same question, different capabilities
45+
46+
### Common Questions
47+
48+
- **"Why Zod?"** - Runtime validation + TypeScript types in one schema
49+
- **"Can tools call other tools?"** - Not directly, but the AI can chain multiple tool calls
50+
- **"What if the API fails?"** - Handle errors gracefully in `execute`, return user-friendly messages
51+
52+
---
53+
554
## Learning Objectives
655
756
By the end of this chapter, you'll understand:
@@ -25,6 +74,9 @@ When you ask "What's the weather in London?", instead of guessing, the AI can **
2574
2675
Every tool has three parts:
2776
77+
<details>
78+
<summary>📄 <strong>Code: Tool Anatomy</strong> (click to expand)</summary>
79+
2880
```typescript
2981
import { tool } from "ai";
3082
import { z } from "zod";
@@ -46,12 +98,19 @@ const myTool = tool({
4698
});
4799
```
48100
101+
</details>
102+
103+
> 💡 **React Parallel**: This is like defining a custom hook with TypeScript props - the schema is your prop types, the execute is your hook logic, and the description is your JSDoc comment.
104+
49105
## The Weather Tool
50106
51107
Here's the complete weather tool implementation:
52108
53109
### File: `lib/ai/tools/get-weather.ts`
54110
111+
<details>
112+
<summary>📄 <strong>Code: Complete Weather Tool</strong> (click to expand)</summary>
113+
55114
```typescript
56115
import { tool } from "ai";
57116
import { z } from "zod";
@@ -80,12 +139,17 @@ export const getWeather = tool({
80139
});
81140
```
82141
142+
</details>
143+
83144
## Wiring Tools into the Chat Route
84145
85146
Add the tool to your chat route:
86147
87148
### File: `app/(chat)/api/chat/route.ts`
88149
150+
<details>
151+
<summary>📄 <strong>Code: Add Tool to Route</strong> (click to expand)</summary>
152+
89153
```typescript
90154
import { streamText } from "ai";
91155
import { myProvider } from "@/lib/ai/providers";
@@ -111,6 +175,10 @@ export async function POST(request: Request) {
111175
}
112176
```
113177
178+
</details>
179+
180+
> 💡 **Key Insight**: `maxSteps: 5` allows the AI to make multiple tool calls in sequence - like calling `getWeather` for two cities to compare them.
181+
114182
## How Tool Calling Works
115183
116184
```

CHAPTER-2.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,55 @@
22

33
In this chapter, we'll create our first **agent** - a specialized AI that can make its own decisions and generate rich responses. We'll build a Tutor agent that explains concepts with different teaching approaches.
44

5+
> **Branch**: `workshop/chapter-02-tutor-agent`
6+
> ```bash
7+
> git checkout workshop/chapter-02-tutor-agent
8+
> ```
9+
10+
---
11+
12+
## Teaching Notes for Presenters
13+
14+
### React Parallels
15+
16+
| AI SDK Concept | React Equivalent | Key Insight |
17+
|----------------|------------------|-------------|
18+
| Agent as tool | Higher-order component (HOC) | Wraps additional logic around a base component |
19+
| `generateText` | Async `fetch` in useEffect | Awaits a result, doesn't stream |
20+
| `CreateAgentProps` | React Context value | Passes session and dataStream to all agents |
21+
| `AgentResult` | Custom hook return type | Standardized shape for all agent outputs |
22+
23+
### Key Talking Points
24+
25+
1. **"Agents are tools that think"**
26+
- Regular tools: fetch data, return it
27+
- Agents: make their own AI call, generate content
28+
- The orchestrator decides WHICH agent, the agent decides HOW to respond
29+
30+
2. **"The tool-as-agent pattern"**
31+
- Agents ARE tools (same interface)
32+
- But they have a brain (call `generateText` internally)
33+
- Think: React component that fetches its own data
34+
35+
3. **"Why not just use the main model?"**
36+
- Separation of concerns (each agent has focused expertise)
37+
- Different prompts for different tasks
38+
- Can use different models per agent (fast for routing, smart for generation)
39+
40+
### Live Demo Tips
41+
42+
- Ask for the same explanation with different approaches ("explain like I'm 5" vs "technical deep-dive")
43+
- Show the console logs to trace the agent flow
44+
- Demonstrate that the orchestrator still responds naturally after the agent
45+
46+
### Common Questions
47+
48+
- **"Can agents call other agents?"** - Not directly in this pattern, but the orchestrator can chain them
49+
- **"Why `generateText` not `streamText`?"** - Agents return complete results; streaming happens at orchestrator level
50+
- **"What's the session for?"** - Personalization, rate limiting, saving user progress
51+
52+
---
53+
554
## Learning Objectives
655
756
By the end of this chapter, you'll understand:
@@ -52,6 +101,9 @@ First, let's define the structure for our agents:
52101

53102
### File: `lib/ai/agents/types.ts`
54103

104+
<details>
105+
<summary>📄 <strong>Code: Agent Type Definitions</strong> (click to expand)</summary>
106+
55107
```typescript
56108
import type { Session } from "next-auth";
57109
import type { DataStreamWriter } from "ai";
@@ -71,6 +123,10 @@ export type AgentResult = {
71123
};
72124
```
73125

126+
</details>
127+
128+
> 💡 **React Parallel**: `CreateAgentProps` is like a Context value - it's the same data passed to every agent, just like how React Context provides the same value to all consumers.
129+
74130
## Model Configuration
75131

76132
Before building agents, understand what `"chat-model"` means. The app uses named models configured in `lib/ai/providers.ts`:

CHAPTER-3.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,55 @@
22

33
In this chapter, we'll add multiple specialized agents and see how they work together. We'll create a Quiz Master that tests knowledge and a Planner that creates study schedules.
44

5+
> **Branch**: `workshop/chapter-03-multi-agent`
6+
> ```bash
7+
> git checkout workshop/chapter-03-multi-agent
8+
> ```
9+
10+
---
11+
12+
## Teaching Notes for Presenters
13+
14+
### React Parallels
15+
16+
| AI SDK Concept | React Equivalent | Key Insight |
17+
|----------------|------------------|-------------|
18+
| Multiple agents | Multiple context providers | Each provides specialized functionality |
19+
| Orchestrator routing | React Router | Picks the right component based on input |
20+
| `generateObject` | Form state with Zod | Returns typed, validated data structures |
21+
| Agent descriptions | Route matching patterns | AI matches intent to description, like URL patterns |
22+
23+
### Key Talking Points
24+
25+
1. **"The AI is your router"**
26+
- Just like React Router matches URLs to components
27+
- The orchestrator matches user intent to agents
28+
- Good descriptions = accurate routing
29+
30+
2. **"`generateObject` is like a smart form submission"**
31+
- You define the schema (like form fields)
32+
- AI fills it in with valid data
33+
- Returns typed JSON, not free-form text
34+
35+
3. **"Agents should be single-purpose"**
36+
- One agent = one job (Single Responsibility)
37+
- Overlapping descriptions = confused routing
38+
- Clear triggers = predictable behavior
39+
40+
### Live Demo Tips
41+
42+
- Ask for a quiz, then a study plan, then an explanation - show all three agents
43+
- Try chained requests: "Explain React hooks, then quiz me on them"
44+
- Show the Zod schema and how it enforces structure
45+
46+
### Common Questions
47+
48+
- **"What if two agents could handle the request?"** - Most specific description wins
49+
- **"Can I have too many agents?"** - Yes! Each adds complexity. Start with 3-5
50+
- **"Why `generateObject` for quiz but `generateText` for tutor?"** - Quiz needs structured data; tutor needs free-form text
51+
52+
---
53+
554
## Learning Objectives
655
756
By the end of this chapter, you'll understand:

0 commit comments

Comments
 (0)