marceloeatworld/replicate-php 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

marceloeatworld/replicate-php

Composer 安装命令:

composer require marceloeatworld/replicate-php

包简介

#1 PHP client for the Replicate API, compatible with Laravel and native PHP, built on Saloon v4

README 文档

README

Latest Version on Packagist GitHub Tests Action Status PHPStan

#1 A framework-agnostic PHP client for the Replicate API compatible with Laravel and native PHP, built on Saloon v4.

Full coverage of the Replicate HTTP API: predictions, models, deployments, trainings, files, collections, hardware, webhooks, and account.

This package is a fork of benbjurstrom/replicate-php which only covered predictions. This version has been entirely rewritten with full API coverage, Saloon v4, PHP 8.2+, and typed DTOs for every endpoint.

Requirements

  • PHP 8.2+

Installation

composer require marceloeatworld/replicate-php

Quick Start

use MarceloEatWorld\Replicate\Replicate;

$replicate = new Replicate(
    apiToken: $_ENV['REPLICATE_API_TOKEN'],
);

Create a prediction

$prediction = $replicate->predictions()->create(
    version: 'stability-ai/sdxl:c221b2b8ef527988fb59bf24a8b97c4561f1c671f73bd389f866bfb27c061316',
    input: ['prompt' => 'a photo of an astronaut riding a horse on mars'],
);

$prediction->id;     // "xyz123"
$prediction->status; // "starting"

Create a prediction using an official model

$prediction = $replicate->models()->createPrediction(
    owner: 'meta',
    name: 'meta-llama-3-70b-instruct',
    input: ['prompt' => 'Write a haiku about PHP'],
);

Synchronous predictions (wait for result)

$prediction = $replicate->predictions()->create(
    version: 'stability-ai/sdxl:c221b2b8ef527988fb59bf24a8b97c4561f1c671f73bd389f866bfb27c061316',
    input: ['prompt' => 'a painting of a cat'],
    wait: 60, // wait up to 60 seconds for completion
);

if ($prediction->status === 'succeeded') {
    $prediction->output; // result is ready
}

Get prediction status

$prediction = $replicate->predictions()->get('xyz123');
$prediction->status; // "succeeded"
$prediction->output; // ["https://replicate.delivery/..."]

List predictions

$list = $replicate->predictions()->list();
$list->results; // array of PredictionData
$list->next;    // cursor for next page

// Paginate
$nextPage = $replicate->predictions()->list(cursor: $list->next);

Cancel a prediction

$replicate->predictions()->cancel('xyz123');

Webhooks

Pass webhook parameters directly to creation methods:

$prediction = $replicate->predictions()->create(
    version: 'owner/model:version',
    input: ['prompt' => 'hello'],
    webhook: 'https://example.com/webhook',
    webhookEventsFilter: ['completed'],
);

Get the webhook signing secret for verification:

$secret = $replicate->webhooks()->getSecret();
$secret->key; // "whsec_..."

Streaming

$prediction = $replicate->predictions()->create(
    version: 'owner/model:version',
    input: ['prompt' => 'hello'],
    stream: true,
);

// If the model supports streaming, use the stream URL
$prediction->urls['stream']; // SSE endpoint URL

Models

// List public models
$models = $replicate->models()->list();

// Get a model
$model = $replicate->models()->get('stability-ai', 'sdxl');

// Create a model
$model = $replicate->models()->create(
    owner: 'your-username',
    name: 'my-model',
    hardware: 'gpu-a40-large',
    visibility: 'private',
);

// Update a model
$model = $replicate->models()->update('your-username', 'my-model', [
    'description' => 'Updated description',
]);

// Delete a model (must be private, no versions)
$replicate->models()->delete('your-username', 'my-model');

Model Versions

$versions = $replicate->models()->listVersions('stability-ai', 'sdxl');
$version = $replicate->models()->getVersion('stability-ai', 'sdxl', 'abc123');
$replicate->models()->deleteVersion('your-username', 'my-model', 'abc123');

Deployments

// List deployments
$deployments = $replicate->deployments()->list();

// Get a deployment
$deployment = $replicate->deployments()->get('your-username', 'my-deployment');

// Create a deployment
$deployment = $replicate->deployments()->create(
    name: 'my-deployment',
    model: 'your-username/my-model',
    version: 'abc123...',
    hardware: 'gpu-a40-large',
    minInstances: 1,
    maxInstances: 3,
);

// Update a deployment
$deployment = $replicate->deployments()->update('your-username', 'my-deployment', [
    'min_instances' => 2,
    'max_instances' => 5,
]);

// Create prediction on a deployment
$prediction = $replicate->deployments()->createPrediction(
    owner: 'your-username',
    name: 'my-deployment',
    input: ['prompt' => 'hello world'],
);

// Delete a deployment
$replicate->deployments()->delete('your-username', 'my-deployment');

Trainings

// Create a training
$training = $replicate->trainings()->create(
    owner: 'stability-ai',
    name: 'sdxl',
    versionId: 'abc123...',
    destination: 'your-username/my-trained-model',
    input: ['train_data' => 'https://example.com/data.zip'],
    webhook: 'https://example.com/training-done',
);

// Get training status
$training = $replicate->trainings()->get($training->id);

// List trainings
$trainings = $replicate->trainings()->list();

// Cancel a training
$replicate->trainings()->cancel($training->id);

Files

// Upload a file
$file = $replicate->files()->upload(
    content: file_get_contents('/path/to/image.jpg'),
    filename: 'image.jpg',
    contentType: 'image/jpeg',
);

// Get file metadata
$file = $replicate->files()->get($file->id);

// List files
$files = $replicate->files()->list();

// Delete a file
$replicate->files()->delete($file->id);

Collections

// List collections
$collections = $replicate->collections()->list();

// Get a collection with its models
$collection = $replicate->collections()->get('text-to-image');
$collection->models; // array of ModelData

Hardware

// List available hardware
$hardware = $replicate->hardware()->list();
// Returns array of HardwareData with name and sku

Account

$account = $replicate->account()->get();
$account->username;
$account->type; // "user" or "organization"

Using with Laravel

Add your credentials to your services config:

// config/services.php
'replicate' => [
    'api_token' => env('REPLICATE_API_TOKEN'),
],

Bind in a service provider:

// app/Providers/AppServiceProvider.php
public function register(): void
{
    $this->app->bind(Replicate::class, fn () => new Replicate(
        apiToken: config('services.replicate.api_token'),
    ));
}

Use anywhere:

$prediction = app(Replicate::class)->predictions()->get($id);

Testing

Use Saloon's built-in mocking:

use Saloon\Http\Faking\MockClient;
use Saloon\Http\Faking\MockResponse;
use MarceloEatWorld\Replicate\Requests\Predictions\GetPrediction;

$mockClient = new MockClient([
    GetPrediction::class => MockResponse::make(['id' => 'xyz', 'status' => 'succeeded']),
]);

$replicate = new Replicate('test-token');
$replicate->withMockClient($mockClient);

$prediction = $replicate->predictions()->get('xyz');
$prediction->status; // "succeeded"

Response Data

All responses are returned as typed data objects:

DTO Description
AccountData Account info
PredictionData Single prediction
PredictionsData Paginated prediction list
ModelData Single model
ModelsData Paginated model list
ModelVersionData Single model version
ModelVersionsData Paginated version list
CollectionData Single collection with models
CollectionsData Paginated collection list
DeploymentData Single deployment
DeploymentsData Paginated deployment list
TrainingData Single training
TrainingsData Paginated training list
FileData Single file metadata
FilesData Paginated file list
HardwareData Hardware option (name + SKU)
WebhookSecretData Webhook signing secret

Credits

License

The MIT License (MIT). See License File for more information.

marceloeatworld/replicate-php 适用场景与选型建议

marceloeatworld/replicate-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 684 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 11 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「API-Client」 「ai」 「replicate」 「machine-learning」 「saloon」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 marceloeatworld/replicate-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 marceloeatworld/replicate-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 marceloeatworld/replicate-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 684
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 31
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-20