Knowledge Graph API
The Knowledge Graph API provides powerful graph-based memory and knowledge management capabilities, enabling you to create structured knowledge representations, manage relationships between entities, and query complex data patterns.
Overview
The knowledge graph module enables you to:
- Templates: Define schemas for knowledge structures with record kinds and edge types
- Instances: Create knowledge graph instances from templates for specific domains
- Memory Records: Store structured data records with typed attributes
- Edges: Define relationships between records with typed edges
- Views: Create query templates for pattern matching and data retrieval
- Querying: Execute complex graph queries with pattern matching
Quick Start Example
// Wait for connection
await codebolt.waitForReady();
// Create a template for a person knowledge graph
const template = await codebolt.knowledgeGraph.createInstanceTemplate({
name: 'Person Network',
description: 'Template for tracking people and relationships',
record_kinds: [
{
name: 'person',
label: 'Person',
description: 'A person entity',
attributes: {
name: { type: 'string', required: true },
age: { type: 'number' },
email: { type: 'string' }
}
}
],
edge_types: [
{
name: 'knows',
label: 'Knows',
description: 'One person knows another',
from_kinds: ['person'],
to_kinds: ['person']
}
]
});
// Create an instance from the template
const instance = await codebolt.knowledgeGraph.createInstance({
templateId: template.data.id,
name: 'My Network'
});
// Add people to the graph
const alice = await codebolt.knowledgeGraph.addMemoryRecord(instance.data.id, {
kind: 'person',
attributes: { name: 'Alice', age: 30, email: 'alice@example.com' }
});
const bob = await codebolt.knowledgeGraph.addMemoryRecord(instance.data.id, {
kind: 'person',
attributes: { name: 'Bob', age: 25, email: 'bob@example.com' }
});
// Create a relationship
await codebolt.knowledgeGraph.addEdge(instance.data.id, {
kind: 'knows',
from_node_id: alice.data.id,
to_node_id: bob.data.id
});
console.log('✅ Knowledge graph created with relationships');
Response Structure
All knowledge graph API functions return responses with a consistent structure:
{
type: string,
success: boolean,
data?: {
// Response-specific data
template: KGInstanceTemplate,
instance: KGInstance,
record: KGMemoryRecord,
edge: KGEdge,
view: KGView,
// ... etc
},
message?: string,
error?: string,
timestamp: string,
requestId: string
}
Key Concepts
Instance Templates
Templates define the schema for knowledge graphs, including:
- Record Kinds: Types of nodes/entities with their attributes
- Edge Types: Types of relationships between entities
Instances
Instances are actual knowledge graphs created from templates, containing:
- Specific records/nodes
- Relationships/edges between nodes
- Queryable graph structures
Memory Records
Records are the nodes in your knowledge graph, containing:
- A kind (type) defined in the template
- Typed attributes (string, number, boolean, date, json)
- Validity period (valid_from, valid_to)
Edges
Edges define relationships between records:
- A kind (type) of relationship
- Source and target node IDs
- Optional attributes
Views & View Templates
Views define query patterns for retrieving data:
- Match patterns to find specific graph structures
- Where clauses for filtering
- With clauses for traversals
- Return specifications for output formatting
Attribute Types
Supported attribute types for records:
string: Text datanumber: Numeric databoolean: True/false valuesdate: Date/time datajson: Nested data structures
- createInstanceTemplate - Creates a new knowledge graph instance template.
- getInstanceTemplate - Gets an instance template by ID.
- listInstanceTemplates - Lists all instance templates.
- updateInstanceTemplate - Updates an instance template.
- deleteInstanceTemplate - Deletes an instance template.
- createInstance - Creates a new knowledge graph instance.
- getInstance - Gets an instance by ID.
- listInstances - Lists instances, optionally filtered by template.
- deleteInstance - Deletes an instance.
- addMemoryRecord - Adds a memory record to an instance.
- addMemoryRecords - Adds multiple memory records to an instance.
- getMemoryRecord - Gets a memory record by ID.
- listMemoryRecords - Lists memory records in an instance.
- updateMemoryRecord - Updates a memory record.
- deleteMemoryRecord - Deletes a memory record.
- addEdge - Adds an edge to an instance.
- addEdges - Adds multiple edges to an instance.
- listEdges - Lists edges in an instance.
- deleteEdge - Deletes an edge.
- createViewTemplate - Creates a view template.
- getViewTemplate - Gets a view template by ID.
- listViewTemplates - Lists view templates.
- updateViewTemplate - Updates a view template.
- deleteViewTemplate - Deletes a view template.
- createView - Creates a view.
- listViews - Lists views for an instance.
- executeView - Executes a view query.
- deleteView - Deletes a view.