-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcot_example.php
More file actions
208 lines (160 loc) · 8.08 KB
/
Copy pathcot_example.php
File metadata and controls
208 lines (160 loc) · 8.08 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
#!/usr/bin/env php
<?php
/**
* Chain of Thought (CoT) Example
*
* Demonstrates reasoning patterns using Chain of Thought prompting.
* Shows both zero-shot and few-shot CoT approaches for complex problem solving.
*
* Usage: php examples/cot_example.php
*
* Make sure ANTHROPIC_API_KEY is set in your .env file
*/
declare(strict_types=1);
require_once __DIR__ . '/../vendor/autoload.php';
use ClaudeAgents\Agents\ChainOfThoughtAgent;
use ClaudeAgents\Reasoning\CoTPrompts;
use ClaudePhp\ClaudePhp;
// Initialize Claude client
$apiKey = getenv('ANTHROPIC_API_KEY');
if (!$apiKey) {
echo "Error: ANTHROPIC_API_KEY not found in environment.\n";
echo "Please set it as an environment variable:\n";
echo " export ANTHROPIC_API_KEY='your-api-key'\n";
echo "Or run with: ANTHROPIC_API_KEY='your-api-key' php examples/cot_example.php\n";
exit(1);
}
$client = new ClaudePhp(apiKey: $apiKey);
echo "╔════════════════════════════════════════════════════════════════╗\n";
echo "║ Chain-of-Thought (CoT) Reasoning Examples ║\n";
echo "╚════════════════════════════════════════════════════════════════╝\n\n";
// Example 1: Zero-shot CoT for Math
echo "━━━ Example 1: Zero-Shot CoT (Math Problem) ━━━\n\n";
$agent1 = new ChainOfThoughtAgent($client, [
'mode' => 'zero_shot',
'trigger' => CoTPrompts::zeroShotTrigger(),
'name' => 'zero_shot_math',
]);
$task1 = "A bakery makes 24 cupcakes per batch. They sell them in boxes of 6. If each box costs \$12, how much money do they make per batch?";
echo "📝 Task: {$task1}\n\n";
$result1 = $agent1->run($task1);
if ($result1->isSuccess()) {
echo "💡 Answer:\n{$result1->getAnswer()}\n\n";
echo "📊 Metadata:\n";
echo " - Iterations: {$result1->getIterations()}\n";
echo " - Mode: {$result1->getMetadata()['reasoning_mode']}\n";
echo " - Tokens: {$result1->getMetadata()['tokens']['input']} in, {$result1->getMetadata()['tokens']['output']} out\n\n";
} else {
echo "❌ Error: {$result1->getError()}\n\n";
}
// Example 2: Few-shot CoT with Math Examples
echo "━━━ Example 2: Few-Shot CoT (With Examples) ━━━\n\n";
$agent2 = new ChainOfThoughtAgent($client, [
'mode' => 'few_shot',
'examples' => CoTPrompts::mathExamples(),
'name' => 'few_shot_math',
]);
$task2 = "If a store has a 30% discount and an item originally costs \$50, what is the sale price after adding 8% sales tax?";
echo "📝 Task: {$task2}\n\n";
$result2 = $agent2->run($task2);
if ($result2->isSuccess()) {
echo "💡 Answer:\n{$result2->getAnswer()}\n\n";
echo "📊 Metadata:\n";
echo " - Iterations: {$result2->getIterations()}\n";
echo " - Mode: {$result2->getMetadata()['reasoning_mode']}\n";
echo " - Tokens: {$result2->getMetadata()['tokens']['input']} in, {$result2->getMetadata()['tokens']['output']} out\n\n";
} else {
echo "❌ Error: {$result2->getError()}\n\n";
}
// Example 3: Logic Problem with CoT
echo "━━━ Example 3: Zero-Shot CoT (Logic Problem) ━━━\n\n";
$agent3 = new ChainOfThoughtAgent($client, [
'mode' => 'zero_shot',
'trigger' => "Let's approach this logically.",
'name' => 'zero_shot_logic',
]);
$task3 = "Five people (Alice, Bob, Carol, David, Eve) finish a race. Alice finishes before Bob but after Carol. David finishes after Bob. Eve finishes before Carol. Who won the race?";
echo "📝 Task: {$task3}\n\n";
$result3 = $agent3->run($task3);
if ($result3->isSuccess()) {
echo "💡 Answer:\n{$result3->getAnswer()}\n\n";
echo "📊 Metadata:\n";
echo " - Iterations: {$result3->getIterations()}\n";
echo " - Mode: {$result3->getMetadata()['reasoning_mode']}\n";
echo " - Tokens: {$result3->getMetadata()['tokens']['input']} in, {$result3->getMetadata()['tokens']['output']} out\n\n";
} else {
echo "❌ Error: {$result3->getError()}\n\n";
}
// Example 4: Few-shot CoT with Logic Examples
echo "━━━ Example 4: Few-Shot CoT (Logic with Examples) ━━━\n\n";
$agent4 = new ChainOfThoughtAgent($client, [
'mode' => 'few_shot',
'examples' => CoTPrompts::logicExamples(),
'name' => 'few_shot_logic',
]);
$task4 = "Three boxes are labeled 'Apples', 'Oranges', and 'Mixed'. All labels are incorrect. You can pick one fruit from one box. How do you correctly label all boxes?";
echo "📝 Task: {$task4}\n\n";
$result4 = $agent4->run($task4);
if ($result4->isSuccess()) {
echo "💡 Answer:\n{$result4->getAnswer()}\n\n";
echo "📊 Metadata:\n";
echo " - Iterations: {$result4->getIterations()}\n";
echo " - Mode: {$result4->getMetadata()['reasoning_mode']}\n";
echo " - Tokens: {$result4->getMetadata()['tokens']['input']} in, {$result4->getMetadata()['tokens']['output']} out\n\n";
} else {
echo "❌ Error: {$result4->getError()}\n\n";
}
// Example 5: Decision Making with CoT
echo "━━━ Example 5: Few-Shot CoT (Decision Making) ━━━\n\n";
$agent5 = new ChainOfThoughtAgent($client, [
'mode' => 'few_shot',
'examples' => CoTPrompts::decisionExamples(),
'name' => 'few_shot_decision',
]);
$task5 = "A small business needs to choose between hiring a full-time developer at \$80k/year or using freelancers at \$50/hour. They estimate needing 30 hours of work per week. Which option should they choose?";
echo "📝 Task: {$task5}\n\n";
$result5 = $agent5->run($task5);
if ($result5->isSuccess()) {
echo "💡 Answer:\n{$result5->getAnswer()}\n\n";
echo "📊 Metadata:\n";
echo " - Iterations: {$result5->getIterations()}\n";
echo " - Mode: {$result5->getMetadata()['reasoning_mode']}\n";
echo " - Tokens: {$result5->getMetadata()['tokens']['input']} in, {$result5->getMetadata()['tokens']['output']} out\n\n";
} else {
echo "❌ Error: {$result5->getError()}\n\n";
}
// Example 6: Custom Trigger Phrase
echo "━━━ Example 6: Custom Trigger Phrase ━━━\n\n";
$agent6 = new ChainOfThoughtAgent($client, [
'mode' => 'zero_shot',
'trigger' => "Let's break this down systematically and show all our work.",
'name' => 'custom_trigger',
]);
$task6 = "If I read 15 pages per day for 3 weeks, and then 20 pages per day for 2 weeks, how many pages total did I read?";
echo "📝 Task: {$task6}\n\n";
$result6 = $agent6->run($task6);
if ($result6->isSuccess()) {
echo "💡 Answer:\n{$result6->getAnswer()}\n\n";
echo "📊 Metadata:\n";
echo " - Iterations: {$result6->getIterations()}\n";
echo " - Mode: {$result6->getMetadata()['reasoning_mode']}\n";
echo " - Tokens: {$result6->getMetadata()['tokens']['input']} in, {$result6->getMetadata()['tokens']['output']} out\n\n";
} else {
echo "❌ Error: {$result6->getError()}\n\n";
}
// Summary
echo "╔════════════════════════════════════════════════════════════════╗\n";
echo "║ Summary ║\n";
echo "╚════════════════════════════════════════════════════════════════╝\n\n";
$results = [$result1, $result2, $result3, $result4, $result5, $result6];
$successful = array_filter($results, fn($r) => $r->isSuccess());
echo "Total Examples: " . count($results) . "\n";
echo "Successful: " . count($successful) . "\n";
echo "Failed: " . (count($results) - count($successful)) . "\n\n";
$totalTokensIn = array_sum(array_map(fn($r) => $r->getMetadata()['tokens']['input'] ?? 0, $successful));
$totalTokensOut = array_sum(array_map(fn($r) => $r->getMetadata()['tokens']['output'] ?? 0, $successful));
echo "Total Tokens Used:\n";
echo " - Input: {$totalTokensIn}\n";
echo " - Output: {$totalTokensOut}\n";
echo " - Total: " . ($totalTokensIn + $totalTokensOut) . "\n\n";
echo "✅ All examples completed!\n";