symfony/ai-replicate-platform
Symfony AI bridge for the Replicate Platform. Provides integration with Replicate’s HTTP API to create and manage predictions from Symfony apps. Includes links to Replicate docs and points contributors to the main symfony/ai repository for issues and PRs.
Laravel + Symfony Components:
HttpClient (v7.3+), which can be integrated into Laravel via:
symfony/http-client-bundle (if using Symfony bundles).HttpClient as a singleton:
$this->app->singleton(\Symfony\Contracts\HttpClient\HttpClientInterface::class, fn () =>
\Symfony\Component\HttpClient\CurlHttpClient::create([
'headers' => ['Authorization' => 'Bearer ' . config('services.replicate.token')],
])
);
Guzzle client by creating a wrapper adapter for Symfony’s HttpClient interface (minimal effort for one-off integrations).ReplicateClient as a service:
$this->app->bind(\Symfony\Component\Ai\Replicate\ReplicateClient::class, function ($app) {
return new \Symfony\Component\Ai\Replicate\ReplicateClient(
$app->make(\Symfony\Contracts\HttpClient\HttpClientInterface::class),
config('services.replicate.token')
);
});
ModelPredicted) that can be bridged to Laravel’s events using a listener:
use Symfony\Component\Ai\Event\ModelPredicted;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ReplicateEventSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
ModelPredicted::class => 'onModelPredicted',
];
}
public function onModelPredicted(ModelPredicted $event): void
{
event(new \App\Events\ReplicatePredictionCompleted($event->getResult()));
}
}
Register the subscriber in AppServiceProvider:
public function boot()
{
$this->app->make(\Symfony\Component\EventDispatcher\EventDispatcherInterface::class)
->addSubscriber(new ReplicateEventSubscriber());
}
config/services.php:
'replicate' => [
'token' => env('REPLICATE_API_TOKEN'),
'default_model' => 'stable-diffusion:abc123',
'timeout' => 30, // seconds
],
Laravel-Specific Enhancements:
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Symfony\Component\Ai\Replicate\ReplicateClient;
class GenerateImageJob implements ShouldQueue
{
use Queueable;
public function handle(ReplicateClient $client)
{
$result = $client->predict('stable-diffusion:abc123', ['prompt' => 'Laravel logo']);
// Store result in DB or S3
}
}
$cacheKey = "replicate:prompt:{$prompt}";
$result = cache()->remember($cacheKey, now()->addHours(1), function () use ($client, $prompt) {
return $client->predict($model, ['prompt' => $prompt]);
});
use Illuminate\Foundation\Http\FormRequest;
class GenerateImageRequest extends FormRequest
{
public function rules(): array
{
return [
'prompt' => 'required|string|max:1000',
'model' => 'sometimes|string|in:stable-diffusion,llama2',
];
}
}
Phase 1: Proof of Concept (1–2 weeks)
composer require symfony/ai-replicate-platform symfony/http-client
config/services.php with Replicate API token.use Symfony\Component\Ai\Replicate\ReplicateClient;
route('generate-image', function (ReplicateClient $client) {
$result = $client->predict('stable-diffusion:abc123', ['prompt' => request('prompt')]);
return response()->json($result);
});
Phase 2: Feature Integration (2–4 weeks)
App\Services\ReplicateService) to handle:
namespace App\Services;
use Symfony\Component\Ai\Replicate\ReplicateClient;
use Illuminate\Support\Facades\Log;
class ReplicateService
{
public function __construct(private ReplicateClient $client) {}
public function generateImage(string $prompt, string $model = 'stable-diffusion:abc123')
{
try {
$result = $this->client->predict($model, ['prompt' => $prompt]);
return $this->processResult($result);
} catch (\Exception $e) {
Log::error("Replicate failed: {$e->getMessage()}");
throw new \RuntimeException('Image generation failed. Please try again later.');
}
}
private function processResult($result): string
{
// Save to S3, return URL, etc.
return "https://bucket.s3.example.com/{$result['output'][0]}";
}
}
AppServiceProvider:
$this->app->bind(ReplicateService::class, function ($app) {
return new ReplicateService($app->make(ReplicateClient::class));
});
Phase 3: Scaling and Abstraction (3–6 weeks)
ReplicateProvider to route models dynamically:
use Symfony\Component\Ai\Provider\ProviderInterface;
class ReplicateProvider implements ProviderInterface
{
public function getModel(string $modelName): string
{
return match ($modelName) {
'image' => 'stable-diffusion:abc123',
'text' => 'llama2:456def',
default => throw new \InvalidArgumentException("Unknown model: {$modelName}"),
};
}
}
$job = new GenerateImageJob($prompt);
dispatch($job);
"extra": {
"laravel": {
"
How can I help you explore Laravel packages today?