ruscoe / openai-php
PHP library for the OpenAI API
Requires
- php: >=5.4.0
- guzzlehttp/guzzle: ^7.5
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.11
This package is auto-updated.
Last update: 2024-11-09 04:37:16 UTC
README
An unofficial library for OpenAI's API.
Requirements
- PHP 5.4.0 or above
- Composer
- An OpenAI API key
Quick set up
git clone git@github.com:ruscoe/openai-php.git
cd openai-php
composer install
Usage examples
The following examples assume you store your API key in an environment variable. To do this on a Linux or MacOS system, run:
export OPENAI_API_KEY=sk-XA0yN...
Be sure to substitute your own API key after OPENAI_API_KEY=
.
Completions
This example asks the gpt-3.5-turbo
model to describe a keyboard.
It instructs the OpenAI API to use more than the default number of
tokens so a reasonable length description can be returned.
<?php require __DIR__ . '/vendor/autoload.php'; // @see https://platform.openai.com/docs/api-reference/authentication $api_key = getenv('OPENAI_API_KEY'); $api = new OpenAI\OpenAICompletions($api_key); $parameters = [ 'max_tokens' => 128, ]; $response = $api->create('gpt-3.5-turbo', 'Describe a keyboard', 1, $parameters); var_dump($response);
The response:
object(stdClass)#34 (6) {
["id"]=>
string(34) "cmpl-7EqnVAO0xpxtWkDepi2s64AG7UAbU"
["object"]=>
string(15) "text_completion"
["created"]=>
int(1683773901)
["model"]=>
string(16) "gpt-3.5-turbo"
["choices"]=>
array(1) {
[0]=>
object(stdClass)#32 (4) {
["text"]=>
string(663) "
A keyboard is a computer peripheral device used for entering data and commands into a computer or other device. It consists of a keypad, usually a set of standard-sized keys corresponding to letters, numbers, symbols, and/or functionality, which can be powered by various means including electricity, batteries, or solar cells. It generally has several special-function keys that can be used to control program functions, such as volume or brightness levels. Modern keyboards often include multimedia hotkeys, and are typically back-lit in order to make typing in the dark easier. Some keyboards may include a palm rest to help reduce fatigue caused by extended"
["index"]=>
int(0)
["logprobs"]=>
NULL
["finish_reason"]=>
string(6) "length"
}
}
["usage"]=>
object(stdClass)#18 (3) {
["prompt_tokens"]=>
int(4)
["completion_tokens"]=>
int(128)
["total_tokens"]=>
int(132)
}
}
Chat
This example sends a simple chat message.
<?php require __DIR__ . '/vendor/autoload.php'; // @see https://platform.openai.com/docs/api-reference/authentication $api_key = getenv('OPENAI_API_KEY'); $api = new OpenAI\OpenAIChat($api_key); $messages = [ (object) ['role' => 'user', 'content' => 'Hello, friend!'], ]; $response = $api->create('gpt-3.5-turbo', $messages); var_dump($response);
The response:
object(stdClass)#35 (6) {
["id"]=>
string(38) "chatcmpl-7EqphlIJngrqxGAiDdtV9JMu89NhQ"
["object"]=>
string(15) "chat.completion"
["created"]=>
int(1683774037)
["model"]=>
string(18) "gpt-3.5-turbo-0301"
["usage"]=>
object(stdClass)#33 (3) {
["prompt_tokens"]=>
int(12)
["completion_tokens"]=>
int(10)
["total_tokens"]=>
int(22)
}
["choices"]=>
array(1) {
[0]=>
object(stdClass)#29 (3) {
["message"]=>
object(stdClass)#19 (2) {
["role"]=>
string(9) "assistant"
["content"]=>
string(40) "Hello there, how may I assist you today?"
}
["finish_reason"]=>
string(4) "stop"
["index"]=>
int(0)
}
}
}
Images
Create image
This example asks for two images of a red six-sided dice in water.
<?php require __DIR__ . '/vendor/autoload.php'; // @see https://platform.openai.com/docs/api-reference/authentication $api_key = getenv('OPENAI_API_KEY'); $api = new OpenAI\OpenAIImages($api_key); $response = $api->createAsURL('A red six-sided dice in water', 2, '256x256');
The response
Create image variation
<?php require __DIR__ . '/vendor/autoload.php'; // @see https://platform.openai.com/docs/api-reference/authentication $api_key = getenv('OPENAI_API_KEY'); $api = new OpenAI\OpenAIImages($api_key); $response = $api->createVariationAsURL('variation.png', 1, '256x256');
The source and response
Create image edit
<?php require __DIR__ . '/vendor/autoload.php'; // @see https://platform.openai.com/docs/api-reference/authentication $api_key = getenv('OPENAI_API_KEY'); $api = new OpenAI\OpenAIImages($api_key); $response = $api->createEditAsURL('edit.png', 'a duck driving a car', 'edit_mask.png', 1, '256x256');
The source and response
Fine-tuning a model
First, take a look at the official documentation on fine-tuning.
Create and upload your training file. Example:
training.jsonl
{"prompt": "bird =", "completion": " animal"}
{"prompt": "dog =", "completion": " animal"}
{"prompt": "cat =", "completion": " animal"}
{"prompt": "limestone =", "completion": " rock"}
{"prompt": "shale =", "completion": " rock"}
{"prompt": "marble =", "completion": " rock"}
<?php require __DIR__ . '/vendor/autoload.php'; // @see https://platform.openai.com/docs/api-reference/authentication $api_key = getenv('OPENAI_API_KEY'); $api = new OpenAI\OpenAIFiles($api_key); $file = $api->uploadFile('training.jsonl');
List your files to get the file ID:
$api = new OpenAI\OpenAIFiles($api_key); $files = $api->getFiles(); var_dump($files);
Create a new training job using the file ID you've obtained:
$api = new OpenAI\OpenAIFineTunes($api_key); $api->create('file-1CO...');
List models to get the name of your new fine-tuned model:
$api = new OpenAI\OpenAIModels($api_key); $models = $api->getModels(); var_dump($models);
The model name will start with curie:ft
and end with the current date and time.
Create a new completion using your fine-tuned model.
$api = new OpenAI\OpenAICompletions($api_key); $response = $api->create('curie:ft...', 'dog =', 1, $parameters);
The response should include "animal".
$api = new OpenAI\OpenAICompletions($api_key); $response = $api->create('curie:ft...', 'marble =', 1, $parameters);
The response should include "rock".