Gen AI/LLMs

May 22, 2024

Streamlit Chatbot in Under 60 Seconds Using Snowflake Cortex

The landscape of conversational AI has undergone a remarkable transformation, shifting from the realm of complex research into the hands of everyday developers. Snowflake's Cortex platform has played a pivotal role in this evolution, democratizing the power of state-of-the-art language models (LLMs) and making it surprisingly easy to build intelligent chatbots.

Imagine crafting a chatbot that can seamlessly engage in natural language dialogue, answer questions with nuanced understanding, and even generate creative text. With Snowflake Cortex, this vision becomes reality through a streamlined process that marries the latest LLMs with the robust data infrastructure that Snowflake provides.

A Glimpse into the Simplicity

The code below offers a glimpse into the simplicity of building a Gen AI chatbot within the Snowflake ecosystem:

>_ Python

import streamlit as st
from snowflake.cortex import Complete
from snowflake.snowpark.context import get_active_session

st.title("Streamlit Chatbot with Snowflake and Cortex")
st.write("This is a chatbot that uses Snowflake Cortex to generate responses. The chat history is maintained during the session.")

# Default model (can be changed later)
if "selected_model" not in st.session_state:
    st.session_state["selected_model"] = "mistral-7b"

# Initialize session state to store chat history
if 'chat_history' not in st.session_state:
    st.session_state['chat_history'] = []

# Sidebar with Model Selection
with st.sidebar:
    st.subheader("Model Options")
    st.selectbox('Select your model:', (
        'mixtral-8x7b',
        'snowflake-arctic',
        'mistral-large',
        'llama3-8b',
        'llama3-70b',
        'reka-flash',
        'mistral-7b',
        'llama2-70b-chat',
        'gemma-7b'
    ), key="selected_model")

# User input
user_input = st.text_input(":bust_in_silhouette: You: ", "")

# Handle button click (added check for empty input)
if st.button("Send") and user_input.strip():
    # Maintain chat history
    st.session_state['chat_history'].append(f":bust_in_silhouette: You: {user_input}")

    # Generate Cortex response using the selected model
    bot_response = Complete(st.session_state["selected_model"], user_input).strip()

    # Maintain chat history
    st.session_state['chat_history'].append(f":robot_face: Bot: {bot_response}")

    # Display chat history
    for chat in st.session_state['chat_history']:
        st.write(chat)

# Clear chat history button
if st.button("Clear Chat"):
    st.session_state['chat_history'] = []

Packages to Import:

- streamlit

- snowflake-snowpark-python

- snowflake-ml-python

In just a few lines, this code harnesses the power of Snowflake's Complete function, which taps into a vast array of pre-trained language models. The intuitive interface of Streamlit allows you to quickly design the chatbot's user interface, while the underlying complexity of model deployment and management is handled effortlessly by Snowflake.

Flexibility and Choice

Notice the sidebar element in the code that empowers users to choose their preferred LLM. This flexibility is a hallmark of Snowflake Cortex, offering a curated collection of cutting-edge models like Mistral, Llama, and others. This allows you to tailor the chatbot's behavior to your specific needs, whether prioritizing speed, creativity, or a specific domain expertise.

Unveiling the Power

Behind the scenes, the Complete function interacts with the selected LLM, handling intricate details such as tokenization, context management, and response generation. This means you can focus on designing engaging conversations and crafting prompts that elicit the desired responses from your chatbot.

The ease with which you can leverage these state-of-the-art models opens up a world of possibilities. Businesses can deploy chatbots for customer service, lead generation, or personalized product recommendations. Educators can create interactive tutoring systems. Creative professionals can even partner with AI to brainstorm ideas or draft content.

A Paradigm Shift

Snowflake Cortex, in essence, is a paradigm shift. It's a testament to how AI is becoming more accessible, empowering individuals and organizations to create powerful conversational experiences without the need for extensive machine learning expertise.

This accessibility fuels a new wave of innovation, where the potential of AI is not limited to a select few but is democratized for all. 

Resources:

Snowflake Copilot User Guide: https://docs.snowflake.com/en/user-guide/snowflake-copilot

Join Our Community

Join our newsletter list for occasional updates, products and insights.

Join Our Community

Join our newsletter list for occasional updates, products and insights.

Join Our Community

Join our newsletter list for occasional updates, products and insights.

More from
Gen AI/LLMs

Snowflake Embeddings and Vector Search

Snowflake Embeddings and Vector Search

Snowflake Embeddings and Vector Search

Snowflake Co-Pilot

Snowflake Co-Pilot

Snowflake Co-Pilot

Snowflake Universal Search

Snowflake Universal Search

Snowflake Universal Search

Streamlit Chatbot in Under 60 Seconds Using Snowflake Cortex

Streamlit Chatbot in Under 60 Seconds Using Snowflake Cortex

Streamlit Chatbot in Under 60 Seconds Using Snowflake Cortex

Snowflake Document AI (DocAI)

Snowflake Document AI (DocAI)

Snowflake Document AI (DocAI)

Snowflake Cortex Finetuning LLMs

Snowflake Cortex Finetuning LLMs

Snowflake Cortex Finetuning LLMs

Copyright 2020-24 - All Right Reserved

Copyright 2020-24 - All Right Reserved

Copyright 2020-24 - All Right Reserved