PHP code example of xddesigners / silverstripe-ai

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

    

xddesigners / silverstripe-ai example snippets


use XD\SilverstripeAI\Services\AIClient;

$client = AIClient::create();

// Generate a plain text response
$result = $client->generateText('Write a short intro for our homepage.');

// Generate a keyed set of fields from context data
$result = $client->generateFields(
    context: ['Title' => 'My Page', 'Content' => 'Existing body text…'],
    fields: ['Title', 'Content', 'MetaDescription'],
    instructions: 'Improve the content for SEO.'
);
// Returns: ['Title' => '…', 'Content' => '…', 'MetaDescription' => '…']

use SilverStripe\Core\Extension;
use Symfony\AI\Platform\PlatformInterface;

class CustomAIPlatformExtension extends Extension
{
    public function updatePlatform(?PlatformInterface &$platform, string $type, string $apiKey, ?string $baseUrl): void
    {
        // Add a new provider…
        if ($type === 'myprovider') {
            $platform = \Symfony\AI\Platform\Bridge\SomeProvider\PlatformFactory::create($apiKey);
        }
        // …or override a built-in one, e.g. point 'openai' at a custom bridge.
    }
}

use SilverStripe\Control\HTTPResponse;
use SilverStripe\Security\Permission;
use SilverStripe\Security\Security;
use XD\SilverstripeAI\Controllers\AIController;

class MyAIController extends AIController
{
    protected function assertAccess(): ?HTTPResponse
    {
        if (!Permission::check('MY_AI_PERMISSION')) {
            return Security::permissionFailure($this);
        }
        return null;
    }
}

POST /ai/generate
mode=text&text=Write+a+short+intro&instructions=Keep+it+friendly