PHP code example of tivins / llm-memory

1. Go to this page and download the library: Download tivins/llm-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/ */

    

tivins / llm-memory example snippets


use Tivins\LlmLib\LLM;
use Tivins\LlmMemory\MemoryStore;
use Tivins\Pdow\Database;

$db       = Database::connect('pgsql:host=127.0.0.1;dbname=memory_project', 'user', 'pass');
$embedder = new LLM('http://127.0.0.1:8081', defaultModel: 'bge-m3-Q8_0.gguf');
$reranker = new LLM('http://127.0.0.1:8082', defaultModel: 'Qwen3-Reranker-4B-Q4_K_M.gguf');

$memory = new MemoryStore($db, $embedder, $reranker);
$memory->migrate();
$memory->ensureCollection('user:42', 'bge-m3-Q8_0.gguf', 1024);

// Store
$memory->remember('user:42', 'Marie is allergic to peanuts.', kind: 'fact');

// Recall
$results = $memory->recall('user:42', 'What foods should Marie avoid?', topN: 3);

foreach ($results as $hit) {
    echo $hit->entry->content . ' (score: ' . $hit->rerankScore . ")\n";
}

// Forget
$memory->forgetInCollection('user:42', $entryId);
$memory->forgetCollection('user:42');

use Tivins\LlmLib\Agent;
use Tivins\LlmLib\ToolRegistry;
use Tivins\LlmMemory\Tools\MemoryTools;

$memoryTools = new MemoryTools($memory, collection: 'user:42');
$agent = new Agent($chatLlm, new ToolRegistry(...$memoryTools->all()));

$result = $agent->runTurn($conversation, $options);