ashleyhindle/laravel-ai-autofill

使用AI轻松自动填充模型属性

0.1.3 2024-09-11 19:41 UTC

This package is auto-updated.

Last update: 2024-09-11 20:02:01 UTC


README

使用AI自动填充模型属性

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

此包监听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属性启用提示创建