Tutorial: Answer questions about time-series revenue data with Cortex Analyst¶
Introduction¶
Cortex Analyst transforms natural-language questions about your data into results by generating and executing SQL queries. This tutorial describes how to set up Cortex Analyst to respond to questions about a time-series revenue data set.
What you will learn¶
- Establish a semantic model for the data set.
- Create a Streamlit app that queries Cortex Analyst.
Prerequisites¶
The following prerequisites are required to complete this tutorial:
- You have a Snowflake account and user with a role that grants the necessary privileges to create a database, schema, tables, stage, and virtual warehouse objects.
- You have Streamlit (https://pypi.org/project/streamlit/) set up on your local system.
Refer to the Snowflake in 20 minutes for instructions to meet these requirements.
Step 1: Setup¶
Getting the sample data¶
You will use a sample dataset downloaded from GitHub (https://github.com/Snowflake-Labs/sfguide-getting-started-with-cortex-analyst/tree/main/data). Download the following data files to your system:
daily_revenue.csvproduct.csvregion.csv
Also download the semantic model YAML (https://github.com/Snowflake-Labs/sfguide-getting-started-with-cortex-analyst/tree/main/revenue_timeseries.yaml) from GitHub.
You might want to take a look at this semantic model before proceeding. The semantic model supplements the SQL schema of each table with additional information that helps Cortex Analyst understand questions about the data. For more information, see Using SQL commands to create and manage semantic views.
Note
In a non-tutorial setting, you would bring your own data, possibly already in a Snowflake table, and develop your own semantic model.
Creating the Snowflake objects¶
Use Snowsight, the Snowflake UI, to create the Snowflake objects needed for this tutorial. After you complete the tutorial, you can drop these objects.
Note
Use a role that can create databases, schemas, warehouses, stages, and tables.
To create the objects:
- Sign in to Snowsight.
- In the navigation menu, select Projects » Worksheets, and then select the + button. A new SQL worksheet appears.
- Paste the SQL code below into the worksheet, then select the Run All from the drop-down menu at the top right of the worksheet.
The SQL above creates the following objects:
- A database named
cortex_analyst_demo - A schema within that database called
revenue_timeseries - Three tables in that schema:
daily_revenue,product_dim, andregion_dim - A stage named
raw_datathat will hold the raw data we will load into these tables - A virtual warehouse named
cortex_analyst_wh
Note
The virtual warehouse is initially suspended. It starts automatically when you run a query.
Step 2: Load the data into Snowflake¶
To get the data from the CSV files into Snowflake, you will upload them to the stage, then load the data from the stage into the tables. At the same time, you will upload the semantic model YAML file for use in a later step.
The files you will upload are:
daily_revenue.csvproduct.csvregion.csvrevenue_timeseries.yaml
To upload the files in Snowsight:
- Sign in to Snowsight.
- In the navigation menu, select Ingestion » Add Data, and then select Load files into a stage.
- Drag the four files you downloaded in the previous step into the Snowsight window.
- Choose the database
cortex_analyst_demoand the stageraw_data, then select the Upload button to upload the files.
Now that you have uploaded the files, load the data from the CSV files by executing the SQL commands below in a Snowsight worksheet.
Note
Only the result of the last command is shown in the output pane. You can run the commands line by line to see the results of each command.
Step 3: Create a Streamlit app to talk to your data through Cortex Analyst¶
To create a Streamlit app that uses Cortex Analyst:
- Create a Python file locally called
analyst_demo.py. - Copy the code below into the file.
- Replace the placeholder values with your account details.
- Run the Streamlit app using
streamlit run analyst_demo.py.
When you run the app, it prompts you to enter a question. Start with “What questions can I ask?” and try some of its suggestions.
Step 4: Clean up¶
Clean up (optional)¶
Execute the following DROP <object> commands to return your system to its state before you began the tutorial:
Dropping the database automatically removes all child database objects such as tables.
Next steps¶
Congratulations! You have successfully built a simple Cortex Analyst app to “talk to your data” in Snowflake.
Additional resources¶
Continue learning using the following resources: