Skip to content

这是一个基于 Hyperf 框架封装的 PHP 客户端组件,用于调用 [Replicate API](https://replicate.com/) 的 AI 推理 API。支持同步推理、Webhook 通知、以及协程等待任务完成

License

Notifications You must be signed in to change notification settings

Zzhenping/replicate-client

Repository files navigation

Replicate Client for Hyperf

这是一个基于 Hyperf 框架封装的 PHP 客户端组件,用于调用 Replicate API 的 AI 推理 API。支持同步推理、Webhook 通知、以及协程等待任务完成。


✨ 功能概览

  • 支持预测任务的创建与执行
  • 支持同步模式调用(适合短时间运行的模型)
  • 支持 Webhook 模式(适合异步回调)
  • 支持协程阻塞等待任务完成(基于 Swoole)
  • 支持任务取消、状态获取、任务列表

📦 安装

composer require zzhenping/replicate-client

🔧 快速生成配置文件

php bin/hyperf.php vendor:publish zzhenping/replicate-client

🧩 使用示例 运行预测任务

use Zzhenping\ReplicateClient\Prediction\Predictions;

class IndexController extends AbstractController
{

    public function __construct(protected Predictions $predictions){}

    public function index()
    {
        $result = $this->predictions->setWebhook()->run('models/black-forest-labs/flux-schnell/predictions', [
            "input" => [
                "prompt" => "black forest gateau cake spelling out the words 'FLUX SCHNELL', tasty, food photography, dynamic shot",
                "go_fast" => true,
                "megapixels"=> "1",
                "num_outputs"=> 1,
                "aspect_ratio"=> "1:1",
                "output_format"=> "webp",
                "output_quality"=> 80,
                "num_inference_steps"=> 4
            ]
        ]);
        return [
            'result' => $result
        ];
    }
}

🧩 方法说明

  • withPool(string $poolName): 为不同的模型池创建独立的请求客户端。
$this->predictions->withPool('default')->run()
  • setSync(): 使用同步模式(设置 Prefer: wait 请求头),适用于运行时间较短的任务。
$this->predictions->setSync()->run()
  • setWebhook(bool $bool = true): 使用 Webhook,接收回调。
$this->predictions->setWebhook()->run()

  • create(string $version, array $payload): 根据模型版本创建一个新的预测任务。
version: 模型版本 ID(从 Replicate 网站获取)
payload: 输入参数,如 prompt、图像 URL 等

  • run(string $version, array $payload): 直接调用指定模型用最新的版本进行预测。
model: 路由路径,如 models/black-forest-labs/flux-schnell/predictions
payload: 输入参数

  • get(string $id): 获取指定 ID 的预测任务状态。

  • cancel(string $id): 取消指定 ID 的预测任务。

  • coroutineWait(string $id): 协程阻塞等待任务完成,适用于要等待返回结果的预测。
\Swoole\Coroutine\run(function () use ($client) {
    $predictions = new Predictions($client);
    $response = $predictions->create('xxx', [
        "input" => [
            "image" => "xxx"
        ]
    ]);
    $result = $predictions->coroutineWait($response);
    $this->assertEquals($result['status'], 'succeeded');
});

📌 注意事项

若启用 Webhook 模式,请确保在初始化 ReplicateClient 时正确设置 webhook 地址。

协程等待功能依赖于 Swoole 环境,请确保开启协程支持。

建议同步模式仅用于运行时间不超过 60 秒的模型。

About

这是一个基于 Hyperf 框架封装的 PHP 客户端组件,用于调用 [Replicate API](https://replicate.com/) 的 AI 推理 API。支持同步推理、Webhook 通知、以及协程等待任务完成

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages