| name | Vizra ADK Memory System |
| description | Implement persistent memory, session context, and vector memory (RAG) for AI agents |
Vizra ADK Memory System
Vizra ADK provides multiple memory types for agents to maintain context across conversations and sessions.
Memory Types
| Type | Purpose | Persistence |
|---|
| Session Memory | Conversation history within a session | Session lifetime |
| User Memory | Information tied to a specific user | Permanent |
| Vector Memory | Semantic search and RAG | Permanent |
Basic Memory Usage
Session-Based Memory
Conversation history persists within a session:
use App\Agents\MyAgent;
$sessionId = 'conversation-123';
$response1 = MyAgent::run('My name is John')
->forUser($user)
->withSession($sessionId)
->go();
$response2 = MyAgent::run('What is my name?')
->forUser($user)
->withSession($sessionId)
->go();
User-Based Memory
Memory persists across all sessions for a user:
$response1 = MyAgent::run('I prefer dark mode and short responses')
->forUser($user)
->go();
$response2 = MyAgent::run('Help me with my project')
->forUser($user)
->go();
Memory Tools
MemoryTool
Allow agents to explicitly store and retrieve memories:
use Vizra\VizraADK\Tools\MemoryTool;
class PersonalAssistantAgent extends BaseLlmAgent
{
protected string $name = 'personal_assistant';
protected string $instructions = <<<'INSTRUCTIONS'
You are a personal assistant. Use the memory tool to:
- Remember important facts the user tells you
- Recall information when asked
- Update memories when information changes
INSTRUCTIONS;
protected array $tools = [
MemoryTool::class,
];
}
The agent can then use commands like:
remember: User's birthday is March 15
recall: birthday
forget: old address
VectorMemoryTool
Enable semantic search for RAG (Retrieval Augmented Generation):
use Vizra\VizraADK\Tools\VectorMemoryTool;
class KnowledgeAgent extends BaseLlmAgent
{
protected string $name = 'knowledge_agent';
protected string $instructions = <<<'INSTRUCTIONS'
You are a knowledge assistant with access to the company documentation.
Use the vector memory tool to search for relevant information before answering questions.
Always cite the source of information.
INSTRUCTIONS;
protected array $tools = [
VectorMemoryTool::class,
];
}
Programmatic Memory Access
Storing Memories
use Vizra\VizraADK\Services\MemoryManager;
$memoryManager = app(MemoryManager::class);
$memoryManager->store(
userId: $user->id,
key: 'preferences',
value: [
'theme' => 'dark',
'language' => 'en',
'notifications' => true
]
);
$memoryManager->store(
userId: $user->id,
key: 'project_alpha_notes',
value: $notes,
tags: ['projects', 'alpha', 'notes']
);
Retrieving Memories
$preferences = $memoryManager->get($user->id, 'preferences');
$projectMemories = $memoryManager->getByTag($user->id, 'projects');
$results = $memoryManager->search($user->id, 'project deadline');
Updating and Deleting
$memoryManager->update(
userId: $user->id,
key: 'preferences',
value: array_merge($currentPrefs, ['theme' => 'light'])
);
$memoryManager->delete($user->id, 'old_data');
$memoryManager->clearUser($user->id);
Vector Memory for RAG
Storing Documents
use Vizra\VizraADK\Services\VectorMemoryManager;
$vectorManager = app(VectorMemoryManager::class);
$vectorManager->store(
content: $documentContent,
metadata: [
'source' => 'company_handbook',
'section' => 'policies',
'updated_at' => now()
]
);
$vectorManager->storeMany([
['content' => $doc1, 'metadata' => ['type' => 'policy']],
['content' => $doc2, 'metadata' => ['type' => 'guide']],
]);
Searching Vector Memory
$results = $vectorManager->search(
query: 'What is the vacation policy?',
limit: 5
);
$results = $vectorManager->search(
query: 'onboarding process',
limit: 5,
filter: ['type' => 'guide']
);
CLI Commands
php artisan vizra:vector:store --file=handbook.pdf
php artisan vizra:vector:store --directory=docs/
php artisan vizra:vector:search "vacation policy"
php artisan vizra:vector:stats
Memory Patterns
Building User Profiles
class ProfileBuildingAgent extends BaseLlmAgent
{
protected string $instructions = <<<'INSTRUCTIONS'
Build a comprehensive user profile by:
1. Asking about their preferences gradually
2. Remembering details they share
3. Inferring preferences from behavior
4. Updating profile as preferences change
INSTRUCTIONS;
protected array $tools = [
MemoryTool::class,
];
public function afterExecution($response, $context)
{
$this->extractAndStorePreferences($response, $context);
}
}
Context-Aware Responses
class ContextAwareAgent extends BaseLlmAgent
{
public function beforeExecution($input, $context)
{
$memories = $this->loadRelevantMemories($context);
$context->setParameter('user_context', $memories);
return $input;
}
protected function loadRelevantMemories($context)
{
$memoryManager = app(MemoryManager::class);
return [
'preferences' => $memoryManager->get($context->getUserId(), 'preferences'),
'recent_topics' => $memoryManager->getByTag($context->getUserId(), 'recent'),
'important' => $memoryManager->getByTag($context->getUserId(), 'important'),
];
}
}
Conversation Summarization
class SummarizingAgent extends BaseLlmAgent
{
protected int $maxHistoryLength = 50;
public function beforeExecution($input, $context)
{
$history = $context->getHistory();
if (count($history) > $this->maxHistoryLength) {
$toSummarize = array_slice($history, 0, -20);
$summary = $this->summarize($toSummarize);
$context->setParameter('conversation_summary', $summary);
$context->setHistory(array_slice($history, -20));
}
return $input;
}
protected function summarize($messages)
{
return SummarizerAgent::run(json_encode($messages))
->withParameters(['style' => 'concise'])
->go();
}
}
Memory Configuration
Config File Settings
return [
'memory' => [
'driver' => env('VIZRA_MEMORY_DRIVER', 'database'),
'ttl' => env('VIZRA_MEMORY_TTL', 86400 * 30),
'max_per_user' => env('VIZRA_MAX_MEMORIES', 1000),
],
'vector_memory' => [
'provider' => env('VIZRA_EMBEDDING_PROVIDER', 'openai'),
'model' => env('VIZRA_EMBEDDING_MODEL', 'text-embedding-3-small'),
'store' => env('VIZRA_VECTOR_STORE', 'meilisearch'),
'chunk_size' => env('VIZRA_CHUNK_SIZE', 500),
'chunk_overlap' => env('VIZRA_CHUNK_OVERLAP', 50),
],
];
Environment Variables
# Memory settings
VIZRA_MEMORY_DRIVER=database
VIZRA_MEMORY_TTL=2592000
# Vector memory / Embeddings
VIZRA_EMBEDDING_PROVIDER=openai
VIZRA_EMBEDDING_MODEL=text-embedding-3-small
VIZRA_VECTOR_STORE=meilisearch
MEILISEARCH_HOST=http://localhost:7700
MEILISEARCH_KEY=your-master-key
Best Practices
1. Scope Memories Appropriately
$memoryManager->store($user->id, 'preferences', $prefs);
$context->setSessionData('current_task', $task);
$memoryManager->store(null, 'data', $data);
2. Clean Up Old Memories
$memoryManager->deleteOlderThan($user->id, now()->subMonths(6));
$memoryManager->store($user->id, 'temp_data', $data, ttl: 3600);
3. Structure Memory Keys
$memoryManager->store($user->id, 'projects.alpha.deadline', $date);
$memoryManager->store($user->id, 'projects.alpha.status', 'active');
$memoryManager->store($user->id, 'note_123', $note, tags: ['notes', 'project_alpha']);
4. Handle Memory Limits
class MemoryAwareAgent extends BaseLlmAgent
{
public function beforeExecution($input, $context)
{
$history = $context->getHistory();
if ($this->estimateTokens($history) > 4000) {
$history = $this->trimHistory($history, 4000);
$context->setHistory($history);
}
return $input;
}
}
Database Tables
Memories are stored in these tables:
agent_memories - Key-value persistent memories
agent_sessions - Session data
agent_messages - Conversation history
agent_vector_memories - Vector embeddings