-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtot_example.php
More file actions
163 lines (122 loc) · 4.78 KB
/
tot_example.php
File metadata and controls
163 lines (122 loc) · 4.78 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
#!/usr/bin/env php
<?php
/**
* Tree of Thoughts (ToT) Example
*
* Demonstrates tree-based exploration for complex problem solving.
* Shows different search strategies with branching and evaluation.
*/
declare(strict_types=1);
require_once __DIR__ . '/../vendor/autoload.php';
use ClaudeAgents\Agents\TreeOfThoughtsAgent;
use ClaudePhp\ClaudePhp;
// Initialize Claude client
$apiKey = getenv('ANTHROPIC_API_KEY');
if (!$apiKey) {
die("Error: ANTHROPIC_API_KEY environment variable not set\n");
}
$client = new ClaudePhp(apiKey: $apiKey);
echo "=== Tree-of-Thoughts Examples ===\n\n";
// Example 1: Best-first search
echo "--- Example 1: Best-First Search ---\n";
echo "Strategy: Explores the most promising branches first\n\n";
$agent = new TreeOfThoughtsAgent($client, [
'branch_count' => 3,
'max_depth' => 3,
'search_strategy' => 'best_first',
'name' => 'tot_best_first',
]);
$task = "Use the numbers 3, 5, 7, 11 with basic operations (+, -, *, /) to make 24";
echo "Task: {$task}\n\n";
$result = $agent->run($task);
if ($result->isSuccess()) {
echo "Result:\n" . $result->getAnswer() . "\n";
$metadata = $result->getMetadata();
echo "\nMetadata:\n";
echo " Strategy: " . $metadata['strategy'] . "\n";
echo " Total Nodes: " . $metadata['total_nodes'] . "\n";
echo " Max Depth: " . $metadata['max_depth'] . "\n";
echo " Path Length: " . $metadata['path_length'] . "\n";
echo " Best Score: " . round($metadata['best_score'], 2) . "\n";
echo " Tokens Used: " . ($metadata['tokens']['input'] + $metadata['tokens']['output']) . "\n";
} else {
echo "Error: " . $result->getError() . "\n";
}
echo "\n" . str_repeat("=", 60) . "\n\n";
// Example 2: Breadth-first search
echo "--- Example 2: Breadth-First Search ---\n";
echo "Strategy: Explores all branches at each level before going deeper\n\n";
$agent = new TreeOfThoughtsAgent($client, [
'branch_count' => 2,
'max_depth' => 2,
'search_strategy' => 'breadth_first',
'name' => 'tot_breadth_first',
]);
$task = "Design a simple mobile app for grocery shopping. What are the key features?";
echo "Task: {$task}\n\n";
$result = $agent->run($task);
if ($result->isSuccess()) {
echo "Result:\n" . $result->getAnswer() . "\n";
$metadata = $result->getMetadata();
echo "\nMetadata:\n";
echo " Strategy: " . $metadata['strategy'] . "\n";
echo " Total Nodes: " . $metadata['total_nodes'] . "\n";
echo " Max Depth: " . $metadata['max_depth'] . "\n";
} else {
echo "Error: " . $result->getError() . "\n";
}
echo "\n" . str_repeat("=", 60) . "\n\n";
// Example 3: Depth-first search
echo "--- Example 3: Depth-First Search ---\n";
echo "Strategy: Explores one branch fully before moving to others\n\n";
$agent = new TreeOfThoughtsAgent($client, [
'branch_count' => 2,
'max_depth' => 3,
'search_strategy' => 'depth_first',
'name' => 'tot_depth_first',
]);
$task = "Plan a 3-day trip to San Francisco. What should I prioritize?";
echo "Task: {$task}\n\n";
$result = $agent->run($task);
if ($result->isSuccess()) {
echo "Result:\n" . $result->getAnswer() . "\n";
$metadata = $result->getMetadata();
echo "\nMetadata:\n";
echo " Strategy: " . $metadata['strategy'] . "\n";
echo " Total Nodes: " . $metadata['total_nodes'] . "\n";
} else {
echo "Error: " . $result->getError() . "\n";
}
echo "\n" . str_repeat("=", 60) . "\n\n";
// Example 4: Complex problem solving
echo "--- Example 4: Complex Problem Solving ---\n";
echo "Strategy: Best-first with deeper exploration\n\n";
$agent = new TreeOfThoughtsAgent($client, [
'branch_count' => 4,
'max_depth' => 4,
'search_strategy' => 'best_first',
'name' => 'tot_complex',
]);
$task = "How can a small e-commerce business reduce shipping costs while maintaining customer satisfaction?";
echo "Task: {$task}\n\n";
$result = $agent->run($task);
if ($result->isSuccess()) {
echo "Result:\n" . $result->getAnswer() . "\n";
$metadata = $result->getMetadata();
echo "\nMetadata:\n";
echo " Strategy: " . $metadata['strategy'] . "\n";
echo " Total Nodes: " . $metadata['total_nodes'] . "\n";
echo " Max Depth: " . $metadata['max_depth'] . "\n";
echo " Path Length: " . $metadata['path_length'] . "\n";
echo " Best Score: " . round($metadata['best_score'], 2) . "/10\n";
} else {
echo "Error: " . $result->getError() . "\n";
}
echo "\n" . str_repeat("=", 60) . "\n\n";
echo "=== Tree-of-Thoughts Complete ===\n\n";
echo "Key Takeaways:\n";
echo " - Best-first: Most efficient for finding optimal solutions\n";
echo " - Breadth-first: Good for exploring all options at each level\n";
echo " - Depth-first: Useful for following one line of reasoning deeply\n";
echo " - Branch count and max depth control exploration vs. token cost\n";
echo "\n";