Knowledge Graphs
Knowledge graphs provide a powerful way to represent and work with connected memories in the Papr Memory API. This guide explains how to build and utilize dynamic knowledge graphs for advanced AI applications.
Understanding Knowledge Graphs
A knowledge graph is a structured representation of information that connects entities (nodes) through relationships (edges). In the Papr Memory API, knowledge graphs help you:
- Represent complex relationships between memories
- Enable more sophisticated traversal and retrieval patterns
- Support reasoning over connected information
- Visualize memory structures
Creating Knowledge Graph Nodes
Nodes represent entities in your knowledge graph:
// Create a node for a customer entity
const customerNode = await papr.graph.createNode({
type: "customer",
properties: {
id: "cust-123",
name: "Jane Smith",
tier: "premium"
},
metadata: {
source: "CRM",
lastUpdated: new Date().toISOString()
}
});
// Create a node for a product entity
const productNode = await papr.graph.createNode({
type: "product",
properties: {
id: "prod-456",
name: "Enterprise Suite",
category: "software"
}
});
Creating Relationships
Edges connect nodes to form the graph structure:
// Create a relationship between customer and product
const relationship = await papr.graph.createEdge({
type: "purchased",
sourceNodeId: customerNode.id,
targetNodeId: productNode.id,
properties: {
date: "2023-04-15",
amount: 1299.99,
currency: "USD"
}
});
Graph Traversal
Navigate through connected memories:
// Find all products purchased by a customer
const purchases = await papr.graph.traverse({
startingNodeId: customerNode.id,
relationship: "purchased",
direction: "outgoing",
maxDepth: 1
});
// Find all customers who purchased a specific product
const customers = await papr.graph.traverse({
startingNodeId: productNode.id,
relationship: "purchased",
direction: "incoming",
maxDepth: 1
});
Path Finding
Discover connections between entities:
// Find all paths between two nodes
const paths = await papr.graph.findPaths({
sourceNodeId: customerNode.id,
targetNodeId: "node-support-ticket-789",
maxDepth: 3,
relationships: ["purchased", "submitted", "related_to"]
});
Graph Queries
Perform complex queries on your knowledge graph:
// Find premium customers who purchased Enterprise Suite in the last 30 days
const results = await papr.graph.query(`
MATCH (c:customer)-[p:purchased]->(prod:product)
WHERE c.tier = 'premium'
AND prod.name = 'Enterprise Suite'
AND p.date > '${thirtyDaysAgo}'
RETURN c, p, prod
`);
Graph Visualization
Generate visualizations of your graph structures:
// Generate a visualization of a customer's product ecosystem
const visualization = await papr.graph.visualize({
centerNodeId: customerNode.id,
maxDepth: 2,
layout: "force-directed",
includeProperties: ["name", "id", "date"]
});
Best Practices
- Plan Your Schema: Design node types and relationships carefully
- Keep Properties Relevant: Only include properties that add value
- Use Typed Relationships: Name relationships clearly to express meaning
- Consider Performance: Limit traverse depth for large graphs
- Maintain Graph Hygiene: Regularly clean up orphaned nodes and obsolete relationships
Next Steps
- Explore Multi-tenant Architecture
- Learn about Advanced Search techniques