LongTermMemory API Reference
API for entity, preference, and fact storage.
Overview
LongTermMemory handles the knowledge graph: entities (POLE+O), user preferences, and facts with relationships.
async with MemoryClient(settings) as client:
# Access via client
long_term = client.long_term
# Add an entity
entity, dedup_result = await long_term.add_entity(
"John Smith",
"PERSON",
subtype="INDIVIDUAL"
)
Entity Operations
add_entity
Add an entity to the knowledge graph.
entity, dedup_result = await long_term.add_entity(
name: str,
entity_type: str,
subtype: str | None = None,
description: str | None = None,
metadata: dict[str, Any] | None = None,
confidence: float = 1.0,
coordinates: tuple[float, float] | None = None,
geocode: bool = False,
deduplicate: bool = True,
)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
|
|
required |
Entity name |
|
|
required |
POLE+O type: |
|
|
|
Subtype (e.g., |
|
|
|
Entity description |
|
|
|
Additional metadata |
|
|
|
Confidence score (0.0-1.0) |
|
|
|
Latitude, longitude for locations |
|
|
|
Auto-geocode location entities |
|
|
|
Check for duplicates |
get_entity_by_name
Get an entity by name.
entity = await long_term.get_entity_by_name(
name: str,
entity_type: str | None = None,
)
search_entities
Semantic search for entities.
results = await long_term.search_entities(
query: str,
entity_type: str | None = None,
limit: int = 10,
min_score: float = 0.0,
)
Preference Operations
add_preference
Add a user preference.
preference = await long_term.add_preference(
category: str,
value: str,
user_id: str | None = None,
importance: float = 0.5,
metadata: dict[str, Any] | None = None,
)
Fact Operations
Deduplication Operations
find_potential_duplicates
Find entities flagged as potential duplicates.
duplicates = await long_term.find_potential_duplicates(limit: int = 100)
# Returns: [(entity1, entity2, confidence), ...]
Provenance Operations
link_entity_to_message
Link an entity to its source message.
await long_term.link_entity_to_message(
entity: Entity,
message_id: str,
confidence: float = 1.0,
start_pos: int | None = None,
end_pos: int | None = None,
context: str | None = None,
)
Geospatial Operations
Entity Model
@dataclass
class Entity:
id: str
name: str
entity_type: str
subtype: str | None
description: str | None
metadata: dict[str, Any]
confidence: float
created_at: datetime
updated_at: datetime
embedding: list[float] | None
aliases: list[str]
@property
def full_type(self) -> str:
"""Returns 'TYPE:SUBTYPE' or just 'TYPE'."""
Example
async with MemoryClient(settings) as client:
# Add entities
person, _ = await client.long_term.add_entity(
"John Smith", "PERSON", subtype="INDIVIDUAL"
)
company, _ = await client.long_term.add_entity(
"Acme Corp", "ORGANIZATION", subtype="COMPANY"
)
# Add preference
await client.long_term.add_preference(
"food", "Loves Italian cuisine", importance=0.9
)
# Search entities
results = await client.long_term.search_entities("software company")
for entity, score in results:
print(f"[{score:.2f}] {entity.name} ({entity.full_type})")