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

serpapi logo
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi

The latest

View all →
Screenshot: Capture Images and PDFs with PHP and Laravel image

Screenshot: Capture Images and PDFs with PHP and Laravel

Read article
Reset Sticky Database Connections Between Laravel Queue Jobs image

Reset Sticky Database Connections Between Laravel Queue Jobs

Read article
PHP Security Releases Fix Two Critical RCE Vulnerabilities image

PHP Security Releases Fix Two Critical RCE Vulnerabilities

Read article
Prevent Stale Database Reads in Laravel 13.38 image

Prevent Stale Database Reads in Laravel 13.38

Read article
Critical PHP Security Release Coming Tomorrow (October 21) image

Critical PHP Security Release Coming Tomorrow (October 21)

Read article
Critical Token Guard Auth Bypass Fixed in Laravel 13.29 and 12.68 image

Critical Token Guard Auth Bypass Fixed in Laravel 13.29 and 12.68

Read article