Basic Update
- Python SDK
- JavaScript SDK
Copy
from gravixlayer import GravixLayer
client = GravixLayer()
memory = client.memory
# Add a memory to update
add_result = memory.add("User likes pizza", user_id="alice")
memory_id = add_result['results'][0]['id']
# Update the memory content
update_result = memory.update(memory_id, "alice", "User loves pizza and Italian cuisine")
print(f"Update result: {update_result['message']}")
# Verify the update
updated_memories = memory.get_all(user_id="alice")
for mem in updated_memories['results']:
if mem['id'] == memory_id:
print(f"Updated content: {mem['memory']}")
break
Copy
Update result: Memory a1b2c3d4-e5f6-7890-abcd-ef1234567890 updated successfully!
Updated content: User loves pizza and Italian cuisine
Copy
import { GravixLayer } from 'gravixlayer';
const client = new GravixLayer();
const memory = client.memory;
// Add a memory to update
const addResult = await memory.add("User likes pizza", "alice");
const memoryId = addResult.results[0].id;
// Update the memory content
const updateResult = await memory.update(memoryId, "alice", "User loves pizza and Italian cuisine");
console.log(`Update result: ${updateResult.message}`);
// Verify the update
const updatedMemories = await memory.getAll("alice");
const updatedMemory = updatedMemories.results.find(mem => mem.id === memoryId);
if (updatedMemory) {
console.log(`Updated content: ${updatedMemory.memory}`);
}
Copy
Update result: Memory a1b2c3d4-e5f6-7890-abcd-ef1234567890 updated successfully!
Updated content: User loves pizza and Italian cuisine
Update in Categories
Update memories in specific categories for better organization:- Python SDK
- JavaScript SDK
Copy
from gravixlayer import GravixLayer
client = GravixLayer()
memory = client.memory
# Set up memories in different categories
print("Setting up categorized memories...")
# Add to user preferences
memory.switch_index("user_preferences")
pref_result = memory.add("User prefers light theme", user_id="alice", metadata={"type": "ui"})
pref_memory_id = pref_result['results'][0]['id']
# Add to work info
memory.switch_index("work_info")
work_result = memory.add("User works as junior developer", user_id="alice", metadata={"type": "job"})
work_memory_id = work_result['results'][0]['id']
print("Added memories to different categories\n")
# Update memory in user preferences
print("Updating user preference...")
memory.switch_index("user_preferences")
pref_update = memory.update(pref_memory_id, "alice", "User strongly prefers dark theme",
metadata={"type": "ui", "strength": "strong"})
print(f"Preference update: {pref_update['message']}")
# Update memory in work info
print("\nUpdating work information...")
memory.switch_index("work_info")
work_update = memory.update(work_memory_id, "alice", "User works as senior software engineer",
metadata={"type": "job", "level": "senior"})
print(f"Work update: {work_update['message']}")
# Verify updates in each category
print("\n=== VERIFICATION ===")
categories = [
("user_preferences", pref_memory_id),
("work_info", work_memory_id)
]
for category, mem_id in categories:
memory.switch_index(category)
memories = memory.get_all(user_id="alice")
for mem in memories['results']:
if mem['id'] == mem_id:
print(f"\n{category}:")
print(f" Content: {mem['memory']}")
print(f" Metadata: {mem.get('metadata', {})}")
print(f" Updated: {mem.get('updated_at', 'N/A')}")
break
# List all available indexes
indexes = memory.list_available_indexes()
print(f"\nAvailable indexes: {indexes}")
Copy
Setting up categorized memories...
✅ Switched to index: user_preferences
✅ Switched to index: work_info
Added memories to different categories
Updating user preference...
✅ Switched to index: user_preferences
Memory b2c3d4e5-f6g7-8901-bcde-f23456789012 updated successfully!
Updating work information...
✅ Switched to index: work_info
Memory c3d4e5f6-g7h8-9012-cdef-345678901234 updated successfully!
=== VERIFICATION ===
✅ Switched to index: user_preferences
user_preferences:
Content: User strongly prefers dark theme
Metadata: {'type': 'ui', 'strength': 'strong'}
Updated: 2025-10-22T10:40:15.123456
✅ Switched to index: work_info
work_info:
Content: User works as senior software engineer
Metadata: {'type': 'job', 'level': 'senior'}
Updated: 2025-10-22T10:40:16.234567
Available indexes: ['gravixlayer_memories', 'user_preferences', 'work_info']
Copy
import { GravixLayer } from 'gravixlayer';
const client = new GravixLayer();
const memory = client.memory;
async function updateInCategories() {
// Set up memories in different categories
console.log("Setting up categorized memories...");
// Add to user preferences
const prefResult = await memory.add("User prefers light theme", "alice", {
indexName: "user_preferences",
metadata: { type: "ui" }
});
const prefMemoryId = prefResult.results[0].id;
// Add to work info
const workResult = await memory.add("User works as junior developer", "alice", {
indexName: "work_info",
metadata: { type: "job" }
});
const workMemoryId = workResult.results[0].id;
console.log("Added memories to different categories\n");
// Update memory in user preferences
console.log("Updating user preference...");
const prefUpdate = await memory.update(prefMemoryId, "alice",
"User strongly prefers dark theme", {
indexName: "user_preferences",
metadata: { type: "ui", strength: "strong" }
});
console.log(`Preference update: ${prefUpdate.message}`);
// Update memory in work info
console.log("\nUpdating work information...");
const workUpdate = await memory.update(workMemoryId, "alice",
"User works as senior software engineer", {
indexName: "work_info",
metadata: { type: "job", level: "senior" }
});
console.log(`Work update: ${workUpdate.message}`);
// Verify updates in each category
console.log("\n=== VERIFICATION ===");
const categories = [
{ name: "user_preferences", memoryId: prefMemoryId },
{ name: "work_info", memoryId: workMemoryId }
];
for (const { name, memoryId } of categories) {
const memories = await memory.getAll("alice", { indexName: name });
const updatedMemory = memories.results.find(mem => mem.id === memoryId);
if (updatedMemory) {
console.log(`\n${name}:`);
console.log(` Content: ${updatedMemory.memory}`);
console.log(` Metadata: ${JSON.stringify(updatedMemory.metadata || {})}`);
console.log(` Updated: ${updatedMemory.updated_at || 'N/A'}`);
}
}
// List all available indexes
const indexes = await memory.listAvailableIndexes();
console.log(`\nAvailable indexes: ${indexes}`);
}
updateInCategories().catch(console.error);
Copy
Setting up categorized memories...
Added memories to different categories
Updating user preference...
Memory b2c3d4e5-f6g7-8901-bcde-f23456789012 updated successfully!
Updating work information...
Memory c3d4e5f6-g7h8-9012-cdef-345678901234 updated successfully!
=== VERIFICATION ===
user_preferences:
Content: User strongly prefers dark theme
Metadata: {"type":"ui","strength":"strong"}
Updated: 2025-10-22T10:40:15.123456
work_info:
Content: User works as senior software engineer
Metadata: {"type":"job","level":"senior"}
Updated: 2025-10-22T10:40:16.234567
Available indexes: ['gravixlayer_memories', 'user_preferences', 'work_info']

