GithubHelp home page GithubHelp logo

diplopito / openai-php Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mariuszduka/openai-php

0.0 0.0 0.0 2.15 MB

OpenAI PHP extension library

Home Page: https://openai-php.com

License: MIT License

C++ 81.18% C 16.43% Makefile 2.39%

openai-php's Introduction

OpenAI PHP extension library

The OpenAI PHP is an extension that allows PHP developers to integrate the powerful capabilities of OpenAI into their applications. Activating the extension on the web server will allow users to quickly access the services offered by OpenAI without additional libraries in PHP.

OpenAI PHP uses the PHP-CPP, OpenAI C++ and JSON for Modern C++ libraries.

Download and install

Download the latest release of the library for Linux (.so files) and Windows (.dll files).
Supported PHP versions: 7.4, 8.0, 8.1 and 8.2.

You can also download the libraries from the website openai-php.com.

Linux

On Linux systems, the libcurl library must be installed.

Before installation, check the installed version of PHP (NTS - non-tread safe, ZTS - thread safe) on your system with the command:

php -v

Unzip the archive and enter the root directory, for example:

tar xf openai-php-binary-linux.tar.gz
cd openai-php-binary-linux

Now, you can use the PHP, MODE and ZTS parameters in the make command. PHP means the PHP version (7.4, 8.0, 8.1, 8.2), MODE means operating modes (cli, fpm or apache2) and ZTS means thread safe (enable) or non-thread safe (disable) version.

The syntax for make is:

make PHP=7.4,8.0,8.1,8.2 MODE=cli,fpm,apache2 ZTS=enable,disable

For example, to install the extension for PHP 8.2, execute the command:

  • for PHP 8.2 ZTS (thread safe), CLI mode:
make PHP=8.2 MODE=cli ZTS=enable
  • for PHP 8.2 NTS (non-thread safe), FPM mode:
make PHP=8.2 MODE=fpm ZTS=disable

By default, if MODE parameter is not defined, the extension is installed for the PHP CLI.

Done! You may need to reload the webserver.

To uninstall the extension, execute the command:

make PHP=7.4,8.0,8.1,8.2 MODE=cli,fpm,apache2 uninstall

Windows

Download the latest release of binaries for Windows. Find the directory where PHP is installed and then upload the extension to the ext directory. For example, if you have installed the XAMPP on your C drive, upload the extension to the C:\xampp\php\ext directory.

Activate the extension by adding the following line to the php.ini file:

extension=openai.dll

After installation and configuration

Use the phpinfo() function to get detailed information about active extensions in PHP.

Basic usage

  1. Generate an API key on the website OpenAI.
  2. Run the following code in a console or browser. Done!
<?php

namespace OpenAI;
use Exception;

$OpenAI = new OpenAI(api_key: "YOUR_API_KEY");

try
{
    $response = $OpenAI->chat(request:
    [
        "model"    => "gpt-3.5-turbo",
        "messages" => 
        [
            "role"    => "user",
            "content" => "Do you know Elon Musk?"
        ]
    ], response: OpenAI::PHP_OPENAI_RESPONSE_ARRAY);

    print_r($response);
}
catch (Exception $e)
{
    echo("Hmm, something went wrong :(").PHP_EOL;
    echo $e->getMessage();
}

unset($OpenAI);

Advanced usage

Enable or disable the saving of logs to a file

The OpenAI PHP extension allows you to save all requests sent to the OpenAI system and all responses to log files. You can activate this function globally in the php.ini file:

openai.log_enable = 1
openai.log_filepath = "./openai.log"

Enabling this feature may increase server load, so the optimal option is to activate logging by the user. An example of activating this function in PHP code by the user might be:

define("OPENAI_LOG_ENABLE", 1);
define("OPENAI_LOG_FILEPATH", "./openai.log");

The format of the returned data

The default response parameter value is OpenAI::PHP_OPENAI_RESPONSE_ARRAY. This means that all functions return data as arrays by default. To return data in JSON format, use the OpenAI::PHP_OPENAI_RESPONSE_JSON constants.

$models = $OpenAI->models(
    response: OpenAI::PHP_OPENAI_RESPONSE_JSON
);

PHP 7

In PHP 7 (and earlier versions), function parameters are declared with a dollar sign followed by the parameter name, like this:

<?php
namespace OpenAI;

$OpenAI = new OpenAI($api_key = "YOUR_API_KEY");

$models = $OpenAI->models(
    $response = OpenAI::PHP_OPENAI_RESPONSE_ARRAY
);

Features

OpenAI API

API reference Method Example file
API models List models openai_models.php
API models Retrieve model openai_model.php
API completions Create completion openai_completions.php
API chat Create chat completion openai_chat.php
API edits Create completion openai_edits.php
API images Create image openai_images_create.php
API images Create image edit openai_images_edit.php
API images Create image variation openai_images_variation.php
API embeddings Create embeddings openai_embeddings.php
API audio Create transcription openai_audio_transcription.php
API audio Create translation openai_audio_translation.php
API files List file openai_files_list.php
API files Upload file openai_files_upload.php
API files Delete file openai_files_delete.php
API files Retrieve file openai_files_retrieve.php
API files Retrieve file content openai_files_content.php
API fine-tunes Create fine-tune openai_finetunes_create.php
API fine-tunes List fine-tune openai_finetunes_list.php
API fine-tunes Retrieve fine-tune openai_finetunes_retrieve.php
API fine-tunes Cancel fine-tune openai_finetunes_cancel.php
API fine-tunes List fine-tune events openai_finetunes_events.php
API fine-tunes Delete fine-tune model openai_finetunes_delete.php
API moderation Create moderation openai_moderations.php

Sample results

Create image Create image edit Create image variation

Installation from source code

Requirements

The following commands have been tested on Ubuntu 22.04.

If you are using Windows 11, you can use WSL to install Ubuntu.

  1. Log in as an administrator and update all libraries.
sudo -i
apt update
apt upgrade
cd /opt
  1. Install the development versions of PHP and libcurl.
apt install php8.1-dev
apt install libcurl4-gnutls-dev
  1. Download OpenAI PHP repo:
git clone https://github.com/mariuszduka/openai-php
cd openai-php
  1. Download and install PHP-CPP repo for PHP 8:
git clone https://github.com/fast-debug/PHP-CPP php-cpp
cd php-cpp
make
make install
make clean
cd ..
  1. Download the OpenAI C++ and JSON for Modern C++ libraries:
wget -O libs/openai-cpp/openai.hpp https://raw.githubusercontent.com/olrea/openai-cpp/main/include/openai/openai.hpp
wget -O libs/openai-cpp/nlohmann/json.hpp https://raw.githubusercontent.com/nlohmann/json/develop/single_include/nlohmann/json.hpp
  1. Update OpenAI C++ code for OpenAI PHP support:
patch libs/openai-cpp/openai.hpp libs/openai.hpp.patch
  1. Build OpenAI PHP extension:
make
make MODE=cli install

The MODE parameter can be cli, fpm or apache2.

Done! You may need to reload the webserver.

  1. Check that the OpenAI PHP extension is active in PHP:
php -m | grep openai
  1. Run the openai-basic.php file with:
php openai-basic.php

Known issues

Correct compilation of the PHP-CPP library causes the most problems, especially if we want to compile the library for Windows. Please check all pull requests as not all are accepted and you have to do it manually.

The optimal variant is to download the latest release of the library for Linux and Windows. All you have to do is download and activate the extension in your PHP configuration.

License

MIT License

Copyright (c) 2023 Mariusz Duka

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

openai-php's People

Contributors

mariuszduka avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.