-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathchains_parallel.php
More file actions
executable file
·229 lines (182 loc) · 8.36 KB
/
Copy pathchains_parallel.php
File metadata and controls
executable file
·229 lines (182 loc) · 8.36 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
#!/usr/bin/env php
<?php
/**
* Parallel Chains Example
*
* Demonstrates how to execute multiple chains in parallel
* with different aggregation strategies.
*/
declare(strict_types=1);
require_once __DIR__ . '/../vendor/autoload.php';
use ClaudeAgents\Chains\LLMChain;
use ClaudeAgents\Chains\ParallelChain;
use ClaudeAgents\Chains\TransformChain;
use ClaudeAgents\Prompts\PromptTemplate;
use ClaudePhp\ClaudePhp;
// Load environment
$dotenv = __DIR__ . '/../.env';
if (file_exists($dotenv)) {
$lines = file($dotenv, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
if (strpos(trim($line), '#') === 0) continue;
if (strpos($line, '=') === false) continue;
[$name, $value] = explode('=', $line, 2);
$_ENV[trim($name)] = trim($value);
}
}
$apiKey = $_ENV['ANTHROPIC_API_KEY'] ?? throw new RuntimeException('ANTHROPIC_API_KEY not set');
$client = new ClaudePhp(apiKey: $apiKey);
echo "╔════════════════════════════════════════════════════════════════════════════╗\n";
echo "║ Parallel Chains Example ║\n";
echo "╚════════════════════════════════════════════════════════════════════════════╝\n\n";
$sampleText = "I love this product! It's amazing and works perfectly. The customer service was excellent too.";
// ============================================================================
// Example 1: Parallel Analysis with Merge Strategy
// ============================================================================
echo "═══ Example 1: Parallel Analysis (Merge) ═══\n\n";
// Create multiple analysis chains
$sentimentChain = LLMChain::create($client)
->withPromptTemplate(PromptTemplate::create(
'Rate the sentiment of this text as positive, negative, or neutral (one word only): {text}'
))
->withMaxTokens(50);
$lengthChain = TransformChain::create(function (array $input): array {
$text = $input['text'] ?? '';
return [
'char_count' => strlen($text),
'word_count' => str_word_count($text),
];
});
$keywordsChain = LLMChain::create($client)
->withPromptTemplate(PromptTemplate::create(
'Extract 3 main keywords from this text (comma-separated): {text}'
))
->withMaxTokens(50);
// Execute all chains in parallel with merge aggregation
$parallelMerge = ParallelChain::create()
->addChain('sentiment', $sentimentChain)
->addChain('length', $lengthChain)
->addChain('keywords', $keywordsChain)
->withAggregation('merge');
echo "Text: \"$sampleText\"\n\n";
echo "Running parallel analysis...\n\n";
$mergeResult = $parallelMerge->invoke(['text' => $sampleText]);
echo "Results (Merged):\n";
foreach ($mergeResult as $key => $value) {
if ($key === 'metadata') continue; // Skip metadata
if (is_array($value)) {
echo "- $key: " . json_encode($value) . "\n";
} else {
echo "- $key: $value\n";
}
}
echo "\n";
// ============================================================================
// Example 2: First Success Strategy
// ============================================================================
echo "═══ Example 2: First Success Strategy ═══\n\n";
$fastChain = TransformChain::create(function (array $input): array {
return ['method' => 'fast', 'result' => 'Quick answer'];
});
$slowChain = LLMChain::create($client)
->withPromptTemplate(PromptTemplate::create('Analyze deeply: {text}'))
->withMaxTokens(100);
$parallelFirst = ParallelChain::create()
->addChain('fast', $fastChain)
->addChain('slow', $slowChain)
->withAggregation('first');
$firstResult = $parallelFirst->invoke(['text' => 'Sample text']);
echo "Using 'first' aggregation - returns first successful result:\n";
echo "Result: " . json_encode($firstResult) . "\n\n";
// ============================================================================
// Example 3: All Results Strategy
// ============================================================================
echo "═══ Example 3: All Results Strategy ═══\n\n";
$chain1 = TransformChain::create(fn($input) => ['analysis' => 'Type A']);
$chain2 = TransformChain::create(fn($input) => ['analysis' => 'Type B']);
$chain3 = TransformChain::create(fn($input) => ['analysis' => 'Type C']);
$parallelAll = ParallelChain::create()
->addChain('typeA', $chain1)
->addChain('typeB', $chain2)
->addChain('typeC', $chain3)
->withAggregation('all');
$allResult = $parallelAll->invoke(['text' => 'Sample']);
echo "Using 'all' aggregation - returns structured results:\n";
echo "Results:\n";
if (isset($allResult['results'])) {
foreach ($allResult['results'] as $name => $result) {
echo " - $name: " . json_encode($result) . "\n";
}
}
echo "\n";
// ============================================================================
// Example 4: Error Handling in Parallel Chains
// ============================================================================
echo "═══ Example 4: Error Handling ═══\n\n";
$successChain = TransformChain::create(function (array $input): array {
return ['status' => 'success', 'data' => 'Good result'];
});
$errorChain = TransformChain::create(function (array $input): array {
throw new RuntimeException('Intentional error for demo');
});
$parallelWithError = ParallelChain::create()
->addChain('success', $successChain)
->addChain('error', $errorChain)
->withAggregation('merge');
echo "Running chains with one that will fail...\n\n";
$errorResult = $parallelWithError->invoke(['test' => 'value']);
echo "Results (other chains still succeeded):\n";
foreach ($errorResult as $key => $value) {
if ($key === 'metadata') {
if (isset($value['errors'])) {
echo "Errors detected:\n";
foreach ($value['errors'] as $chainName => $error) {
echo " - $chainName: $error\n";
}
}
} else if (is_array($value)) {
echo "- $key: " . json_encode($value) . "\n";
} else {
echo "- $key: $value\n";
}
}
echo "\n";
// ============================================================================
// Example 5: Real-world Use Case - Multi-perspective Analysis
// ============================================================================
echo "═══ Example 5: Multi-perspective Analysis ═══\n\n";
$technicalChain = LLMChain::create($client)
->withPromptTemplate(PromptTemplate::create(
'Analyze from a technical perspective (1 sentence): {text}'
))
->withMaxTokens(100);
$businessChain = LLMChain::create($client)
->withPromptTemplate(PromptTemplate::create(
'Analyze from a business perspective (1 sentence): {text}'
))
->withMaxTokens(100);
$userChain = LLMChain::create($client)
->withPromptTemplate(PromptTemplate::create(
'Analyze from a user experience perspective (1 sentence): {text}'
))
->withMaxTokens(100);
$multiPerspective = ParallelChain::create()
->addChain('technical', $technicalChain)
->addChain('business', $businessChain)
->addChain('user', $userChain)
->withAggregation('all')
->withTimeout(60000);
$perspectives = $multiPerspective->invoke([
'text' => 'A new mobile app that helps users track their daily water intake.',
]);
echo "Product: Mobile app for tracking water intake\n\n";
echo "Multi-perspective Analysis:\n";
if (isset($perspectives['results'])) {
foreach ($perspectives['results'] as $perspective => $result) {
echo " $perspective: " . ($result['result'] ?? 'N/A') . "\n";
}
}
echo "\n";
echo "╔════════════════════════════════════════════════════════════════════════════╗\n";
echo "║ Examples Complete ║\n";
echo "╚════════════════════════════════════════════════════════════════════════════╝\n";