定制 jumas-cola/php-yandex-gpt 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

jumas-cola/php-yandex-gpt

最新稳定版本:1.0.1

Composer 安装命令:

composer require jumas-cola/php-yandex-gpt

包简介

A PHP library for seamless interaction with Yandex GPT (Generative Pre-trained Transformer) API, providing text generation, tokenization, and embedding functionalities.

README 文档

README

This PHP library provides a convenient interface to interact with Yandex GPT (Generative Pre-trained Transformer) API for text generation, tokenization, and obtaining embeddings.

Installation

To install the library via Composer, use the following command:

composer require teariot/php-yandex-gpt

Usage

Ensure you have obtained the necessary OAuth token and folder ID from Yandex GPT.

Text Completion

To generate text completions, use the complete method:

<?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);
}
?>

Or you can use asynchronous text generation.

<?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 [];
}
?>

Enhanced Usage of complete Method

This variation showcases an extended use case of the complete method by incorporating system messages along with user messages.

<?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);
}
?>

Tokenization

For tokenizing text, utilize the tokenize method:

<?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);
}
?>

Obtaining Embeddings

To obtain embeddings from text data, use the embedding method:

<?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);
}
?>

Remember to replace 'YOUR_OAUTH_TOKEN' and 'YOUR_FOLDER_ID' with your actual credentials obtained from Yandex GPT.

For detailed information on available parameters and configurations, please refer to the library documentation or Yandex GPT API documentation.

License

This library is licensed under the MIT License - see the LICENSE file for details.

统计信息

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

GitHub 信息

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

其他信息

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