vityakut / proxyapi
Proxy Api PHP for Laravel is a supercharged PHP API client that allows you to interact with the Open AI API
Requires
- php: ^8.1.0
- guzzlehttp/guzzle: ^7.8.1
- laravel/framework: ^9.46.0|^10.34.2|^11.0
- openai-php/client: ^v0.8.0
Requires (Dev)
- laravel/pint: ^1.13.6
- pestphp/pest: ^2.27.0
- pestphp/pest-plugin-arch: ^2.4.1
- phpstan/phpstan: ^1.10.47
- symfony/var-dumper: ^6.4.0|^7.0.1
This package is auto-updated.
Last update: 2024-10-26 20:15:28 UTC
README
Proxy Api PHP for Laravel is a community-maintained PHP API client that allows you to interact with the Open AI Proxy API. If you or your business relies on this package, it's important to support the developers who have contributed their time and effort to create and maintain this valuable tool:
Note: This repository contains the integration code of the OpenAI PHP for Laravel. 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 vityakut/proxyapi
Next, execute the install command:
php artisan proxyapi:install
This will create a config/proxyapi.php
configuration file in your project, which you can modify to your needs
using environment variables.
Blank environment variables for the Proxy API key are already appended to your .env
file.
PROXYAPI_API_KEY=sk-...
Finally, you may use the ProxyApi
facade to access the OpenAI API:
use vityakut\ProxyApi\Facades\ProxyApi; $result = ProxyApi::chat()->create([ 'model' => 'gpt-3.5-turbo', 'messages' => [ ['role' => 'user', 'content' => 'Hello!'], ], ]); echo $result->choices[0]->message->content; // Hello! How can I assist you today?
Configuration
Configuration is done via environment variables or directly in the configuration file (config/proxyapi.php
).
ProxyAPI Key
Specify your Proxy API Key. This will be used to authenticate with the Proxy API - you can find your API key on your OpenAI dashboard, at https://proxyapi.ru/.
PROXYAPI_API_KEY=
Request Timeout
The timeout may be used to specify the maximum number of seconds to wait for a response. By default, the client will time out after 30 seconds.
PROXYAPI_REQUEST_TIMEOUT=
Usage
For usage examples, take a look at the openai-php/client repository.
Testing
The OpenAI
facade comes with a fake()
method that allows you to fake the API responses.
The fake responses are returned in the order they are provided to the fake()
method.
All responses are having a fake()
method that allows you to easily create a response object by only providing the parameters relevant for your test case.
use OpenAI\Laravel\Facades\OpenAI; use OpenAI\Responses\Completions\CreateResponse; ProxyApi::fake([ CreateResponse::fake([ 'choices' => [ [ 'text' => 'awesome!', ], ], ]), ]); $completion = ProxyApi::completions()->create([ 'model' => 'gpt-3.5-turbo-instruct', 'prompt' => 'PHP is ', ]); expect($completion['choices'][0]['text'])->toBe('awesome!');
After the requests have been sent there are various methods to ensure that the expected requests were sent:
// assert completion create request was sent ProxyApi::assertSent(Completions::class, function (string $method, array $parameters): bool { return $method === 'create' && $parameters['model'] === 'gpt-3.5-turbo-instruct' && $parameters['prompt'] === 'PHP is '; });
For more testing examples, take a look at the openai-php/client repository.
ProxyApi PHP for Laravel is an open-sourced software licensed under the MIT license.