softcreatr/php-perplexity-ai-sdk 问题修复 & 功能扩展

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

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

softcreatr/php-perplexity-ai-sdk

Composer 安装命令:

composer require softcreatr/php-perplexity-ai-sdk

包简介

A powerful and easy-to-use PHP SDK for the pplx (Perplexity) API, allowing seamless integration of advanced AI-powered features into your PHP projects..

README 文档

README

Build Latest Release ISC licensed Plant Tree Codecov branch Code Climate maintainability

This PHP library provides a simple wrapper for the PerplexityAI API, allowing you to easily integrate the PerplexityAI API into your PHP projects.

Features

  • Easy integration with PerplexityAI API
  • Supports all PerplexityAI API endpoints
  • Streaming support for real-time responses in chat completions
  • Utilizes PSR-17 and PSR-18 compliant HTTP clients and factories for making API requests

Requirements

Installation

You can install the library via Composer:

composer require softcreatr/php-perplexity-ai-sdk

Usage

First, include the library in your project:

<?php

require_once 'vendor/autoload.php';

Then, create an instance of the PerplexityAI class with your API key, an HTTP client, an HTTP request factory, and an HTTP stream factory:

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);

Now you can call any supported PerplexityAI API endpoint using the magic method __call:

$response = $pplx->createChatCompletion([], 
    [
        'model' => 'sonar',
        '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();
}

Streaming Example

You can enable real-time streaming for chat completions:

$streamCallback = static function ($data) {
    if (isset($data['choices'][0]['delta']['content'])) {
        echo $data['choices'][0]['delta']['content'];
    }
};

$pplx->createChatCompletion([],
    [
        'model' => 'sonar',
        'messages' => [
            [
                'role' => 'user',
                'content' => 'Tell me a story about a brave knight.',
            ],
        ],
        'stream' => true,
    ],
    $streamCallback
);

For more details on how to use each endpoint, refer to the PerplexityAI API documentation, and the examples provided in the repository.

Supported Methods

Chat Completions

Changelog

For a detailed list of changes and updates, please refer to the CHANGELOG.md file. We adhere to Semantic Versioning and document notable changes for each release.

Known Problems and limitations

Streaming Support

Streaming is now supported for real-time token generation in chat completions. Please make sure you are handling streams correctly using a callback, as demonstrated in the examples.

License

This library is licensed under the ISC License. See the LICENSE file for more information.

Maintainers 🛠️

Sascha Greuel
Sascha Greuel

Contributors ✨

softcreatr/php-perplexity-ai-sdk 适用场景与选型建议

softcreatr/php-perplexity-ai-sdk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.88k 次下载、GitHub Stars 达 14, 最近一次更新时间为 2023 年 10 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「sdk」 「nlp」 「ai」 「api-wrapper」 「machine-learning」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 softcreatr/php-perplexity-ai-sdk 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 softcreatr/php-perplexity-ai-sdk 我们能提供哪些服务?
定制开发 / 二次开发

基于 softcreatr/php-perplexity-ai-sdk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 10.88k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 14
  • 点击次数: 39
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 14
  • Watchers: 1
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: ISC
  • 更新时间: 2023-10-19