ashleyhindle / laravel-ai-autofill
使用AI轻松自动填充模型属性
0.1.3
2024-09-11 19:41 UTC
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
使用AI自动填充模型属性
此包监听saved
模型事件,然后添加一个队列作业,使用每个模型1个API查询来自动填充属性,来源可以是OpenAI、Anthropic或Ollamo。
示例: 当这篇文章被保存时,'tagline'属性将自动被一个AI生成的字符串填充,这个字符串是一个“荒谬的点击诱饵标题”。
<?php use AshleyHindle\AiAutofill\AiAutofill; use Illuminate\Database\Eloquent\Model; class Article extends Model { use AiAutofill; protected $autofill = ['tagline' => 'ridiculous click-bait tagline']; }
安装
composer require ashleyhindle/laravel-ai-autofill
设置
php artisan ai-autofill:install
然后你将有一个config/ai-autofill.php
文件来设置你的提供商。你需要确保你已经设置了必要的.env
变量。
关键的是
OPENAI_API_KEY= ANTHROPIC_API_KEY= OLLAMA_URL= OLLAMA_MODEL=
使用
模型特质使用
简单地在你的模型中使用特质,并添加$autofill
数组,键为你要自动填充的属性,值为填充这些属性时使用的提示。
除了$autofillExclude
属性外,模型名称和模型属性都提供给LLM作为上下文,所以$autofill
中的提示可以非常简单。
示例
<?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); } }
测试
composer test
鸣谢
许可证
MIT许可证(MIT)。请参阅许可证文件以获取更多信息。
待办事项
- 更优雅地处理OpenAI故障
- 添加配置文件支持
- 允许覆盖系统提示
- 允许设置队列名称和最大尝试次数
- 通过PHP属性启用提示创建