A language model handles sentences well and cannot vouch for where a fact came from. A knowledge graph is the reverse: it holds facts with clear sources and is hard to question in everyday language. So the two are increasingly combined. There is no W3C standard in this area yet and it moves fast, so this lesson covers only three patterns that are likely to last.
1. Give the graph as grounding
Finding relevant material and handing it to the model before it answers is called retrieval-augmented generation (RAG). Usually the material is passages of documents. With a graph, the things named in the question are found first, and the triples reached by following a few arrows from them are handed over. This suits questions that need several facts joined, such as "which shelter did the animal Minji keeps come from". Together with approaches that first build a graph from documents, it also goes by the name GraphRAG.
2. Turn the question into a query
The model translates a person's question into SPARQL and the store runs it. The answer comes straight from the data, so questions that count or filter by a condition are answered exactly. For the model to use the right IRIs, it has to be told the classes, properties and labels of the ontology.
# Question: "How many animals does Minji keep?"
# Given to the model: ex:Person, ex:Animal, ex:keeps (a person keeps an animal)
PREFIX ex: <https://example.org/>
SELECT (COUNT(?pet) AS ?n) WHERE {
ex:minji ex:keeps ?pet .
}A generated query can be wrong. Check the syntax before running it, and send it back if it contains an IRI the ontology does not have.
3. Extract facts from documents and fit them to an ontology
A model can be given sentences and asked for triples. Without a reference, "keeps", "owns" and "looks after" become three different relations and the graph does not connect. The ontology is that reference.
| Step | The role of the ontology and related standards |
|---|---|
| Extraction | Provide the list of usable classes and properties and have the model choose only from it |
| Matching things | Make the same thing carry the same IRI. This is the previous lesson |
| Validation | Check required values and kinds of value with SHACL shapes and drop what does not fit |
| Recording the source | Record with PROV-O which document a fact came from and which model extracted it |
What to watch for
- A model can invent a plausible IRI. Accept only IRIs that really exist in the ontology and the data.
- Store facts extracted by a model apart from facts a person has confirmed. Separate named graphs make this easy.
- If the graph is wrong, the grounding is wrong. Grounding does not remove the need for quality checks.
- Embedding search, which finds text of similar meaning, and graph traversal work together. Similar wording finds the starting point, and following arrows gathers the related facts.
Common misunderstandings
- With a knowledge graph attached, the model is never wrong.
- There is less room for error, and the grounds for an answer can be checked. The model can still misread the facts it was given.
- With language models around, ontologies are no longer needed.
- Without a reference for what to extract and what to call it, every extraction produces a different graph.
- Just put the whole ontology in the prompt.
- A large ontology does not fit, and it blurs the model. Select the classes and properties relevant to the question.
Having come this far, you can read nearly every item on an ontology page here. Pick an ontology in your field and start by opening its hierarchy tree.
See it in a real ontology
- PROV-O: The PROV Ontology
The ontology for recording the source of an extracted fact and how it was produced.