OpenAI PHP SDK : Most downloaded, forked, contributed, huge community supported, and used PHP (Laravel , Symfony, Yii, Cake PHP or any PHP framework) SDK for Op
# OpenAI API Client in PHP
## If you want to ensure this project stays alive and continues to faster improve, please please consider supporting it. Your help means a lot. Thank you!
* [Buy me a coffee](https://www.buymeacoffee.com/orhane)
* [Patreon](https://patreon.com/orhann)
> ### 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*
# Featured in
# Comparison With Other Packages
| Project Name | Required PHP Version (Lower is better) | 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 [
[email protected]](mailto:
[email protected])) |
| 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://platform.openai.com/docs/overview) and [examples](https://platform.openai.com/docs/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://platform.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)
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://platform.openai.com/docs/api-reference/models/list)
- [x] [Retrieve model](https://platform.openai.com/docs/api-reference/models/retrieve)
- Completions
- [x] [Create completion](https://platform.openai.com/docs/api-reference/completions/create)
- Edits
- [x] [Create edits](https://platform.openai.com/docs/api-reference/edits/create)
- Images
- [x] [Create image](https://platform.openai.com/docs/api-reference/images/create)
- [x] [Create image edit](https://platform.openai.com/docs/api-reference/images/create-edit)
- [x] [Create image variation](https://platform.openai.com/docs/api-reference/images/create-variation)
- Embeddings
- [x] [Create embeddings](https://platform.openai.com/docs/api-reference/embeddings/create)
- Audio
- [x] [Text to Speech (TTS)](https://platform.openai.com/docs/guides/text-to-speech)
- [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://platform.openai.com/docs/api-reference/files/list)
- [x] [Upload file](https://platform.openai.com/docs/api-reference/files/upload)
- [x] [Delete file](https://platform.openai.com/docs/api-reference/files/delete)
- [x] [Retrieve file](https://platform.openai.com/docs/api-reference/files/retrieve)
- [x] [Retrieve file content](https://platform.openai.com/docs/api-reference/files/retrieve-content)
- Fine-tunes
- [x] [Create fine-tune (beta)](https://platform.openai.com/docs/api-reference/fine-tunes/create)
- [x] [List fine-tunes (beta)](https://platform.openai.com/docs/api-reference/fine-tunes/list)
- [x] [Retrieve fine-tune (beta)](https://platform.openai.com/docs/api-reference/fine-tunes/retrieve)
- [x] [Cancel fine-tune (beta)](https://platform.openai.com/docs/api-reference/fine-tunes/cancel)
- [x] [List fine-tune events (beta)](https://platform.openai.com/docs/api-reference/fine-tunes/events)
- [x] [Delete fine-tune model (beta)](https://platform.openai.com/docs/api-reference/fine-tunes/delete-model)
- Moderation
- [x] [Create moderation](https://platform.openai.com/docs/api-reference/moderations/create)
- ~~Engines~~ *(deprecated)*
- ~~[List engines](https://platform.openai.com/docs/api-reference/engines/list)~~
- ~~[Retrieve engine](https://platform.openai.com/docs/api-reference/engines/retrieve)~~
- Assistants (beta)
- [x] [Create assistant](https://platform.openai.com/docs/api-reference/assistants/createAssistant)
- [x] [Retrieve assistant](https://platform.openai.com/docs/api-reference/assistants/getAssistant)
- [x] [Modify assistant](https://platform.openai.com/docs/api-reference/assistants/modifyAssistant)
- [x] [Delete assistant](https://platform.openai.com/docs/api-reference/assistants/deleteAssistant)
- [x] [Lists assistants](https://platform.openai.com/docs/api-reference/assistants/listAssistants)
- [x] [Create assistant file](https://platform.openai.com/docs/api-reference/assistants/createAssistantFile)
- [x] [Retrieve assistant file](https://platform.openai.com/docs/api-reference/assistants/getAssistantFile)
- [x] [Delete assistant file](https://platform.openai.com/docs/api-reference/assistants/deleteAssistantFile)
- [x] [List assistant files](https://platform.openai.com/docs/api-reference/assistants/listAssistantFiles)
- Threads (beta)
- [x] [Create thread](https://platform.openai.com/docs/api-reference/threads/createThread)
- [x] [Retrieve thread](https://platform.openai.com/docs/api-reference/threads/getThread)
- [x] [Modify thread](https://platform.openai.com/docs/api-reference/threads/modifyThread)
- [x] [Delete thread](https://platform.openai.com/docs/api-reference/threads/deleteThread)
- Messages (beta)
- [x] [Create message](https://platform.openai.com/docs/api-reference/messages/createMessage)
- [x] [Retrieve message](https://platform.openai.com/docs/api-reference/messages/getMessage)
- [x] [Modify message](https://platform.openai.com/docs/api-reference/messages/modifyMessage)
- [x] [Lists messages](https://platform.openai.com/docs/api-reference/messages/listMessages)
- [x] [Retrieve message file](https://platform.openai.com/docs/api-reference/messages/getMessageFile)
- [x] [List message files](https://platform.openai.com/docs/api-reference/messages/listMessageFiles)
- Runs (beta)
- [x] [Create run](https://platform.openai.com/docs/api-reference/runs/createRun)
- [x] [Retrieve run](https://platform.openai.com/docs/api-reference/runs/getRun)
- [x] [Modify run](https://platform.openai.com/docs/api-reference/runs/modifyRun)
- [x] [Lists runs](https://platform.openai.com/docs/api-reference/runs/listRuns)
- [x] [Submit tool outputs](https://platform.openai.com/docs/api-reference/runs/submitToolOutputs)
- [x] [Cancel run](https://platform.openai.com/docs/api-reference/runs/cancelRun)
- [x] [Create thread and run](https://platform.openai.com/docs/api-reference/runs/createThreadAndRun)
- [x] [Retrieve run step](https://platform.openai.com/docs/api-reference/runs/getRunStep)
- [x] [List run steps](https://platform.openai.com/docs/api-reference/runs/listRunSteps)
## 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.
```
…
```
_Run the server with the following command_
```shell
php -S localhost:8000 -t .
```
## NVIDIA NIM INTEGRATION
orhanerday/open-ai supports Nvidia NIM. The below example is MixtralAI. Check https://build.nvidia.com/explore/discover for more examples.
```
…
```
## 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