teariot/php-yandex-gpt

一个用于与Yandex GPT(生成式预训练变换器)API无缝交互的PHP库,提供文本生成、分词和嵌入功能。

1.1.1 2024-04-05 09:38 UTC

This package is auto-updated.

Last update: 2024-09-05 10:30:45 UTC


README

此PHP库提供了一个方便的接口,用于与Yandex GPT(生成式预训练变换器)API交互,以实现文本生成、分词和获取嵌入。

安装

要通过Composer安装库,请使用以下命令

composer require teariot/php-yandex-gpt

用法

请确保您已从Yandex GPT获取了必要的OAuth令牌和文件夹ID。

文本补全

要生成文本补全,请使用complete方法

<?php

const OAuthToken = 'YOUR_OAUTH_TOKEN';
const folder_id = 'YOUR_FOLDER_ID';

public static function complete(string $message): array
{
    $cloud = new Cloud(self::OAuthToken, self::folder_id);
    $completion = new Completion();
    
    $completion->setModelUri(self::folder_id, 'yandexgpt-lite/latest')
            ->addText([
                [
                    'role' => $completion::USER,
                    'text' => $message,
                ]
            ]);

    $result = $cloud->request($completion);
    return json_decode($result, true);
}
?>

或者您可以使用异步文本生成。

<?php

const OAuthToken = 'YOUR_OAUTH_TOKEN';
const folder_id = 'YOUR_FOLDER_ID';

public static function complete(string $message): array
{
    $cloud = new Cloud(self::OAuthToken, self::folder_id);
    $completion = new Completion();
    
    $completion->setModelUri(self::folder_id, 'yandexgpt-lite/latest')
            ->addText([
                [
                    'role' => $completion::USER,
                    'text' => $message,
                ]
            ])
            ->isAsync();
            
    $taskData = $cloud->request($completion);
    $taskData = json_decode($taskData, true);
    
    $operation = new Operation();
    if (!empty($taskData) && isset($taskData['id'])) {
        $operation = $operation->waitAndGet($result['id'])
            ->setTimeOut(240);  // Optional: Sets the timeout for the operation. Default timeout is 120 seconds.
            
        $result = $cloud->request($operation);
        $result = json_decode($result, true);
        return json_decode($result, true);
    }
    return [];
}
?>

complete方法的高级使用

此变体展示了如何通过结合系统消息和用户消息来扩展complete方法的使用场景。

<?php

const OAuthToken = 'YOUR_OAUTH_TOKEN';
const folder_id = 'YOUR_FOLDER_ID';

public static function complete(string $systemMessage, string $userMessage): array
{
    $cloud = new Cloud(self::OAuthToken, self::folder_id);
    $completion = new Completion();
    
    $completion->setModelUri(self::folder_id, 'yandexgpt-lite/latest')
            ->addText([
                [
                    'role' => $completion::SYSTEM,
                    'text' => $systemMessage,
                ],
                [
                    'role' => $completion::USER,
                    'text' => $message,
                ],
            ]);

    $result = $cloud->request($completion);
    return json_decode($result, true);
}
?>

分词

为了对文本进行分词,请使用tokenize方法

<?php

const OAuthToken = 'YOUR_OAUTH_TOKEN';
const folder_id = 'YOUR_FOLDER_ID';

public static function tokenize(string $message): array
{
    $cloud = new Cloud(self::OAuthToken, self::folder_id);
    $tokenize = new Tokenize($message);
    $tokenize->setModelUri(self::folder_id, 'yandexgpt/latest');
    
    $result = $cloud->request($tokenize);
    return json_decode($result, true);
}
?>

获取嵌入

要从文本数据中获取嵌入,请使用embedding方法

<?php

const OAuthToken = 'YOUR_OAUTH_TOKEN';
const folder_id = 'YOUR_FOLDER_ID';

public static function embedding(string $message): array
{
    $cloud = new Cloud(self::OAuthToken, self::folder_id);
    $embedding = new Embedding($message);
    $embedding->setModelUri(self::folder_id, 'text-search-query/latest');
    
    $result = $cloud->request($embedding);
    return json_decode($result, true);
}
?>

请记住将'YOUR_OAUTH_TOKEN''YOUR_FOLDER_ID'替换为您从Yandex GPT获得的实际凭证。

有关可用参数和配置的详细信息,请参阅库文档或Yandex GPT API文档。

许可

本库采用MIT许可 - 请参阅LICENSE文件以获取详细信息。