benbjurstrom / replicate-php
Replicate API的PHP客户端
v0.3.0
2024-02-15 16:52 UTC
Requires
- php: ^8.1.0
- saloonphp/saloon: ^3.0
Requires (Dev)
- laravel/pint: ^1.4
- pestphp/pest: ^2.0.0
- pestphp/pest-plugin-arch: 2.5.0
- phpstan/phpstan: ^1.9.11
- symfony/var-dumper: ^6.2.3
README
这是一个与框架无关的PHP客户端,用于Replicate.com,基于惊人的Saloon v3 🤠 库。使用它可以直接从您的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)。有关更多信息,请参阅许可证文件。