In this tutorial we will go through an introduction to setting up cortex analyst. We'll show two ways to deploy it via streamlit in Snowflake and Flask.
Video
Video still in development.
Requirements
Snowflake account, you can use a free trial. We also assume no complex security needs.
userolesysadmin;-- Create a database to store our schemas.createdatabaseifnotexistsraw;-- Create the schema. The schema stores all our objectss.createschemaifnotexistsraw.cortex;/* Warehouses are synonymous with the idea of compute resources in other systems. We will use this warehouse to call our user defined function.*/createwarehouseifnotexistsdevelopmentwarehouse_size=xsmallauto_suspend=30initially_suspended=true;-- We'll use this stage "folder" to store our semantic layer.createstagefilesdirectory=(ENABLE=true);usedatabaseraw;useschemacortex;usewarehousedevelopment;
Now that we have our example uploaded we can head to cortex analyst under the AI / ML Studio.
We'll select our semantic layer from our stage and click open.
Now that it's open we can immediately start playing with the semantic layer / chatbot.
We can see that the chatbot gives us example questions we can click and get results from. These examples are in the semantic layer.
We can also leave edit mode to show what it can potentially look like when we deploy the applicaiton.
You can see that our chatbot not only gives us the data in a table but also as a query. This can be changed in the future to remove the sql and or add a diagram / chart.
Edit Semantic Layer
In the playground we can also edit the semantic layer by using the GUI interface or by code. A great note here is that I found it helpful to use chatgpt to write my semantic layer code for me. I just provided it the table defenitions and it built the rest. Took me only 15 mins to get it to my liking with some great example quries.
Monitoring
You can monitor the usage of this semantic layer by going to the monitoring tab, this allows you to see how people are using your chatbot to help correct it without asking for feedback.
Deployment
Now that we have our cortex analyst chatbot built we'll want to deploy it. In this section we have two example platforms but you can deploy to many more.
Streamlit in Snowflake
Lets deploy our semantic layer / chatbot in streamlit in Snowflake.
To start lets create a new streamlit application in Snowflake.
Lets give our streamlit app a name, where to put it and finally the warehouse that will be used.
Note
If you put your semantic file somewhere else you'll just update it the string of the location.
Now that we have the app, lets copy in the code from the example application that we downloaded earlier and hit run.
Now you can see the chatbot and start using it and sharing it with any team who needs a sales chatbot.
Flask Website
Lets move to a build use case where we want to add our semantic layer into our application. In this example we'll build two additional semantic layers and then deploy them in our flask application. Make sure we download the necessary files for this section.
Next lets upload the new semantic layers to our stage.
Flask
Lets walk throught the process of setting up the example files and starting our flask application. First we'll want to update our Snowflake account information in the app.py file.
# If your snowflake account has an underscore then you must use the# account locator here for host. Otherwise it can we the same as account.HOST="<Account Identifier>"ACCOUNT="<Account Identifier>"USER="<Username>"PASSWORD="<Password>"ROLE="<role>"
# If your snowflake account has an underscore then you must use the# account locator here for host. Otherwise use your account URL.HOST="EASYCONNECT-ACCOUNT.snowflakecomputing.com"ACCOUNT="EASYCONNECT-ACCOUNT"USER="danielwilczak"PASSWORD="..."ROLE="ACCOUNTADMIN"
# If your snowflake account has an underscore then you must use the# account locator here for host. Otherwise use your account URL.HOST="CVB15898.snowflakecomputing.com"ACCOUNT="EASYCONNECT-TUTORIALS_AWS"USER="danielwilczak"PASSWORD="..."ROLE="ACCOUNTADMIN"
Next we'll want to start a python virtual environment and install the packages we need. This may be different on windows.