Get startedGet started for free

Build a simple AI app in Snowflake

1. Build a simple AI app in Snowflake

Are you ready to get building? You have everything you need to build a simple AI app. That is, you created your Snowflake free trial account, a database, a schema, and your first Snowflake notebook as well. In this video, we are going to build a simple AI app with Snowflake. This app won't be anything complex or sophisticated, and that is intentional. The point of this exercise is for you to immediately get hands-on with Snowflake, as well as get high-level exposure to the Snowflake AI features, so you can see how easy it is to get started with AI on Snowflake. Okay, here's the scenario. You're a data scientist at a ski gear company that has customers across different countries. Customers typically call the company to complain about damage to their ski gear. You're tasked with analyzing the customer support call transcripts to identify top customer complaints. You have access to raw, unstructured call transcript data, and we are going to use natural language processing to process them and find the root cause of each complaint. To achieve that, here's what we'll do. Let us load the data into Snowflake, analyze the call transcripts using Snowflake AI functions, and deliver the insights through an AI app. What are Snowflake AI functions? We'll dive deeper into those functions later. For now, let's understand on a high level what they are so you can build a mental model of it. Snowflake AI function gives you a serverless access to perform common AI tasks, such as summarization, translation, and so on, all in Snowflake. Follow along with me to build this simple app. Now is a good time to stop the video and make sure you're logged into your Snowflake account. Let's begin by loading the data. We will retrieve the raw, unstructured customer support call transcripts from AWS S3 and import them into a Snowflake table. This dataset contains information such as the date of the customer support call, the country and language of the customer, the name and category of the product, type of damage to the product, and the transcript of the conversation between the customer and the customer support representative. To build this app, we'll need the Snowflake notebook that you should have downloaded when you completed the reading prior to this video. If you haven't downloaded the notebook yet, please pause this video and come back after you downloaded it from the reading. In Snowsight, navigate to the Projects tab in the left panel, click Workspace. On the top left, click the Add New button and then select the Upload Files option. Select the notebook you downloaded from the reading section prior to this video. Once the notebook is open, connect to the compute service by clicking the Connect button in the top middle. Once it's connected, you can list your installed packages. To do this, create a new Python cell and type `!pip list`. You can see the Streamlit and Python packages are already installed in this notebook. Type `!pip install snowflake-ml-python` to install the Snowflake ML Python package. Once this is done, you can run the notebook cells one after another to build your first AI app. Let's understand what each cell in the Snowflake notebook does. In the first cell, you can see that we are importing pandas and the Streamlit packages to be used later in the code. We will also create a Snowpark session to process the call transcripts in the app. Also note this is a Python cell. Snowflake Notebooks supports Python, SQL, and Markdown cells. That means you can write SQL and Python and run them all together in the same notebook. The raw, unstructured transcript data is stored in a CSV format in an AWS S3 bucket. In cell 2, we create a new Snowflake database, virtual warehouse, and schema to store the call transcript data. Notice that cell 2 is a SQL cell and we will run the SQL commands to create these Snowflake objects. You could also do this in Python, of course, if that's your preference. Next, in cell 3, we create a CSV file format to read CSV files from S3. There is an external stage in Snowflake pointing to the S3 bucket where the data is stored. We create the call transcripts table in Snowflake to load raw transcript data into Snowflake. The call transcripts table has various fields to capture the customer complaints of ski gear company. It has columns such as the date of the customer support call, country and language of the customer, name and category of the product, type of damage to the product, and the transcript of the conversation between the customer and the customer support representative. Once we create the call transcripts table, the `COPY INTO` statement is used to bulk copy the data from the external stage into a Snowflake table. Before we continue further in this notebook, let us verify if the call transcript data is loaded into the Snowflake table. Navigate to the databases tab on the left panel. You will see the list of all the databases. Click on the ski gear support DB and then the ski gear support schema. Under tables, you will find the call transcripts and click on it. You can see the table definition on the right side. If you click on the columns tab, you will see the different columns and their data types as well. Now, if you click on the data preview, you will see a sample of a few rows of data. Now, let's hop back to the notebook and understand what the rest of the notebook cells are running. Let's go to cell 4, which is a sample transcript copied from the dataset for your reference. In cell 5, we'll use the Snowflake AI functions named complete to process the transcript and summarize it. Next, we import the complete function and create a prompt for instructing the large language model to summarize the call transcript into a JSON format. You can modify this prompt as well. This prompt instructs the model to generate an output in JSON format with three key fields, product name, defect, and summary. Generating the summary in JSON format is helpful because this allows us to use it programmatically. In cell 6, we invoke the Llama 3.1 and pass on the prompt and call the transcript to complete the function. It's important to once again remind you that models are constantly updating, so always check Snowflake documentation to find the latest and best model for your use case. Given a prompt, the complete function generates a response using your choice of supported language model. Since this model is not great at responding to the prompt in a highly accurate manner, we can try other foundation models and see if we get a cleaner JSON output. In cell 7, I'll show how you can change the large language model to Mistral 7b by simply changing the argument of the complete function. It is that simple to experiment with different LLMs using Snowflake AI. The Mistral model outputs only the JSON summary we prompted for. This is great. At last, we'll build our Streamlit app. To do this, navigate to the Streamlit app.py file within the Generate AI Streaming app folder. The code snippet here wraps the complete function call into a summarize function in Python. This is to build a simple UI using Streamlit for the summarization app. When you run this app, it outputs a big text box and a clickable summarize button below it. Now, copy the sample call transcript from cell 4 of the notebook and paste it into the Streamlit app text box. And click on the summarize button. You'll see how the model summarizes the transcript into JSON format with three keys, product name, defect, and summary. And that's it. Firstly, I have a bonus cell in the notebook. In cell 9, you can see how to invoke the AI_COMPLETE function using Snowflake SQL, if you prefer to do it that way. Second, there's an alternative way for you to run the entire Snowflake notebook instead of running it by each cell. You can click the play button that says run at the top left. This will run all the operations from all your notebook cells in the file starting at the very top. In our case, we ran each cell individually so I can walk you through the code snippets in each cell and understand what they do. Also, cell 8 or 9 are interactive cells. You need to input the transcript into the text box for models to summarize it. You can run the interactive notebook cells individually as well. And that's it. With this, we are done with module 1. Let's quickly do a recap of what we did. First, you loaded the call transcript dataset from the AWS S3 bucket into a Snowflake table and then you used the Snowflake Cortex complete function to prompt the foundational models with instructions to summarize the transcripts in JSON format. Finally, you built a Streamlit UI for this AI application. This is a great start that shows how easy it is to get value from the data using generative AI in Snowflake. In the next module, we'll dive deeper into the Snowflake AI functions, using them to extract information from our unstructured data. You will stitch the functions together into a meaningful architecture to get the structured data output as well.

2. Let's practice!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.