teariot/php-yandex-gpt 问题修复 & 功能扩展

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

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

teariot/php-yandex-gpt

最新稳定版本:1.1.2

Composer 安装命令:

composer require teariot/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.

统计信息

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

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-01-09

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固