friendsofhyperf/openai-client

The openai client component for Hyperf.

v3.1.48 2024-11-28 08:49 UTC

README

Latest Test Latest Version on Packagist Total Downloads GitHub license

OpenAI PHP for Laravel is a supercharged community PHP API client that allows you to interact with the Open AI API.

Note: This repository contains the integration code of the OpenAI PHP for Hyperf. If you want to use the OpenAI PHP client in a framework-agnostic way, take a look at the openai-php/client repository.

Get Started

Requires PHP 8.1+

First, install OpenAI via the Composer package manager:

composer require friendsofhyperf/openai-client

Next, publish the configuration file:

php bin/hyperf.php vendor:publish friendsofhyperf/openai-client

This will create a config/autoload/openai.php configuration file in your project, which you can modify to your needs using environment variables:

OPENAI_API_KEY=sk-...

Finally, you may use the OpenAI\Client instance from container to access the OpenAI API:

use OpenAI\Client;

$result = di(OpenAI\Client::class)->completions()->create([
    'model' => 'text-davinci-003',
    'prompt' => 'PHP is',
]);

echo $result['choices'][0]['text']; // an open-source, widely-used, server-side scripting language.

Azure

In order to use the Azure OpenAI Service, it is necessary to construct the client manually using the factory.

$client = OpenAI::factory()
    ->withBaseUri('{your-resource-name}.openai.azure.com/openai/deployments/{deployment-id}')
    ->withHttpHeader('api-key', '{your-api-key}')
    ->withQueryParam('api-version', '{version}')
    ->make();

To use Azure, you must deploy a model, identified by the {deployment-id}, which is already incorporated into the API calls. As a result, you do not have to provide the model during the calls since it is included in the BaseUri.

Therefore, a basic sample completion call would be:

$result = $client->completions()->create([
    'prompt' => 'PHP is'
]);

Usage

For usage examples, take a look at the openai-php/client repository.

Contact

License

OpenAI PHP for Hyperf is an open-sourced software licensed under the MIT license.