rubix / client
The PHP client SDK for Rubix ML Server.
Fund package maintenance!
andrewdalpino
Requires
- php: >=7.4
- guzzlehttp/guzzle: ^7.2
- guzzlehttp/psr7: ^1.7
- psr/http-message: ^1.0
- rubix/ml: ^2.0
- symfony/polyfill-php80: ^1.17
Requires (Dev)
- friendsofphp/php-cs-fixer: 2.18.*
- phpstan/extension-installer: ^1.0
- phpstan/phpstan: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-11-20 00:16:24 UTC
README
The PHP client SDK for Rubix ML Server.
Installation
Install Rubix Client SDK using Composer:
$ composer require rubix/client
Requirements
- PHP 7.4 or above
Documentation
The latest documentation can be found below.
Table of Contents
Clients
Clients allow you to communicate directly with a model server using a friendly object-oriented interface inside your PHP applications. Under the hood, clients handle all the networking communication and content negotiation for you so you can write programs as if the model was directly accessible in your applications.
Return the predictions from the model:
public predict(Dataset $dataset) : array
use Rubix\Client\RESTClient; $client = new RESTClient('127.0.0.1', 8080); // Import a dataset $predictions = $client->predict($dataset);
Calculate the joint probabilities of each sample in a dataset:
public proba(Dataset $dataset) : array
Calculate the anomaly scores of each sample in a dataset:
public score(Dataset $dataset) : array
Async Clients
Clients that implement the Async Client interface have asynchronous versions of all the standard client methods. All asynchronous methods return a Promises/A+ object that resolves to the return value of the response. Promises allow you to perform other work while the request is processing or to execute multiple requests in parallel. Calling the wait()
method on the promise will block until the promise is resolved and return the value.
public predictAsync(Dataset $dataset) : Promise
$promise = $client->predictAsync($dataset); // Do something else $predictions = $promise->wait();
Return a promise for the probabilities predicted by the model:
public probaAsync(Dataset $dataset) : Promise
Return a promise for the anomaly scores predicted by the model:
public scoreAsync(Dataset $dataset) : Promise
REST Client
The REST Client communicates with the HTTP Server through the JSON REST API it exposes.
Interfaces: Client, AsyncClient
Parameters
Example
use Rubix\Client\RESTClient; use Rubix\Client\HTTP\Middleware\BasicAuthenticator; use Rubix\Client\HTTP\Middleware\CompressRequestBody; use Rubix\Client\HTTP\Middleware\BackoffAndRetry; use Rubix\Client\HTTP\Encoders\Gzip; $client = new RESTClient('127.0.0.1', 443, true, [ new BasicAuthenticator('user', 'password'), new CompressRequestBody(new Gzip(1)), new BackoffAndRetry(), ], 0.0, true);
Client Middleware
Similarly to Server middleware, client middlewares are functions that hook into the request/response cycle but from the client end. Some of the server middlewares have accompanying client middleware such as Basic Authenticator and Shared Token Authenticator.
Backoff and Retry
The Backoff and Retry middleware handles Too Many Requests (429) and Service Unavailable (503) responses by retrying the request after waiting for a period of time to avoid overloading the server even further. An acceptable backoff period is gradually achieved by multiplicatively increasing the delay between retries.
Parameters
Example
use Rubix\Client\HTTP\Middleware\BackoffAndRetry; $middleware = new BackoffAndRetry(5, 0.5);
Basic Authenticator (Client Side)
Adds the necessary authorization headers to the request using the Basic scheme.
Parameters
Example
use Rubix\Client\HTTP\Middleware\BasicAuthenticator; $middleware = new BasicAuthenticator('morgan', 'secret');
Compress Request Body
Apply the Gzip compression algorithm to the request body.
Parameters
Example
use Rubix\Client\HTTP\Middleware\CompressRequestBody; $middleware = new CompressRequestBody(5, 65535);
Shared Token Authenticator (Client Side)
Adds the necessary authorization headers to the request using the Bearer scheme.
Parameters
Example
use Rubix\Client\HTTP\Middleware\SharedtokenAuthenticator; $middleware = new SharedTokenAuthenticator('secret');
License
The code is licensed MIT and the documentation is licensed CC BY-NC 4.0.