webboy/open-ai-api-client
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.
webboy/open-ai-api-client 适用场景与选型建议
webboy/open-ai-api-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 45 次下载、GitHub Stars 达 52, 最近一次更新时间为 2023 年 03 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 webboy/open-ai-api-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webboy/open-ai-api-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 45
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 52
- 点击次数: 10
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-03-19