OpenAI for PHP

orhanerday
2 min readDec 31, 2022

--

The openai PHP package is a PHP library that allows developers to access and use the functionality provided by the OpenAI API from their PHP applications. The OpenAI API is a set of machine learning models and tools that enable developers to build intelligent applications using the latest advances in artificial intelligence.

To install the openai PHP package, you can use the Composer package manager by running the following command:

composer require orhanerday/open-ai

Before you can start using the openai PHP package, you will need to obtain an API key from OpenAI. You can do this by signing up for an account on the OpenAI website and obtaining a key through the API dashboard. Once you have your API key, you will need to set it as an environment variable. This can be done using the export command on Linux or macOS, or the set command on Windows.

To use the openai PHP package, you will need to include the autoloader file in your PHP script, and then create an instance of the OpenAi class, passing in your API key as an argument. You can then call various methods on the OpenAi instance to access the functionality provided by the OpenAI API, such as generating text with the GPT-3 model or performing language translation with the OpenAI Transformer.

Here’s an example of how you might use the openai PHP package to generate text using the GPT-3 model:

<?php

require __DIR__ . '/vendor/autoload.php'; // remove this line if you use a PHP Framework.

use Orhanerday\OpenAi\OpenAi;

$open_ai_key = getenv('OPENAI_API_KEY');
$open_ai = new OpenAi($open_ai_key);

$generated_text = $open_ai->completion([
'model' => 'davinci',
'prompt' => 'Hello',
'temperature' => 0.9,
'max_tokens' => 150,
'frequency_penalty' => 0,
'presence_penalty' => 0.6,
]);

echo $generated_text;

This code creates an instance of the OpenAi class using your API key, and then calls the completion method with a set of parameters to generate text using the GPT-3 model. The generated text is then output to the console.

Overall, the openai PHP package is a useful tool for developers looking to build intelligent applications using the OpenAI API from within their PHP code. Whether you want to generate text, translate languages, or perform other tasks using the latest advances in artificial intelligence, the openai PHP package has you covered.

Github Repo: https://github.com/orhanerday/open-ai

--

--