<?php
declare(strict_types=1);
require 'vendor/autoload.php';
use Poml\Toolkit\OrchestrationEngine;
use Poml\Toolkit\Parsing\PomlParser;
use Poml\Toolkit\Connectors\DummyConnector;
use Poml\Toolkit\Connectors\HttpConnector;
echo "--- POML Toolkit Advanced Example ---\\n\\n";
// 1. Define a tool (a simple PHP function)
function send_notification(string $message): string {
$output = "NOTIFICATION SENT: {$message}\\n";
echo $output;
return "Sent successfully";
}
// 2. Define the complex POML workflow
$pomlXml = <<<'XML'
<?xml version="1.0"?>
<poml>
<prompt model="json-generator">
<message>Generate a JSON object with a user 'name' and a 'status' of 'active'.</message>
<!-- Extract the status field from the JSON response into a variable called 'user_status' -->
<variable name="user_status" from="json" path="status"/>
</prompt>
<!-- Check the variable we just extracted -->
<if condition="{{user_status}} == 'active'">
<tool function="notify">
<param name="message">User is active, proceeding to next step.</param>
</tool>
<else>
<tool function="notify">
<param name="message">User is not active, aborting.</param>
</tool>
</else>
</if>
</poml>
XML;
// 3. Set up the Orchestration Engine
$engine = new OrchestrationEngine();
$parser = new PomlParser();
// 4. Register tools and connectors
$engine->registerTool('notify', 'send_notification');
// Use a DummyConnector that returns a valid JSON for this example.
class JsonDummyConnector extends DummyConnector {
public function execute(string $prompt): string {
return '{"name": "John Doe", "status": "active"}';
}
}
$engine->registerConnector('json-generator', new JsonDummyConnector());
/*
// To use a real HTTP endpoint, you would do this instead:
$apiKey = getenv('OPENAI_API_KEY'); // It's best practice to use environment variables
$apiUrl = 'https://api.openai.com/v1/chat/completions';
$engine->registerConnector(
'gpt-4',
new HttpConnector($apiUrl, 'gpt-4', $apiKey)
);
*/
// 5. Parse and run the orchestration
try {
echo "Parsing POML...\\n";
$orchestration = $parser->parse($pomlXml);
echo "Running orchestration...\\n\\n";
$finalResult = $engine->run($orchestration);
echo "\\nOrchestration finished.\\n";
echo "Final Result (from 'last_result'): " . print_r($finalResult, true) . "\\n";
} catch (\Exception $e) {
echo "\\nAn error occurred: " . $e->getMessage() . "\\n";
echo "Trace: \\n" . $e->getTraceAsString() . "\\n";
}
|