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.
What are Entities?
Section titled “What are Entities?”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.
Creating Entities
Section titled “Creating Entities”To create a new entity, send a POST request to the /entities endpoint:
// Example of creating an entityconst 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 entityRequired Parameters
Section titled “Required Parameters”| Parameter | Type | Description |
|---|---|---|
tenantId | string | ID of the tenant this entity belongs to |
type | string | The type of entity (e.g., “person”, “place”, “concept”) |
name | string | A human-readable name for the entity |
Optional Parameters
Section titled “Optional Parameters”| Parameter | Type | Description |
|---|---|---|
properties | object | Additional properties specific to this entity |
aliases | string[] | Alternative names or identifiers for this entity |
metadata | object | Additional metadata for the entity |
Retrieving Entities
Section titled “Retrieving Entities”To retrieve a specific entity by its ID:
// Example of retrieving a specific entityconst 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 entitySearching for Entities
Section titled “Searching for Entities”To search for entities based on various criteria:
// Example of searching for entitiesconst 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 entitiesSearch Parameters
Section titled “Search Parameters”| Parameter | Type | Description |
|---|---|---|
tenantId | string | ID of the tenant to search entities for |
type | string | Optional filter by entity type |
query | string | Search query to match against entity names and aliases |
limit | number | Maximum number of results to return (default: 20) |
Creating Relationships
Section titled “Creating Relationships”Relationships connect entities to each other, forming the edges in your knowledge graph. To create a relationship:
// Example of creating a relationship between entitiesconst 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 relationshipRequired Parameters for Relationships
Section titled “Required Parameters for Relationships”| Parameter | Type | Description |
|---|---|---|
tenantId | string | ID of the tenant this relationship belongs to |
sourceId | string | ID of the source entity |
targetId | string | ID of the target entity |
type | string | The type of relationship (e.g., “KNOWS”, “LOCATED_IN”) |
Retrieving Entity Relationships
Section titled “Retrieving Entity Relationships”To retrieve all relationships for a specific entity:
// Example of retrieving relationships for an entityconst 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 relationshipsEntity-Memory Connections
Section titled “Entity-Memory Connections”Entities can be connected to memories by including entityIds when creating a memory:
// Example of creating a memory with entity connectionsconst 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.
Best Practices for Entity Management
Section titled “Best Practices for Entity Management”-
Use consistent entity types across your application to maintain a clean knowledge graph
-
Create meaningful relationships that accurately represent how entities are connected
-
Use entity IDs in memories to build a rich network of interconnected information
-
Keep entity properties focused on attributes that don’t change frequently
-
Use aliases for entities that might be referred to by different names
What to Learn Next
Section titled “What to Learn Next”Now that you understand how to work with entities and their relationships, your next step should be:
- 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.