kambo / huggingface-php

v0.1.0 2023-05-08 07:50 UTC

This package is auto-updated.

Last update: 2024-09-08 10:46:48 UTC


README

GitHub Workflow Status (main) Total Downloads Latest Version License

Huggingface PHP 是一个由社区维护的 PHP API 客户端,允许您与 Hugging Face API 交互。

目录

入门

需要 PHP 8.1+

首先,通过 Composer 包管理器安装 OpenAI

composer require kambo/huggingface-php

确保允许 php-http/discovery Composer 插件运行或安装,或者如果您的项目尚未集成 PSR-18 客户端,请手动安装客户端。

composer require guzzlehttp/guzzle

然后,与 Hugging Face 的 API 交互

use Kambo\Huggingface\Huggingface;
use Kambo\Huggingface\Enums\Type;

$yourApiKey = getenv('HUGGINGFACE_API_KEY');
$client = Huggingface::client($yourApiKey);

$result = $client->inference()->create([
    'model' => 'gpt2',
    'inputs' => 'The goal of life is?',
    'type' => Type::TEXT_GENERATION,
]);

echo $result['generated_text']."\n";

使用方法

推理 资源

创建

使用所选模型执行推理。

$response = $client->inference()->create([
    'model' => 'gpt2',
    'inputs' => 'The goal of life is?',
    'type' => Type::TEXT_GENERATION,
]);


$response->toArray(); // ['type' => .., 'generated_text' => ...]

承认

该库在源代码级别受到 PHP OpenAI 客户端的启发。部分代码直接复制自该出色的库。

Huggingface PHP 是一个开源软件,受 MIT 许可 许可。