softcreatr / php-perplexity-ai-sdk
一个强大且易于使用的PHP SDK,用于pplx API,允许将高级AI功能无缝集成到您的PHP项目中。
1.1.0
2024-01-10 14:37 UTC
Requires
- php: >=7.4
- ext-fileinfo: *
- ext-json: *
- ext-mbstring: *
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
- psr/http-message: ^1.0
Requires (Dev)
README
此PHP库为PerplexityAI API提供了一个简单的包装器,允许您轻松地将PerplexityAI API集成到您的PHP项目中。
功能
- 轻松集成PerplexityAI API
- 支持所有PerplexityAI API端点
- 使用符合PSR-17和PSR-18的HTTP客户端和工厂来制作API请求
要求
- PHP 7.4或更高版本
- PSR-17 HTTP工厂实现(例如,guzzle/psr7 或 nyholm/psr7)
- PSR-18 HTTP客户端实现(例如,guzzlehttp/guzzle 或 symfony/http-client)
安装
您可以通过 Composer 安装此库
composer require softcreatr/php-perplexity-ai-sdk
使用方法
首先,在您的项目中包含此库
<?php require_once 'vendor/autoload.php';
然后,使用API密钥、组织(可选)、HTTP客户端、HTTP请求工厂和HTTP流工厂创建一个 PerplexityAI
类的实例
use SoftCreatR\PerplexityAI\PerplexityAI; $apiKey = 'your_api_key'; // Replace these lines with your chosen PSR-17 and PSR-18 compatible HTTP client and factories $httpClient = new YourChosenHttpClient(); $requestFactory = new YourChosenRequestFactory(); $streamFactory = new YourChosenStreamFactory(); $uriFactory = new YourChosenUriFactory(); $pplx = new PerplexityAI($requestFactory, $streamFactory, $uriFactory, $httpClient, $apiKey);
现在您可以使用魔术方法 __call
调用任何受支持的PerplexityAI API端点
$response = $pplx->createChatCompletion([ 'model' => 'mistral-7b-instruct', 'messages' => [ [ 'role' => 'system', 'content' => 'Be precise and concise.' ], [ 'role' => 'user', 'content' => 'How many stars are there in our galaxy?' ] ], ]); // Process the API response if ($response->getStatusCode() === 200) { $responseObj = json_decode($response->getBody()->getContents(), true); print_r($responseObj); } else { echo "Error: " . $response->getStatusCode(); }
有关如何使用每个端点的更多详细信息,请参阅 PerplexityAI API文档,以及存储库中提供的 示例。
支持的方法
聊天完成
变更日志
有关更改和更新的详细列表,请参阅 CHANGELOG.md 文件。我们遵循 语义版本控制 并记录每个版本的显著更改。
已知问题和限制
流支持
目前不支持流。计划尽快解决这个问题。目前请注意,这些方法不能用于流式传输目的。
如果您需要流式传输功能,请考虑使用其他实现或关注此库的未来更新。
许可
此库根据ISC许可证授权。有关更多信息,请参阅 LICENSE 文件。