ZenPipe is a simple and flexible PHP pipeline library that allows you to chain operations together to process, transform, or act on input.
$calculator = zenpipe()
->pipe(fn($price, $next) => $next($price * 0.8)) // 20% discount
->pipe(fn($price, $next) => $next($price * 1.1)); // add 10% tax
$calculator(100); // $88 (100 -> 80 -> 88)You can also run the pipeline on demand:
zenpipe(100)
->pipe(fn($price, $next) => $next($price * 0.8)) // 20% discount
->pipe(fn($price, $next) => $next($price * 1.1)) // add 10% tax
->process(); // 88- PHP 8.2 or higher
composer require dynamik-dev/zenpipe-phpPipeline operations are functions that take an input and return a processed value. They can be passed as a single function or as an array with the class name and method name.
$pipeline = zenpipe()
->pipe(fn($input, $next) => $next(strtoupper($input)));You can also use class methods as operations:
class MyClass
{
public function uppercase($input)
{
return strtoupper($input);
}
}
$pipeline = zenpipe()
->pipe([MyClass::class, 'uppercase']);You can also pass an array of operations:
$pipeline = zenpipe()
->pipe([
fn($input, $next) => $next(strtoupper($input)),
[MyClass::class, 'uppercase']
]);This pipeline can be used for RAG processes, where the output of one model is used as input for another.
$ragPipeline = zenpipe()
->pipe(fn($query, $next) => $next([
'query' => $query,
'embeddings' => OpenAI::embeddings()->create([
'model' => 'text-embedding-3-small',
'input' => $query
])->embeddings[0]->embedding
]))
->pipe(fn($data, $next) => $next([
...$data,
'context' => Qdrant::collection('knowledge-base')
->search($data['embeddings'], limit: 3)
->map(fn($doc) => $doc->content)
->join("\n")
]))
->pipe(fn($data, $next) => $next(
OpenAI::chat()->create([
'model' => 'gpt-4-turbo-preview',
'messages' => [
[
'role' => 'system',
'content' => 'Answer using the provided context only.'
],
[
'role' => 'user',
'content' => "Context: {$data['context']}\n\nQuery: {$data['query']}"
]
]
])->choices[0]->message->content
));
$answer = $ragPipeline("What's our refund policy?");This pipeline can be used to validate an email address.
$emailValidationPipeline = zenpipe()
->pipe(function($input, $next) {
return $next(filter_var($input, FILTER_VALIDATE_EMAIL));
})
->pipe(function($email, $next) {
if (!$email) {
return false;
}
$domain = substr(strrchr($email, "@"), 1);
$mxhosts = [];
$mxweight = [];
if (getmxrr($domain, $mxhosts, $mxweight)) {
return $next(true);
}
// If MX records don't exist, check for A record as a fallback
return $next(checkdnsrr($domain, 'A'));
});
$result = $emailValidationPipeline('example@example.com');See CONTRIBUTING.md for details.
The MIT License (MIT). See LICENSE for details.
- Add support for PSR-15 middleware
