c-delouvencourt / replicate-php
Replicate API 的 PHP 客户端
资助包维护!
benbjurstrom
Requires
- php: ^8.1.0
- sammyjo20/saloon: ^2.0
Requires (Dev)
- laravel/pint: ^1.4
- pestphp/pest: ^2.0.0
- pestphp/pest-plugin-arch: 2.x-dev
- phpstan/phpstan: ^1.9.11
- symfony/var-dumper: ^6.2.3
README
这是一个基于惊人的 Saloon v2 🤠 库构建的,与框架无关的 PHP 客户端,用于 Replicate.com。使用它可以从您的 PHP 应用程序轻松地与机器学习模型(如 Stable Diffusion)交互。
目录
🚀 快速入门
使用 composer 安装。
composer require benbjurstrom/replicate-php
创建一个新的 api 实例。
use BenBjurstrom\Replicate\Replicate; ... $api = new Replicate( apiToken: $_ENV['REPLICATE_API_TOKEN'], );
然后使用它来调用您的模型(或称为 Replicate 中的“创建预测”)。
$version = 'db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf'; $input = [ 'model' => 'stable-diffusion-2-1', 'prompt' => 'a photo of an astronaut riding a horse on mars', 'negative_prompt' => 'moon, alien, spaceship', 'width' => 768, 'height' => 768, 'num_inference_steps' => 50, 'guidance_scale' => 7.5, 'scheduler' => 'DPMSolverMultistep', 'seed' => null, ]; $data = $api->predictions()->create($version, $input); $data->id; // yfv4cakjzvh2lexxv7o5qzymqy
请注意,输入参数将根据您使用哪个版本(模型)而有所不同。在这个例子中,版本 db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf 是一个针对速度优化的 Stable Diffusion 2.1 模型。
与 Laravel 一起使用
首先,将您的凭据添加到服务配置文件中。
// config/services.php 'replicate' => [ 'api_token' => env('REPLICATE_API_TOKEN'), ],
在服务提供者中绑定 Replicate
类。
// app/Providers/AppServiceProvider.php public function register() { $this->app->bind(Replicate::class, function () { return new Replicate( apiToken: config('services.replicate.api_token'), ); }); }
然后在应用程序的任何地方使用它。
$data = app(Replicate::class)->predictions()->get($id);
使用 Saloon 的出色 响应录制 测试您的集成。
use Saloon\Laravel\Saloon; // composer require sammyjo20/saloon-laravel "^2.0" ... Saloon::fake([ MockResponse::fixture('getPrediction'), ]); $id = 'yfv4cakjzvh2lexxv7o5qzymqy'; // The initial request will check if a fixture called "getPrediction" // exists. Because it doesn't exist yet, the real request will be // sent and the response will be recorded to tests/Fixtures/Saloon/getPrediction.json. $data = app(Replicate::class)->predictions()->get($id); // However, the next time the request is made, the fixture will // exist, and Saloon will not make the request again. $data = app(Replicate::class)->predictions()->get($id);
响应数据
所有响应都以数据对象的形式返回。详细信息可以在以下类属性中找到
Webhooks
Replicate 允许您配置一个 webhook,当您的预测完成后被调用。要这样做,请在调用 create
方法之前将 withWebhook($url)
连接到您的 api 实例。例如
$api->predictions()->withWebhook('https://www.example.com/webhook')->create($version, $input); $data->id; // la5xlbbrfzg57ip5jlx6obmm5y
可用的预测方法
get()
用于获取现有预测的详细信息。如果预测已完成,结果将在输出属性下。
use BenBjurstrom\Replicate\Data\PredictionData; ... $id = 'la5xlbbrfzg57ip5jlx6obmm5y' /* @var PredictionData $data */ $data = $api->predictions()->get($id); $data->output[0]; // https://replicate.delivery/pbxt/6UFOVtl1xCJPAFFiTB2tfveYBNRLhLmJz8yMQAYCOeZSFhOhA/out-0.png
list()
用于获取预测的游标分页列表。返回 PredictionsData 对象。
use BenBjurstrom\Replicate\Data\PredictionsData ... /* @var PredictionsData $data */ $data = $api->predictions()->list( cursor: '123', // optional ); $data->results[0]->id; // la5xlbbrfzg57ip5jlx6obmm5y
create()
用于创建新的预测(调用模型)。返回 PredictionData 对象。
use BenBjurstrom\Replicate\Data\PredictionData; ... $version = '5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa'; $input = [ 'text' => 'Alice' ]; /* @var PredictionData $data */ $data = $api->predictions() ->withWebhook('https://www.example.com/webhook') // optional ->create($version, $input); $data->id; // la5xlbbrfzg57ip5jlx6obmm5y
致谢
许可
MIT 许可证(MIT)。有关更多信息,请参阅 许可文件。