secondtruth/huggingface

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

This package is auto-updated.

Last update: 2024-09-10 03:17:29 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 许可证