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

1.0.0 2025-09-05 13:09 UTC

This package is auto-updated.

Last update: 2025-10-07 13:57:03 UTC


README

Laravel Perplexity is a convenient wrapper for interacting with the Perplexity API in Laravel applications.

Installation

  1. Install the package

    composer require gridwb/laravel-perplexity
  2. Publish the configuration file

    php artisan vendor:publish --tag="perplexity-config"
  3. 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.