1. Go to this page and download the library: Download easygithdev/phpopenai library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
easygithdev / phpopenai example snippets
EasyGithDev\PHPOpenAI\Helpers\ModelEnum;
use EasyGithDev\PHPOpenAI\OpenAIClient;
$apiKey = getenv('OPENAI_API_KEY');
$response = (new OpenAIClient($apiKey))->Completion()->create(
ModelEnum::TEXT_DAVINCI_003,
"Say this is a test",
)->toObject();
// Response as stClass object
echo '<pre>', print_r($response, true), '</pre>';
$response = (new OpenAIApi(getenv('OPENAI_API_KEY')))->Completion()->create(
ModelEnum::TEXT_DAVINCI_003,
"Say this is a test",
);
$apiKey = getenv('OPENAI_API_KEY');
$org = getenv('OPENAI_API_ORG');
// Passing the organization to the client
$response = (new OpenAIClient($apiKey, $org))
$apiKey = getenv('OPENAI_API_KEY');
// Create a new router, with origine url and version
$route = new OpenAIRoute(
'https://api.openai.com',
'v1'
);
// Get a specific Url
echo $route->completionCreate() , '<br>';
// Passing the router to the client
$response = (new OpenAIClient($apiKey))
->setRoute($route);
$response = (new OpenAIClient($apiKey))->Completion()->create(
ModelEnum::TEXT_DAVINCI_003,
"Say this is a test",
)->toObject();
// Response as a stClass object
echo '<pre>', print_r($response, true), '</pre>';
$response = (new OpenAIClient($apiKey))->Completion()->create(
ModelEnum::TEXT_DAVINCI_003,
"Say this is a test",
)->toArray();
// Response as an associative array
echo '<pre>', print_r($response, true), '</pre>';
try {
$response = (new OpenAIClient('BAD KEY'))
->Completion()
->create(
ModelEnum::TEXT_DAVINCI_003,
"Say this is a test",
)
->toObject();
} catch (Throwable $t) {
echo nl2br($t->getMessage());
die;
}
$handler = (new OpenAIClient('BAD KEY'))->Completion()->create(
ModelEnum::TEXT_DAVINCI_003,
"Say this is a test",
);
$response = $handler->getResponse();
$contentTypeValidator = $handler->getContentTypeValidator();
if (!(new StatusValidator($response))->validate() or
!(new $contentTypeValidator($response))->validate()) {
echo $response->getBody();
}
$response = (new OpenAIClient($apiKey))->Chat()->create(
ModelEnum::GPT_3_5_TURBO,
[
new ChatMessage(ChatMessage::ROLE_SYSTEM, "You are a helpful assistant."),
new ChatMessage(ChatMessage::ROLE_USER, "Who won the world series in 2020?"),
new ChatMessage(ChatMessage::ROLE_ASSISTANT, "The Los Angeles Dodgers won the World Series in 2020."),
new ChatMessage(ChatMessage::ROLE_USER, "Where was it played?"),
]
)->toObject();
$response = (new OpenAIClient($apiKey))->Completion()->create(
ModelEnum::TEXT_DAVINCI_003,
"Say this is a test",
)->toObject();
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
...
(new OpenAIClient($apiKey))->Completion()->create(
model: "text-davinci-003",
prompt: "Translate this into 1. French, 2. Spanish and 3. Japanese:\n\nWhat rooms do you have available?\n\n1.",
temperature: 0.3,
max_tokens: 100,
top_p: 1.0,
frequency_penalty: 0.0,
presence_penalty: 0.0,
stream: true
)->getResponse();
$response = (new OpenAIClient($apiKey))->Edit()->create(
"What day of the wek is it?",
ModelEnum::TEXT_DAVINCI_EDIT_001,
"Fix the spelling mistakes",
)->toObject();
function displayUrl($url)
{
return '<img src="' . $url . '" />';
}
$response = (new OpenAIClient($apiKey))->Image()->create(
"a rabbit inside a beautiful garden, 32 bit isometric",
n: 2,
size: ImageSizeEnum::is256,
)->toObject();
$response = (new OpenAIClient($apiKey))->Image()->createEdit(
image: __DIR__ . '/../../assets/image_edit_original.png',
mask: __DIR__ . '/../../assets/image_edit_mask2.png',
prompt: 'a sunlit indoor lounge area with a pool containing a flamingo',
size: ImageSizeEnum::is512,
)->toObject();
$response = (new OpenAIClient($apiKey))->Embedding()->create(
ModelEnum::TEXT_EMBEDDING_ADA_002,
"The food was delicious and the waiter...",
)->toObject();