openai-php/laravel
Composer 安装命令:
composer require openai-php/laravel
包简介
OpenAI PHP for Laravel is a supercharged PHP API client that allows you to interact with the Open AI API
README 文档
README
OpenAI PHP for Laravel is a community-maintained PHP API client that allows you to interact with the Open AI API. If you or your business relies on this package, it's important to support the developers who have contributed their time and effort to create and maintain this valuable tool:
- Nuno Maduro: github.com/sponsors/nunomaduro
- Sandro Gehri: github.com/sponsors/gehrisandro
Note: This repository contains the integration code of the OpenAI PHP for Laravel. If you want to use the OpenAI PHP client in a framework-agnostic way, take a look at the openai-php/client repository.
Get Started
Requires PHP 8.2+
First, install OpenAI via the Composer package manager:
composer require openai-php/laravel
Next, execute the install command:
php artisan openai:install
This will create a config/openai.php configuration file in your project, which you can modify to your needs
using environment variables.
Blank environment variables for the OpenAI API key and organization id are already appended to your .env file.
OPENAI_API_KEY=sk-... OPENAI_ORGANIZATION=org-...
Finally, you may use the OpenAI facade to access the OpenAI API:
use OpenAI\Laravel\Facades\OpenAI; $response = OpenAI::responses()->create([ 'model' => 'gpt-5', 'input' => 'Hello!', ]); echo $response->outputText; // Hello! How can I assist you today?
Configuration
Configuration is done via environment variables or directly in the configuration file (config/openai.php).
OpenAI API Key and Organization
Specify your OpenAI API Key and organization. This will be used to authenticate with the OpenAI API - you can find your API key and organization on your OpenAI dashboard, at https://openai.com.
OPENAI_API_KEY= OPENAI_ORGANIZATION=
OpenAI Project
For implementations that require a project ID, you can specify the OpenAI project ID in your environment variables.
OPENAI_PROJECT=proj_...
OpenAI API Base URL
The base URL for the OpenAI API. By default, this is set to api.openai.com/v1.
OPENAI_BASE_URL=
Request Timeout
The timeout may be used to specify the maximum number of seconds to wait for a response. By default, the client will time out after 30 seconds.
OPENAI_REQUEST_TIMEOUT=
Usage
For usage examples, take a look at the openai-php/client repository.
Testing
The OpenAI facade comes with a fake() method that allows you to fake the API responses.
The fake responses are returned in the order they are provided to the fake() method.
All responses are having a fake() method that allows you to easily create a response object by only providing the parameters relevant for your test case.
use OpenAI\Laravel\Facades\OpenAI; use OpenAI\Responses\Responses\CreateResponse; OpenAI::fake([ CreateResponse::fake([ 'choices' => [ [ 'text' => 'awesome!', ], ], ]), ]); $response = OpenAI::responses()->create([ 'model' => 'gpt-5', 'input' => 'PHP is ', ]); expect($response->outputText)->toBe('awesome!');
After the requests have been sent there are various methods to ensure that the expected requests were sent:
use OpenAI\Resources\Responses; // assert completion create request was sent OpenAI::assertSent(Responses::class, function (string $method, array $parameters): bool { return $method === 'create' && $parameters['model'] === 'gpt-5' && $parameters['prompt'] === 'PHP is '; });
For more testing examples, take a look at the openai-php/client repository.
OpenAI PHP for Laravel is an open-sourced software licensed under the MIT license.
openai-php/laravel 适用场景与选型建议
openai-php/laravel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.1M 次下载、GitHub Stars 达 3.73k, 最近一次更新时间为 2022 年 12 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「api」 「client」 「language」 「sdk」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 openai-php/laravel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 openai-php/laravel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 openai-php/laravel 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PSR-7 compatible library for making CRUD API endpoints
Mock PSR-18 HTTP client
Asynchronous MQTT client built on React
Client for Google Directions API to add interpolated points to a route consisting of given coordinates.
Alfabank REST API integration
A Flickr wrapper to allow you to call the Flickr api with Guzzle as the backend.Goal is to have 100% Flickr api coverage rather than just upload/display photos (currently at 23%).
统计信息
- 总下载量: 9.1M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3736
- 点击次数: 56
- 依赖项目数: 79
- 推荐数: 4
其他信息
- 授权协议: MIT
- 更新时间: 2022-12-26