laswitchtech/php-openai

OpenAI Library for PHP Applications

v1.0.3 2023-03-02 17:19 UTC

This package is auto-updated.

Last update: 2024-04-30 00:51:30 UTC


README

GitHub repo logo

phpOpenAI

License GitHub repo size GitHub top language Version

Features

  • Easy to use OpenAI cURL Library

Why you might need it

If you are looking for an easy way to integrate OpenAI in your project. This PHP Class is for you.

Can I use this?

Sure!

License

This software is distributed under the GNU General Public License v3.0 license. Please read LICENSE for information on the software availability and distribution.

Requirements

  • PHP >= 5.6.0

Security

Please disclose any vulnerabilities found responsibly – report security issues to the maintainers privately.

Installation

Using Composer:

composer require laswitchtech/php-openai

How do I use it?

In this documentations, we will use a table called users for our examples.

Example

Initiate phpOpenAI

//Import phpOpenAI class into the global namespace
//These must be at the top of your script, not inside a function
use LaswitchTech\phpOpenAI\phpOpenAI;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Initiate phpOpenAI
$phpOpenAI = new phpOpenAI('YOUR_API_KEY');

Completions

//Import phpOpenAI class into the global namespace
//These must be at the top of your script, not inside a function
use LaswitchTech\phpOpenAI\phpOpenAI;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Initiate phpOpenAI
$phpOpenAI = new phpOpenAI('YOUR_API_KEY');

//Request
$phpOpenAI->completions([
  'model' => 'text-davinci-003',
  'prompt' => 'Say this is a test',
  'temperature' => 0,
  'max_tokens' => 7,
  'top_p' => 1,
  'n' => 1,
  'stream' => false,
  'logprobs' => null,
  'stop' => '\n',
]); // Returns an array

Edits

//Import phpOpenAI class into the global namespace
//These must be at the top of your script, not inside a function
use LaswitchTech\phpOpenAI\phpOpenAI;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Initiate phpOpenAI
$phpOpenAI = new phpOpenAI('YOUR_API_KEY');

//Request
$phpOpenAI->edits([
  'model' => 'text-davinci-edit-001',
  'input' => 'What day of the wek is it?',
  'instruction' => 'Fix the spelling mistakes',
]); // Returns an array

Moderations

//Import phpOpenAI class into the global namespace
//These must be at the top of your script, not inside a function
use LaswitchTech\phpOpenAI\phpOpenAI;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Initiate phpOpenAI
$phpOpenAI = new phpOpenAI('YOUR_API_KEY');

//Request
$phpOpenAI->moderations([
  'model' => 'text-moderation-stable',
  'input' => 'I want to kill them.',
]); // Returns an array