PHP code example of smart-memory / laravel-smart-thread-memory

1. Go to this page and download the library: Download smart-memory/laravel-smart-thread-memory library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

smart-memory / laravel-smart-thread-memory example snippets


use LaravelAiMemory\Facades\AiMemory;

$thread = AiMemory::createThread([
    'owner_type' => 'user',
    'owner_id' => $user->id,
    'title' => 'Stripe invoice sync',
]);

$message = AiMemory::recordMessage($thread, [
    'role' => 'user',
    'content' => 'We decided to use Stripe webhooks for invoice sync.',
]);

AiMemory::remember([
    'thread_id' => $thread->id,
    'message_id' => $message->id,
    'owner_type' => 'user',
    'owner_id' => $user->id,
    'type' => 'decision',
    'content' => 'Invoice sync should be triggered by Stripe webhooks.',
    'importance' => 5,
]);

$related = AiMemory::detectRelatedThreads(
    content: 'Can we continue invoice sync retries?',
    owner: ['owner_type' => 'user', 'owner_id' => $user->id],
);

$context = AiMemory::retrieveContext(
    query: 'What did we decide about invoice sync?',
    owner: ['owner_type' => 'user', 'owner_id' => $user->id],
);

$prompt = <<<PROMPT
Use this prior context when it is relevant:

{$context->formattedContext}

User message:
{$message}
PROMPT;

$related = AiMemory::detectRelatedThreads(
    content: $request->input('message'),
    owner: $request->user(),
);

use LaravelAiMemory\Contracts\EmbeddingProvider;

$this->app->singleton(EmbeddingProvider::class, LocalEmbeddingProvider::class);
bash
composer  vendor:publish --tag=ai-memory-config
php artisan vendor:publish --tag=ai-memory-migrations
php artisan migrate