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

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
Agent Run Observability in Laravel AI SDK 0.11 image

Agent Run Observability in Laravel AI SDK 0.11

Read article
Debounced Queued Event Listeners in Laravel image

Debounced Queued Event Listeners in Laravel

Read article
Statamic Mailables Viewer Previews Laravel Emails in the Control Panel image

Statamic Mailables Viewer Previews Laravel Emails in the Control Panel

Read article
Laravel Tackle: Run an AI Coding Agent in Your Laravel App image

Laravel Tackle: Run an AI Coding Agent in Your Laravel App

Read article
Queue::forward(): Reroute Laravel Queues in One Place image

Queue::forward(): Reroute Laravel Queues in One Place

Read article
Laravel Read-Through Filesystem: Lazy Storage Migration image

Laravel Read-Through Filesystem: Lazy Storage Migration

Read article