friendsofhyperf / openai-client
The openai client component for Hyperf.
Fund package maintenance!
huangdijia
hdj.me/sponsors
Requires
- php: >=8.1
- hyperf/config: ~3.1.0
- hyperf/di: ~3.1.0
- hyperf/guzzle: ~3.1.0
- openai-php/client: ^0.10.0
- dev-main / 3.1.x-dev
- v3.1.41
- v3.1.31
- v3.1.29
- v3.1.28.2
- v3.1.28.1
- v3.1.28
- v3.1.27
- v3.1.17
- v3.1.1
- v3.1.0
- v3.1.0-rc.22
- v3.1.0-rc.4
- v3.1.0-beta.20
- v3.1.0-beta.15
- v3.1.0-beta.9
- v3.1.0-beta.1
- 3.0.x-dev
- v3.0.105
- v3.0.85
- v3.0.80
- v3.0.70
- v3.0.55
- v3.0.54
- v3.0.0
- 0.x-dev
- v0.5.1
- v0.5.0
- v0.4.3-beta.1
- v0.4.2
- v0.4.1
- v0.4.0
- v0.3.4
- v0.3.2
- v0.3.0
- v0.2.1
- v0.2.0
This package is auto-updated.
Last update: 2024-10-25 03:50:55 UTC
README
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.