Daanalytics

Asking ChatGPT Anything using Streamlit in Snowflake and the 5W1H Method

A few weeks ago I shared a blog about how to create an ‘Ask ChatGPT Anything’ using Streamlit and the 5W1H Method. The goal of that blog was to create a Streamlit Application and using the OpenAI API to send prompts, constructed using the 5W1H Method, to ChatGPT.

The goal of this blog is to take the previous blog and go for Streamlit in Snowflake (SiS, currently in Public Preview on AWS), still using the OpenAI API to send prompts, constructed using the 5W1H Method, to ChatGPT.

ChatGPT

When it comes to ChatGPT, the better you specify your question, the better ChatGPT’s answer. One possible way to specify these prompts is by using the 5W1H Method. Using this method, you construct a prompt via answers to the following 6 questions; who?, what?, where?, why?, when? and how?

5W1H Method

According to ChatGPT, the 5W1H Method should give you the following insight:

  • Who? — Understanding “who” can help in tailoring the language and complexity of the response.
  • What? — Specifying “what” ensures that the AI provides the type and format of information you desire.
  • Where? — Defining “where” can help in receiving region or context-specific answers.
  • Why? — Knowing “why” can help in determining the depth and angle of the AI’s response.
  • When? — Framing “when” can help narrow down the context of the information provided.
  • How? — Clarifying “how” can guide the AI in structuring its answer in the most useful way.

Asking ChatGPT Anything

We are going to build a Streamlit in Snowflake application to chat with ChatGPT, building ChatGPT prompts using the 5W1H Method.

ChatGPT

  • OpenAI API
  • 5W1H Method

Streamlit in Snowflake

  • Setup Streamlit
    • Streamlit Snowflake objects
    • Streamlit Application objects
  • Streamlit Application

Create Streamlit in Snowflake objects

Streamlit in Snowflake

Streamlit in Snowflake is another option to create, deploy and share Streamlit applications. In this case the application is created closely where the data resides. Another example of one of the bases of the Snowflake Data Cloud; bringing workloads to the data instead of the other way around.

Before we can create a Streamlit Application in Streamlit we need to create a series of Snowflake objects:

  • Create Streamlit User
  • Create Streamlit Role
  • Create Streamlit Warehouse
  • Create Streamlit  Database
  • Create Streamlit Schema
  • Create Streamlit Stage
  • Grants on Streamlit Snowflake objects
    • e.g. Grant STREAMLIT on Schema

Find the script to create the Streamlit in Snowflake objects on GitHub.

Setup OpenAI (External Network Integration)

In the previous blog we used the OpenAI API from within the Streamlit application. This time we will use Snowflake’s External Network Integration functionality, currently in Public Preview on AWS and Azure.

Before we setup the External Network Integration, we need to generate an OpenAI API Key

Generate OpenAI API Key

To be able to chat with ChatGPT via the OpenAI API, you need to have a OpenAI API Key for authentication. You can generate one by setting up an account on; https://platform.openai.com.

Generate OpenAI API Key

Setup External Network Integration in Snowflake

Setting up an External Network Integration to the OpenAI API in Snowflake is a four step proces:

  1. Create a network rule
  2. Create a secret
  3. Create an external access integration
  4. Create the OpenAI Response UDF

Find the script to setup the External Network Integration to the OpenAI API in Snowflake on GitHub.

Building the Streamlit Application

At this point everything is set for building the Streamlit Application. A lot of steps are similar to the previous blogpost. The biggest difference is the fact that the OpenAI API logic is in Snowflake.

  • Import required packages
    • Streamlit
    • Snowpark
  • OpenAI model Engine
  • Page configuration
  • Sidebar configuration (optional)
  • Create Main Application
    • Collecting user input according to the 5W1H Method
    • Sending user input to ChatGPT function
      • Submit the prompt_output to the ‘Get OpenAI Output’-UDF
        • Return the chat response
    • Display the ChatGPT response

Find the script to the Streamlit application on GitHub.

Setup Streamlit in Snowflake

There are two ways to create an Streamlit application inside Snowflake; either via the Snowsight UI or from within a Snowsight SQL worksheet. The former automatically creates the Streamlit application and the necessary files in the right location. Creating the Streamlit application from within a Snowsight SQL worksheet requires more manual steps, but also a little bit more control on how the Streamlit application is created.

In this example we create a Streamlit application from within a Snowsight SQL worksheet. Therefore we need to execute some SQL and upload two files to a Snowflake Stage (a Streamlit application file and an environment.yml)

The environment.yml holds the required Python packages. You can only install packages listed in the Snowflake Anaconda Channel. Other channels are not supported.

Find an example of the Streamlit application file and the environment.yml on GitHub.

Summary

In this blog I showed how to create a similar application as created in a previous blog. This time I used Streamlit in Snowflake and Snowflake’s External Network Integration to use the OpenAI API. The prompt to ask ChatGPT for output has still been constructed using the 5W1H Method.

It was interesting to see how I could use two Public Preview features to create, deploy and share Streamlit applications and access an external API. Both from within Snowflake.

Till next time.

Snowflake Data Superhero. Online also known as; DaAnalytics.

Daan Bakboord

DaAnalytics signature picture with Data Superhero avatar.

Bekijk ook:

Why didn't we see this coming?

Why didn’t we see this coming?

Early this month I attended the two days International Master Class in Strategic Intelligence executed by Rodenberg Tillman & Associates. If you’re really determined to move beyond simply gathering data and truly understand its strategic impact, this Master Class is designed for you. The Master Class is built around the Six Building Blocks™, ensuring comprehensive coverage of the critical aspects of Strategic Management and Intelligence. It’s perfect for business professionals who aim to excel by integrating Strategic Intelligence into their everyday practices, gaining the insights necessary to not only anticipate, but shape the future.

Lees verder »
Amsterdam User Group Meeting October 2024

Snowflake Dutch User Group – October 2024

Last night I had the privilege to organize a Snowflake ❄️ User Group in Snowflake’s Amsterdam Office.

Johan van der Kooij shared his experiences regarding optimizing Snowflake from a cost & performance perspective. He shared practical hints, as well as example queries, that you can use to optimize your Snowflake environment.

Lees verder »
Snowflake Data Cloud Summit - Wrap Up

Snowflake Snowflake Data Cloud Summit — Wrap Up

Snowflake Data Cloud Summit proved that after all this years the core idea remains the same and is still strong. Technology should serve and Snowflake makes things simple. One Single Unified Platform, one product and one engine. Ease of use and Govenance. Maximum efficiency and maximum simplicity.

Bring the processing of data to the data instead of the other way around. Snowflake as a Platform where you build and share your Data, Apps and AI Products. Your data never has to leave the Platform and Snowflake takes care of this Platform.

Lees verder »