webboy/open-ai-api-client 问题修复 & 功能扩展

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

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

webboy/open-ai-api-client

最新稳定版本:1.1.2

Composer 安装命令:

composer require webboy/open-ai-api-client

包简介

PHP client for the OpenAI API

README 文档

README

A simple community-maintained PHP client library for interacting with the OpenAI API. This package provides an easy way to use OpenAI's GPT models for tasks such as text generation and completion.

Please note that this is an unofficial library.

This library is handy because it returns back a raw response as array which can then be used by any kind of adapter class.

Requirements

  • PHP 8.0 or higher
  • Guzzle HTTP client

Installation

You can install the package via Composer:

composer require webboy/open-ai-api-client

Usage examples

First, create an instance of the Endpoint classes with your API key:

require_once('vendor/autoload.php');

use Dotenv\Dotenv;
use Webboy\OpenAiApiClient\Endpoints\OpenAICompletions;
use Webboy\OpenAiApiClient\Exceptions\OpenAIClientException;

$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();

$apiKey = $_ENV['OPENAI_API_KEY'];

$client = new OpenAICompletions($apiKey);

$options['model']   = 'text-davinci-003';
$options['prompt']  = 'What time is it?';

try {
    print_r($client->create($options));
} catch (OpenAIClientException $exception) {
    die('OpenAI error occured: '.$exception->getMessage());
}

The above code will generate something like this:

Array
(
    [id] => cmpl-6wAQB0aPhvbxMAyIdz98z8jpPeKdq
    [object] => text_completion
    [created] => 1679321103
    [model] => text-davinci-003
    [choices] => Array
        (
            [0] => Array
                (
                    [text] =>It is 6:25 PM.
                    [index] => 0
                    [logprobs] =>
                    [finish_reason] => stop
                )
        )
    [usage] => Array
        (
            [prompt_tokens] => 5
            [completion_tokens] => 9
            [total_tokens] => 14
        )
)

Testing

Endpoint classes are created to accept HTTP client as a parameter, which enables mocking tests to be performed without making real API calls. If you need to make real API calls, feel free to create a testuit. To run PHPUnit tests:

./vendor/bin/phpunit

License

The PHP OpenAI API Client is open-sourced software licensed under the MIT license.

统计信息

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

GitHub 信息

  • Stars: 52
  • Watchers: 9
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-03-19