Skip to content

ZenPipe is a simple and flexible PHP pipeline library that allows you to chain operations together to process, transform, or act on input.

License

Notifications You must be signed in to change notification settings

dynamik-dev/zenpipe-php

Repository files navigation

ZenPipe

ZenPipe Logo

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

Sections

  1. ZenPipe
  2. Requirements
  3. Installation
  4. Usage
  5. Examples
  6. Contributing
  7. License
  8. Roadmap

Requirements

  • PHP 8.2 or higher

Installation

composer require dynamik-dev/zenpipe-php

Usage

Pipeline Operations

Pipeline 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)));

Class Methods as Operations

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']
    ]);

Examples

RAG Processes

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?");

Email Validation

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');

Contributing

See CONTRIBUTING.md for details.

License

The MIT License (MIT). See LICENSE for details.

Roadmap

  • Add support for PSR-15 middleware

About

ZenPipe is a simple and flexible PHP pipeline library that allows you to chain operations together to process, transform, or act on input.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages