OpenAI SDK for PHP

Packages

December 29th, 2021

OpenAI SDK for PHP

The orhanerday/open-ai package is a PHP SDK for accessing the OpenAI GPT-3 API. It supports the complete, search, answer, classification, and engine APIs.

Here's an example of using the OpenAI classification API with this package:

1$response = $open_ai->classification([
2 "examples" => [
3 ["A happy moment", "Positive"],
4 ["I am sad.", "Negative"],
5 ["I am feeling awesome", "Positive"]
6 ],
7 "labels" => ["Positive", "Negative", "Neutral"],
8 "query" => "It is a rainy day :(",
9 "search_model" => "ada",
10 "model" => "curie",
11]);

You'll get back the following classification for the "It is a rainy day :(" query:

1{
2 "completion": "cmpl-4KKvYbROfgIroNbeTBPmAmAzKZcUC",
3 "label": "Negative",
4 "model": "curie:2020-05-03",
5 "object": "classification",
6 "search_model": "ada",
7 "selected_examples": [
8 {
9 "document": 1,
10 "label": "Negative",
11 "text": "I am sad."
12 },
13 {
14 "document": 0,
15 "label": "Positive",
16 "text": "A happy moment"
17 },
18 {
19 "document": 2,
20 "label": "Positive",
21 "text": "I am feeling awesome"
22 }
23 ]
24}

This package is lightweight, with no external dependencies apart from the cURL and JSON PHP extensions. You'll be required to decode the JSON API responses to work with the data:

1$response = $open_ai->classification([/* ... */]);
2$data = json_decode($response, true);

To get started with this package, you'll first want to be familiar with the OpenAI API documentation and examples.

You can learn more about this package, get full installation instructions, and view the source code on GitHub.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.