Skip to content

Giant-Peach-Design/laravel-ai-memory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel AI Memory

Persistent long-term memory for Laravel AI agents. Two tools — search and save — that let agents remember and recall information across conversations, backed by pgvector.

Installation

composer require giantpeach/laravel-ai-memory

Run the migration:

php artisan migrate

Optionally publish the config:

php artisan vendor:publish --tag=ai-memory-config

Quick Start

Add Memory::tools() to any agent's tools() method:

use GiantPeach\AiMemory\Facades\Memory; use Laravel\Ai\Contracts\Agent; use Laravel\Ai\Contracts\HasTools; use Laravel\Ai\Promptable; class PersonalAssistant implements Agent, HasTools { use Promptable; public function __construct(private User $user) {} public function instructions(): string { return 'You are a personal assistant. Use memory to recall past preferences.'; } public function tools(): iterable { return Memory::tools($this->user); } }

The agent now has two tools available:

  • Search memory — vector similarity search over stored memories (uses the SDK's SimilaritySearch)
  • Save memory — stores a new memory with an auto-generated embedding

Using the trait

For the common single-owner case, the HasMemories trait provides sugar:

use GiantPeach\AiMemory\Concerns\HasMemories; class PersonalAssistant implements Agent, HasTools { use Promptable, HasMemories; public function __construct(private User $user) {} public function memoryOwner(): Model { return $this->user; } public function tools(): iterable { return $this->memoryTools(); } }

Scoping

Memories are associated with Eloquent models via a polymorphic relationship. Memory::tools() accepts flexible scoping to handle different scenarios.

Single owner

Search and save both target the same model:

Memory::tools($this->user);

Multi-scope (team + user)

Search across multiple owners, save to a specific one:

Memory::tools( search: [$this->team, $this->user], save: $this->user, );

Anonymous / no user

Scope to an org or team only:

Memory::tools($this->team);

Conditional scoping

if ($this->user) { return Memory::tools(search: [$this->team, $this->user], save: $this->user); } return Memory::tools($this->team);

Querying Memories Outside Agents

Add the Memorable trait to any Eloquent model to access its memories directly:

use GiantPeach\AiMemory\Concerns\Memorable; class User extends Model { use Memorable; } $user->memories; $user->memories()->where('source', 'agent')->count();

How It Works

  1. Save — when the agent calls the save tool, a Memory record is created with the content and an embedding generated inline via Str::of($content)->toEmbeddings()
  2. Search — when the agent calls the search tool, the SDK's SimilaritySearch auto-embeds the query and performs a pgvector similarity search, filtered to the scoped owner(s)

Memories are atomic — each one is a single row with its own embedding. No chunking is needed since agent-created memories are short, focused pieces of information.

Configuration

// config/ai-memory.php return [ // Must match your embedding model (1536 for text-embedding-3-small) 'dimensions' => 1536, 'search' => [ 'max_results' => 10, 'min_similarity' => 0.4, ], ];

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages