

# How a custom ontology is used in agentic retrieval

When an Agent Context contains a custom ontology, agentic retrieval uses it to find the data that answers a question, and it gives the retrieval agent your definitions and rules to use when it answers. This topic explains how. For where else AWS Context uses an ontology, see [Entry points at a glance](ontology-entry-points.md).

This topic applies to a custom ontology that AWS Context has imported into the Agent Context. A built-in ontology types the entities that AWS Context extracts, but AWS Context doesn’t import its terms into the Agent Context as nodes. For how a custom ontology gets into an Agent Context, see [Import into the Agent Context](ontology-entry-points.md#ontology-entry-points-import) and [Start grounding by associating an ontology](ontology-grounding-start.md).

## The ontology is part of the Agent Context
<a name="agentic-retrieval-custom-ontology-graph"></a>

When AWS Context imports a custom ontology, its classes and properties become nodes in the [Amazon Neptune Analytics](https://docs.aws.amazon.com/neptune-analytics/latest/userguide/what-is-neptune-analytics.html) graph associated with the Agent Context. The links between them, such as hierarchy, equivalence, and rules, become edges in the same graph. AWS Context also indexes each class and property by its name and its definition, so search can match them by keyword and by meaning. The retrieval agent reads only the parts of the ontology that a question reaches, so a large ontology doesn’t make every request larger.

The retrieval agent reaches the ontology the same way it reaches the rest of the Agent Context, through search and traversal:
+ A search can return a class or property that matches the question, such as the `Account` class for a question about accounts.
+ From a class, the retrieval agent can follow links to related classes and to the catalog tables grounded to the class.
+ From a grounded table, the retrieval agent can follow the link to its class and read the class’s definition.

This gives the retrieval agent a second way to reach your data: by business concept, not only by table name or document text. This matters most when table and column names are cryptic or differ from the words that people use.

## Questions that use your terms
<a name="agentic-retrieval-custom-ontology-terms"></a>

When search or traversal returns an ontology term, AWS Context checks whether the question names that term. It looks for the term’s name and for other names that the ontology gives it, for example its `rdfs:label`, `skos:prefLabel`, and `skos:altLabel` values. If the question names the term, the retrieval agent gets more than the search result: AWS Context gives it the term’s rules (see [Rules on a class](#agentic-retrieval-custom-ontology-rules)) and tells it what the term’s links to other terms mean (see [What links between terms mean](#agentic-retrieval-custom-ontology-links)).

These other names help only after search has returned the term. Search itself finds a term by its name and its definition, not by its labels. So name your classes and properties with the words that people use in questions, and mention other common names, such as code names and abbreviations, in the definition.

## What links between terms mean
<a name="agentic-retrieval-custom-ontology-links"></a>

An ontology links its terms with standard predicates, for example `skos:closeMatch` for a term that can stand for another. When search or traversal results include two terms linked by one of the following predicates, AWS Context tells the retrieval agent what the link means for the question. The retrieval agent then includes the linked term as well as the original one.

 `owl:equivalentClass`, `owl:equivalentProperty`, `owl:sameAs`, `skos:exactMatch`   
The two terms mean the same thing.

 `skos:closeMatch`   
One term can stand for the other.

 `dcterms:replaces`, `dcterms:isReplacedBy`   
One term is a newer name for the other. Data can use either name.

 `rdfs:subClassOf`, `rdfs:subPropertyOf`, `skos:broader`, `skos:narrower`   
One term is a more specific kind of the other. A question about the general term covers each of its specific kinds.

The retrieval agent can still read links with other predicates, because they’re in the Agent Context, but AWS Context doesn’t give the retrieval agent specific direction about them.

## Rules on a class
<a name="agentic-retrieval-custom-ontology-rules"></a>

A class can carry rules: conditions that every member of the class meets. When a question involves such a class, AWS Context gives the retrieval agent the complete rule, written out in plain words. The retrieval agent has the rule in context when it answers. For example, it can use the rule as a filter in the SQL that it runs on a table grounded to the class. AWS Context reads the following kinds of rules:
+  **SHACL shapes** – conditions on the members of the class and their properties, in shapes that target the class. These include property shapes, lists of allowed values, logical combinations, and SPARQL constraints. AWS Context reports a shape marked `sh:deactivated` as not applied.
+  **OWL restrictions** – conditions on the values of the class’s properties, such as `owl:someValuesFrom`, `owl:allValuesFrom`, `owl:hasValue`, and cardinalities.
+  **Defined classes** – a class defined as equal to a combination of conditions, such as `owl:equivalentClass` with `owl:intersectionOf`.
+  **General class axioms** – conditions that are enough to make something a member of the class.
+  **Datatype restrictions** – a property’s data type restricted by facets (`owl:withRestrictions`), such as minimum and maximum values, patterns, and lengths, or by a list of values (`owl:oneOf`).
+  **Inherited rules** – rules from the class’s parent classes, up to three levels up, labeled as inherited.
+  **Broader terms** – rules on up to two terms that the class is linked to with `skos:broader`, shown with the class’s own rules.

A question can name a term that has no rule of its own but is linked to a term with a rule by `skos:exactMatch` or `skos:closeMatch`. In that case, AWS Context gives the retrieval agent the linked term’s rule and says how the two terms are linked.

 AWS Context stores a rule as a chain of linked nodes. Without this help, the retrieval agent would have to read a rule one link at a time, and it could stop before it reads all of it. AWS Context gives the full rule for a term that the question names and for the term that best matches a search. For other classes with rules, it tells the retrieval agent that a rule exists, and the retrieval agent can read the rule when it needs it.

## Other parts of the ontology
<a name="agentic-retrieval-custom-ontology-other"></a>

 AWS Context imports every part of the ontology into the Agent Context, including parts that it doesn’t handle in the ways described in this topic. The retrieval agent can read these parts in search and traversal results, and it can use what it knows about the standard vocabularies to interpret them. Examples are inverse, transitive, and symmetric properties (`owl:inverseOf`, `owl:TransitiveProperty`, `owl:SymmetricProperty`), property chains, keys (`owl:hasKey`), disjoint classes, and custom annotation properties.

## Who can see the ontology
<a name="agentic-retrieval-custom-ontology-access"></a>

Ontology nodes don’t carry document access control lists. Anyone who can query an Agent Context can see the ontology in it, including the names, definitions, and rules of its terms. Don’t put information in an ontology that only some users should see. For how access control works, see [Governing access](governing-access.md).

## Search and traversal without agentic retrieval
<a name="agentic-retrieval-custom-ontology-search-apis"></a>

When you call search and traversal yourself ([Search and traverse an Agent context](search-and-traverse.md)), the results include ontology nodes and their links as graph data. The guidance on links and the rules described in this topic are added only for agentic retrieval.

## Example
<a name="agentic-retrieval-custom-ontology-example"></a>

AnyCompany Bank has a custom ontology with an `Account` class and an `UnfundedAccount` subclass. The `UnfundedAccount` class has a rule, written as a SHACL shape: an account whose balance is zero or less. AWS Context has grounded the catalog table `acct_mstr` to the `Account` class.

A branch manager asks, "How many unfunded accounts does each branch have?" Search returns the `UnfundedAccount` class. Because the question names the class, AWS Context gives the retrieval agent the rule on the class and explains that `UnfundedAccount` is a more specific kind of `Account`. The retrieval agent follows the link from `Account` to the `acct_mstr` table. With the rule in context, it can write SQL that counts the accounts with a balance of zero or less, and cite the table in its answer.

## Write an ontology that works well in retrieval
<a name="agentic-retrieval-custom-ontology-tips"></a>

For how to name, describe, and link the terms in an ontology so that they work well in retrieval, see [Prepare your ontology file](ontology-prepare-file.md).