sabatinomasala/replicate-php

Replicate API 的 PHP 客户端

1.2.0 2024-08-09 15:11 UTC

This package is auto-updated.

Last update: 2024-09-16 11:31:57 UTC


README

这是一个 Replicate 的 PHP 客户端。它允许您从 PHP 代码中运行模型,并在 Replicate 上执行各种其他操作。

致谢

本软件包基于以下工作的改进:https://github.com/sawirricardo/replicate-php

视频

如果您是视觉学习者,可以查看展示此软件包的视频

Watch the video

安装

您可以通过 composer 安装此软件包

composer require sabatinomasala/replicate-php

获取 API 令牌

您可以从 https://replicate.com/account/api-tokens 获取 API 令牌

创建预测

您可以通过以下方式创建预测

// Create a prediction
$prediction = $client->predictions()->create('stability-ai/sdxl:7762fd07cf82c948538e41f63f77d685e02b063e37e496e96eefd46c929f9bdc', [
    'prompt' => 'a cat wearing a cowboy hat',
]);

$id = $prediction->json('id');

// Fetch prediction
$prediction = $client->predictions()->get($id);
dd($prediction->json())

运行模型

您可以通过以下方式运行模型并等待输出

$token = env('REPLICATE_TOKEN');
$client = new SabatinoMasala\Replicate\Replicate($token);
$output = $client->run('stability-ai/sdxl:7762fd07cf82c948538e41f63f77d685e02b063e37e496e96eefd46c929f9bdc', [
    'prompt' => 'a cat wearing a cowboy hat',
], function($prediction) {
    // You can log the current state of the prediction
    \Log::info('Progress', $prediction->json());
});

dd($output[0]);

链式多个模型

因为 'run' 返回模型的输出,所以您可以像这样链式多个模型

$output = $client->run('stability-ai/sdxl:7762fd07cf82c948538e41f63f77d685e02b063e37e496e96eefd46c929f9bdc', [
    'prompt' => 'a cat wearing a cowboy hat',
]);

$caption = $client->run('salesforce/blip:2e1dddc8621f72155f24cf2e0adbde548458d3cab9f00c0139eea840d0ac4746', [
    'task' => 'image_captioning',
    'image' => $output[0],
]);

dd($caption); // Caption: a drawing of a cat wearing a cowboy hat

与官方模型一起使用

官方模型,如 Llama,有不同的 API 端点。此客户端会根据您提供的输入自动切换端点

// This will do an API call to https://api.replicate.com/v1/models/meta/meta-llama-3-70b-instruct/predictions instead of https://api.replicate.com/v1/predictions
$output = $replicate->run('meta/meta-llama-3-70b-instruct', [
    'prompt' => 'I want to travel to Japan, give me an itinerary',
    'max_tokens' => 1000,
], function($prediction) {
    \Log::info($prediction->json('output'));
});

致谢

许可证

MIT 许可证 (MIT)。请参阅 许可证文件 获取更多信息。