Skip to content

Allows to fill Eloquent model attributes with ChatGPT generated text.

License

Notifications You must be signed in to change notification settings

dmsemenov/openai-attribute

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel OpenaiAttribute

GitHub license Latest Version on Packagist Total Downloads

Laravel package that allows to define ChatGPT generated attributes for Laravel model like description or other "text" attribute.

Installation

  • Install package
 composer require dmsemenov/openai-attribute

Configuration

Publish config if needed:

 php artisan vendor:publish --provider="Dmsemenov\OpenaiAttribute\OpenaiAttributeServiceProvider"

It is possible to specify request options in published config or in the model generatedAttributes() method.
Available request options see at OpenApi documentation page.

    // OpenAI api key
    'api_key' => env('OPENAI_API_KEY'),

    // Queue name for generate attributes jobs
    'queue_name' => env('OPENAI_QUEUE_NMAE', 'openai_generate'),

    // Default options for api chat completions requests.
    'default_options' => [
        'model' => env('OPENAI_API_MODEL', 'gpt-3.5-turbo'),
        'temperature' => 0.3,
        'max_tokens' => 100,
        'top_p' => 1.0,
        'frequency_penalty' => 0.0,
        'presence_penalty' => 0.0
    ],

Usage

Trait HasGeneratedAttributes should be added to model. it contains list of model attributes with prompt and options (if needed, see default_options):

public function generatedAttributes(): array
{
    return [
        'description' => [
            'prompt' => 'Tell info about [model:name]". Wrap each paragraph to tag <p>',
            'max_tokens' => 1000
        ],

        'slug' => [
            'prompt' => 'Generate slug from [model:name]',
            'max_tokens' => 10
        ],
    ];
}

Models that fits condition in [MyModel]->NeedsGenerateAttributes() will be selected for attributes generation. Default: all.

Artisan commands to generate attributes:

php artisan openai:generate App\\Models\\[MyModel] --queued

or generate instantly without queue option:

php artisan openai:generate App\\Models\\[MyModel]

or specify model ids list:

php artisan openai:generate App\\Models\\[MyModel] --ids=1 --ids=2

Other useful commands:

Display queue size:
php artisan queue:monitor openai_generate

Remove jobs from queue:
php artisan queue:clear --queue=openai_generate

Run workers:
php artisan queue:work --queue=openai_generate

Change log

Please see the changelog for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see contributing.md for details and a todolist.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

MIT. Please see the license file for more information.