PHP code example of projectsaturnstudios / pocketflow-php

1. Go to this page and download the library: Download projectsaturnstudios/pocketflow-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

projectsaturnstudios / pocketflow-php example snippets



ProjectSaturnStudios\PocketFlowPHP\{Node, Flow};

class HelloNode extends Node 
{
    public function exec(mixed $prep_res): mixed 
    {
        return "Hello, " . ($prep_res['name'] ?? 'World') . "!";
    }
}

class OutputNode extends Node 
{
    public function prep(mixed &$shared): mixed 
    {
        return $shared; // Pass through shared data
    }
    
    public function exec(mixed $prep_res): mixed 
    {
        echo $prep_res['greeting'] ?? 'No greeting found';
        return 'done';
    }
    
    public function post(mixed &$shared, mixed $prep_res, mixed $exec_res): mixed 
    {
        return $exec_res;
    }
}

// Create nodes
$helloNode = new HelloNode();
$outputNode = new OutputNode();

// Chain them
$helloNode->next($outputNode, 'success');

// Create flow and run
$flow = new Flow($helloNode);
$shared = ['name' => 'PocketFlow'];

$result = $flow->_run($shared);


// Bring your own LLM client
use OpenAI\Client as OpenAIClient;

class LLMNode extends Node 
{
    public function __construct(private OpenAIClient $client) {}
    
    public function prep(mixed &$shared): mixed 
    {
        return ['prompt' => $shared['prompt'] ?? 'Say hello!'];
    }
    
    public function exec(mixed $prep_res): mixed 
    {
        $response = $this->client->chat()->create([
            'model' => 'gpt-3.5-turbo',
            'messages' => [
                ['role' => 'user', 'content' => $prep_res['prompt']]
            ]
        ]);
        
        return $response->choices[0]->message->content;
    }
    
    public function post(mixed &$shared, mixed $prep_res, mixed $exec_res): mixed 
    {
        $shared['llm_response'] = $exec_res;
        return 'success';
    }
}

// Usage
$client = OpenAI::client('your-api-key');
$llmNode = new LLMNode($client);
$outputNode = new OutputNode();

$llmNode->next($outputNode, 'success');
$flow = new Flow($llmNode);

$shared = ['prompt' => 'Write a haiku about PHP'];
$flow->_run($shared);


class ChatNode extends Node 
{
    public function __construct(private $llmClient) {}
    
    public function prep(mixed &$shared): mixed 
    {
        // Get user input
        echo "You: ";
        $input = trim(fgets(STDIN));
        
        if ($input === 'exit') {
            return ['action' => 'exit'];
        }
        
        $shared['messages'][] = ['role' => 'user', 'content' => $input];
        return ['messages' => $shared['messages']];
    }
    
    public function exec(mixed $prep_res): mixed 
    {
        if ($prep_res['action'] === 'exit') {
            return 'exit';
        }
        
        // Call your LLM here
        $response = $this->llmClient->chat($prep_res['messages']);
        return $response;
    }
    
    public function post(mixed &$shared, mixed $prep_res, mixed $exec_res): mixed 
    {
        if ($exec_res === 'exit') {
            echo "Goodbye!\n";
            return 'exit';
        }
        
        echo "AI: $exec_res\n\n";
        $shared['messages'][] = ['role' => 'assistant', 'content' => $exec_res];
        
        return 'continue'; // Self-loop
    }
}

// Create self-looping chat
$chatNode = new ChatNode($yourLLMClient);
$chatNode->next($chatNode, 'continue'); // Self-loop!

$flow = new Flow($chatNode);
$shared = ['messages' => []];
$flow->_run($shared);

$batchNode = new BatchNode();
$batchNode->setItems(['item1', 'item2', 'item3']);
$batchFlow = new BatchFlow($batchNode);

// composer ventLoop\Loop;

$asyncNode = new AsyncNode();
$asyncFlow = new AsyncFlow($asyncNode);
// Parallel execution with promises

$nodeA->next($nodeB, 'success');
$nodeA->next($nodeC, 'error'); 
$nodeA->next($nodeD, 'retry');
bash
composer