-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtot_agent.php
More file actions
executable file
·475 lines (375 loc) · 18.6 KB
/
tot_agent.php
File metadata and controls
executable file
·475 lines (375 loc) · 18.6 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#!/usr/bin/env php
<?php
/**
* Tutorial 8: Tree of Thoughts (ToT) - Working Example
*
* Demonstrates the Tree of Thoughts pattern for exploring multiple
* reasoning paths, evaluating them, and backtracking when needed.
*/
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 8: Tree of Thoughts (ToT) - Multi-Path Reasoning ║\n";
echo "╚════════════════════════════════════════════════════════════════════════════╝\n\n";
// ============================================================================
// Helper Functions
// ============================================================================
/**
* Generate multiple thought branches
*/
function generateThoughts($client, $problem, $context = '', $count = 3) {
$prompt = "";
if ($context) {
$prompt .= "Context so far: {$context}\n\n";
}
$prompt .= "Problem: {$problem}\n\n";
$prompt .= "Generate {$count} different approaches or next steps. ";
$prompt .= "For each, provide:\n";
$prompt .= "1. The approach/step\n";
$prompt .= "2. Brief reasoning\n\n";
$prompt .= "Format as:\n";
$prompt .= "Approach 1: [description]\n";
$prompt .= "Reasoning: [why this might work]\n\n";
try {
$response = $client->messages()->create([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 1536,
'messages' => [
['role' => 'user', 'content' => $prompt]
]
]);
return extractTextContent($response);
} catch (Exception $e) {
return "Error generating thoughts: {$e->getMessage()}";
}
}
/**
* Evaluate a thought branch
*/
function evaluateThought($client, $thought, $problem) {
$prompt = "Problem: {$problem}\n\n";
$prompt .= "Proposed approach: {$thought}\n\n";
$prompt .= "Evaluate this approach on a scale of 1-10, considering:\n";
$prompt .= "- Likelihood of success (0-5 points)\n";
$prompt .= "- Efficiency/simplicity (0-5 points)\n\n";
$prompt .= "Provide:\n";
$prompt .= "Score: X/10\n";
$prompt .= "Reasoning: [brief explanation]";
try {
$response = $client->messages()->create([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 512,
'messages' => [
['role' => 'user', 'content' => $prompt]
]
]);
$text = extractTextContent($response);
// Extract score
if (preg_match('/Score:\s*(\d+)/', $text, $matches)) {
$score = (int)$matches[1];
} else {
$score = 5; // Default
}
return ['score' => $score, 'evaluation' => $text];
} catch (Exception $e) {
return ['score' => 0, 'evaluation' => "Error: {$e->getMessage()}"];
}
}
/**
* Visualize tree structure
*/
function visualizeTree($nodes, $indent = 0) {
foreach ($nodes as $i => $node) {
$prefix = str_repeat(" ", $indent);
$branch = $indent > 0 ? "├─ " : "";
echo $prefix . $branch . $node['label'];
if (isset($node['score'])) {
echo " [Score: {$node['score']}/10]";
}
echo "\n";
if (isset($node['children'])) {
visualizeTree($node['children'], $indent + 1);
}
}
}
// ============================================================================
// Example 1: Game of 24 Problem
// ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "Example 1: Game of 24 - Classic ToT Problem\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
$game24Problem = "Use the numbers 4, 6, 7, 8 exactly once with operations (+, -, ×, ÷) to make 24.";
echo "Problem: {$game24Problem}\n\n";
echo "Step 1: Generate initial approaches\n";
echo str_repeat("-", 80) . "\n";
$thoughts = generateThoughts($client, $game24Problem, '', 4);
echo $thoughts . "\n\n";
echo "Step 2: Let's evaluate one promising approach in detail\n";
echo str_repeat("-", 80) . "\n";
$approach = "Try: 6 ÷ (8 - 7) × 4";
echo "Evaluating: {$approach}\n\n";
$evaluation = evaluateThought($client, $approach, $game24Problem);
echo $evaluation['evaluation'] . "\n\n";
echo "Step 3: Execute the approach\n";
echo str_repeat("-", 80) . "\n";
$executePrompt = "Problem: {$game24Problem}\n\n";
$executePrompt .= "Approach: {$approach}\n\n";
$executePrompt .= "Execute this step by step and verify if it equals 24.";
try {
$response = $client->messages()->create([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 1024,
'messages' => [
['role' => 'user', 'content' => $executePrompt]
]
]);
echo extractTextContent($response) . "\n\n";
} catch (Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
echo "💡 ToT explores multiple paths and backtracks from unsuccessful ones!\n";
echo str_repeat("═", 80) . "\n\n";
// ============================================================================
// Example 2: Creative Writing with Branching
// ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "Example 2: Creative Writing - Exploring Story Paths\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
$storyStart = "A detective enters an abandoned mansion. " .
"The door slams shut behind her. " .
"She hears footsteps upstairs.";
echo "Story so far: {$storyStart}\n\n";
echo "Step 1: Generate possible continuations\n";
echo str_repeat("-", 80) . "\n";
$continuations = generateThoughts(
$client,
"Continue this mystery story in an interesting way",
$storyStart,
3
);
echo $continuations . "\n\n";
echo "Step 2: Evaluate continuations for drama and coherence\n";
echo str_repeat("-", 80) . "\n";
// For demo, evaluate one continuation
$sampleContinuation = "She draws her weapon and carefully climbs the stairs, " .
"noticing fresh muddy footprints that weren't there before.";
echo "Evaluating: \"{$sampleContinuation}\"\n\n";
$eval = evaluateThought(
$client,
$sampleContinuation,
"Continuation should be dramatic, coherent, and advance the mystery"
);
echo $eval['evaluation'] . "\n\n";
echo "💡 ToT helps explore creative options before committing to one path!\n";
echo str_repeat("═", 80) . "\n\n";
// ============================================================================
// Example 3: Logic Puzzle with Backtracking
// ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "Example 3: Logic Puzzle - Knights and Knaves\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
$puzzle = "On an island, knights always tell the truth and knaves always lie. " .
"You meet two people, A and B. " .
"A says: 'We are both knaves.' " .
"What are A and B?";
echo "Puzzle: {$puzzle}\n\n";
echo "Exploring multiple reasoning paths...\n";
echo str_repeat("-", 80) . "\n\n";
$systemPrompt = "You solve logic puzzles by exploring different possibilities. " .
"For each possibility, check if it leads to a contradiction.";
// Path exploration prompt
$explorationPrompt = "Puzzle: {$puzzle}\n\n" .
"Explore these possibilities:\n" .
"1. Assume A is a knight\n" .
"2. Assume A is a knave\n\n" .
"For each assumption, check if it's consistent with A's statement. " .
"Show your reasoning for each path.";
try {
$response = $client->messages()->create([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 1536,
'system' => $systemPrompt,
'messages' => [
['role' => 'user', 'content' => $explorationPrompt]
]
]);
echo extractTextContent($response) . "\n\n";
} catch (Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
echo "💡 ToT systematically explores logical possibilities!\n";
echo str_repeat("═", 80) . "\n\n";
// ============================================================================
// Example 4: Visualizing the Thought Tree
// ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "Example 4: Visualizing the Thought Tree\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
echo "Problem: Find the best route for a delivery\n\n";
// Simulated tree structure for visualization
$thoughtTree = [
[
'label' => 'Problem: Deliver to 3 locations',
'score' => null,
'children' => [
[
'label' => 'Route A: Location 1 → 2 → 3',
'score' => 7,
'children' => [
['label' => 'Distance: 15 km', 'score' => 6],
['label' => 'Time: 45 min', 'score' => 7]
]
],
[
'label' => 'Route B: Location 2 → 1 → 3',
'score' => 5,
'children' => [
['label' => 'Distance: 20 km', 'score' => 4],
['label' => 'Time: 60 min ✗ (too long)', 'score' => 3]
]
],
[
'label' => 'Route C: Location 1 → 3 → 2',
'score' => 9,
'children' => [
['label' => 'Distance: 12 km ✓', 'score' => 9],
['label' => 'Time: 35 min ✓ (best)', 'score' => 9]
]
]
]
]
];
echo "Thought Tree Visualization:\n";
echo str_repeat("-", 80) . "\n";
visualizeTree($thoughtTree);
echo "\n";
echo "Selected Path: Route C (highest scores)\n";
echo "Decision: Location 1 → 3 → 2 (12km, 35min)\n\n";
echo "💡 Visualizing helps understand exploration process!\n";
echo str_repeat("═", 80) . "\n\n";
// ============================================================================
// Example 5: Comparison - CoT vs ToT
// ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "Example 5: Comparing Chain of Thought vs Tree of Thoughts\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
$testProblem = "You have 9 identical-looking coins, but one is counterfeit and lighter. " .
"You have a balance scale. What's the minimum number of weighings needed?";
echo "Problem: {$testProblem}\n\n";
// CoT approach
echo "🔹 Chain of Thought (Single Path):\n";
echo str_repeat("-", 80) . "\n";
try {
$cotResponse = $client->messages()->create([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 1024,
'messages' => [
['role' => 'user', 'content' => $testProblem . "\n\nLet's solve this step by step."]
]
]);
$cotAnswer = extractTextContent($cotResponse);
echo substr($cotAnswer, 0, 300) . "...\n\n";
} catch (Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
// ToT approach
echo "🌳 Tree of Thoughts (Multiple Paths):\n";
echo str_repeat("-", 80) . "\n";
$totPrompt = "Problem: {$testProblem}\n\n" .
"Generate 3 different strategies for solving this. " .
"Then evaluate which strategy is most efficient.";
try {
$totResponse = $client->messages()->create([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 1536,
'messages' => [
['role' => 'user', 'content' => $totPrompt]
]
]);
echo extractTextContent($totResponse) . "\n\n";
} catch (Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
echo "💡 ToT explores alternatives before committing, often finding better solutions!\n";
echo str_repeat("═", 80) . "\n\n";
// ============================================================================
// Example 6: Practical Application - Code Optimization
// ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "Example 6: Code Optimization Strategies\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
$codeScenario = "We have a PHP function that processes 10,000 database records. " .
"It currently takes 30 seconds. How can we optimize it?";
echo "Scenario: {$codeScenario}\n\n";
echo "Exploring optimization strategies with ToT...\n";
echo str_repeat("-", 80) . "\n\n";
$strategiesPrompt = "Scenario: {$codeScenario}\n\n" .
"Generate 3 different optimization strategies. " .
"For each, explain:\n" .
"1. The approach\n" .
"2. Expected performance gain\n" .
"3. Implementation complexity\n" .
"4. Potential risks";
try {
$response = $client->messages()->create([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 1536,
'messages' => [
['role' => 'user', 'content' => $strategiesPrompt]
]
]);
echo extractTextContent($response) . "\n\n";
} catch (Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
echo "💡 ToT helps evaluate trade-offs between different technical approaches!\n";
echo str_repeat("═", 80) . "\n\n";
// ============================================================================
// Summary
// ============================================================================
echo "╔════════════════════════════════════════════════════════════════════════════╗\n";
echo "║ Tutorial Summary ║\n";
echo "╚════════════════════════════════════════════════════════════════════════════╝\n\n";
echo "✅ Tree of Thoughts Concepts Demonstrated:\n\n";
echo "1️⃣ Multi-Path Exploration\n";
echo " • Generate multiple approaches\n";
echo " • Don't commit to first idea\n";
echo " • Explore alternatives\n\n";
echo "2️⃣ Evaluation & Scoring\n";
echo " • Rate each approach\n";
echo " • Compare options objectively\n";
echo " • Select most promising paths\n\n";
echo "3️⃣ Backtracking\n";
echo " • Recognize dead ends\n";
echo " • Abandon unsuccessful paths\n";
echo " • Try alternative approaches\n\n";
echo "4️⃣ Systematic Exploration\n";
echo " • BFS, DFS, or best-first\n";
echo " • Structured search\n";
echo " • Complete coverage\n\n";
echo "5️⃣ Applications\n";
echo " • Puzzles and games\n";
echo " • Creative writing\n";
echo " • Logic problems\n";
echo " • Optimization tasks\n";
echo " • Strategic planning\n\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
echo "🎯 When to Use Tree of Thoughts:\n\n";
echo " ✓ Complex problems with multiple solutions\n";
echo " ✓ When first idea might not be best\n";
echo " ✓ Puzzles requiring exploration\n";
echo " ✓ Strategic decisions with trade-offs\n";
echo " ✓ Creative tasks needing options\n\n";
echo "⚠️ ToT Limitations:\n\n";
echo " ✗ More expensive (multiple API calls)\n";
echo " ✗ Takes longer than CoT\n";
echo " ✗ Overkill for simple problems\n";
echo " ✗ Requires good evaluation functions\n\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
echo "🚀 ToT enables sophisticated multi-path reasoning!\n\n";
echo "Next: Tutorial 9 - Plan-and-Execute for structured action\n";
echo "→ tutorials/09-plan-and-execute/\n\n";