Skip to main content

Knowledge graph (property graph-style) model and serialization for web resources

Project description

Onya is a knowledge graph expression and implementation. This repository combines a data model and format spec with a Python parser and API implementation.

Note: Using our provided onya-graph.SKILL.md with your favorite AI coding agent is a good way to automate Onya authoring and explore the format. Try providing a text document and asking it to generate a graph therefrom.

Python quick start

PyPI - Version PyPI - Python Version

Installation

Requires Python 3.12 or later. The package is still in early development, so install directly from source:

git clone https://github.com/OoriData/Onya.git
cd Onya
pip install -U .

Command line tool

You can use the built-in CLI to export directly from an Onya Literate (.onya) file to the Mermaid diagram format:

onya convert test/resource/schemaorg/thingsfallapart.onya > out.mmd

Then use a site such as https://mermaid.live/ to generate a diagram such as:

Running MLX-LM generate within Python

Basic Python Usage

Here's a simple example demonstrating the core Onya API. First, let's define a small friendship graph in Onya Literate format:

# @docheader

* @document: http://example.org/friendship-graph
* @nodebase: http://example.org/people/
* @schema: https://schema.org/

# Chuks [Person]

* name: Chukwuemeka Okafor
* nickname: Chuks
* age: 28

# Ify [Person]

* name: Ifeoma Obasi
* nickname: Ify
* age: 27

Parse this graph and interact with it using the Python API.

from onya.graph import graph
from onya.serial.literate import LiterateParser

# Parse the Onya Literate text into a graph
onya_text = '''
# @docheader

* @document: http://example.org/friendship-graph
* @nodebase: http://example.org/people/
* @schema: https://schema.org/

# Chuks [Person]

* name: Chukwuemeka Okafor
* nickname: Chuks
* age: 28

# Ify [Person]

* name: Ifeoma Obasi
* nickname: Ify
* age: 27
'''

g = graph()
op = LiterateParser()
result = op.parse(onya_text, g)
doc_iri = result.doc_iri
print(f'Parsed document: {doc_iri}')
print(f'Graph has {len(g)} nodes')

# Access nodes and their properties
chuks = g['http://example.org/people/Chuks']
ify = g['http://example.org/people/Ify']

# Get a specific property value
for prop in chuks.getprop('https://schema.org/name'):
    print(f'Name: {prop.value}')

# Add a friendship edge between Chuks and Ify
friendship = chuks.add_edge('https://schema.org/knows', ify)
print(f'Added edge: {friendship}')

# Add nested properties to the friendship (metadata about the relationship)
friendship.add_property('https://schema.org/startDate', '2018-03-15')
friendship.add_property('https://schema.org/description', 'Met at university')

# Add a new property to Ify
ify.add_property('https://schema.org/jobTitle', 'Software Engineer')

# Modify a property by removing the old one and adding a new one
age_props = list(chuks.getprop('https://schema.org/age'))
for prop in age_props:
    chuks.remove_property(prop)
chuks.add_property('https://schema.org/age', '29')

# Traverse edges
for edge in chuks.traverse('https://schema.org/knows'):
    friend = edge.target
    for name_prop in friend.getprop('https://schema.org/name'):
        print(f'Chuks knows: {name_prop.value}')
    # Access nested properties on the edge
    for date_prop in edge.getprop('https://schema.org/startDate'):
        print(f'  Friends since: {date_prop.value}')

# Find all nodes of a certain type
for person in g.typematch('https://schema.org/Person'):
    for name_prop in person.getprop('https://schema.org/name'):
        print(f'Person in graph: {name_prop.value}')

# Reserialize to Onya literate
from onya.serial.literate import write

write(g)

This example demonstrates:

  • Parsing Onya Literate format
  • Accessing nodes and properties
  • Adding edges with nested properties (reified relationships)
  • Modifying properties
  • Traversing the graph
  • Querying by type

Visualization / export

Onya includes simple serializers to help you visualize graphs:

  • Graphviz (DOT): from onya.serial import graphvizgraphviz.write(g, out=f) (see demo/graphviz_basic/)
  • Mermaid (flowchart): from onya.serial import mermaidmermaid.write(g, out=f) (see demo/mermaid_basic/; quick viewing via Mermaid Live Editor)

Command line tool

You can use the built-in CLI to export directly from an Onya Literate (.onya) file:

# Mermaid (default)
onya convert test/resource/schemaorg/thingsfallapart.onya > out.mmd

# Graphviz DOT
onya convert test/resource/schemaorg/thingsfallapart.onya --dot > out.dot

Acknowledgments

Onya is primarily developed by the crew at Oori Data. We offer LLMOps, data pipelines and software engineering services around AI/LLM applications.

Background

Onya is based on experience from developing Versa and also working on the MicroXML spec and implementations thereof.

The URL used for metavocabulary is managed via purl.org.

The name is from Igbo "ọ́nyà", web, snare, trap, and by extension, network. The expanded sense is ọ́nyà úchè, web of knowledge.

Contributing

Contributions welcome! We're interested in feedback from the community about what works and what doesn't in real-world usage. To get help with the code implementation, read CONTRIBUTING.md.

License

The specification is under CC BY 4.0 to encourage broad adoption and derivative work while ensuring attribution. We want the format itself to be as open and reusable as possible, allowing anyone to create implementations in any language or adapt the format for their specific needs.

Related Work

  • networkx: Network Analysis in Python
  • Apache AGE: PostgreSQL Extension that for graphs. ANSI SQL & openCypher over the same DB.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

onya-0.3.0.tar.gz (230.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

onya-0.3.0-py3-none-any.whl (32.3 kB view details)

Uploaded Python 3

File details

Details for the file onya-0.3.0.tar.gz.

File metadata

  • Download URL: onya-0.3.0.tar.gz
  • Upload date:
  • Size: 230.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for onya-0.3.0.tar.gz
Algorithm Hash digest
SHA256 62085220c562cc075fdd2150f5299dac4dba66a38e2d43bf18adb9657682114d
MD5 01fa04cc4fd11bd2788e7a7d3544d1fc
BLAKE2b-256 0e096b10b271165cfe0830b9b40930183fdee1157fe2fc4999fa55c61c42cbb5

See more details on using hashes here.

Provenance

The following attestation bundles were made for onya-0.3.0.tar.gz:

Publisher: publish.yml on OoriData/Onya

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file onya-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: onya-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 32.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for onya-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e335204ee52f7e0fdc2ae9943d5d5f48242fffb369b651f13772cf54dd552414
MD5 95054cbbf4af2489442993a514e70830
BLAKE2b-256 5363ad9f5a4d5d03ecfb9ce3477f0a430d61b6db651eb4961a1a30e1b76568ff

See more details on using hashes here.

Provenance

The following attestation bundles were made for onya-0.3.0-py3-none-any.whl:

Publisher: publish.yml on OoriData/Onya

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page