dmsemenov / openai-attribute
Allow to fill model attribute from OpenAi response.
Requires
- php: ^8.1
- illuminate/support: ^8.0|^9.0|^10.0
- openai-php/client: ^0.6.3
Requires (Dev)
- orchestra/testbench: ~8
- phpunit/phpunit: ~9.0
README
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
- Add your OpenAI api key
OPENAI_API_KEY
to.env
. Key can be found here: https://platform.openai.com/account/api-keys
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 dimitr.semenov@gmail.com instead of using the issue tracker.
Credits
License
MIT. Please see the license file for more information.