SDK

Laravel package

Add memory to a Laravel app with a facade, per-user scoping and Laravel AI SDK tools

neurophic/laravel wraps the PHP SDK for Laravel. It needs PHP 8.3 or later and Laravel 12 or later.

Install

composer
composer require neurophic/laravel

Set your project key in .env:

.env
NEUROPHIC_API_KEY=nph_...

The service provider is discovered automatically. NEUROPHIC_BASE_URL and NEUROPHIC_TIMEOUT (seconds) are optional; to edit the config file, publish it:

php artisan vendor:publish --tag=neurophic-config

Facade

The Neurophic facade takes the same arguments as the PHP SDK client.

use Neurophic\Laravel\Facades\Neurophic;

Neurophic::ingest('user-123', 'Prefers window seats on long flights.');

$markdown = Neurophic::retrieve('user-123', 'travel preferences');

The client is also bound in the container, so you can type-hint Neurophic\Neurophic in a constructor.

Per-user scoping

Neurophic::for() binds the client to one user, so you never pass an identifier.

use Neurophic\Enums\MemoryRole;
use Neurophic\Enums\RerankMode;
use Neurophic\Laravel\Facades\Neurophic;

$memory = Neurophic::for($request->user());

$memory->ingest('Prefers window seats on long flights.');
$memory->ingest('Suggested the 09:40 from Heathrow.', role: MemoryRole::Assistant);

$context = $memory->context('Booking a flight to Lisbon next month');
$markdown = $memory->retrieve('travel preferences', rerank: RerankMode::Fast)->markdown();
$result = $memory->retrieve('travel preferences', sources: true)->structured();

$page = $memory->list(page: 1, perPage: 20);
$one = $memory->get($id);
$memory->delete($id);
  • An authenticated user. The identifier is the user's auth identifier, and the display name is its name attribute when it has one.
  • A string. Used as the identifier as it is. Pass a display name as the second argument: Neurophic::for('user-123', 'Ada').

Learn more under Get started > Identifiers.

Laravel AI SDK tools

Two tools give a Laravel AI SDK agent memory for one user. The Laravel AI SDK is optional, so install it first with composer require laravel/ai.

namespace App\Ai\Agents;

use App\Models\User;
use Laravel\Ai\Contracts\Agent;
use Laravel\Ai\Contracts\HasTools;
use Laravel\Ai\Promptable;
use Neurophic\Enums\MemoryRole;
use Neurophic\Enums\RerankMode;
use Neurophic\Laravel\Ai\RecallTool;
use Neurophic\Laravel\Ai\RememberTool;

class TravelAssistant implements Agent, HasTools
{
    use Promptable;

    public function __construct(public User $user) {}

    public function instructions(): string
    {
        return 'You are a travel assistant. Use recall before answering and remember what the user shares.';
    }

    public function tools(): iterable
    {
        return [
            RecallTool::for(
                $this->user,
                roles: [MemoryRole::User, MemoryRole::Assistant],
                sources: true,
                rerank: RerankMode::Fast,
            ),
            RememberTool::for($this->user),
        ];
    }
}

roles, sources and rerank are applied to every search and are set by you, never by the model: roles for whose words to return, sources to also return the original turns, and rerank for a second ranking stage before the cut. All three are optional.

Warning:If a call fails, the tool throws a NeurophicException.

Context in the system prompt

To put memory in front of the model on every turn without giving it a tool, add the context block to the agent's instructions:

public function instructions(): string
{
    $base = 'You are a travel assistant.';
    $context = Neurophic::for($this->user)->context('travel preferences and upcoming trips');

    return $context === '' ? $base : "{$base}\n\n## Context from memory\n{$context}";
}

context returns an empty string when nothing is relevant. Learn more under API reference > Context.

Errors

Every failed call throws a Neurophic\Exceptions\NeurophicException; see PHP SDK > Errors.

Licence

The package is MIT-licensed; use of the API it calls is governed by the Terms of Use.