Download the PHP package easygithdev/phpopenai without Composer
On this page you can find all versions of the php package easygithdev/phpopenai. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download easygithdev/phpopenai
More information about easygithdev/phpopenai
Files in easygithdev/phpopenai
Package phpopenai
Short Description PHPOpenAI is a wrapper to access OpenAI API
License MIT
Informations about the package phpopenai
PHPOpenAI
PHPOpenAI
is a community-maintained library that enables the use of the OpenAI
API in PHP.
The project is written in PHP and can be used to easily integrate the OpenAI API
into your existing PHP project.
System Requirements
This project is based on PHP version 8.1 in order to use features such as enumerations. This project does not require any external dependencies. However, you must have the cURL extension installed for it to work properly.
- PHP version >= 8.1
- cURL extension
Installation
The project uses Composer to manage dependencies. If you haven't already installed Composer, you can do so by following the instructions on the official Composer website.
Packagist install
To install the project, you can install the package from packagist.org using the following command:
Writing a first example
To use the OpenAI API
, you need to sign up on their website and obtain an API key. Once you have your API key, you can use it in your PHP code to send requests to the OpenAI API.
To find out how to get your key, go to the following address:
https://help.openai.com/en/articles/4936850-where-do-i-find-my-secret-api-key.
Here's an example code that shows you how to use the OpenAI API in PHP:
This code instantiates a new OpenAIApi
object with an API key, and then creates a new Completion
object to perform text completion with the GPT-3 AI language model provided by OpenAI.
The create()
method is called on the Completion
object to generate a new text completion. It takes two parameters:
- the first parameter is the ID of the GPT-3 model to use for completion. In this case, it uses the TEXT_DAVINCI_003 model.
- the second parameter is the prompt or input text for which the completion will be generated. In this case, the prompt is "Say this is a test".
The result of the completion is returned in the $response
variable. The result can then be used for further processing, such as displaying the completed text or feeding it into another part of the program for additional processing.
Manage the API Key
You can use an environment variable to store your key. You can then use this variable as in the following example:
You can put the variable in Apache configuration file :
And then restart the service.
Now, you can use the environment variable by calling the getenv()
function of PHP.
Manage the organization
If you wish to provide information about your organization, you must proceed as follows.
Manage the API's Url
If you need to modify the API's URL, you can proceed as follows:
To redefine a route, you need to extend the OpenAIRoute
class or implement the Route
interface.
Manage the reponses
The API returns responses in JSON format. To facilitate access to the different information, you can call toObject()
or toArray()
methods of the Handler object to access the data.
Manage the errors
Sometimes, the API returns errors. Therefore, it is necessary to be able to identify what caused the problem. To handle this difficulty, you have many options.
If you are using a Handler object with toObject()
or toArray()
methods, just use a try-catch
structure.
If you are using the CurlResponse
object, you can check that an error has occurred using the validators.
An example of application
Here is a video showing an application that allows you to create images with a painting style defined by the user. This application is created using the PHPOpenAI project.
https://user-images.githubusercontent.com/3519890/230593418-22f26562-e88c-4a92-9601-93bb671a16c0.mp4
You can find the code here:
https://github.com/EasyGithDev/PHPOpenAI-Playground.git.
Code samples
Integrating OpenAI into your application is now as simple as a few lines of code.
You can find all codes here:
https://github.com/EasyGithDev/PHPOpenAI-Examples.
Text Completion using ChatGPT
Learn more about chat completion.
Text Completion using GPT-3
Learn more about text completion.
Text Completion using stream
The stream attribute in the OpenAI API is an optional parameter that you can use to control the data flow returned by the API. If you set this option to True, the API will return a response as a streaming data rather than a single response.
This means that you can retrieve the results of the API as they become available, rather than waiting for the complete response before processing them. This option can be useful for applications that require real-time processing of large amounts of data.
https://user-images.githubusercontent.com/3519890/229144053-d32e6416-0980-44ea-97b7-b2d4a2d26a5d.mp4
Text Edit
Image Generation Using DALL·E
Learn more about image generation.
Image Variation Using DALL·E
Learn more about image variation.
Image Edit Using DALL·E
Embedding
Audio Transcription (Speech to text) using Whisper
Learn more about audio transcription.
Audio Translation (Speech to text) using Whisper
Learn more about audio translation.
Model List
Model Retrieve
Model Delete
File List
File Upload
File Delete
File Retrieve
File Retrieve Content
Fine-tune List
Fine-tune Create
Fine-tune Retrieve
Fine-tune List Events
Fine-tune Cancel
Moderation
Testing
To run all tests:
To run only one test :
Summary
PHPOpenAI is a useful project for PHP developers who want to easily integrate the OpenAI API into their projects. With simple installation and the use of Composer, text classification, image generation and named entity recognition into your PHP application.
All versions of phpopenai with dependencies
ext-curl Version *