ashleyhindle / laravel-ai-autofill
Easily autofill model properties with AI
Requires
- php: ^8.2
- illuminate/contracts: ^10.0||^11.0
- openai-php/laravel: ^0.10.1
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^9.0.0||^8.22.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.7
- pestphp/pest-plugin-laravel: ^2.3
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
README
Autofill model properties with AI
This package listens to the saved
model event, then adds a queued job that autofills the properties from either OpenAI, Anthropic or Ollamo, using 1 API query per model.
Example: When this article is saved the 'tagline' property will be automatically filled by an AI generated string that's a 'ridiculous click-bait tagline'
<?php use AshleyHindle\AiAutofill\AiAutofill; use Illuminate\Database\Eloquent\Model; class Article extends Model { use AiAutofill; protected $autofill = ['tagline' => 'ridiculous click-bait tagline']; }
Installation
composer require ashleyhindle/laravel-ai-autofill
Setup
php artisan ai-autofill:install
You'll then have a config/ai-autofill.php
file that sets up your providers. You'll need to ensure you have the necessary .env
variables setup.
The key ones are:
OPENAI_API_KEY= ANTHROPIC_API_KEY= OLLAMA_URL= OLLAMA_MODEL=
Usage
Model Trait Usage
Simply use the trait in your model, and add the $autofill
array with the keys as the properties you want to autofill, and the values as the prompts you want to use to fill them.
The model name and model properties, except $autofillExclude
properties, are provided to the LLM for context, so the prompts in $autofill
can be very simple.
Example:
<?php use AshleyHindle\AiAutofill\AiAutofill; use AshleyHindle\AiAutofill\Autofills\Tags; use Illuminate\Database\Eloquent\Model; class Article extends Model { use AiAutofill; protected $autofill = [ 'tagline' => 'ridiculous click-bait tagline', // simple string 'tags' => Tags::class, // AiAutofill tested & provided prompt 'seo_description' // local function ]; protected $autofillExclude = ['authors_email']; // Won't be included in the prompt context public function autofillSeoDescription() { $bannedBrandsFromDatabase = ['Nike', 'Reebok', 'Umbro']; return 'Concise SEO description not including any of these brands: ' . implode(', ', $bannedBrandsFromDatabase); } }
Testing
composer test
Credits
License
The MIT License (MIT). Please see License File for more information.
TODO
- Handle OpenAI failures more gracefully
- Add config file support
- Allow overriding system prompt
- Allow setting queue name and max attempts
- Enable prompt creation through PHP Attributes