Skip to content

Use ohi with Cognee

Use ohi to give Cognee standard ontologies in OWL format. Cognee turns documents, chats or code into knowledge graphs used by AI agents. On its own, Cognee makes up labels for the people, companies and other things it finds, so the same thing can end up with different labels. An ontology gives it a fixed set of terms to use instead.

ohi finds these ontologies, downloads them with their dependencies and pins their versions in a lock file. Everyone on your team then loads the same files, so everyone’s graph uses the same terms. This guide uses W3C Organization and FOAF.

To use other ontologies, search on ohi.dev or use ohi search.

  1. ohi downloads the ontologies as .owl files. It writes down which ones you chose in ohi.yaml, and the exact files in ohi.lock.json.
  2. Your script gives Cognee the paths to those files.
  3. When Cognee reads your data, it checks each thing it finds against the ontologies.
  1. Create a project that saves ontologies as .owl files in an ontologies folder:

    Terminal window
    ohi init cognee-ontologies --default_format owl-turtle --to ontologies
    cd cognee-ontologies

    Cognee’s documentation uses owl-rdfxml, but owl-turtle works just as well and it is much easier to read and review.

  2. Add your ontologies:

    Terminal window
    ohi add w3c/org@2014
    ohi add foaf/foaf@2014-01-14

    Each one lands in its own folder, such as ontologies/w3c/org/2014/, along with any ontologies it depends on.

  3. Check that the files are there:

    Terminal window
    find ontologies -name '*.owl*'

    In PowerShell, run Get-ChildItem ontologies -Recurse -Filter *.owl*. You should see two files, org.owl.ttl and foaf.owl.ttl.

  4. Commit ohi.yaml and ohi.lock.json to git. Your teammates can then run ohi sync to get the exact same files, so everyone’s graph uses the same ontology terms. Learn more in Manage ontology projects.

Load your data into Cognee with the ontologies

Section titled “Load your data into Cognee with the ontologies”

This is the script from Cognee’s Ontology Quickstart, with ontology_path pointed at the files you just downloaded and the forget line commented out.

  1. Save this script as ontology_quickstart.py in the project folder.

    Careful: cognee.forget(everything=True) deletes everything Cognee has stored on your computer.

    import asyncio
    from pathlib import Path
    import cognee
    from cognee.modules.ontology.ontology_config import Config
    from cognee.modules.ontology.rdf_xml.RDFLibOntologyResolver import RDFLibOntologyResolver
    async def main():
    # Uncomment to start from a clean state. This deletes all Cognee data.
    # await cognee.forget(everything=True)
    texts = ["Winnie the Pooh works at Tesla as head of eco-honey program."]
    # Changed ontology_path: every .owl file ohi downloaded.
    ontology_path = [
    str(p) for p in Path(__file__).parent.glob("ontologies/**/*.owl*")
    ]
    # Create full config structure manually
    config: Config = {
    "ontology_config": {
    "ontology_resolver": RDFLibOntologyResolver(ontology_file=ontology_path)
    }
    }
    # self_improvement=False skips Cognee's extra enrichment pass, which makes more LLM calls.
    # The graph is built either way.
    await cognee.remember(texts, config=config, self_improvement=False)
    if __name__ == "__main__":
    asyncio.run(main())
  2. Run it:

    Terminal window
    python ontology_quickstart.py

To try it on your own data, replace texts with your own text. You can also pass a file path, or a list of paths.

The script loads every ontology file under ontologies/, so ontologies you add later are picked up automatically.

The easiest way to check is to look at the graph. Save this as show_graph.py in the project folder, run it with python show_graph.py, and open graph.html in your browser:

import asyncio
import cognee
asyncio.run(cognee.visualize_graph("graph.html"))

Cognee graph where Winnie the Pooh is linked to person, the eco-honey program to project, and Tesla to formalorganization

Pink nodes with a green ring come from your ontologies. Here, Winnie the Pooh is linked to person and the eco-honey program to project, both from FOAF. Tesla is linked to formalorganization from W3C Organization. That term comes from ontologies/w3c/org/2014/org.owl, the file ohi downloaded.

Click a node with a green ring to see where it came from. Here, person groups Winnie the Pooh and is marked ontology valid, so Cognee took it from FOAF.

Cognee node panel for person, linked to Winnie the Pooh and marked ontology valid

If no node has a green ring, check the start of Cognee’s log. If there are no Ontology loaded successfully lines, Cognee didn’t find the files: check that the script is next to ohi.yaml and that the ontologies folder has .owl or .owl.ttl files in it.

  1. Find the latest version with ohi inspect <ontology_id>, for example ohi inspect w3c/org.
  2. Change the version in ohi.yaml and run ohi sync.
  3. The new version goes into a new folder, named after its version. Delete the old version’s folder, so the script doesn’t load both.
  4. Delete the old graph and build it again with the new terms: add await cognee.forget(dataset="main_dataset") at the start of main() and run the script. If you use Cognee inside an app that keeps running, restart it.
  • To delete everything Cognee stored, uncomment the cognee.forget(everything=True) line in the script and run it again.
  • To remove the ontologies, delete the ontologies folder. ohi sync brings them back.

NeverBlink, the team behind ohi, maintains this guide, not the Cognee team. If something does not work, open an ohi issue or ask on Discord. For problems with Cognee itself, use the Cognee issue tracker.