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

acquaintsoft logo
Acquaint Softtech

Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Visit Acquaint Softtech

The latest

View all →
Laravel, reimagined image

Laravel, reimagined

Read article
Laravel Rust: A New Rust Web Framework From Laravel image

Laravel Rust: A New Rust Web Framework From Laravel

Read article
Laravel Converse and Agent Kit for Chat Bots and AI Agents image

Laravel Converse and Agent Kit for Chat Bots and AI Agents

Read article
Test Tool Approval Flows in the Laravel AI SDK image

Test Tool Approval Flows in the Laravel AI SDK

Read article
Cutting Laravel News' Laravel Cloud Bill Nearly in Half image

Cutting Laravel News' Laravel Cloud Bill Nearly in Half

Read article
Inertia UI Notifications: Toasts and a Laravel Inbox image

Inertia UI Notifications: Toasts and a Laravel Inbox

Read article