xzq-chat/tp5-client
Composer 安装命令:
composer require xzq-chat/tp5-client
包简介
Chat SDK for ThinkPHP 5
README 文档
README
这是一个基于PHP 7.4的Chat SDK,专门为ThinkPHP 5框架优化。
安装
composer require xzq-chat/tp5-client
配置
在ThinkPHP 5的配置文件中添加Chat配置:
// config/chat.php return [ 'api_key' => 'your-api-key', 'organization' => 'your-organization-id', // 可选 'base_uri' => 'https://api.openai.com/v1', 'timeout' => 30, ];
使用方法
初始化
use xzq\chat\Client; // 在控制器中使用 public function index() { $client = new Client(config('chat')); }
文本生成
$response = $client->completions()->create([ 'model' => 'gpt-3.5-turbo', 'messages' => [ ['role' => 'user', 'content' => 'Hello!'] ] ]);
流式文本生成
$stream = $client->chat()->createStreamed([ 'model' => 'gpt-3.5-turbo', 'messages' => [ ['role' => 'user', 'content' => 'Hello!'] ] ]); foreach ($stream as $response) { echo $response['choices'][0]['delta']['content'] ?? ''; }
图片生成
$response = $client->images()->create([ 'prompt' => 'A beautiful sunset', 'n' => 1, 'size' => '1024x1024' ]);
音频处理
// 生成语音 $audio = $client->audio()->speech([ 'model' => 'tts-1', 'input' => 'Hello, how are you?', 'voice' => 'alloy' ]); // 流式语音生成 $stream = $client->audio()->speechStreamed([ 'model' => 'tts-1', 'input' => 'Hello, how are you?', 'voice' => 'alloy' ]); // 转录音频 $transcription = $client->audio()->transcribe([ 'file' => fopen('audio.mp3', 'r'), 'model' => 'whisper-1' ]); // 翻译音频 $translation = $client->audio()->translate([ 'file' => fopen('audio.mp3', 'r'), 'model' => 'whisper-1' ]);
文件处理
// 上传文件 $file = fopen('training_data.jsonl', 'r'); $response = $client->files()->upload([ 'file' => $file, 'purpose' => 'fine-tune' ]); // 获取文件列表 $files = $client->files()->list(); // 获取文件信息 $fileInfo = $client->files()->retrieve('file-abc123'); // 获取文件内容 $content = $client->files()->content('file-abc123'); // 删除文件 $client->files()->delete('file-abc123'); // 使用文件数组方式上传 $response = $client->files()->upload([ 'file' => [ 'file' => fopen('training_data.jsonl', 'r'), 'filename' => 'training_data.jsonl', 'headers' => [ 'Content-Type' => 'application/json' ] ], 'purpose' => 'fine-tune' ]);
主要特性
- 完全兼容PHP 7.4
- 专为ThinkPHP 5优化
- 支持所有Chat API功能
- 支持流式响应
- 支持音频处理
- 支持文件上传和管理
- 简单易用的接口
- 完善的错误处理
- 支持异步请求
注意事项
- 确保PHP版本 >= 7.4
- 需要安装ThinkPHP 5框架
- 需要有效的Chat API密钥
- 流式响应需要PHP支持生成器(Generator)
- 文件上传时注意文件大小限制和类型限制
错误处理
try { $response = $client->completions()->create([...]); } catch (\xzq\chat\Exceptions\ApiException $e) { // 处理API错误 Log::error('Chat API Error: ' . $e->getMessage()); } catch (\Exception $e) { // 处理其他错误 Log::error('Error: ' . $e->getMessage()); }
贡献
欢迎提交Issue和Pull Request。
许可证
MIT License
xzq-chat/tp5-client 适用场景与选型建议
xzq-chat/tp5-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 05 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「api」 「client」 「sdk」 「chat」 「thinkphp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 xzq-chat/tp5-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 xzq-chat/tp5-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 xzq-chat/tp5-client 相关的其它包
同方向 / 同关键字的高下载量 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%).
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-05-30