PHP code example of langgraph / langgraph-php

1. Go to this page and download the library: Download langgraph/langgraph-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/ */

    

langgraph / langgraph-php example snippets


use LangGraph\UnifiedGraph\StateGraph;
use LangGraph\UnifiedGraph\State\State;

// 1. Define the state structure for your graph
class MyState extends State {
    // Your state properties here
}

// 2. Initialize the graph with the state class
$graph = new StateGraph(MyState::class);

// 3. Add nodes, which are functions that modify the state
$graph->addNode('start', function (MyState $state) {
    echo "Executing node: start\n";
    $state->set('message', 'Hello from the start!');
    return $state;
});

$graph->addNode('middle', function (MyState $state) {
    echo "Executing node: middle\n";
    $state->set('message', $state->get('message') . ' ... and now the middle!');
    return $state;
});

$graph->addNode('end', function (MyState $state) {
    echo "Executing node: end\n";
    $state->set('message', $state->get('message') . ' ... finished!');
    return $state;
});

// 4. Define the workflow by adding edges
$graph->addEdge('start', 'middle');
$graph->addEdge('middle', 'end');

// 5. Set the entry and finish points
$graph->setEntryPoint('start');
$graph->setFinishPoint('end');

// 6. Compile the graph and run it
$runnable = $graph->compile();
$initialState = new MyState(['value' => 1]);
$finalState = $runnable->execute($initialState);

print_r($finalState->getData());
// Outputs: [ 'value' => 1, 'message' => 'Hello from the start! ... and now the middle! ... finished!' ]

// A function to decide the next step
$decider = function (MyState $state) {
    if ($state->get('value', 0) > 5) {
        return 'end_workflow';
    } else {
        return 'continue_processing';
    }
};

// Add a conditional edge from 'start'
$graph->addConditionalEdges('start', $decider, [
    'end_workflow' => 'end',
    'continue_processing' => 'middle',
]);

use LangGraph\Agent\AgentFactory;
use LangGraph\Model\Factory\ModelFactory;
use LangGraph\Model\Config\ModelConfig;

// Setup factories
$modelConfig = new ModelConfig(delBasedAgent(
    'researcher',
    'deepseek', // Or another model like 'qwen'
    'You are a helpful research assistant.'
);

// Use the agent to process a query
$response = $researcherAgent->execute('What is LangGraph?');
echo $response;

use LangGraph\UnifiedGraph\Checkpoint\MemoryCheckpointSaver;

// Set a checkpoint saver on the graph
$checkpointSaver = new MemoryCheckpointSaver();
$graph->setCheckpointSaver($checkpointSaver);

$runnable = $graph->compile();

// Run the workflow with a unique ID to enable checkpointing
$threadId = 'user_session_123';
$initialState = new MyState(['value' => 1]);
$finalState = $runnable->execute($initialState, $threadId);

// You can now retrieve checkpoints for this thread
$checkpoints = $checkpointSaver->list($threadId);
bash
composer