1. Go to this page and download the library: Download shanginn/mem0 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/ */
shanginn / mem0 example snippets
em0\Mem0;
use Mem0\DTO\Message;
use Mem0\Enum\Role;
// Retrieve your API key from environment variables or secure storage
$apiKey = getenv('MEM0_API_KEY');
if (!$apiKey) {
die("MEM0_API_KEY environment variable not set.\n");
}
// Initialize the client
$mem0 = new Mem0($apiKey);
// Add a simple memory
$response = $mem0->add(
messages: "Hi, my name is Alice and I love hiking",
userId: 'user-123'
);
echo "Memory added successfully!\n";
dump($response);
// Simple text memory
$mem0->add(
messages: "I prefer vegetarian restaurants",
userId: 'user-123'
);
// Rich conversational memory
$messages = [
new Message(Role::USER, "What's the weather like?"),
new Message(Role::ASSISTANT, "It's sunny and 75°F today."),
new Message(Role::USER, "Perfect for a bike ride!")
];
$response = $mem0->add(
messages: $messages,
userId: 'user-123',
metadata: ['location' => 'San Francisco', 'activity' => 'cycling']
);
// Memory with expiration and custom instructions
$mem0->add(
messages: "Temporary project preferences",
userId: 'user-123',
expirationDate: new DateTime('2024-12-31'),
customInstructions: "Focus on project-related preferences only",
immutable: false
);