Skip to main content

Calling models from dbt

Modelbit models are accessible from dbt using the same syntax as calling them from Redshift and Snowflake.

Getting inferences in a dbt model

In any dbt model my_model.sql file, you can access the latest version of a Modelbit model named score_lead with a function call like my_schema.score_lead_latest. Find this function call in your model's "API Endpoints" screen.

For example, to build a table of leads including their lead scores, use code like this in your leads_with_scores.sql model:

select
leads.*,
demo_schema.score_lead_latest(
hdyhau,
industry,
num_employees
) as lead_score
from demo_schema.leads

When this model is built, Modelbit will call your score_lead model for new predictions.

dbt materialization strategies

When dbt calls Modelbit depends on your dbt model's materialization strategy:

  • Table: dbt table (aka. "full") models call Modelbit for inferences on every row in the dbt model, every time the model is built.
  • Incremental: dbt incremental models call Modelbit for inferences on new rows in the dbt model, whenever the model is built.
  • View: dbt view and models call Modelbit for an inference when the dbt model is accessed in SQL.
  • Ephemeral: dbt ephemeral models call Modelbit for an inference when each time a dbt model accessing the ephemeral model is built.

Mock functions in staging

Modelbit automatically creates mock functions you can use in development and staging environments to avoid unnecessary Modelbit calls. To have your Modelbit SQL function access the mock instead of the deployed machine learning model, set the MODELBIT_MOCK SQL variable to 'MOCK'.

In dbt, you can choose to set this variable or not in a macro. If, like most dbt projects, your project uses the target variable to determine your environment, then you can write a macro like set_modelbit_mock_status with code like this:

{% macro set_modelbit_mock_status() %}
{% if target.name == 'prod' %}
{{ return("UNSET MODELBIT_MOCK;") }}
{% else %}
{{ return("SET MODELBIT_MOCK='MOCK';") }}
{% endif %}
{% endmacro %}

Finally, call your macro at the top of your model. For example, you can use:

{{ config(
materialized='incremental',
sql_header=set_modelbit_mock_status(),
) }}