Model#

relationalai
class Model(
    name: str,
    *,
    profile: str | None = None,
    connection: SnowflakeConnection | None = None
)

A RelationalAI Model is made up of objects representing real-world entities, categorized by type, and rules defining their properties and relationships. The Model class is used to create, manage, and query models.

Parameters#

NameTypeDescription
namestrThe name of the model. Must be at least three characters. May only contain letters, numbers, and underscores.
profilestr or NoneOptional name of the configuration profile to use when creating the model. If None, the currently active profile is used.
connectionSession or NoneOptional Snowpark Session object to use when creating the model. If None, connection details in the configuration profile set by the profile parameter are used. Ignored in Azure-based projects.

Attributes#

NameTypeDescription
Model.namestrThe name of the model.

Methods#

The core methods of the Model class are used to create Type instances and contexts for defining rules and querying the model.

NameDescriptionReturns
.Type(name)Creates a Type for categorizing objects in the model.Type
.rule()Adds a rule to the model.Context
.query()Queries the model.Context

.rule() and .query() return a Context object, which is a Python context manager and must be used in a with statement.

The following methods create contexts for expressing conditional logic in rules and queries and must be called from inside a .rule() or .query() context’s with block:

NameDescriptionReturns
.found()Checks that a subquery has at least one result.Context
.not_found()Checks that a subquery has no results.Context
.match()Matches objects to one or more subqueries. Similar to a Python match or a SQL CASE statment.Context
.case()Creates a subquery inside of a .match() context.Context

Example#

import relationalai as rai

# Create a new model named "people."
model = rai.Model("people")

# Get the Person table from the 'sandbox.public' schema in Snowflake.
Person = model.Type("Person", source="sandbox.public.person")

# Create an Adult type in the model.
Adult = model.Type("Adult")

# The model.rule() context manager is used to define rules in the model.
# This rule adds all people over 18 to the Adult type.
with model.rule():
    person = Person()
    person.age >= 18
    person.set(Adult)

# The model.query() context manager is used to query the model.
# This query returns the names of all adults in the model.
with model.query() as select:
    adult = Adult()
    response = select(adult.name)

print(response.results)
# Output:
#     name
# 0  Alice
# 1    Bob
IMPORTANT

Before you can access a Snowflake table in a model, such as the sandbox.public.person table in the preceding example, you must import the table using the rai imports:stream CLI command or the RelationalAI SQL Library. Your Snowflake user must have the correct privileges to create a stream on the table. Contact your Snowflake administrator if you need help importing a Snowflake table.

To use a configuration profile other than the active profile, pass the profile name to the profile parameter when creating the model:

# NOTE: The profile name must be passed as a keyword argument.
model = rai.Model("people", profile="my_profile")

To use a Snowpark Session other than the one specified in the configuration profile, pass a Session object to the connection parameter when creating the model:

from snowflake.snowpark import Session

connection_parameters = {
    "user": <USER>,
    "password": <PASSWORD>,
    "account": <ACCOUNT>,
    "warehouse": <WAREHOUSE>,
    "database": <DATABASE>,
    "schema": <SCHEMA>
)

conn = Session.builder.configs(connection_parameters).create()

# NOTE: The connection object must be passed as a keyword argument.
model = rai.Model("people", connection=conn)

See the Snowflake docs for more details on creating Snowpark Session objects.

See Also#