-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcot_agent.php
More file actions
executable file
·349 lines (273 loc) · 15.4 KB
/
cot_agent.php
File metadata and controls
executable file
·349 lines (273 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/env php
<?php
/**
* Tutorial 7: Chain of Thought (CoT) - Working Example
*
* Demonstrates Chain of Thought prompting for step-by-step reasoning
* without external tools. Shows zero-shot, few-shot, and complex reasoning.
*/
require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../../examples/helpers.php';
require_once __DIR__ . '/../helpers.php';
use ClaudePhp\ClaudePhp;
loadEnv(__DIR__ . '/../../.env');
$client = new ClaudePhp(apiKey: getApiKey());
echo "╔════════════════════════════════════════════════════════════════════════════╗\n";
echo "║ Tutorial 7: Chain of Thought (CoT) Reasoning ║\n";
echo "╚════════════════════════════════════════════════════════════════════════════╝\n\n";
// ============================================================================
// Example 1: Zero-Shot CoT - Math Word Problem
// ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "Example 1: Zero-Shot CoT - Math Word Problem\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
$mathProblem = "A baker makes 24 cupcakes. She sells them in boxes of 6. " .
"If each box costs \$12, how much money does she make in total?";
echo "Problem: {$mathProblem}\n\n";
// Test without CoT first
echo "📋 Without Chain of Thought:\n";
echo str_repeat("-", 80) . "\n";
try {
$response = $client->messages()->create([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 512,
'messages' => [
['role' => 'user', 'content' => $mathProblem]
]
]);
echo extractTextContent($response) . "\n\n";
} catch (Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
// Now with CoT
echo "🧠 With Chain of Thought (Zero-Shot):\n";
echo str_repeat("-", 80) . "\n";
try {
$cotPrompt = $mathProblem . "\n\nLet's solve this step by step.";
$response = $client->messages()->create([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 1024,
'messages' => [
['role' => 'user', 'content' => $cotPrompt]
]
]);
echo extractTextContent($response) . "\n\n";
echo "💡 Notice: With CoT, we see the complete reasoning process!\n";
} catch (Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
echo str_repeat("═", 80) . "\n\n";
// ============================================================================
// Example 2: Few-Shot CoT - Providing Examples
// ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "Example 2: Few-Shot CoT - Learning from Examples\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
// Create a system prompt with examples
$fewShotSystem = "You are a math tutor who solves problems step by step. " .
"Here are examples of how to approach problems:\n\n" .
"Example 1:\n" .
"Q: If a book costs \$15 and is on 20% discount, what's the sale price?\n" .
"A: Let me work through this:\n" .
" Step 1: Calculate the discount amount: 20% of \$15 = \$15 × 0.20 = \$3\n" .
" Step 2: Subtract discount from original price: \$15 - \$3 = \$12\n" .
" Final Answer: The sale price is \$12\n\n" .
"Example 2:\n" .
"Q: A car travels at 60 mph for 2.5 hours. How far does it go?\n" .
"A: Let me solve this step by step:\n" .
" Step 1: Use the formula Distance = Speed × Time\n" .
" Step 2: Plug in the values: Distance = 60 mph × 2.5 hours\n" .
" Step 3: Calculate: Distance = 150 miles\n" .
" Final Answer: The car travels 150 miles\n\n" .
"Now solve problems using this same step-by-step format.";
$newProblem = "A pizza is cut into 8 slices. If a family of 4 people each eats 2 slices, " .
"what fraction of the pizza is left?";
echo "Problem: {$newProblem}\n\n";
echo "Using few-shot examples to guide reasoning format...\n\n";
try {
$response = $client->messages()->create([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 1024,
'system' => $fewShotSystem,
'messages' => [
['role' => 'user', 'content' => $newProblem]
]
]);
echo extractTextContent($response) . "\n\n";
echo "💡 Notice: The response follows the same structured format as the examples!\n";
} catch (Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
echo str_repeat("═", 80) . "\n\n";
// ============================================================================
// Example 3: Complex Reasoning - Logic Puzzle
// ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "Example 3: Complex Reasoning - Logic Puzzle\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
$logicPuzzle = "Three friends Alice, Bob, and Carol are sitting in a row. " .
"Alice is not sitting next to Carol. " .
"Bob is sitting to the right of Alice. " .
"Who is sitting in the middle?";
echo "Puzzle: {$logicPuzzle}\n\n";
echo "Solving with structured reasoning...\n\n";
$structuredSystem = "You are a logic expert. For each problem:\n" .
"1. Identify the constraints\n" .
"2. List possible arrangements\n" .
"3. Eliminate invalid options\n" .
"4. Determine the solution\n" .
"5. Verify the answer";
try {
$response = $client->messages()->create([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 1024,
'system' => $structuredSystem,
'messages' => [
['role' => 'user', 'content' => $logicPuzzle]
]
]);
echo extractTextContent($response) . "\n\n";
} catch (Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
echo str_repeat("═", 80) . "\n\n";
// ============================================================================
// Example 4: Multi-Step Problem with Verification
// ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "Example 4: Multi-Step Problem with Self-Verification\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
$complexProblem = "A store offers this deal: Buy 2 get 1 free on items priced at \$20 each. " .
"Sarah wants to buy 5 items. How much will she pay?";
echo "Problem: {$complexProblem}\n\n";
$verificationSystem = "You solve problems step by step, then verify your answer. " .
"Format:\n" .
"1. Understand the problem\n" .
"2. Plan the solution\n" .
"3. Execute step by step\n" .
"4. Verify by checking your work\n" .
"5. State final answer";
try {
$response = $client->messages()->create([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 1024,
'system' => $verificationSystem,
'messages' => [
['role' => 'user', 'content' => $complexProblem]
]
]);
echo extractTextContent($response) . "\n\n";
echo "💡 Notice: Self-verification helps catch errors!\n";
} catch (Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
echo str_repeat("═", 80) . "\n\n";
// ============================================================================
// Example 5: Comparing Different CoT Approaches
// ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "Example 5: Comparing CoT Trigger Phrases\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
$testProblem = "If you have 3 apples and buy 2 more, then give away half, how many do you have?";
echo "Problem: {$testProblem}\n\n";
$triggerPhrases = [
"Let's think step by step.",
"Let's work this out systematically.",
"Let's break this down.",
"Let's approach this logically."
];
foreach ($triggerPhrases as $i => $phrase) {
echo "Trigger " . ($i + 1) . ": \"{$phrase}\"\n";
echo str_repeat("-", 80) . "\n";
try {
$response = $client->messages()->create([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 512,
'messages' => [
['role' => 'user', 'content' => $testProblem . "\n\n" . $phrase]
]
]);
$answer = extractTextContent($response);
// Show first 150 characters
echo substr($answer, 0, 150);
if (strlen($answer) > 150) echo "...";
echo "\n\n";
} catch (Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
}
echo "💡 All trigger phrases work, but some may produce more detailed reasoning.\n";
echo str_repeat("═", 80) . "\n\n";
// ============================================================================
// Example 6: CoT for Non-Mathematical Reasoning
// ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "Example 6: CoT for Decision Making\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
$decisionProblem = "Should a small business choose MySQL or PostgreSQL for their database? " .
"Consider: ease of use, performance, cost, community support, and scalability.";
echo "Decision: {$decisionProblem}\n\n";
$decisionSystem = "You are a technical consultant. Analyze decisions by:\n" .
"1. Identifying key factors\n" .
"2. Evaluating each option against each factor\n" .
"3. Weighing pros and cons\n" .
"4. Making a recommendation with reasoning";
try {
$response = $client->messages()->create([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 1536,
'system' => $decisionSystem,
'messages' => [
['role' => 'user', 'content' => $decisionProblem]
]
]);
echo extractTextContent($response) . "\n\n";
echo "💡 CoT works for qualitative reasoning, not just math!\n";
} catch (Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
echo str_repeat("═", 80) . "\n\n";
// ============================================================================
// Summary
// ============================================================================
echo "╔════════════════════════════════════════════════════════════════════════════╗\n";
echo "║ Tutorial Summary ║\n";
echo "╚════════════════════════════════════════════════════════════════════════════╝\n\n";
echo "✅ Chain of Thought Techniques Demonstrated:\n\n";
echo "1️⃣ Zero-Shot CoT\n";
echo " • Simple trigger phrases\n";
echo " • 'Let's think step by step'\n";
echo " • Works without examples\n\n";
echo "2️⃣ Few-Shot CoT\n";
echo " • Provide reasoning examples\n";
echo " • Consistent output format\n";
echo " • Better structured responses\n\n";
echo "3️⃣ Structured Reasoning\n";
echo " • Define reasoning steps\n";
echo " • System prompts guide process\n";
echo " • Comprehensive analysis\n\n";
echo "4️⃣ Self-Verification\n";
echo " • Check work after solving\n";
echo " • Catch potential errors\n";
echo " • Increase confidence\n\n";
echo "5️⃣ Flexible Applications\n";
echo " • Math problems\n";
echo " • Logic puzzles\n";
echo " • Decision making\n";
echo " • Analysis tasks\n\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
echo "🎯 When to Use Chain of Thought:\n\n";
echo " ✓ Problems requiring logical reasoning\n";
echo " ✓ When transparency is important\n";
echo " ✓ Educational contexts\n";
echo " ✓ No external tools needed\n";
echo " ✓ Step-by-step explanation valuable\n\n";
echo "⚠️ When NOT to Use CoT Alone:\n\n";
echo " ✗ Need exact calculations (use calculator tools)\n";
echo " ✗ Require real-time data (use web search)\n";
echo " ✗ Complex computations (use specialized tools)\n";
echo " ✗ External API calls needed (use ReAct)\n\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
echo "🚀 CoT is powerful for pure reasoning tasks!\n\n";
echo "Next: Tutorial 8 - Tree of Thoughts for multi-path exploration\n";
echo "→ tutorials/08-tree-of-thoughts/\n\n";