1. Go to this page and download the library: Download tectalic/openai 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/ */
tectalic / openai example snippets
$openaiClient = \Tectalic\OpenAi\Manager::build(
new \GuzzleHttp\Client(),
new \Tectalic\OpenAi\Authentication(getenv('OPENAI_API_KEY'))
);
/** @var \Tectalic\OpenAi\Models\ChatCompletions\CreateResponse $response */
$response = $openaiClient->chatCompletions()->create(
new \Tectalic\OpenAi\Models\ChatCompletions\CreateRequest([
'model' => 'gpt-4',
'messages' => [
[
'role' => 'user',
'content' => 'Will using a well designed and supported third party package save time?'
],
],
])
)->toModel();
echo $response->choices[0]->message->content;
// Yes, using a well-designed and supported third-party package can save time during software development.
// It allows you to focus on the core functionality of your application without having to reinvent the wheel or spend resources developing the same functionality from scratch.
// A good third-party package can provide reliability, efficiency, and continued support with updates and bug fixes, which in turn facilitates faster development and a more stable final product.
// Additionally, using widely adopted packages can also increase the chances of compatibility with other software components and make it easier for other developers to understand and work with your code.
$openaiClient = \Tectalic\OpenAi\Manager::build(
new \GuzzleHttp\Client(),
new \Tectalic\OpenAi\Authentication(getenv('OPENAI_API_KEY'))
);
/** @var \Tectalic\OpenAi\Models\ChatCompletions\CreateResponse $response */
$response = $openaiClient->chatCompletions()->create(new CreateRequest([
'model' => 'gpt-3.5-turbo-0613',
'messages' => [
['role' => 'user', 'content' => 'What\'s the weather like in Boston?']
],
'functions' => [
[
'name' => 'get_current_weather',
'description' => 'Get the current weather in a given location',
'parameters' => new \Tectalic\OpenAi\Models\ChatCompletions\CreateRequestFunctionsItemParameters(
[
'type' => 'object',
'properties' => [
'location' => [
'type' => 'string',
'description' => 'The worldwide city and state, e.g. San Francisco, CA',
],
'format' => [
'type' => 'string',
'description' => 'The temperature unit to use. Infer this from the users location.',
'enum' => ['celsius', 'farhenheit'],
],
'num_days' => [
'type' => 'integer',
'description' => 'The number of days to forecast',
],
],
'
$openaiClient = \Tectalic\OpenAi\Manager::build(new \GuzzleHttp\Client(), new \Tectalic\OpenAi\Authentication(getenv('OPENAI_API_KEY')));
/** @var \Tectalic\OpenAi\Models\Completions\CreateResponse $response */
$response = $openaiClient->completions()->create(
new \Tectalic\OpenAi\Models\Completions\CreateRequest([
'model' => 'text-davinci-003',
'prompt' => 'Will using a third party package save time?',
])
)->toModel();
echo $response->choices[0]->text;
// Using a third party package can save time because you don't have to write the code yourself.
$openaiClient = \Tectalic\OpenAi\Manager::build(new \GuzzleHttp\Client(), new \Tectalic\OpenAi\Authentication(getenv('OPENAI_API_KEY')));
/** @var \Tectalic\OpenAi\Models\Completions\CreateResponse $response */
$response = $openaiClient->completions()->create(
new \Tectalic\OpenAi\Models\Completions\CreateRequest([
'model' => 'code-davinci-002',
'prompt' => "// PHP 8\n// A variable that saves the current date and time",
'max_tokens' => 256,
'stop' => ";",
])
)->toModel();
echo $response->choices[0]->text;
// $now = date("Y-m-d G:i:s")
$openaiClient = \Tectalic\OpenAi\Manager::build(new \GuzzleHttp\Client(), new \Tectalic\OpenAi\Authentication(getenv('OPENAI_API_KEY')));
/** @var \Tectalic\OpenAi\Models\ImagesGenerations\CreateResponse $response */
$response = $openaiClient->imagesGenerations()->create(
new \Tectalic\OpenAi\Models\ImagesGenerations\CreateRequest([
'prompt' => 'A cute baby sea otter wearing a hat',
'size' => '256x256',
'n' => 5
])
)->toModel();
foreach ($response->data as $item) {
var_dump($item->url);
}
$openaiClient = \Tectalic\OpenAi\Manager::build(new \GuzzleHttp\Client(), new \Tectalic\OpenAi\Authentication(getenv('OPENAI_API_KEY')));
/** @var \Tectalic\OpenAi\Models\AudioTranscriptions\CreateResponse $response */
$response = $openaiClient->audioTranscriptions()->create(
new \Tectalic\OpenAi\Models\AudioTranscriptions\CreateRequest([
'file' => '/full/path/to/audio/file.mp3',
'model' => 'whisper-1',
])
)->toModel();
echo $response->text;
// Your audio transcript in your source language...
$openaiClient = \Tectalic\OpenAi\Manager::build(new \GuzzleHttp\Client(), new \Tectalic\OpenAi\Authentication(getenv('OPENAI_API_KEY')));
/** @var \Tectalic\OpenAi\Models\AudioTranslations\CreateResponse $response */
$response = $openaiClient->audioTranslations()->create(
new \Tectalic\OpenAi\Models\AudioTranslations\CreateRequest([
'file' => '/full/path/to/audio/file.mp3',
'model' => 'whisper-1',
])
)->toModel();
echo $response->text;
// Your audio transcript in English...
// Load your project's composer autoloader (if you aren't already doing so).
use Symfony\Component\HttpClient\Psr18Client;
use Tectalic\OpenAi\Authentication;
use Tectalic\OpenAi\Client;
use Tectalic\OpenAi\Manager;
// Build a Tectalic OpenAI REST API Client globally.
$auth = new Authentication(getenv('OPENAI_API_KEY'));
$httpClient = new Psr18Client();
Manager::build($httpClient, $auth);
// or
// Build a Tectalic OpenAI REST API Client manually.
$auth = new Authentication(getenv('OPENAI_API_KEY'));
$httpClient = new Psr18Client();
$client = new Client($httpClient, $auth, Manager::BASE_URI);
use Tectalic\OpenAi\Handlers\AudioTranscriptions;
(new AudioTranscriptions())->create();
$client->audioTranscriptions()->create();
use Tectalic\OpenAi\Handlers\AudioTranscriptions;
$model = (new AudioTranscriptions())->create()->toModel();
use Tectalic\OpenAi\Handlers\AudioTranscriptions;
$array = (new AudioTranscriptions())->create()->toArray();
use Tectalic\OpenAi\Handlers\AudioTranscriptions;
$response = (new AudioTranscriptions())->create()->getResponse();
use Tectalic\OpenAi\Authentication;
use Tectalic\OpenAi\Client;
use Tectalic\OpenAi\ClientException;
use Tectalic\OpenAi\Manager;
// Build a Tectalic OpenAI REST API Client globally.
$auth = new Authentication('token');
Manager::build($httpClient, $auth);
$handler = new AudioTranscriptions();
// Perform a request
try {
$model = $handler->create()->toModel();
// Do something with the response model...
} catch (ClientException $e) {
// Error response received. Retrieve the HTTP response code and response body.
$responseBody = $handler->toArray();
$responseCode = $handler->getResponse()->getStatusCode();
// Handle the error...
}