gridwb / laravel-perplexity
perplexity.ai API for Laravel
Installs: 48
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/gridwb/laravel-perplexity
Requires
- php: >=8.2
- guzzlehttp/guzzle: ^7.9.0
- illuminate/contracts: ^10.0||^11.0||^12.0
- spatie/laravel-data: ^4.13
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9|^3.0
- laravel/pint: ^1.14
- orchestra/testbench: ^8.22|^9.0|^10.0
- pestphp/pest: ^2.0|^3.0
- pestphp/pest-plugin-laravel: ^2.0|^3.0
README
Laravel Perplexity is a convenient wrapper for interacting with the Perplexity API in Laravel applications.
Installation
-
Install the package
composer require gridwb/laravel-perplexity
-
Publish the configuration file
php artisan vendor:publish --tag="perplexity-config" -
Add environment variables
PERPLEXITY_API_URL=https://api.perplexity.ai PERPLEXITY_API_KEY=your-api-key-here
Usage
Synchronous completions request:
<?php use Gridwb\LaravelPerplexity\Facades\Perplexity; $result = Perplexity::chat()->completions([ 'model' => 'sonar', 'messages' => [ [ 'role' => 'user', 'content' => 'How many stars are there in our galaxy?', ], ], ]); foreach ($result->choices as $choice) { echo $choice->message->content; // full content }
Streamed completions request:
<?php use Gridwb\LaravelPerplexity\Facades\Perplexity; use Gridwb\LaravelPerplexity\Responses\Chat\CompletionResponse; $stream = Perplexity::chat()->completionsStreamed([ 'model' => 'sonar', 'messages' => [ [ 'role' => 'user', 'content' => 'How many stars are there in our galaxy?', ], ], ]); /** @var CompletionResponse $response */ foreach ($stream as $response) { foreach ($response->choices as $choice) { echo $choice->delta->content; // delta content } }
Create an asynchronous completion:
<?php use Gridwb\LaravelPerplexity\Facades\Perplexity; $completion = Perplexity::chat()->createAsyncCompletion([ 'request' => [ 'model' => 'sonar-deep-research', 'messages' => [ [ 'role' => 'user', 'content' => 'How many stars are there in our galaxy?', ], ], ], ]);
List asynchronous completions:
<?php use Gridwb\LaravelPerplexity\Facades\Perplexity; $limit = 10; $nextToken = '<string>'; $completions = Perplexity::chat()->listAsyncCompletions($limit, $nextToken);
Get a specific asynchronous completion:
<?php use Gridwb\LaravelPerplexity\Facades\Perplexity; $requestId = '<string>'; $completion = Perplexity::chat()->getAsyncCompletion($requestId);
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
License
The MIT License (MIT). Please see License File for more information.