ChatGpt reusable objects
composer require angelxmoreno/prompt-classes
First thing you will need to get started is an OpenAI api key.
To get started, visit the official OpenAI platform website. If you're new, create an account using simple steps. Next, sign in using your OpenAI account email and password, or use your Google/Microsoft account. Once logged in, you'll find your name and profile icon on the top-right corner of the OpenAI platform homepage. To obtain an API Key, click on your name in the top-right corner for a dropdown menu, and select "View API keys." On the resulting page, find the "Create new secret key" option in the center. If you don't have an API key, click this button to generate one. Remember to save the API key immediately, as you won't be able to retrieve it once the window closes.
-
Create a PromptClass
<?php declare(strict_types=1); namespace YourNamespace\Prompts; use PromptClasses\Core\AbstractPrompt; class HelloPrompt extends AbstractPrompt { public string $promptTpl = 'Say "Hello {{name}}. Good {{time_of_day}}"'; public array $promptParams = ['name', 'time_of_day']; }
-
Instantiate and send
$helloPrompt = new HelloPrompt($openaiApiKey); $response = $helloPrompt->send('John', 'Morning'); // returns `Hello John. Good Morning`
The order of params is the order of the
$promptParams
array. This means that if you change the$promptParams
for theHelloPrompt
class to['time_of_day','name'];
, you would have to call->send()
like so:$response = $helloPrompt->send('Morning', 'John'); // still returns `Hello John. Good Morning`
-
Create a PromptClass
<?php declare(strict_types=1); namespace YourNamespace\Prompts; use PromptClasses\Core\AbstractPrompt; class MetaDescriptionPrompt extends AbstractPrompt { public string $promptTpl = 'Write a meta description no longer than 160 characters including spaces for a product page with the title "{{title}}"'; public array $promptParams = ['title']; }
-
Instantiate and send
$metaDescriptionPrompt = new MetaDescriptionPrompt($openaiApiKey); $response = $metaDescriptionPrompt->send('Supernatural Dean Winchester That was Scary Vintage Sunset T-Shirt'); // returns `Get the Supernatural Dean Winchester 'That was Scary' Vintage Sunset T-Shirt. Show your love for the hit TV show with this iconic design.`
Under the hood the library uses orhanerday/open-ai. The default configuration for the calls to OpenAI Chat Completions are:
Name | Current Value | Info |
---|---|---|
model | gpt-3.5-turbo | ID of the model to use. See Model overview for descriptions of them. |
temperature | 1 | What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. |
To discover more options available take a look at the official documentation for the chat completions endpoint.
Values can be updated for your PromptClass by extending the $openaiParams
property of the AbstractPrompt
class like so:
<?php
declare(strict_types=1);
namespace YourNamespace\Prompts;
use PromptClasses\Core\AbstractPrompt;
class MetaDescriptionPrompt extends AbstractPrompt
{
public string $promptTpl = 'Write a meta description no longer than 160 characters including spaces for a product page with the title "{{title}}"';
public array $promptParams = ['title'];
protected array $openaiParams = [
'model' => 'gpt-3.5-turbo',
'temperature' => 0.2,
'frequency_penalty' => 1.8
];
}
PHP 7.4+
For bugs and feature requests, please use the issues section of this repository.
Licensed under the MIT License. Redistributions of the source code included in this repository must retain the copyright notice found in each file.