Skip to content

semantics

relationalai

Build and query RelationalAI semantic models.

The relationalai.semantics package provides a small, composable Python DSL for:

  • Creating a Model
  • Declaring concepts (entity types) and relationships with Concept and Relationship
  • Adding definitions (base data and logic) with define
  • Filtering and composing queries with where
  • Selecting values with select and materializing results (for example with Fragment.to_df)

Most functions in this module are convenience wrappers. They operate on the single “active” model (the only Model that has been created). For quick interactive use (e.g., notebooks) these convenience wrappers (select, where, etc.) can keep code terse. For larger applications and libraries, prefer calling methods on an explicit model instance (e.g., Model.select, Model.where, and Model.define). This avoids ambiguity if another module (or test) creates a second Model.

In addition to the query helpers, this module re-exports commonly used DSL types such as Concept, Relationship, and Property from frontend.base, as well as core value types such as Any, semantics.frontend.core.String, and semantics.frontend.core.Number from frontend.core.

Create a semantic model:

>>> from relationalai.semantics import Model, define, select, where, String, Integer
>>> m = Model()

Declare a Person concept:

>>> Person = m.Concept("Person")

Declare properties on Person:

>>> Person.name = m.Property(f"{Person} is named {String:name}")
>>> Person.age = m.Property(f"{Person} is {Integer:age} years old")

Define some Person entities:

>>> define(
... Person.new(name="Alice", age=10),
... Person.new(name="Bob", age=30),
... )

Select names of people over 18:

>>> where(Person.age > 18).select(Person.name).to_df()

Attributes exposed by this module.

Functions exposed by this module.

Classes exposed by this module.

Submodules and subpackages available under this namespace.