Ontology RAG Using CLI

Description

Use command-line tools to build Ontology RAG workflows with custom schemas

Difficulty

Advanced

Duration

30 min

You will need
  • TrustGraph deployed (Quick Start)
  • Familiarity with Ontology RAG concepts and OWL ontologies
Goal

Use CLI tools to import ontologies, extract structured knowledge, and query ontology-based knowledge graphs.

This guide covers the same Ontology RAG workflow as the Ontology RAG guide, but using command-line tools instead of the Workbench.

New to Ontology RAG? Read the Ontology RAG guide first to understand the concepts, workflow, and see visual examples.

This guide demonstrates:

  • Loading ontologies via CLI
  • Creating ontology-based flows
  • Processing documents with schema-guided extraction
  • Querying with Ontology RAG

Step-by-Step Guide

Step 1: Load Your Document

Download and load the example document:

wget -O phantom-cargo.md https://raw.githubusercontent.com/trustgraph-ai/example-data/refs/heads/main/tracking/operation-phantom-cargo.md

tg-add-library-document \
  --name "PHANTOM CARGO" \
  --description "Intelligence report: Operation PHANTOM CARGO" \
  --tags 'maritime,intelligence,cargo,grey arms' \
  --id https://trustgraph.ai/doc/phantom-cargo \
  --kind text/plain \
  phantom-cargo.md

Step 2: Load the Ontology

We’ll use the SSN/SOSA ontology (W3C standard for sensors and observations). For details about this ontology, see the Ontology RAG guide.

Download and install the ontology:

wget -O ssn-ontology.json https://raw.githubusercontent.com/trustgraph-ai/example-data/refs/heads/main/tracking/ssn-ontology.json

cat ssn-ontology.json | tg-put-config-item --type ontology --key ssn --stdin

Step 3: Create a Collection

Create an ‘intelligence’ collection:

tg-set-collection -n Intelligence -d 'Intelligence analysis' intelligence

Step 4: Create the Flow

Create an Ontology RAG flow:

tg-start-flow -n onto-rag -i onto-rag -d "Ontology RAG"

Step 5: Submit the Document for Processing

Submit the document for processing:

tg-start-library-processing \
    --flow-id onto-rag \
    --document-id https://trustgraph.ai/doc/phantom-cargo \
    --collection intelligence \
    --processing-id urn:processing-03

Step 6: Monitoring (Optional)

Processing can take time for large documents. For monitoring details, see the Ontology RAG guide monitoring section.

Step 7: Query with Ontology RAG

Query the ontology-based knowledge graph:

tg-invoke-graph-rag \
    -f onto-rag -C intelligence \
    -q 'What intelligence resources were using during the PHANTOM CARGO operation?'

Expected output:

The intelligence resources used during the PHANTOM CARGO operation were:
* SIGINT
* MASINT
* Electro-Optical HUMINT
* FININT
* AIS
* synthetic aperture radar (SAR)
* GPS coordinates

Explore visually: For graph exploration and ontology visualization, see the visual tools in the Ontology RAG guide.

Next Steps