Skip to content

Entity Management

import Callout from ’../../../components/Callout.astro’;

The Memory API provides powerful capabilities for managing entities and their relationships. This guide will show you how to create, retrieve, and work with entities in your knowledge graph.

Entities represent distinct objects, concepts, people, places, or any other discrete elements in your knowledge domain. They form the nodes in your knowledge graph and can be connected through relationships.

Entities allow you to organize memories around specific subjects, making it easier to build context and retrieve relevant information.

To create a new entity, send a POST request to the /entities endpoint:

// Example of creating an entity
const response = await fetch('https://api.example.com/entities', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
},
body: JSON.stringify({
tenantId: "tenant-123",
type: "person",
name: "Alice Smith",
properties: {
age: 32,
occupation: "Software Engineer",
interests: ["AI", "Machine Learning", "Hiking"]
}
})
});
const result = await response.json();
console.log(result.entity.id); // The unique ID of the created entity
ParameterTypeDescription
tenantIdstringID of the tenant this entity belongs to
typestringThe type of entity (e.g., “person”, “place”, “concept”)
namestringA human-readable name for the entity
ParameterTypeDescription
propertiesobjectAdditional properties specific to this entity
aliasesstring[]Alternative names or identifiers for this entity
metadataobjectAdditional metadata for the entity

To retrieve a specific entity by its ID:

// Example of retrieving a specific entity
const response = await fetch('https://api.example.com/entities/ent-123456', {
method: 'GET',
headers: {
'Authorization': 'Bearer YOUR_API_KEY'
}
});
const result = await response.json();
console.log(result.entity); // The retrieved entity

To search for entities based on various criteria:

// Example of searching for entities
const response = await fetch('https://api.example.com/entities/search', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
},
body: JSON.stringify({
tenantId: "tenant-123",
type: "person",
query: "Alice",
limit: 10
})
});
const result = await response.json();
console.log(result.entities); // Array of matching entities
ParameterTypeDescription
tenantIdstringID of the tenant to search entities for
typestringOptional filter by entity type
querystringSearch query to match against entity names and aliases
limitnumberMaximum number of results to return (default: 20)

Relationships connect entities to each other, forming the edges in your knowledge graph. To create a relationship:

// Example of creating a relationship between entities
const response = await fetch('https://api.example.com/relationships', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
},
body: JSON.stringify({
tenantId: "tenant-123",
sourceId: "ent-alice",
targetId: "ent-bob",
type: "KNOWS",
properties: {
since: "2023-01-01",
strength: "strong"
}
})
});
const result = await response.json();
console.log(result.relationship.id); // The unique ID of the created relationship
ParameterTypeDescription
tenantIdstringID of the tenant this relationship belongs to
sourceIdstringID of the source entity
targetIdstringID of the target entity
typestringThe type of relationship (e.g., “KNOWS”, “LOCATED_IN”)

To retrieve all relationships for a specific entity:

// Example of retrieving relationships for an entity
const response = await fetch('https://api.example.com/entities/ent-alice/relationships', {
method: 'GET',
headers: {
'Authorization': 'Bearer YOUR_API_KEY'
}
});
const result = await response.json();
console.log(result.relationships); // Array of relationships

Entities can be connected to memories by including entityIds when creating a memory:

// Example of creating a memory with entity connections
const response = await fetch('https://api.example.com/memory', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
},
body: JSON.stringify({
tenantId: "tenant-123",
content: "Alice mentioned she's working on a new AI project.",
type: "long-term",
entityIds: ["ent-alice", "ent-ai-project"]
})
});

This automatically establishes connections between the memory and the specified entities.

  1. Use consistent entity types across your application to maintain a clean knowledge graph

  2. Create meaningful relationships that accurately represent how entities are connected

  3. Use entity IDs in memories to build a rich network of interconnected information

  4. Keep entity properties focused on attributes that don’t change frequently

  5. Use aliases for entities that might be referred to by different names

When designing your entity model, think about the types of questions you'll want to answer with your knowledge graph. This will help you determine which entities and relationships to create.

Now that you understand how to work with entities and their relationships, your next step should be:

  1. Next: Advanced RAG Techniques - Implement semantic search for better context

Following this sequence will complete your knowledge of the Memory API and prepare you for advanced implementation.