Attribution is a core concept in Memori that allows you to segregate memories by user (Entity) and workflow (Process). This ensures that the right memories are recalled in the right context.Important: Attribution is required for Memori to function properly. If you do not provide any attribution, Memori cannot create or recall memories for you.
Unique identifier for the specific workflow or agent (e.g., ‘customer-support-bot’, ‘sales-assistant’). If not provided, the existing processId is preserved.
Returns:Memori - Returns the Memori instance for method chaining
import { Memori } from '@memorilabs/memori';import { OpenAI } from 'openai';const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });const memori = new Memori().llm.register(client);async function handleUserMessage(userId: string, message: string) { // Set attribution for this specific user memori.attribution(userId, 'customer-support-agent'); const response = await client.chat.completions.create({ model: 'gpt-4o-mini', messages: [{ role: 'user', content: message }], }); return response.choices[0].message.content;}// Each user gets their own memory contextawait handleUserMessage('[email protected]', 'My favorite color is blue');await handleUserMessage('[email protected]', 'My favorite color is red');
You can update just the entity or just the process without affecting the other:
import { Memori } from '@memorilabs/memori';const memori = new Memori();// Set both entity and processmemori.attribution('user-123', 'agent-v1');// Update only the process (entity remains 'user-123')memori.attribution(undefined, 'agent-v2');// Update only the entity (process remains 'agent-v2')memori.attribution('user-456', undefined);
You can also directly access and modify the attribution configuration:
import { Memori } from '@memorilabs/memori';const memori = new Memori();// Direct configuration accessmemori.config.entityId = 'user-123';memori.config.processId = 'my-agent';// Read current attributionconsole.log(`Entity: ${memori.config.entityId}`);console.log(`Process: ${memori.config.processId}`);
While you can modify config.entityId and config.processId directly, using the attribution() method is recommended as it supports method chaining and is more idiomatic.
import { Memori } from '@memorilabs/memori';import { OpenAI } from 'openai';const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });const memori = new Memori().llm.register(client);// User A tells the sales agent their favorite colormemori.attribution('user-a', 'sales-agent');await client.chat.completions.create({ model: 'gpt-4o-mini', messages: [{ role: 'user', content: 'My favorite color is blue' }],});// User B tells the support agent their favorite colormemori.attribution('user-b', 'support-agent');await client.chat.completions.create({ model: 'gpt-4o-mini', messages: [{ role: 'user', content: 'My favorite color is red' }],});// Later, when User A asks the sales agent about their color,// they'll get "blue" - not "red" from User Bmemori.attribution('user-a', 'sales-agent');const response = await client.chat.completions.create({ model: 'gpt-4o-mini', messages: [{ role: 'user', content: 'What is my favorite color?' }],});// AI will recall: "blue"