Skip to content

The OpenAi PHP Client is a user-friendly PHP library designed to facilitate interactions with the OpenAI Rest API.

License

Notifications You must be signed in to change notification settings

thojou/openai-php-client

Repository files navigation

OpenAi PHP Client

Static Badge CI Coverage License

The OpenAi PHP Client is a user-friendly PHP library designed to facilitate interactions with the OpenAI Rest API.

Requirements

  • PHP version >= 8.1

Installation

You can effortlessly install the OpenAi PHP Client using the popular package manager composer.

composer require thojou/openai-php-client

Usage

To begin utilizing the capabilities of the OpenAi PHP Client, you'll need an active OpenAI API key. If you don't have one yet, you can obtain it here.

<?php

use Thojou\OpenAi\OpenAi;

require_once __DIR__ . '/vendor/autoload.php';

$apiKey = "<YOUR API KEY>";

$openAi = new OpenAi($apiKey);
$result = $openAI->chat()->completion([
    'model' => 'gpt-3.5-turbo',
    'prompt' => 'This is a test',
]);

echo $result['choices'][0]['message']['content']; // Prints the openai chat answer

For more practical examples, please refer to the examples folder.

Limitations

Certain endpoints, namely Edits, Fine-Tunes and Completions, are not integrated due to their deprecated status within the OpenAI API.

Documentation

The OpenAi PHP Client is designed to seamlessly align with the request and response formats meticulously outlined in the OpenAI API documentation. This comprehensive resource offers all the essential information about request structures and expected responses.

OpenAi Class

The OpenAi class is a central component of the OpenAI API interaction library. It provides methods for accessing different API endpoints such as Models, Chat, Images, Embeddings, Audio, Files, Fine-Tuning, and Moderations.

Constructor

public function __construct(
    string $apiKey,
    string $baseUri = 'https://api.openai.com/v1/'
)

Creates an instance of the OpenAi class.

  • Parameters:
    • $apiKey (string): The API key to authenticate with the OpenAI API.
    • $baseUri (string, optional): The base URI for API requests. Defaults to 'https://api.openai.com/v1/'.

Methods

models()
public function models(): Models

Returns an instance of the Models endpoint.

  • Returns:
    • Models: An instance of the Models endpoint.
chat()
public function chat(): Chat

Returns an instance of the Chat endpoint.

  • Returns:
    • Chat: An instance of the Chat endpoint.
images()
public function images(): Images

Returns an instance of the Images endpoint.

  • Returns:
    • Images: An instance of the Images endpoint.
embeddings()
public function embeddings(): Embeddings

Returns an instance of the Embeddings endpoint.

  • Returns:
    • Embeddings: An instance of the Embeddings endpoint.
audio()
public function audio(): Audio

Returns an instance of the Audio endpoint.

  • Returns:
    • Audio: An instance of the Audio endpoint.
files()
public function files(): Files

Returns an instance of the Files endpoint.

  • Returns:
    • Files: An instance of the Files endpoint.
fineTuning()
public function fineTuning(): FineTuning

Returns an instance of the FineTuning endpoint.

  • Returns:
    • FineTuning: An instance of the FineTuning endpoint.
moderation()
public function moderation(): Moderations

Returns an instance of the Moderations endpoint.

  • Returns:
    • Moderations: An instance of the Moderations endpoint.

This class provides a structured way to interact with various endpoints of the OpenAI API. Each method returns an instance of the corresponding endpoint class, allowing you to perform API operations more efficiently.

License

This project is licensed under the generous and permissive MIT license.

About

The OpenAi PHP Client is a user-friendly PHP library designed to facilitate interactions with the OpenAI Rest API.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages