Context Handling Guide
This guide explains how to effectively manage context in your memories using the Papr Memory API. Context helps create richer, more meaningful memories that are easier to retrieve and understand.
Understanding Context
In Papr Memory, context can be added in several ways:
- Conversation Context: Dialog history that led to the memory
- Metadata Context: Additional information about the memory
- Hierarchical Context: Organizational structure of memories
- Emotional Context: Sentiment and emotional indicators
Conversation Context
When storing memories from conversations, include relevant dialog:
const memory = {
content: "Decision to implement new feature based on user feedback",
type: "text",
context: [
{
role: "user",
content: "Our users are requesting a dark mode feature"
},
{
role: "assistant",
content: "I understand. How high priority is this feature?"
},
{
role: "user",
content: "Very high. Users are having eye strain issues."
}
],
metadata: {
topics: "feature request, user feedback, accessibility",
hierarchical_structures: "Product/Features/UI/DarkMode"
}
};
const response = await fetch('https://memory.papr.ai/v1/memory', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': '<your-api-key>',
'Accept-Encoding': 'gzip'
},
body: JSON.stringify(memory)
});
Metadata Context
Use metadata to add rich contextual information:
const memory = {
content: "Quarterly planning meeting outcomes",
type: "text",
metadata: {
topics: "planning, strategy, objectives",
hierarchical_structures: "Business/Planning/Quarterly",
createdAt: new Date().toISOString(),
location: "Conference Room A",
"emoji tags": "📊,🎯,📅",
"emotion tags": "focused, optimistic, determined",
conversationId: "q2-planning-2024",
sourceUrl: "https://meetings.company.com/q2-planning"
}
};
Hierarchical Context
Create meaningful navigation paths:
// Helper function to build hierarchical paths
function buildHierarchy(categories: string[]): string {
return categories.join('/');
}
const memory = {
content: "New feature specification",
type: "text",
metadata: {
hierarchical_structures: buildHierarchy([
"Product",
"Development",
"Features",
"DarkMode"
]),
topics: "feature specification, development, UI"
}
};
Emotional Context
Add emotional context to improve retrieval relevance:
// Helper function to analyze and tag emotions
function addEmotionalContext(content: string, metadata: any) {
// Your emotion analysis logic here
return {
...metadata,
"emotion tags": "determined, excited", // Example tags
"emoji tags": "💪,✨,🎯" // Relevant emojis
};
}
const memory = {
content: "Team successfully launched new feature ahead of schedule",
type: "text",
metadata: addEmotionalContext(content, {
topics: "launch, milestone, achievement",
hierarchical_structures: "Projects/Features/Launch"
})
};
Linking Related Memories
Connect related memories using conversation IDs and metadata:
// Create a thread of related memories
async function createMemoryThread(memories: Memory[], threadId: string) {
const batchResponse = await fetch('https://memory.papr.ai/v1/memory/batch', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': '<your-api-key>',
'Accept-Encoding': 'gzip'
},
body: JSON.stringify({
memories: memories.map(memory => ({
...memory,
metadata: {
...memory.metadata,
conversationId: threadId,
createdAt: new Date().toISOString()
}
})),
batch_size: 10
})
});
return response.json();
}
// Example usage
const threadId = 'feature-123-discussion';
const memories = [
{
content: "Initial feature discussion",
type: "text",
metadata: {
topics: "feature planning",
hierarchical_structures: "Product/Features/Planning"
}
},
{
content: "Technical requirements defined",
type: "text",
metadata: {
topics: "technical specs",
hierarchical_structures: "Product/Features/Technical"
}
},
{
content: "Implementation timeline agreed",
type: "text",
metadata: {
topics: "timeline, planning",
hierarchical_structures: "Product/Features/Timeline"
}
}
];
await createMemoryThread(memories, threadId);
Context-Aware Search
Leverage context in your search queries:
async function contextualSearch(
topic: string,
context: {
timeframe?: string,
emotion?: string,
hierarchy?: string
}
) {
let query = `Find detailed information about ${topic}`;
if (context.timeframe) {
query += ` from ${context.timeframe}`;
}
if (context.emotion) {
query += ` with a ${context.emotion} sentiment`;
}
if (context.hierarchy) {
query += ` within the ${context.hierarchy} area`;
}
const response = await fetch('https://memory.papr.ai/v1/search', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': '<your-api-key>',
'X-Client-Type': 'your_app_name',
'Accept-Encoding': 'gzip'
},
body: JSON.stringify({
query,
rank_results: false
})
});
return response.json();
}
// Example usage
const results = await contextualSearch('feature feedback', {
timeframe: 'the last month',
emotion: 'positive',
hierarchy: 'Product/Features'
});
Best Practices
Rich Context
- Include relevant conversation history
- Add detailed metadata
- Use consistent hierarchical structures
- Include emotional context when relevant
Context Organization
- Use clear, consistent hierarchies
- Link related memories with conversation IDs
- Maintain consistent metadata schemas
Search Optimization
- Write detailed, context-aware queries
- Use hierarchical structures in searches
- Consider emotional context in relevance
Performance
- Balance context detail with storage efficiency
- Use batch operations for related memories
- Enable compression for search requests
Common Patterns
Project Documentation
const projectMemory = {
content: "Project kickoff meeting notes",
type: "text",
metadata: {
topics: "project planning, requirements, timeline",
hierarchical_structures: "Projects/NewFeature/Planning",
"emoji tags": "🚀,📋,⏱️",
conversationId: "proj-123-kickoff"
},
context: [
{
role: "user",
content: "What are our main objectives for this quarter?"
},
{
role: "assistant",
content: "Based on the discussion, the main objectives are..."
}
]
};
Customer Feedback
const feedbackMemory = {
content: "Customer feedback on new UI",
type: "text",
metadata: {
topics: "customer feedback, UI, usability",
hierarchical_structures: "Feedback/UI/Design",
"emotion tags": "satisfied, impressed",
"emoji tags": "👍,✨,🎨"
}
};
Technical Documentation
const technicalMemory = {
content: "API endpoint documentation",
type: "text",
metadata: {
topics: "API, documentation, technical",
hierarchical_structures: "Technical/API/Endpoints",
sourceUrl: "https://github.com/company/repo/docs"
}
};
Next Steps
- Explore our API Reference for detailed endpoint information
- Check out our Memory Management Guide for more memory handling strategies
- View our Retrieval Guide for advanced search techniques