Laravel Packages

OpenAI SDK for PHP

Published
OpenAI SDK for PHP image

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:

$response = $open_ai->classification([
"examples" => [
["A happy moment", "Positive"],
["I am sad.", "Negative"],
["I am feeling awesome", "Positive"]
],
"labels" => ["Positive", "Negative", "Neutral"],
"query" => "It is a rainy day :(",
"search_model" => "ada",
"model" => "curie",
]);

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

{
"completion": "cmpl-4KKvYbROfgIroNbeTBPmAmAzKZcUC",
"label": "Negative",
"model": "curie:2020-05-03",
"object": "classification",
"search_model": "ada",
"selected_examples": [
{
"document": 1,
"label": "Negative",
"text": "I am sad."
},
{
"document": 0,
"label": "Positive",
"text": "A happy moment"
},
{
"document": 2,
"label": "Positive",
"text": "I am feeling awesome"
}
]
}

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:

$response = $open_ai->classification([/* ... */]);
$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.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Sponsored

tinkerwell logo
Tinkerwell

Enjoy coding and debugging in an editor designed for fast feedback and quick iterations. It's like a shell for your application – but with multi-line editing, code completion, and more.

Visit Tinkerwell

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article