Home/Documentation

Ontology

The Ontology resource manages domain-specific schemas. Define concepts, relationships, and hierarchies to structure how facts are extracted and organized in your knowledge graph.

POSTclient.ontology.load()
HTTP:POST /v1/ontology

Load or update an ontology domain with concepts and relationships.

Parameters

domainstrRequired

Unique identifier for the ontology domain (e.g., "software_engineering").

namestrRequired

Human-readable name for the ontology.

conceptslist[OntologyConceptRequest | dict]Required

List of concepts to define in the ontology.

descriptionstrOptional

Description of the ontology domain.

relationshipslist[OntologyRelationshipRequest | dict]Optional

Relationships between concepts.

Concept Structure

Concept Fields

concept_idstrRequired

Unique identifier for the concept.

namestrRequired

Human-readable name.

descriptionstrOptional

Description of the concept.

aliaseslist[str]Optional

Alternative names for the concept.

Relationship Structure

Relationship Fields

source_concept_idstrRequired

ID of the source concept.

target_concept_idstrRequired

ID of the target concept.

relationship_typestrRequired

Type of relationship (e.g., "built_with", "is_a", "has_a").

Example

load_ontology.pypython
1from memoair import MemoAir
2
3client = MemoAir()
4
5# Define a software engineering ontology
6client.ontology.load(
7 domain="software_engineering",
8 name="Software Engineering Concepts",
9 description="Common concepts in software development",
10 concepts=[
11 {
12 "concept_id": "python",
13 "name": "Python",
14 "description": "A high-level programming language",
15 "aliases": ["py", "python3"],
16 },
17 {
18 "concept_id": "fastapi",
19 "name": "FastAPI",
20 "description": "Modern Python web framework",
21 },
22 {
23 "concept_id": "pytorch",
24 "name": "PyTorch",
25 "description": "Machine learning framework",
26 },
27 {
28 "concept_id": "web_framework",
29 "name": "Web Framework",
30 "description": "Framework for building web applications",
31 },
32 {
33 "concept_id": "ml_framework",
34 "name": "ML Framework",
35 "description": "Framework for machine learning",
36 },
37 ],
38 relationships=[
39 {
40 "source_concept_id": "fastapi",
41 "target_concept_id": "python",
42 "relationship_type": "built_with",
43 },
44 {
45 "source_concept_id": "pytorch",
46 "target_concept_id": "python",
47 "relationship_type": "built_with",
48 },
49 {
50 "source_concept_id": "fastapi",
51 "target_concept_id": "web_framework",
52 "relationship_type": "is_a",
53 },
54 {
55 "source_concept_id": "pytorch",
56 "target_concept_id": "ml_framework",
57 "relationship_type": "is_a",
58 },
59 ],
60)
GETclient.ontology.get()
HTTP:GET /v1/ontology/{domain}

Retrieve a loaded ontology by domain.

1# Get ontology details
2ontology = client.ontology.get(domain="software_engineering")
3
4print(f"Name: {ontology.name}")
5print(f"Concepts: {len(ontology.concepts)}")
6print(f"Relationships: {len(ontology.relationships)}")
GETclient.ontology.list()
HTTP:GET /v1/ontology

List all loaded ontology domains.

1# List all ontologies
2ontologies = client.ontology.list()
3
4for ont in ontologies:
5 print(f"- {ont.domain}: {ont.name} ({ont.concept_count} concepts)")
GETclient.ontology.stats()
HTTP:GET /v1/ontology/{domain}/stats

Get statistics for a specific ontology.

1stats = client.ontology.stats(domain="software_engineering")
2
3print(f"Domain: {stats.domain}")
4print(f"Concepts: {stats.concept_count}")
5print(f"Relationships: {stats.relationship_count}")
DELETEclient.ontology.delete()
HTTP:DELETE /v1/ontology/{domain}

Delete an ontology domain and all its concepts.

1# Delete an ontology
2client.ontology.delete(domain="software_engineering")

Using Ontologies with Memory

Once an ontology is loaded, you can map extracted facts to ontology concepts during memory ingestion:

ontology_mapping.pypython
1from memoair import MemoAir, Message
2
3client = MemoAir()
4
5# Add memory with ontology mapping
6client.memories.add(
7 group_id="user:john",
8 messages=[
9 Message(
10 content="I've been learning FastAPI for building APIs",
11 role_type="user"
12 ),
13 ],
14 map_to_ontology=True,
15 ontology_domain="software_engineering",
16 ontology_confidence_threshold=0.7,
17)
18
19# The extracted fact "John uses FastAPI" will be linked to:
20# - The "fastapi" concept in the ontology
21# - Which is related to "python" and "web_framework"

Ontology in Tripartite Search

When you enable search_ontology=True in tripartite search, MemoAir includes ontology concepts in the results:

1results = client.search.tripartite(
2 query="What Python frameworks does John know?",
3 user_id="user:john",
4 search_ontology=True, # Include ontology in search
5)
6
7# Results might include:
8# [user] John uses FastAPI
9# [ontology] FastAPI is a web framework built with Python
10# [ontology] Python is a high-level programming language

Tip: Ontologies help your AI understand domain-specific terminology and relationships, improving search relevance and enabling more accurate fact extraction.