soiposervices/replicate-php

Replicate API的PHP客户端

资助包维护!
benbjurstrom

0.0.1 2024-02-08 16:57 UTC

This package is auto-updated.

Last update: 2024-09-08 23:16:36 UTC


README

这是一个与框架无关的PHP客户端,用于Replicate.com,基于惊人的Saloon v3 🤠 库构建。使用它可以直接从您的PHP应用程序中轻松地与机器学习模型(如Stable Diffusion)交互。

Latest Version on Packagist GitHub Tests Action Status

目录

🚀 快速入门

使用Composer安装。

composer require soiposervices/replicate-php

创建一个新的api实例。

use SoipoServices\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 SoipoServices\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 SoipoServices\Replicate\Data\PredictionsData
...

/* @var PredictionsData $data */
$data = $api->predictions()->list(
    cursor: '123', // optional
);

$data->results[0]->id; // la5xlbbrfzg57ip5jlx6obmm5y

create()

用于创建新的预测(调用模型)。返回PredictionData对象。

use SoipoServices\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)。请参阅许可证文件以获取更多信息。

如果您喜欢这个项目,请考虑支持SoipoServices

Buy Me A Coffee