The Snowflake Native App Framework is a powerful way for application providers to build, deploy and market applications via the Snowflake Marketplace. In this example you will learn how to incorporate Snowflake's Cortex Suite into a Native App that will create a ‘CineBot' that can recommend Movies.
In preparation for building our Snowflake Native App we need to download the code artifacts for the Native App.
The code for the Native App is on Github. Start by cloning or downloading the repository into a separate folder.
git clone https://github.com/Snowflake-Labs/sfguide-build-chatbot-with-snowflake-native-app-snowflake-cortex.git
To simulate a Native App provider experience we will create a role called ‘nactx_role' and grant it the necessary privileges required to create an Application Package as well as create a database that will store our app code.
use role accountadmin;
create role if not exists nactx_role;
grant role nactx_role to role accountadmin;
grant create warehouse on account to role nactx_role;
grant create database on account to role nactx_role;
grant create application package on account to role nactx_role;
grant create application on account to role nactx_role with grant option;
use role nactx_role;
create database if not exists cortex_app;
create schema if not exists cortex_app.napp;
create stage if not exists cortex_app.napp.app_stage;
create warehouse if not exists wh_nap with warehouse_size='xsmall';
To simulate the app consumer experience we will create a role called ‘nac' and grant it the necessary privileges required to create Applications as well as set up a database to house the data we'll be querying with our Snowflake Native App.
use role accountadmin;
create role if not exists nac;
grant role nac to role accountadmin;
grant create warehouse on account to role nac;
grant create database on account to role nac;
grant create application on account to role nac;
use role nac;
create warehouse if not exists wh_nac with warehouse_size='medium';
create database if not exists movies;
create schema if not exists movies.data;
use schema movies.data;
CREATE STAGE MOVIES.DATA.MY_STAGE
URL='s3://sfquickstarts/vhol_build_2024_native_app_cortex_search/movies_metadata.csv'
DIRECTORY = (
ENABLE = true
AUTO_REFRESH = true
);
CREATE FILE FORMAT MOVIES.DATA.CSV_FILE_FORMAT
TYPE=CSV
SKIP_HEADER=1
FIELD_DELIMITER=','
TRIM_SPACE=TRUE
FIELD_OPTIONALLY_ENCLOSED_BY='"'
REPLACE_INVALID_CHARACTERS=TRUE
DATE_FORMAT=AUTO
TIME_FORMAT=AUTO
TIMESTAMP_FORMAT=AUTO;
CREATE TABLE movies.data.movies_metadata (
adult BOOLEAN,
belongs_to_collection VARCHAR,
budget NUMBER(38, 0),
genres VARCHAR,
homepage VARCHAR,
id NUMBER(38, 0),
imdb_id VARCHAR,
original_language VARCHAR,
original_title VARCHAR,
overview VARCHAR,
popularity VARCHAR,
poster_path VARCHAR,
production_companies VARCHAR,
production_countries VARCHAR,
release_date DATE,
revenue NUMBER(38, 0),
runtime NUMBER(38, 1),
spoken_languages VARCHAR,
status VARCHAR,
tagline VARCHAR,
title VARCHAR,
video BOOLEAN,
vote_average NUMBER(38, 1),
vote_count NUMBER(38, 0)
);
COPY INTO MOVIES.DATA.MOVIES_METADATA
FROM @my_stage FILE_FORMAT = (FORMAT_NAME = CSV_FILE_FORMAT) ON_ERROR = CONTINUE;
create or replace table movies_raw as
select title, budget::string as budget, overview, popularity, release_date::string as release_date,
runtime::string as runtime
from movies.data.movies_metadata;
With all of our Snowflake Native App assets uploaded to our Snowflake account we can now create our Application Package using our Provider role. Since we're doing this in a single Snowflake account we will also grant the Consumer role privileges to install it.
use role nactx_role;
create application package cortex_app_pkg;
After creating the application package we'll need to upload the Native App code to the the CORTEX_APP.NAPP.APP_STAGE stage. This can be accomplished by navigating to this stage using Snowsight - click on the ‘Database' icon on the left side navigation bar and then on the CORTEX_APP database > NAPP schema > APP_STAGE stage. You will need to do the following:
When this is done succesfully your we're now ready to create the Application Package.
alter application package cortex_app_pkg add version v1 using @cortex_app.napp.app_stage;
grant install, develop on application package cortex_app_pkg to role nac;
To simulate the app consumer experience we will create a role called ‘nac' and grant it the necessary privileges required to create Applications as well as set up a database to house the data we'll be querying with our Snowflake Native App.
use role nac;
create application cortex_app_instance from application package cortex_app_pkg using version v1;
grant all on database movies to application cortex_app_instance;
grant all on schema movies.data to application cortex_app_instance;
grant all on table movies.data.movies_raw to application cortex_app_instance;
grant usage on warehouse wh_nac to application cortex_app_instance;
--This is a special step required to allow Native Apps to utilized Cortex Functions in a consumer database
use role accountadmin;
GRANT IMPORTED PRIVILEGES ON DATABASE SNOWFLAKE TO APPLICATION cortex_app_instance;
use role nac;
call CORTEX_APP_INSTANCE.CORE.TABLE_CHUNKER();
call CORTEX_APP_INSTANCE.CORE.CREATE_CORTEX_SEARCH();
At this point you can navigate the the Application by clicking on ‘Data Products' on the left side of the Snowsight screen and then by clicking on ‘Apps'. You can click on the Cortext_App_Instance application and it will bring up the chatbot where you can ask it to recommend movies.
To clean up your environment you can run the following series of commands.
--clean up consumer objects
use role NAC;
drop application cortex_app_instance cascade;
drop warehouse wh_nac;
drop database movies;
--clean up provider objects
use role nactx_role;
drop application package cortex_app_pkg;
drop database cortex_app;
drop warehouse wh_nap;
--clean up prep objects
use role accountadmin;
drop role nactx_role;
drop role nac;
Congratulations! You've now deployed a Snowflake Native App that utilizes the Snowflake Cortex Suite to enhance the capabilities that a Native App Provider can give to their consumers.
In this Quickstart we covered the following:
This Quickstart can provide a template for you to accomplish the basic steps of building a Snowflake Native App that includes the Snowflake Cortex Suite to deploy & monetize whatever unique code to your Snowflake consumers accounts.