定制 gwhthompson/filament-ai-forms 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

gwhthompson/filament-ai-forms

Composer 安装命令:

composer require gwhthompson/filament-ai-forms

包简介

AI-powered form generation for Filament v4/v5 using Laravel AI SDK

README 文档

README

Latest Version on Packagist codecov PHPStan License

AI-powered form generation for Filament, built on the Laravel AI SDK.

Describe what each field represents with aiSchema(). An AI agent fills them in. Your field types and validation rules act as constraints, so you always get valid data back.

Requirements

Installation

composer require gwhthompson/filament-ai-forms

Publish the config file:

php artisan vendor:publish --tag="filament-ai-forms-config"

Register the plugin in your panel provider:

use Gwhthompson\FilamentAiForms\FilamentAiFormsPlugin;

public function panel(Panel $panel): Panel
{
    return $panel
        ->plugin(FilamentAiFormsPlugin::make());
}

Quick Start

Mark fields with aiSchema() and add AiGenerateAction to your page:

use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Textarea;
use Gwhthompson\FilamentAiForms\Actions\AiGenerateAction;

public static function form(Form $form): Form
{
    return $form->schema([
        TextInput::make('name')
            ->aiSchema(description: 'The company name'),

        Textarea::make('description')
            ->aiSchema(
                description: 'A marketing description',
                prompt: 'Write 2-3 sentences',
            ),
    ]);
}

protected function getHeaderActions(): array
{
    return [
        AiGenerateAction::make(),
    ];
}

The action opens a wizard: select fields, generate, review, then accept or reject each value.

Configuration

// config/filament-ai-forms.php

return [
    'agents' => [
        'generation' => env('AI_FORMS_GENERATION_AGENT'),
        'chat' => env('AI_FORMS_CHAT_AGENT'),
    ],
    'logging' => [
        'enabled' => env('AI_FORMS_LOGGING', true),
        'path' => storage_path('logs/ai-generation'),
    ],
];
Env var Purpose
AI_FORMS_GENERATION_AGENT Agent class for bulk generation
AI_FORMS_CHAT_AGENT Agent class for chat refinement
AI_FORMS_LOGGING Enable/disable generation logging (default: true)

You can also set agents on the plugin directly:

FilamentAiFormsPlugin::make()
    ->agent(MyGenerationAgent::class)
    ->chatAgent(MyChatAgent::class)

aiSchema Parameters

Parameter Type Default Purpose
enabled bool true Enable/disable generation for this field
description string null What this field represents
prompt string null Instructions for the agent
required bool true Whether the agent must fill this field
examples array [] Example values to guide output
pattern string null Regex pattern constraint

AiGenerateAction

Bulk-generate multiple fields at once. Add it as a header action or form action.

Method Purpose
agent(string|Closure) Custom agent class
systemPrompt(string|Closure) System instructions
contextProvider(Closure) Pass context data (URLs, etc.)
beforeGeneration(Closure) Pre-generation hook
afterGeneration(Closure) Post-generation hook
logEnabled(bool) Enable/disable logging
tools(array) Pass tools to default agent (e.g., WebSearch)
logPath(string) Custom log path
AiGenerateAction::make()
    ->systemPrompt('You are a business data specialist.')
    ->contextProvider(fn ($action) => [
        'url' => $action->getRecord()->website_url,
    ])

AiChatAction

Refine a single field through a chat interface. Add it as a suffix action on any field.

Method Purpose
agent(string|Closure) Custom agent class
systemPrompt(string|Closure) System instructions
initialPrompt(string|Closure) Pre-fill the chat input
contextPrompt(string|Closure) Additional context
Textarea::make('bio')
    ->aiSchema(description: 'Professional biography')
    ->suffixAction(
        AiChatAction::make()
            ->systemPrompt('You are a professional copywriter.')
            ->initialPrompt('Help me write a compelling bio')
    )

Custom Agents

Agents use the Laravel AI SDK. Configure model, provider, and temperature with PHP attributes.

use Laravel\Ai\Contracts\Agent;
use Laravel\Ai\Contracts\HasStructuredOutput;
use Laravel\Ai\Contracts\HasTools;
use Laravel\Ai\Promptable;
use Laravel\Ai\Attributes\Provider;
use Laravel\Ai\Attributes\Model;
use Laravel\Ai\Attributes\Temperature;

#[Provider('openai')]
#[Model('gpt-4o')]
#[Temperature(0.1)]
class MyGenerationAgent implements Agent, HasStructuredOutput, HasTools
{
    use Promptable;

    public function tools(): array
    {
        return [
            new \Laravel\Ai\Tools\WebSearch,
        ];
    }
}

Key interfaces:

  • HasStructuredOutput -- for generation agents (returns typed data)
  • Conversational -- for chat agents (maintains message history)
  • HasTools -- adds tool usage (web search, web fetch, etc.)
  • HasMiddleware -- adds middleware to the agent pipeline

Agent resolution order: action-level ->agent() > plugin-level ->agent() > config value > built-in default.

Testing

The Laravel AI SDK provides test helpers to fake agent responses:

use Laravel\Ai\Facades\Agent;

Agent::fake([
    ['name' => 'Acme Corp', 'description' => 'A test company'],
]);

// ... trigger the action ...

Agent::assertPrompted(fn ($prompt) => str_contains($prompt, 'company name'));

Use Agent::preventStrayPrompts() to catch unexpected agent calls in your test suite.

How It Works

The package converts your Filament form schema into a JSON schema that the agent follows. Your Laravel validation rules (required, max length, enum values) become constraints in that schema. The agent returns structured data matching your field types, and you review each value before it touches the form.

Custom Themes

If you have a custom Filament theme, add the package views to your theme's @source directive:

@source '../../../../vendor/gwhthompson/filament-ai-forms/resources/views';

The chat interface uses named CSS classes (fi-ai-chat-*) for easy customisation:

/* Custom user bubble colour */
.fi-ai-chat-message-user .fi-ai-chat-message-content {
    @apply bg-indigo-600;
}

/* Hide delete buttons */
.fi-ai-chat-message-delete-btn {
    @apply hidden;
}

Credits

License

The MIT License (MIT). Please see License File for more information.

gwhthompson/filament-ai-forms 适用场景与选型建议

gwhthompson/filament-ai-forms 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 130 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 12 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「Forms」 「laravel」 「agents」 「ai」 「filament」 「structured-outputs」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 gwhthompson/filament-ai-forms 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 gwhthompson/filament-ai-forms 我们能提供哪些服务?
定制开发 / 二次开发

基于 gwhthompson/filament-ai-forms 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 130
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 20
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-18