dbfdg
# OpenAI API Client in PHP
> ### ChatGPT API is currently supported, [click here](#chat-as-known-as-chatgpt-api) for the implementation introductions.
*A message from creator,
Thank you for visiting the __@orhanerday/open-ai__ repository! If you find this repository helpful or useful, we encourage you to **star** it
on GitHub. Starring a repository is a way to show your support for the project. It also helps to increase the visibility
of the project and to let the community know that it is valuable. Thanks again for your support and we hope you find the
repository useful!
Orhan*
[](https://packagist.org/packages/orhanerday/open-ai)
[](https://packagist.org/packages/orhanerday/open-ai)
# Featured in
[](https://blog.jetbrains.com/phpstorm/2022/12/php-annotated-december-2022/#:~:text=orhanerday/open%2Dai%20%E2%80%93%20A%20PHP%20SDK%20for%20accessing%20the%20OpenAI%20GPT%2D3%20API)
[](https://laravel-news.com/openai-sdk-for-php)
[](http://tubring.cn/articles/59)
# Comparison With Other Packages
| Project Name | Required PHP Version | Description | Type (Official / Community) | Support |
|------------------------|----------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------|----------------------------------------------------------------------------------------------------------------------------------------|
| **orhanerday/open-ai** | **PHP 7.4+** | **Most downloaded, forked, contributed, huge community supported, and used PHP SDK for OpenAI GPT-3 and DALL-E. It also supports chatGPT-like streaming.** | Community | Available, ([Community driven Discord Server](https://discord.gg/xpGUD528XJ) or personal mail [orhan@duck.com](mailto:orhan@duck.com)) |
| openai-** */c****t | PHP 8.1+ | OpenAI PHP API client. | Community | - |
## About this package
Fully open-source and secure community-maintained, PHP SDK for accessing the OpenAI GPT-3 API.
> #### For more information, you can read laravel news [blog post](https://laravel-news.com/openai-sdk-for-php).
> #### Free support is available. [Join our discord server](#join-our-discord-server)
> #### To get started with this package, you'll first want to be familiar with the [OpenAI API documentation](https://beta.openai.com/docs/introduction) and [examples](https://beta.openai.com/examples). Also you can get help from our discord channel that called [#api-support](https://discord.gg/R9CpVUdqQR)
## News
- orhanerday/open-ai added to community libraries php [section](https://beta.openai.com/docs/libraries/php).
- orhanerday/open-ai featured
on [PHPStorm blog post](https://blog.jetbrains.com/phpstorm/2022/12/php-annotated-december-2022/#:~:text=orhanerday/open%2Dai%20%E2%80%93%20A%20PHP%20SDK%20for%20accessing%20the%20OpenAI%20GPT%2D3%20API),
thanks JetBrains!
> Requires PHP 7.4+
## Join our discord server

[Click here to join the Discord server](https://discord.gg/xpGUD528XJ)
## Support this project
As you may know, OpenAI PHP is an open-source project wrapping tool for OpenAI. We rely on the support of our community
to continue developing and maintaining the project, and one way that you can help is by making a donation.
Donations allow us to cover expenses such as hosting costs(for testing), development tools, and other resources that are
necessary to keep the project running smoothly. Every contribution, no matter how small, helps us to continue improving
OpenAI PHP for everyone.
If you have benefited from using OpenAI PHP and would like to support its continued development, we would greatly
appreciate a donation of any amount. You can make a donation through;
* [Buy me a coffee](https://www.buymeacoffee.com/orhane)
* [Patreon](https://patreon.com/orhann)
* [Click here for the Coinbase QR](#btc) **Bitcoin** > 34w2DftWGkDqDbYMixkmdWWMLmaP9uTRz7
* [Click here for the Coinbase QR](#doge) **Dogecoin** > DHiqcZox9M8kYDn7BkesnN6Z2kJ7dYG9Lc
* [Click here for the Coinbase QR](#eth) **Ethereum** > 0x135E2D5d7AC40c6850f844BA589D68e91a268Ceb
Thank you for considering a donation to Orhanerday/OpenAI PHP SDK. Your support is greatly appreciated and helps to
ensure that the project can continue to grow and improve.
*Sincerely,*
**Orhan Erday** / Creator.
# Documentation
Please visit https://orhanerday.gitbook.io/openai-php-api-1/
# Endpoint Support
- Chat
- [x] [ChatGPT API](#chat-as-known-as-chatgpt-api)
- Models
- [x] [List models](https://beta.openai.com/docs/api-reference/models/list)
- [x] [Retrieve model](https://beta.openai.com/docs/api-reference/models/retrieve)
- Completions
- [x] [Create completion](https://beta.openai.com/docs/api-reference/completions/create)
- Edits
- [x] [Create edits](https://beta.openai.com/docs/api-reference/edits/create)
- Images
- [x] [Create image](https://beta.openai.com/docs/api-reference/images/create)
- [x] [Create image edit](https://beta.openai.com/docs/api-reference/images/create-edit)
- [x] [Create image variation](https://beta.openai.com/docs/api-reference/images/create-variation)
- Embeddings
- [x] [Create embeddings](https://beta.openai.com/docs/api-reference/embeddings/create)
- Audio
- [x] [Create transcription](https://platform.openai.com/docs/api-reference/audio/create)
- [x] [Create translation](https://platform.openai.com/docs/api-reference/audio/create)
- Files
- [x] [List files](https://beta.openai.com/docs/api-reference/files/list)
- [x] [Upload file](https://beta.openai.com/docs/api-reference/files/upload)
- [x] [Delete file](https://beta.openai.com/docs/api-reference/files/delete)
- [x] [Retrieve file](https://beta.openai.com/docs/api-reference/files/retrieve)
- [x] [Retrieve file content](https://beta.openai.com/docs/api-reference/files/retrieve-content)
- Fine-tunes
- [x] [Create fine-tune (beta)](https://beta.openai.com/docs/api-reference/fine-tunes/create)
- [x] [List fine-tunes (beta)](https://beta.openai.com/docs/api-reference/fine-tunes/list)
- [x] [Retrieve fine-tune (beta)](https://beta.openai.com/docs/api-reference/fine-tunes/retrieve)
- [x] [Cancel fine-tune (beta)](https://beta.openai.com/docs/api-reference/fine-tunes/cancel)
- [x] [List fine-tune events (beta)](https://beta.openai.com/docs/api-reference/fine-tunes/events)
- [x] [Delete fine-tune model (beta)](https://beta.openai.com/docs/api-reference/fine-tunes/delete-model)
- Moderation
- [x] [Create moderation](https://beta.openai.com/docs/api-reference/moderations/create)
- ~~Engines~~ *(deprecated)*
- ~~[List engines](https://beta.openai.com/docs/api-reference/engines/list)~~
- ~~[Retrieve engine](https://beta.openai.com/docs/api-reference/engines/retrieve)~~
## Installation
You can install the package via composer:
```bash
composer require orhanerday/open-ai
```
## Quick Start ⚡
Before you get starting, you should set OPENAI_API_KEY as ENV key, and set OpenAI key as env value with the following
commands;
_Powershell_
```powershell
$Env:OPENAI_API_KEY = "sk-gjtv....."
```
_Cmd_
```cmd
set OPENAI_API_KEY=sk-gjtv.....
```
_Linux or macOS_
```shell
export OPENAI_API_KEY=sk-gjtv.....
```
> Getting issues while setting up env? Please read
> the [article](https://help.openai.com/en/articles/5112595-best-practices-for-api-key-safety) or you can check
> my [StackOverflow answer](https://stackoverflow.com/a/73904271/15196622) for the Windows® ENV setup.
Create your `index.php` file and paste the following code part into the file.
```php
chat([
'model' => 'gpt-3.5-turbo',
'messages' => [
[
"role" => "system",
"content" => "You are a helpful assistant."
],
[
"role" => "user",
"content" => "Who won the world series in 2020?"
],
[
"role" => "assistant",
"content" => "The Los Angeles Dodgers won the World Series in 2020."
],
[
"role" => "user",
"content" => "Where was it played?"
],
],
'temperature' => 1.0,
'max_tokens' => 4000,
'frequency_penalty' => 0,
'presence_penalty' => 0,
]);
var_dump($chat);
echo "
";
echo "
";
echo "
";
// decode response
$d = json_decode($chat);
// Get Content
echo($d->choices[0]->message->content);
```
_Run the server with the following command_
```shell
php -S localhost:8000 -t .
```
## Usage
### Load your key from an environment variable.
> According to the following code `$open_ai` is the base variable for all open-ai operations.
```php
use Orhanerday\OpenAi\OpenAi;
$open_ai = new OpenAi(env('OPEN_AI_API_KEY'));
```
## Requesting organization
For users who belong to multiple organizations, you can pass a header to specify which organization is used for an API
request.
Usage from these API requests will count against the specified organization's subscription quota.
````php
$open_ai_key = getenv('OPENAI_API_KEY');
$open_ai = new OpenAi($open_ai_key);
$open_ai->setORG("org-IKN2E1nI3kFYU8ywaqgFRKqi");
````
## Base URL
You can specify Origin URL with `setBaseURL()` method;
````php
$open_ai_key = getenv('OPENAI_API_KEY');
$open_ai = new OpenAi($open_ai_key,$originURL);
$open_ai->setBaseURL("https://ai.example.com/");
````
## Use Proxy
You can use some proxy servers for your requests api;
````php
$open_ai->setProxy("http://127.0.0.1:1086");
````
## Set header
```php
$open_ai->setHeader(["Connection"=>"keep-alive"]);
```
## Get cURL request info
> ### !!! WARNING:Your API key will expose if you add this method to your code, therefore remove the method before deployment. Be careful !
You can get cURL info after the request.
````php
$open_ai = new OpenAi($open_ai_key);
echo $open_ai->listModels(); // you should execute the request FIRST!
var_dump($open_ai->getCURLInfo()); // You can call the request
````
## Chat (as known as ChatGPT API)
Given a chat conversation, the model will return a chat completion response.
```php
$complete = $open_ai->chat([
'model' => 'gpt-3.5-turbo',
'messages' => [
[
"role" => "system",
"content" => "You are a helpful assistant."
],
[
"role" => "user",
"content" => "Who won the world series in 2020?"
],
[
"role" => "assistant",
"content" => "The Los Angeles Dodgers won the World Series in 2020."
],
[
"role" => "user",
"content" => "Where was it played?"
],
],
'temperature' => 1.0,
'max_tokens' => 4000,
'frequency_penalty' => 0,
'presence_penalty' => 0,
]);
```
## Accessing the Element
```php
choices[0]->message->content);
```
> ### Related: [ChatGPT Clone Project](#chatgpt-clone-project)
## Completions
Given a prompt, the model will return one or more predicted completions, and can also return the probabilities of
alternative tokens at each position.
```php
$complete = $open_ai->completion([
'model' => 'text-davinci-002',
'prompt' => 'Hello',
'temperature' => 0.9,
'max_tokens' => 150,
'frequency_penalty' => 0,
'presence_penalty' => 0.6,
]);
```
### Stream Example
This feature might sound familiar from [ChatGPT](https://chat.openai.com/chat).