PHP code example of tourze / dify-dsl

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

    

tourze / dify-dsl example snippets




use Tourze\DifyDsl\Builder\WorkflowBuilder;
use Tourze\DifyDsl\Generator\DifyGenerator;

// Create an AI assistant workflow
$app = WorkflowBuilder::create()
    ->setName("AI Assistant")
    ->setDescription("A helpful AI assistant")
    ->setMode("workflow")
    ->setIcon("🤖")

    // Add start node
    ->addStartNode(function($node) {
        $node->addVariableFromArray("query", "text-input", true, "User query");
        $node->addVariableFromArray("context", "paragraph", false, "Context information");
    })

    // Add LLM node
    ->addLLMNode(null, function($node) {
        $node->setTitle("Intelligent Response")
             ->setModel("gpt-4", "openai", "chat")
             ->setSystemPrompt("You are a helpful AI assistant")
             ->setUserPrompt("User query: {{#start.query#}}\n\nContext: {{#start.context#}}");
    })

    // Add end node
    ->addEndNode(function($node) {
        $node->addOutput("result", ["llm", "text"]);
    })

    ->build();

// Generate YAML
$generator = new DifyGenerator();
$yaml = $generator->generatePretty($app);
echo $yaml;



use Tourze\DifyDsl\Parser\DifyParser;

$parser = new DifyParser();
$app = $parser->parseFile('workflow.yml');

echo "App name: " . $app->getName() . "\n";
echo "Nodes: " . count($app->getWorkflow()->getGraph()->getNodes()) . "\n";

// Traverse nodes
$nodes = $app->getWorkflow()->getGraph()->getNodes();
foreach ($nodes as $node) {
    echo "Node: " . $node->getId() . " (" . $node->getNodeType() . ")\n";
}