Get startedGet started for free

Building out the front-end

1. Building out the front-end

We've got the RAG we've built in a notebook, now let's move it to a Streamlit application for people to use. To open Streamlit, we'll choose the Projects icon on the left, and then click Streamlit. Now, we'll click the plus button on the top right to create a Streamlit app. In the dialog box, we'll choose a name, database, and schema. Last, we'll choose a warehouse and click Create. This will create a template app. We want to remove all of the code here and replace it with our own. To get the code, you'll want to open up the repository you cloned from GitHub before. In the Module 2 folder, you'll see a Python file called Chat with Unstructured Data App. Open that up and copy all of the code, and then paste it into your Streamlit app. Before we click Run, we'll add the Snowflake ML Python and Snowflake Core libraries to our app. Now we're good to go. First, we'll look at the code in the left pane. We start with all of our imports for Streamlit in Snowflake. Now, we title our app, Chat with the Federal Reserve. Next, we set up a simple cortex search retriever, just as we did before. Note that you need to adjust the database and schema to match what you're using in your environment. Next, we create a Python class named Rag, just as we did in the notebook, with just two key changes. The first change is that we'll add a new method that tracks the conversation history called BuildMessagesWithContext. This method both tracks the conversation and formats the context chunks so that they can be used to answer the user's question. The second key change is we add streaming to Cortex Complete. This is done by just setting Stream equal to True. When we do so, Complete will now return a generator we'll access via Streamlit's built-in method, WriteStream. Then, we instantiate the Rag. Now, let's get into the Streamlit fun. We'll first add a MessagesSession state variable as an empty list. And, we'll add a ClearConversation button to clear it when needed. Next, we'll create the display for message history. Depending on the role, we'll write out user and AI messages in different formats. We can do that using the ChatMessageContainer by setting the name as User or Assistant. If we want, we can also add an avatar. This is a good spot to insert your favorite emoji. Now, we can call our main Rag methods. We'll use st.spinner Containers to get that nice loading feeling for each step so that the user knows what's happening while they wait. And, we'll also display our retrieved context chunks in an expander for the user to click into and read. Then, we'll build the message history, including the user query and context chunks, and we'll generate the stream. To wrap it up, we'll define Main. We'll add the chat bar along with a suggestion so our user knows a first question to ask. We'll write the user input to a ChatMessage and append it to the message history. We'll write the stream from our Rag completion in a ChatMessage using st.WriteStream. And last, we'll store the full text of the stream after it's done streaming to the conversation history session state variable. Imagine doing this with your own document set and giving the ability to ask questions of your data directly to users in your organization. Now, let's look at it in action. We have the app set up with a clear conversation button up top to reset when we need to. We can now ask questions of the FOMC documents that the Rag is built on top of. We can ask questions like, what was the Fed Funds Rate Target Range effective September 19th of 2024? Notice that the app displays our question and then lists relevant context found. Under that, we see the retrieved documents, and these are available to us in an expander. This lets us take a look at the context chunks that the app retrieved from CortexSearch. When we are done looking at them, we can minimize them. Depending on the query you passed and the volume of documents that Rag is searching, this could take a bit of time. Once the retrieved context is completed, then it's passed to the LLM, and Anthropic Cloud streams out a response to us, and then we get a get answer. Pretty cool. You just completed your hands-on build of the CortexSearch service, and you now have a complete Rag that you can adapt to other use cases. Nice work. In the next video, we'll wrap up this module and talk a little bit about what you can expect when you start the next one. I'll see you then.

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.