final class AppServiceProvider extends ServiceProvider
{
public function
register(): void
{
$this->app->singleton(
abstract:
Client::class,
concrete:
fn () => OpenAI::client(
apiToken: strval(config('openai.api_key')),
),
);
}
}
Now, every time I try and inject the OpenAI Client
magnificence right into a constructor or anywhere else, it's going to come
pre-setup for me.
final class GenerateAdFromProduct implements ShouldQueue
{
use Dispatchable;
use
InteractsWithQueue;
use Queueable;
use
SerializesModels;
public function
__construct(
public
readonly string $text,
public
readonly int $product,
) {}
public function
handle(Client $client): void
{
$response =
$client->completions()->create([
'model'
=> 'text-davinci-003',
'prompt'
=> $this->text,
'temperature'
=> 0.5,
'max_tokens' => 100,
'top_p'
=> 1.0,
'frequency_penalty' => 0.0,
'presence_penalty' => 0.0,
]);
DB::transaction(fn () => Product::query()->find(
id:
$this->product,
))->update(['ai_description' => $response['choices'][0]['text']]);
}
}
This uses the settings defined on the instance page with out
searching too deep into it. Of path, in case you do that in production, I
fantastically propose looking at those settings greater carefully.
enum Model: string
{
case ADA = 'text-ada-001';
case BABBAGE =
'text-babbage-001';
case CURIE =
'text-curie-001';
case DAVINCI =
'text-davinci-003';
}
Step one is complete. Let's have a look at the OpenAI client
request now.
$client->completions()->create([
'model' =>
Model::DAVINCI->value,
'prompt' =>
$this->text,
'temperature'
=> 0.5,
'max_tokens' =>
100,
'top_p' => 1.0,
'frequency_penalty' => 0.0,
'presence_penalty'
=> 0.0,
]);
Pretty correct. The rest of the settings are particular to
the version and result I am looking to acquire from what I can inform from the
documentation. So that is some thing this is purposefully set up so that I can
get commercial text from another text body. To me, this is an Advertising
Transformer. It transforms something prompt you supply it into an
advertisement. So, with that during thoughts - let's create a particular
magnificence to create this.
final class AdvertisementTransformer
{
public static
function transform(string $prompt): array
{
return [
'model'
=> Model::DAVINCI->value,
'prompt'
=> $prompt,
'temperature' => 0.5,
'max_tokens' => 100,
'top_p'
=> 1.0,
'frequency_penalty' => 0.0,
'presence_penalty' => 0.0,
];
}
}
We are extracting the good judgment of creating a final touch
to a committed elegance to be able to permit us to reuse it without difficulty.
Let's look back on the OpenAI consumer request now:
$client->completions()->create(
parameters:
AdvertisementTransformer::transform(
prompt: $this->text,
),
);
This, to me, as a minimum, is smooth and understandable.
Looking at this, I am passing within the text from the job to a transformer a
good way to remodel to the required parameters for an advertisement text
generator.
The output for this could be the following:
{
"object":
"text_completion",
"created":
1672769063,
"model":
"text-davinci-003",
"choices":
[
{
"textual content": "Are you a #Laravel
developer trying to live updated with the modern day information and updates?
Look no in addition than Laravel News! With over 10K customers daily, you'll be
able to stay knowledgeable and study from the respectable news outlet for the
Laravel atmosphere.
"index": 0,
"logprobs": null,
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 82,
"completion_tokens": 72,
"total_tokens": 154
}
}
As you may see we've an array of choices, every with a text
key. To retrieve this we just want to get entry to it like we normally would in
PHP:
$response = $client->completions()->create(
parameters:
AdvertisementTransformer::transform(
prompt:
$this->text,
),
);
DB::transaction(fn () => Product::query()->find(
id:
$this->product,
))->update(['ai_description' =>
$response['choices'][0]['text']]);
All you currently need to do is create the standard
transformers that you might use on your utility, tweak the parameters to the
factor that you recognize they'll work for you, and you're free to hold on.
0 Comments