Skip to content

Commit e7271ee

Browse files
NiallJoeMaherclaude
andcommitted
docs: fix CHAPTER-1 to show correct systemPrompt and route patterns
- Updated system prompt section to show updating regularPrompt constant - Updated route handler to show correct systemPrompt({ selectedChatModel, requestHints }) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent a0cca0e commit e7271ee

File tree

1 file changed

+44
-31
lines changed

1 file changed

+44
-31
lines changed

CHAPTER-1.md

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -146,43 +146,40 @@ export const getWeather = tool({
146146

147147
## Wiring Tools into the Chat Route
148148

149-
Add the tool to your chat route:
149+
Add the tool to your chat route. The route already has streaming set up - you just need to add the tool:
150150

151151
### File: `app/(chat)/api/chat/route.ts`
152152

153153
```typescript
154-
import { stepCountIs, streamText } from "ai";
155-
import { myProvider } from "@/lib/ai/providers";
156-
import { systemPrompt } from "@/lib/ai/prompts";
154+
// Add this import at the top
157155
import { getWeather } from "@/lib/ai/tools/get-weather";
158156

159-
export async function POST(request: Request) {
160-
const { messages, selectedChatModel } = await request.json();
161-
162-
const result = streamText({
163-
model: myProvider.languageModel(selectedChatModel),
164-
system: systemPrompt({ selectedChatModel }),
165-
messages,
166-
// Stop after 5 tool call steps
167-
stopWhen: stepCountIs(5),
168-
// Disable tools for reasoning models
169-
experimental_activeTools:
170-
selectedChatModel === "chat-model-reasoning" ? [] : ["getWeather"],
171-
// Add tools here!
172-
tools: {
173-
getWeather,
174-
},
175-
});
176-
177-
return result.toDataStreamResponse();
178-
}
157+
// Inside the POST handler, the streamText call should include:
158+
const result = streamText({
159+
model: myProvider.languageModel(selectedChatModel),
160+
system: systemPrompt({ selectedChatModel, requestHints }),
161+
messages: convertToModelMessages(uiMessages),
162+
// Stop after 5 tool call steps
163+
stopWhen: stepCountIs(5),
164+
// Disable tools for reasoning models
165+
experimental_activeTools:
166+
selectedChatModel === "chat-model-reasoning" ? [] : ["getWeather"],
167+
experimental_transform: smoothStream({ chunking: "word" }),
168+
// Add tools here!
169+
tools: {
170+
getWeather,
171+
},
172+
});
179173
```
180174

175+
The full route handler uses `createUIMessageStream` with `JsonToSseTransformStream` for streaming - the key thing is adding `getWeather` to the `tools` object and `"getWeather"` to `experimental_activeTools`.
176+
181177
### Key Configuration
182178

183179
- **`stopWhen: stepCountIs(5)`**: Limits tool call chains to 5 steps
184180
- **`experimental_activeTools`**: Conditionally enables/disables tools (disabled for reasoning model)
185181
- **`tools`**: Object containing all available tools
182+
- **`requestHints`**: Contains user's location (useful for "What's the weather?" without a city)
186183

187184
## How Tool Calling Works
188185

@@ -254,23 +251,39 @@ export function Weather({ current, current_units, cityName }: WeatherProps) {
254251
})}
255252
```
256253

257-
## Updating the System Prompt
254+
## Updating the System Prompt (Optional)
255+
256+
The AI will use tools based on their `description` field, so you don't *need* to update the system prompt. However, you can optionally add tool documentation to help the AI understand when to use tools.
258257

259-
Help the AI know when to use tools:
258+
The system prompt is built from multiple parts. The simplest way to add tool info is to update the `regularPrompt` constant:
260259

261260
```typescript
262261
// lib/ai/prompts.ts
263-
export const systemPrompt = () => `
264-
You are a helpful AI assistant.
262+
263+
// Update this constant to include tool documentation
264+
export const regularPrompt = `You are a friendly study buddy assistant! Keep your responses concise and helpful.
265265
266266
## Tools Available
267267
- **getWeather**: Use this when users ask about weather conditions.
268-
Ask for a city name if not provided.
269-
270-
Today's date is ${new Date().toLocaleDateString()}.
268+
You can provide a city name like "Paris" or "Tokyo".
271269
`;
270+
271+
// The systemPrompt function combines regularPrompt with location hints
272+
// No need to change this function - it already works!
273+
export const systemPrompt = ({
274+
selectedChatModel,
275+
requestHints,
276+
}: {
277+
selectedChatModel: string;
278+
requestHints: RequestHints;
279+
}) => {
280+
const requestPrompt = getRequestPromptFromHints(requestHints);
281+
return `${regularPrompt}\n\n${requestPrompt}`;
282+
};
272283
```
273284

285+
**Note**: The `requestHints` add the user's location context (city, country, lat/lon), which is useful for the weather tool - the AI can use the user's location as a default if they just say "What's the weather?"
286+
274287
## Try It Out: Weather Tool
275288

276289
Now that you've wired up the weather tool, test it with these prompts. Click the **"What is the weather in San Francisco?"** button in the chat, or try these variations:

0 commit comments

Comments
 (0)