PHP code example of pdro-lucas / filament-ai-writer
1. Go to this page and download the library: Download pdro-lucas/filament-ai-writer 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/ */
pdro-lucas / filament-ai-writer example snippets
use PdroLucas\FilamentAiWriter\Actions\AiWriterAction;
use Filament\Forms\Components\MarkdownEditor;
MarkdownEditor::make('content')
->hintAction(
AiWriterAction::make()
->targetField('content')
->prompt('
You are a content writing expert.
Write rich Markdown with headers, lists, and well-structured paragraphs.
Return ONLY the Markdown content, no explanations.
')
),
use Filament\Forms\Components\Textarea;
Textarea::make('excerpt')
->hintAction(
AiWriterAction::make('ai_excerpt')
->targetField('excerpt')
->prompt('
Write a concise and engaging summary in 2-3 sentences.
Tone: professional but accessible.
Return ONLY the summary.
')
),
use Filament\Forms\Components\TextInput;
TextInput::make('slug')
->hintAction(
AiWriterAction::make('ai_slug')
->targetField('slug')
->contextFields(['title'])
->prompt('
Generate a URL-friendly slug based on the provided title.
Return ONLY the slug, lowercase, hyphenated, no special characters.
')
->silent()
),
use Filament\Forms\Components\Select;
$categories = Category::all()->pluck('name', 'id');
Select::make('category_id')
->label('Category')
->options($categories)
->hintAction(
AiWriterAction::make('ai_category')
->targetField('category_id')
->contextFields(['title', 'body'])
->valueMap($categories->toArray())
->prompt('
Based on the title and body, choose the most appropriate category.
Return ONLY the numeric ID of the chosen category, nothing else.
')
->silent()
),
use Filament\Forms\Components\TagsInput;
TagsInput::make('tags')
->hintAction(
AiWriterAction::make('ai_tags')
->targetField('tags')
->contextFields(['title', 'body'])
->allowedValues(['laravel', 'filament', 'php', 'api', 'frontend', 'backend'])
->normalizeArrayCase()
->prompt('
Suggest relevant tags based on the title and body.
Return ONLY a raw JSON array of strings from the allowed list, no markdown, no explanation.
Example: ["laravel","filament","php"]
')
->silent()
),
// Dynamically from the database
$tags = Tag::all()->pluck('name')->toArray();
TagsInput::make('tags')
->hintAction(
AiWriterAction::make('ai_tags')
->targetField('tags')
->contextFields(['title', 'body'])
->allowedValues($tags)
->normalizeArrayCase()
->prompt('
Suggest relevant tags based on the title and body.
Return ONLY a raw JSON array of strings from the allowed list, no markdown, no explanation.
Example: ["laravel","filament","php"]
')
->silent()
),
TagsInput::make('tags')
->hintAction(
AiWriterAction::make('ai_tags')
->targetField('tags')
->contextFields(['title', 'body'])
->expectArray()
->normalizeArrayCase()
->prompt('
Suggest up to five relevant tags based on the title and body.
Return ONLY a raw JSON array of strings, no markdown, no explanation.
Example: ["laravel","filament","php"]
')
->silent()
),
use Filament\Forms\Components\TextInput;
TextInput::make('meta_description')
->hintAction(
AiWriterAction::make()
->targetField('meta_description')
->beforeGenerate(function (): bool {
if (auth()->user()->credits <= 0) {
return false;
}
return true;
})
->prompt('Write a meta description.')
),
// AppServiceProvider
use PdroLucas\FilamentAiWriter\Actions\AiWriterAction;
public function boot(): void
{
AiWriterAction::globalBeforeGenerate(function (): bool {
if (auth()->user()->cannot('use-ai-writer')) {
Notification::make()
->warning()
->title('AI writing is not available')
->send();
return false;
}
return true;
});
}
// app/Listeners/DebitUserCredits.php
class DebitUserCredits
{
public function handle(AiTextGenerated $event): void
{
$user = $event->payload['user'];
if ($user === null) {
return;
}
$user->decrement('ai_credits');
}
}
use PdroLucas\FilamentAiWriter\Contracts\AiProvider;
public function register(): void
{
$this->app->bind(AiProvider::class, fn () => new MyCustomProvider());
}
src/
Actions/
AiWriterAction.php # The Filament Action
Contracts/
AiProvider.php # Interface for AI providers
Events/
AiTextGenerated.php # Event dispatched after generation
Providers/
AnthropicProvider.php
OpenAiProvider.php
GeminiProvider.php
FilamentAiWriterServiceProvider.php
config/
filament-ai-writer.php
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.