Laravel package that allows to define ChatGPT generated attributes for Laravel model like description or other "text" attribute.
- 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
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
],
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
Please see the changelog for more information on what has changed recently.
$ composer test
Please see contributing.md for details and a todolist.
If you discover any security related issues, please email dimitr.semenov@gmail.com instead of using the issue tracker.
MIT. Please see the license file for more information.