RelationalAI Python API Reference#
This reference provides detailed information on the classes and functions available in the relationalai
Python package.
RelationalAI requires Python 3.9, 3.10, or 3.11.
Table Of Contents#
Installation#
You can install the relationalai
Python package locally using pip
:
#pip install relationalai
See Getting Started: Local Installation for complete instructions.
If you’re using a cloud notebook, like Snowflake, Google Colab, or Hex, refer to Getting Started: Cloud Notebooks for instructions on installing relationalai
in those environments.
Usage#
The relationalai
Python package is used to build and query models that represent the entities, rules, and relationships that define your organization, its data, and the logic that drives its operations and decisions.
RAI models require a connection to the RAI Native App to evaluate queries.
This connection can be configured with a raiconfig.toml
file created using the rai init
CLI command or using the Config
class in a Python script or notebook:
#import relationalai as rai
# Create a model using a raiconfig.toml file.
model = rai.Model("MyModel")
# Create a model using a Config object.
cfg = rai.Config({
"user": "<SNOWFLAKE_USER>",
"password": "<SNOWFLAKE_PASSWORD>",
"account": "<SNOWFLAKE_ACCOUNT>",
"role": "<SNOWFLAKE_ROLE>",
"warehouse": "<SNOWFLAKE_WAREHOUSE>",
"rai_app_name": "relationalai",
}
model = rai.Model("MyModel", config=cfg)
See the configuration guide for more information on configuring your model’s connection to the RAI Native App.
The relationalai
package offers core classes and standard library modules for building models and analyzing data.
Models are written using RAI’s declarative Python query-builder syntax.
Check out the guides in Develop With RAI for a general introduction to building models.
Core Classes#
The following classes are used to define RAI models and write rules and queries:
Class | Description |
---|---|
Model | Represents a RAI model. Used to create types and rule or query contexts. |
Type | Represents a type in the model. Used to access properties and create Instance objects. |
Property | Represents a property of a type. Primarily used to declare properties. |
Context | Represents a rule or query in the model. Used to access query results. |
ContextSelect | Used to select results in a query. |
Producer | Represents a variable in a rule or query context whose value is unknown until evlauated by the RAI Native App. Base class for Instance , InstanceProperty , and Expression . |
Instance | Represents an instance of a type. Used to set types and properties of objects in the model and to access values assigned to properties. |
InstanceProperty | Represents a property value of an instance. |
Expression | Represents the result of a mathematical or logical operation. |
Here’s an example of how these classes are used in a simple model:
#import relationalai as rai
model = rai.Model("MyModel")
#^^^^-- Model
Person = rai.Type("Person")
Adult = rai.Type("Adult")
#^^^^-- Type
Person.name.declare()
Person.age.declare()
#^^^^^^^^^-- Property
with model.rule():
# ^^^^^^^^^^^^-- Context
bob = Person.add(id=1)
bob.set(name="Bob", age=30)
# ^^^-- Instance (Producer)
with model.rule():
# vvvvvv-- Instance (Producer)
person = Person()
# vvvvvvvvvv-- InstanceProperty (Producer)
person.age >= 18
# ^^^^^^^^^^^^^^^^-- Expression (Producer)
person.set(Adult)
# ^^^^^-- Type
# Context
# /
# vvvvvvvvvvvvv vvvvvv-- ContextSelect
with model.query() as select:
adult = Adult()
response = select(adult.name, adult.age)
# ^^^^^^^^ ^^^^^^^^^^ ^^^^^^^^^
# \ \/
# Context InstanceProperty
See the Core Concepts guide for more details on how these classes fit together.
Standard Library#
The relationalai.std
namespace contains tools for working with various kinds of data in a model.
It also provides APIs for more advanced analytical operations, like graph algorithms.
Built-ins#
The following functions are available for import directly from relationalai.std
:
Function | Description |
---|---|
alias() | Renames a result column in a query. |
as_bool() | Converts a filter expression to a boolean. |
as_rows() | Converts a list of data to a Rows object for passing data from Python to the model without defining a Type or using a dynamic rule or query context. |
Submodules#
For working with data, the following submodules are available:
Module | Description |
---|---|
relationalai.std.inspect | Tools for inspecting data. |
relationalai.std.aggregates | Functions for aggregating and grouping data. |
relationalai.std.math | Mathematical operations and functions. |
relationalai.std.dates | Tools for working with dates and times. |
relationalai.std.strings | Functions for working with string data. |
relationalai.std.re | Tools for working with regular expressions. |
relationalai.std.graphs | Classes for creating graphs and performing graph algorithms. |
Native App Management#
The relationalai
package also provides access to common Native App management procedures, such as activating and deactivating the app and managing data streams, through an instance of the Provider
class:
#import relationalai as rai
# Create a Provider instance and connect using the active profile in your
# raiconfig.toml file. Use the config parameter to connect using a Config
# object if you're not using a raiconfig.toml file.
app = rai.Provider()
# Activate the RAI Native App.
app.activate()
# Build a model, write rules, and run queries...
# Deactivate the RAI Native App.
app.deactivate()
Note that several methods of the Provider
class require connecting to the native app using a Snowflake user that has been granted the app_admin
application role.
See Manage RAI for more information on managing the RAI Native App.