spatie/laravel-url-ai-transformer
Laravel package to transform URLs and their web content with AI. Extract structured data (JSON-LD), generate summaries, images, or custom outputs via transformers and prompts. Runs via an Artisan command and stores results in the database for later retrieval.
Installation:
composer require spatie/laravel-url-ai-transformer
php artisan vendor:publish --provider="Spatie\UrlAiTransformer\UrlAiTransformerServiceProvider"
Publish the config file to adjust AI service configurations (e.g., OpenAI API key).
First Use Case: Transform a URL's content into a structured summary or extract key points:
use Spatie\UrlAiTransformer\Facades\UrlAiTransformer;
$url = 'https://example.com/article';
$summary = UrlAiTransformer::transform($url, 'summarize');
Key Configurations:
config/url-ai-transformer.php.summarize, extract_key_points) in the config.Basic Transformations: Use the facade or service container to transform URLs:
// Summarize a URL
$summary = UrlAiTransformer::transform($url, 'summarize');
// Extract key points
$keyPoints = UrlAiTransformer::transform($url, 'extract_key_points');
Custom Transformations: Extend the package by creating custom transformers:
namespace App\Services;
use Spatie\UrlAiTransformer\Transformers\Transformer;
class CustomTransformer extends Transformer
{
public function transform(string $content): string
{
return "Custom logic: " . $content;
}
}
Register the transformer in config/url-ai-transformer.php under transformers.
Queueing Long-Running Tasks: Dispatch transformations to a queue for async processing:
use Spatie\UrlAiTransformer\Jobs\TransformUrl;
TransformUrl::dispatch($url, 'summarize')
->onQueue('ai_transformations');
Caching Responses: Cache AI-generated responses to avoid redundant API calls:
UrlAiTransformer::transform($url, 'summarize', cacheFor: now()->addHours(1));
Route::post('/transform', function (Request $request) {
return UrlAiTransformer::transform($request->url, $request->type);
});
UrlAiTransformer::transform($url, 'summarize')->then(function ($summary) {
// Save to DB or notify users
});
API Rate Limits:
UrlAiTransformer::setRetryPolicy(3, 1000); // Retry 3 times with 1s delay
config/url-ai-transformer.php logging.Content Length Limits:
Dynamic URL Fetching:
spatie/url-fetcher to fetch URL content. Ensure URLs are accessible and not blocked by robots.txt.Transformer Registration:
transform() method. Forgetting this will throw runtime errors.UrlAiTransformer::enableDebugMode(); // Logs raw AI responses and errors
storage/logs/laravel.log for failed transformations or API errors.Custom AI Services:
Extend the Spatie\UrlAiTransformer\Services\AiService class to support new providers (e.g., Anthropic, Cohere):
class CustomAiService extends AiService
{
public function generate(string $prompt): string
{
// Custom logic for your AI provider
}
}
Register the service in the config.
Pre/Post-Processing: Use middleware to modify content before/after transformation:
UrlAiTransformer::addTransformerMiddleware(function ($content) {
return str_replace(['[REDACTED]', '(CONFIDENTIAL)'], '', $content);
});
Fallback Mechanisms: Implement fallback transformers for when the primary AI service fails:
UrlAiTransformer::setFallbackTransformer('fallback_summarize');
config/url-ai-transformer.php to avoid specifying it repeatedly:
'default_transformer' => 'summarize',
.env variables to switch AI services between environments (e.g., AI_SERVICE=openai for local, AI_SERVICE=huggingface for production).How can I help you explore Laravel packages today?