Created
June 20, 2024 14:54
-
-
Save orellabac/47a3e6a3cdadc42d5e2eb1c51dd23123 to your computer and use it in GitHub Desktop.
Simple Functional Chatbot with Streamlit and Cortex API in Snowflake.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import streamlit as st | |
import json | |
from snowflake.snowpark import Session | |
session = Session.builder.getOrCreate() | |
st.title("☃️ Frosty") | |
# Initialize the chat messages history | |
if "messages" not in st.session_state.keys(): | |
st.session_state.messages = [{"role": "assistant", "content": "How can I help?"}] | |
# Prompt for user input and save | |
if prompt := st.chat_input(): | |
st.session_state.messages.append({"role": "user", "content": prompt}) | |
# display the existing chat messages | |
for message in st.session_state.messages: | |
with st.chat_message(message["role"]): | |
st.write(message["content"]) | |
# If last message is not from assistant, we need to generate a new response | |
if st.session_state.messages[-1]["role"] != "assistant": | |
# Call LLM | |
with st.chat_message("assistant"): | |
with st.spinner("Thinking..."): | |
query = json.dumps([{"role": m["role"], "content": m["content"]} for m in st.session_state.messages]) | |
results = session.sql(f"select snowflake.cortex.complete('mistral-large',parse_json($${query}$$))").first() | |
response = results[0] | |
st.write(response) | |
message = {"role": "assistant", "content": response} | |
st.session_state.messages.append(message) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment