STREAMLIT DOCS | |
EVERNOTE : | |
https://www.evernote.com/shard/s313/nl/41973486/e7b40a50-9f4c-d816-2dc1-ae078af37fc8/ | |
25-03-2024 | |
βββββ | |
Build a basic LLM chat app - Streamlit Docs | |
https://docs.streamlit.io/knowledge-base/tutorials/build-conversational-apps | |
Build a basic LLM chat app | |
Introduction | |
The advent of large language models like GPT has revolutionized the ease of developing chat-based applications. Streamlit offers several Chat elements, enabling you to build Graphical User Interfaces (GUIs) for conversational agents or chatbots. Leveraging session state along with these elements allows you to construct anything from a basic chatbot to a more advanced, ChatGPT-like experience using purely Python code. | |
In this tutorial, we'll start by walking through Streamlit's chat elements, st.chat_message and st.chat_input. Then we'll proceed to construct three distinct applications, each showcasing an increasing level of complexity and functionality: | |
1. First, we'll Build a bot that mirrors your input to get a feel for the chat elements and how they work. We'll also introduce session state and how it can be used to store the chat history. This section will serve as a foundation for the rest of the tutorial. | |
2. Next, you'll learn how to Build a simple chatbot GUI with streaming. | |
3. Finally, we'll Build a ChatGPT-like app that leverages session state to remember conversational context, all within less than 50 lines of code. | |
Here's a sneak peek of the LLM-powered chatbot GUI with streaming we'll build in this tutorial: | |
Play around with the above demo to get a feel for what we'll build in this tutorial. A few things to note: | |
* There's a chat input at the bottom of the screen that's always visible. It contains some placeholder text. You can type in a message and press Enter or click the run button to send it. | |
* When you enter a message, it appears as a chat message in the container above. The container is scrollable, so you can scroll up to see previous messages. A default avatar is displayed to your messages' left. | |
* The assistant's responses are streamed to the frontend and are displayed with a different default avatar. | |
Before we start building, let's take a closer look at the chat elements we'll use. | |
Chat elements | |
Streamlit offers several commands to help you build conversational apps. These chat elements are designed to be used in conjunction with each other, but you can also use them separately. | |
st.chat_message lets you insert a chat message container into the app so you can display messages from the user or the app. Chat containers can contain other Streamlit elements, including charts, tables, text, and more. st.chat_input lets you display a chat input widget so the user can type in a message. | |
For an overview of the API, check out this video tutorial by Chanin Nantasenamat (@dataprofessor), a Senior Developer Advocate at Streamlit. | |
Streamlit | |
13,7K abonnees | |
Introducing Streamlit Chat Elements | |
st.chat_message | |
st.chat_message lets you insert a multi-element chat message container into your app. The returned container can contain any Streamlit element, including charts, tables, text, and more. To add elements to the returned container, you can use with notation. | |
st.chat_message's first parameter is the name of the message author, which can be either "user" or "assistant" to enable preset styling and avatars, like in the demo above. You can also pass in a custom string to use as the author name. Currently, the name is not shown in the UI but is only set as an accessibility label. For accessibility reasons, you should not use an empty string. | |
Here's an minimal example of how to use st.chat_message to display a welcome message: | |
import streamlit as st with st.chat_message("user"): st.write("Hello π") | |
Notice the message is displayed with a default avatar and styling since we passed in "user" as the author name. You can also pass in "assistant" as the author name to use a different default avatar and styling, or pass in a custom name and avatar. See the API reference for more details. | |
import streamlit as st import numpy as np with st.chat_message("assistant"): st.write("Hello human") st.bar_chart(np.random.randn(30, 3)) | |
While we've used the preferred with notation in the above examples, you can also just call methods directly in the returned objects. The below example is equivalent to the one above: | |
import streamlit as st | |
import numpy as np | |
message = st.chat_message("assistant") message.write("Hello human") message.bar_chart(np.random.randn(30, 3)) | |
So far, we've displayed predefined messages. But what if we want to display messages based on user input? | |
st.chat_input | |
st.chat_input lets you display a chat input widget so the user can type in a message. The returned value is the user's input, which is None if the user hasn't sent a message yet. You can also pass in a default prompt to display in the input widget. Here's an example of how to use st.chat_input to display a chat input widget and show the user's input: | |
import streamlit as st | |
prompt = st.chat_input("Say something") | |
if prompt: | |
st.write(f"User has sent the following prompt: {prompt}") | |
Pretty straightforward, right? Now let's combine st.chat_message and st.chat_input to build a bot the mirrors or echoes your input. | |
Build a bot that mirrors your input | |
In this section, we'll build a bot that mirrors or echoes your input. More specifically, the bot will respond to your input with the same message. We'll use st.chat_message to display the user's input and st.chat_input to accept user input. We'll also use session state to store the chat history so we can display it in the chat message container. | |
First, let's think about the different components we'll need to build our bot: | |
* Two chat message containers to display messages from the user and the bot, respectively. | |
* A chat input widget so the user can type in a message. | |
* A way to store the chat history so we can display it in the chat message containers. We can use a list to store the messages, and append to it every time the user or bot sends a message. Each entry in the list will be a dictionary with the following keys: role (the author of the message), and content (the message content). | |
import streamlit as st st.title("Echo Bot") | |
if "messages" not in st.session_state: st.session_state.messages = [] for message in st.session_state.messages: with st.chat_message(message["role"]): st.markdown(message["content"]) | |
In the above snippet, we've added a title to our app and a for loop to iterate through the chat history and display each message in the chat message container (with the author role and message content). We've also added a check to see if the messages key is in st.session_state. If it's not, we initialize it to an empty list. This is because we'll be adding messages to the list later on, and we don't want to overwrite the list every time the app reruns. | |
Now let's accept user input with st.chat_input, display the user's message in the chat message container, and add it to the chat history. | |
if prompt := st.chat_input("What is up?"): | |
with st.chat_message("user"): st.markdown(prompt) st.session_state.messages.append({"role": "user", "content": prompt}) | |
We used the := operator to assign the user's input to the prompt variable and checked if it's not None in the same line. If the user has sent a message, we display the message in the chat message container and append it to the chat history. | |
All that's left to do is add the chatbot's responses within the if block. We'll use the same logic as before to display the bot's response (which is just the user's prompt) in the chat message container and add it to the history. | |
response = f"Echo: {prompt}" with st.chat_message("assistant"): st.markdown(response) st.session_state.messages.append({"role": "assistant", "content": response}) | |
Putting it all together, here's the full code for our simple chatbot GUI and the result: | |
View full codeexpand_more | |
import streamlit as st | |
st.title("Echo Bot") | |
# Initialize chat history | |
if "messages" not in st.session_state: | |
st.session_state.messages = [] | |
# Display chat messages from history on app rerun | |
for message in st.session_state.messages: | |
with st.chat_message(message["role"]): | |
st.markdown(message["content"]) | |
# React to user input | |
if prompt := st.chat_input("What is up?"): | |
# Display user message in chat message container | |
st.chat_message("user").markdown(prompt) | |
# Add user message to chat history | |
st.session_state.messages.append({"role": "user", "content": prompt}) | |
response = f"Echo: {prompt}" | |
# Display assistant response in chat message container | |
with st.chat_message("assistant"): | |
st.markdown(response) | |
# Add assistant response to chat history | |
st.session_state.messages.append({"role": "assistant", "content": response}) | |
While the above example is very simple, it's a good starting point for building more complex conversational apps. Notice how the bot responds instantly to your input. In the next section, we'll add a delay to simulate the bot "thinking" before responding. | |
Build a simple chatbot GUI with streaming | |
In this section, we'll build a simple chatbot GUI that responds to user input with a random message from a list of pre-determind responses. In the next section, we'll convert this simple toy example into a ChatGPT-like experience using OpenAI. | |
Just like previously, we still require the same components to build our chatbot. Two chat message containers to display messages from the user and the bot, respectively. A chat input widget so the user can type in a message. And a way to store the chat history so we can display it in the chat message containers. | |
Let's just copy the code from the previous section and add a few tweaks to it. | |
import streamlit as st | |
import random | |
import time | |
st.title("Simple chat") | |
if "messages" not in st.session_state: st.session_state.messages = [] for message in st.session_state.messages: with st.chat_message(message["role"]): st.markdown(message["content"]) if prompt := st.chat_input("What is up?"): with st.chat_message("user"): st.markdown(prompt) st.session_state.messages.append({"role": "user", "content": prompt}) | |
The only difference so far is we've changed the title of our app and added imports for random and time. We'll use random to randomly select a response from a list of responses and time to add a delay to simulate the chatbot "thinking" before responding. | |
All that's left to do is add the chatbot's responses within the if block. We'll use a list of responses and randomly select one to display. We'll also add a delay to simulate the chatbot "thinking" before responding (or stream its response). Let's make a helper function for this and insert it at the top of our app. | |
def response_generator(): | |
response = random.choice( [ "Hello there! How can I assist you today?", "Hi, human! Is there anything I can help you with?", "Do you need help?", ] ) for word in response.split(): yield word + " " time.sleep(0.05) | |
Back to writing the response in our chat interface, we'll use st.write_stream to write out the streamed response with a typewriter effect. | |
with st.chat_message("assistant"): | |
response = st.write_stream(response_generator()) st.session_state.messages.append({"role": "assistant", "content": response}) | |
Above, we've added a placeholder to display the chatbot's response. We've also added a for loop to iterate through the response and display it one word at a time. We've added a delay of 0.05 seconds between each word to simulate the chatbot "thinking" before responding. Finally, we append the chatbot's response to the chat history. As you've probably guessed, this is a naive implementation of streaming. We'll see how to implement streaming with OpenAI in the next section. | |
Putting it all together, here's the full code for our simple chatbot GUI and the result: | |
View full codeexpand_more | |
import streamlit as st | |
import random | |
import time | |
# Streamed response emulator | |
def response_generator(): | |
response = random.choice( | |
[ | |
"Hello there! How can I assist you today?", | |
"Hi, human! Is there anything I can help you with?", | |
"Do you need help?", | |
] | |
) | |
for word in response.split(): | |
yield word + " " | |
time.sleep(0.05) | |
st.title("Simple chat") | |
# Initialize chat history | |
if "messages" not in st.session_state: | |
st.session_state.messages = [] | |
# Display chat messages from history on app rerun | |
for message in st.session_state.messages: | |
with st.chat_message(message["role"]): | |
st.markdown(message["content"]) | |
# Accept user input | |
if prompt := st.chat_input("What is up?"): | |
# Add user message to chat history | |
st.session_state.messages.append({"role": "user", "content": prompt}) | |
# Display user message in chat message container | |
with st.chat_message("user"): | |
st.markdown(prompt) | |
# Display assistant response in chat message container | |
with st.chat_message("assistant"): | |
response = st.write_stream(response_generator()) | |
# Add assistant response to chat history | |
st.session_state.messages.append({"role": "assistant", "content": response}) | |
Play around with the above demo to get a feel for what we've built. It's a very simple chatbot GUI, but it has all the components of a more sophisticated chatbot. In the next section, we'll see how to build a ChatGPT-like app using OpenAI. | |
Build a ChatGPT-like app | |
Now that you've understood the basics of Streamlit's chat elements, let's make a few tweaks to it to build our own ChatGPT-like app. You'll need to install the OpenAI Python library and get an API key to follow along. | |
Install dependencies | |
First let's install the dependencies we'll need for this section: | |
pip install openai streamlit | |
Add OpenAI API key to Streamlit secrets | |
Next, let's add our OpenAI API key to Streamlit secrets. We do this by creating .streamlit/secrets.toml file in our project directory and adding the following lines to it: | |
OPENAI_API_KEY = "YOUR_API_KEY" | |
Write the app | |
Now let's write the app. We'll use the same code as before, but we'll replace the list of responses with a call to the OpenAI API. We'll also add a few more tweaks to make the app more ChatGPT-like. | |
import streamlit as st from openai import OpenAI st.title("ChatGPT-like clone") client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"]) if "openai_model" not in st.session_state: st.session_state["openai_model"] = "gpt-3.5-turbo" if "messages" not in st.session_state: st.session_state.messages = [] for message in st.session_state.messages: with st.chat_message(message["role"]): st.markdown(message["content"]) if prompt := st.chat_input("What is up?"): st.session_state.messages.append({"role": "user", "content": prompt}) with st.chat_message("user"): st.markdown(prompt) | |
All that's changed is that we've added a default model to st.session_state and set our OpenAI API key from Streamlit secrets. Here's where it gets interesting. We can replace our emulated stream with the model's responses from OpenAI: | |
with st.chat_message("assistant"): stream = client.chat.completions.create( model=st.session_state["openai_model"], messages=[ {"role": m["role"], "content": m["content"]} for m in st.session_state.messages ], stream=True, ) response = st.write_stream(stream) st.session_state.messages.append({"role": "assistant", "content": response}) | |
Above, we've replaced the list of responses with a call to OpenAI().chat.completions.create. We've set stream=True to stream the responses to the frontend. In the API call, we pass the model name we hardcoded in session state and pass the chat history as a list of messages. We also pass the role and content of each message in the chat history. Finally, OpenAI returns a stream of responses (split into chunks of tokens), which we iterate through and display each chunk. | |
Putting it all together, here's the full code for our ChatGPT-like app and the result: | |
View full codeexpand_more | |
from openai import OpenAI | |
import streamlit as st | |
st.title("ChatGPT-like clone") | |
client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"]) | |
if "openai_model" not in st.session_state: | |
st.session_state["openai_model"] = "gpt-3.5-turbo" | |
if "messages" not in st.session_state: | |
st.session_state.messages = [] | |
for message in st.session_state.messages: | |
with st.chat_message(message["role"]): | |
st.markdown(message["content"]) | |
if prompt := st.chat_input("What is up?"): | |
st.session_state.messages.append({"role": "user", "content": prompt}) | |
with st.chat_message("user"): | |
st.markdown(prompt) | |
with st.chat_message("assistant"): | |
stream = client.chat.completions.create( | |
model=st.session_state["openai_model"], | |
messages=[ | |
{"role": m["role"], "content": m["content"]} | |
for m in st.session_state.messages | |
], | |
stream=True, | |
) | |
response = st.write_stream(stream) | |
st.session_state.messages.append({"role": "assistant", "content": response}) | |
Congratulations! You've built your own ChatGPT-like app in less than 50 lines of code. | |
We're very excited to see what you'll build with Streamlit's chat elements. Experiment with different models and tweak the code to build your own conversational apps. If you build something cool, let us know on the Forum or check out some other Generative AI apps for inspiration. π | |
forum | |
Still have questions? | |
Our forums are full of helpful information and Streamlit experts. | |
β¦ | |
ZIE OOK: | |
AI-Yash/st-chat: Streamlit Component, for a Chatbot UI | |
https://github.com/AI-Yash/st-chat | |
β¦ | |
Build an LLM app using LangChain - Streamlit Docs | |
https://docs.streamlit.io/knowledge-base/tutorials/llm-quickstart | |
Build an LLM app using LangChain | |
OpenAI, LangChain, and Streamlit in 18 lines of code | |
In this tutorial, you will build a Streamlit LLM app that can generate text from a user-provided prompt. This Python app will use the LangChain framework and Streamlit. Optionally, you can deploy your app to Streamlit Community Cloud when you're done. | |
This tutorial is adapted from a blog post by Chanin Nantesanamat: LangChain tutorial #1: Build an LLM-powered app in 18 lines of code. | |
Objectives | |
1. Get an OpenAI key from the end user. | |
2. Validate the user's OpenAI key. | |
3. Get a text prompt from the user. | |
4. Authenticate OpenAI with the user's key. | |
5. Send the user's prompt to OpenAI's API. | |
6. Get a response and display it. | |
Bonus: Deploy the app on Streamlit Community Cloud! | |
Prerequisites | |
* Python 3.8+ | |
* Streamlit | |
* LangChain | |
* OpenAI API key | |
Setup coding environment | |
In your IDE (integrated coding environment), open the terminal and install the following three Python libraries: | |
pip install streamlit openai langchain | |
Create a requirements.txt file located in the root of your working directory and save these dependencies. This is necessary for deploying the app to the Streamlit Community Cloud later. | |
streamlit openai langchain | |
Building the app | |
The app is only 18 lines of code: | |
import streamlit as st from langchain.llms import OpenAI st.title('π¦π Quickstart App') openai_api_key = st.sidebar.text_input('OpenAI API Key', type='password') def generate_response(input_text): llm = OpenAI(temperature=0.7, openai_api_key=openai_api_key) st.info(llm(input_text)) with st.form('my_form'): text = st.text_area('Enter text:', 'What are the three key pieces of advice for learning how to code?') submitted = st.form_submit_button('Submit') if not openai_api_key.startswith('sk-'): st.warning('Please enter your OpenAI API key!', icon='β ') if submitted and openai_api_key.startswith('sk-'): generate_response(text) | |
To start, create a new Python file and save it asΒ streamlit_app.py in the root of your working directory. | |
1. Import the necessary Python libraries.β¨import streamlit as st from langchain.llms import OpenAI β¨ | |
2. Create the app's title using st.title.β¨st.title('π¦π Quickstart App') β¨ | |
3. Add a text input box for the user to enter their OpenAI API key.β¨openai_api_key = st.sidebar.text_input('OpenAI API Key', type='password') β¨ | |
4. Define a function to authenticate to OpenAI API with the user's key, send a prompt, and get an AI-generated response. This function accepts the user's prompt as an argument and displays the AI-generated response in a blue box using st.info.β¨def generate_response(input_text): llm = OpenAI(temperature=0.7, openai_api_key=openai_api_key) st.info(llm(input_text)) β¨ | |
5. Finally, use st.form() to create a text box (st.text_area()) for user input. When the user clicks Submit, the generate-response() function is called with the user's input as an argument.β¨with st.form('my_form'): text = st.text_area('Enter text:', 'What are the three key pieces of advice for learning how to code?') submitted = st.form_submit_button('Submit') if not openai_api_key.startswith('sk-'): st.warning('Please enter your OpenAI API key!', icon='β ') if submitted and openai_api_key.startswith('sk-'): generate_response(text) β¨ | |
6. Remember to save your file! | |
7. Return to your computer's terminal to run the app.β¨streamlit run streamlit_app.py β¨ | |
Deploying the app | |
To deploy the app to the Streamlit Cloud, follow these steps: | |
1. Create a GitHub repository for the app. Your repository should contain two files:β¨your-repository/ βββ streamlit_app.py βββ requirements.txt β¨ | |
2. Go to Streamlit Community Cloud, click theΒ New appΒ button from your workspace, then specify the repository, branch, and main file path. Optionally, you can customize your app's URL by choosing a custom subdomain. | |
3. Click theΒ Deploy!Β button. | |
Your app will now be deployed to Streamlit Community Cloud and can be accessed from around the world! π | |
Conclusion | |
Congratulations on building an LLM-powered Streamlit app in 18 lines of code! π₯³ You can use this app to generate text from any prompt that you provide. The app is limited by the capabilities of the OpenAI LLM, but it can still be used to generate some creative and interesting text. | |
We hope you found this tutorial helpful! Check out more examples to see the power of Streamlit and LLM. π | |
Happy Streamlit-ing! π | |
forum | |
Still have questions? | |
Our forums are full of helpful information and Streamlit experts. | |
βββββ | |
Cheat sheet - Streamlit Docs | |
https://docs.streamlit.io/library/cheatsheet | |
Cheat Sheet | |
This is a summary of the docs, as of Streamlit v1.31.0. | |
Install & Import | |
pip install streamlit | |
streamlit run first_app.py | |
# Import convention | |
>>> import streamlit as st | |
Pre-release features | |
pip uninstall streamlit | |
pip install streamlit-nightly --upgrade | |
Learn more about experimental features | |
Command line | |
streamlit --help | |
streamlit run your_script.py | |
streamlit hello | |
streamlit config show | |
streamlit cache clear | |
streamlit docs | |
streamlit --version | |
Magic commands | |
# Magic commands implicitly | |
# call st.write(). | |
"_This_ is some **Markdown***" | |
my_variable | |
"dataframe:", my_data_frame | |
Display text | |
st.write("Most objects") # df, err, func, keras! | |
st.write(["st", "is <", 3]) # see * | |
st.write_stream(my_generator) | |
st.write_stream(my_llm_stream) | |
st.text("Fixed width text") | |
st.markdown("_Markdown_") # see * | |
st.latex(r""" e^{i\pi} + 1 = 0 """) | |
st.title("My title") | |
st.header("My header") | |
st.subheader("My sub") | |
st.code("for i in range(8): foo()") | |
* optional kwarg unsafe_allow_html = True | |
Display data | |
st.dataframe(my_dataframe) | |
st.table(data.iloc[0:10]) | |
st.json({"foo":"bar","fu":"ba"}) | |
st.metric("My metric", 42, 2) | |
Display media | |
st.image("./header.png") | |
st.audio(data) | |
st.video(data) | |
st.video(data, subtitles="./subs.vtt") | |
Display charts | |
st.area_chart(df) | |
st.bar_chart(df) | |
st.line_chart(df) | |
st.map(df) | |
st.scatter_chart(df) | |
st.altair_chart(chart) | |
st.bokeh_chart(fig) | |
st.graphviz_chart(fig) | |
st.plotly_chart(fig) | |
st.pydeck_chart(chart) | |
st.pyplot(fig) | |
st.vega_lite_chart(df) | |
Add widgets to sidebar | |
# Just add it after st.sidebar: | |
>>> a = st.sidebar.radio("Select one:", [1, 2]) | |
# Or use "with" notation: | |
>>> with st.sidebar: | |
>>> st.radio("Select one:", [1, 2]) | |
Columns | |
# Two equal columns: | |
>>> col1, col2 = st.columns(2) | |
>>> col1.write("This is column 1") | |
>>> col2.write("This is column 2") | |
# Three different columns: | |
>>> col1, col2, col3 = st.columns([3, 1, 1]) | |
# col1 is larger. | |
# You can also use "with" notation: | |
>>> with col1: | |
>>> st.radio("Select one:", [1, 2]) | |
Tabs | |
# Insert containers separated into tabs: | |
>>> tab1, tab2 = st.tabs(["Tab 1", "Tab2"]) | |
>>> tab1.write("this is tab 1") | |
>>> tab2.write("this is tab 2") | |
# You can also use "with" notation: | |
>>> with tab1: | |
>>> st.radio("Select one:", [1, 2]) | |
Expandable containers | |
>>> expand = st.expander("My label") | |
>>> expand.write("Inside the expander.") | |
>>> pop = st.popover("Button label") | |
>>> pop.checkbox("Show all") | |
# You can also use "with" notation: | |
>>> with expand: | |
>>> st.radio("Select one:", [1, 2]) | |
Control flow | |
# Stop execution immediately: | |
st.stop() | |
# Rerun script immediately: | |
st.rerun() | |
# Navigate to another page: | |
st.switch_page("pages/my_page.py") | |
# Group multiple widgets: | |
>>> with st.form(key="my_form"): | |
>>> username = st.text_input("Username") | |
>>> password = st.text_input("Password") | |
>>> st.form_submit_button("Login") | |
Display interactive widgets | |
st.button("Click me") | |
st.download_button("Download file", data) | |
st.link_button("Go to gallery", url) | |
st.page_link("app.py", label="Home") | |
st.data_editor("Edit data", data) | |
st.checkbox("I agree") | |
st.toggle("Enable") | |
st.radio("Pick one", ["cats", "dogs"]) | |
st.selectbox("Pick one", ["cats", "dogs"]) | |
st.multiselect("Buy", ["milk", "apples", "potatoes"]) | |
st.slider("Pick a number", 0, 100) | |
st.select_slider("Pick a size", ["S", "M", "L"]) | |
st.text_input("First name") | |
st.number_input("Pick a number", 0, 10) | |
st.text_area("Text to translate") | |
st.date_input("Your birthday") | |
st.time_input("Meeting time") | |
st.file_uploader("Upload a CSV") | |
st.camera_input("Take a picture") | |
st.color_picker("Pick a color") | |
# Use widgets' returned values in variables: | |
>>> for i in range(int(st.number_input("Num:"))): | |
>>> foo() | |
>>> if st.sidebar.selectbox("I:",["f"]) == "f": | |
>>> b() | |
>>> my_slider_val = st.slider("Quinn Mallory", 1, 88) | |
>>> st.write(slider_val) | |
# Disable widgets to remove interactivity: | |
>>> st.slider("Pick a number", 0, 100, disabled=True) | |
Build chat-based apps | |
# Insert a chat message container. | |
>>> with st.chat_message("user"): | |
>>> st.write("Hello π") | |
>>> st.line_chart(np.random.randn(30, 3)) | |
# Display a chat input widget at the bottom of the app. | |
>>> st.chat_input("Say something") | |
# Display a chat input widget inline. | |
>>> with st.container(): | |
>>> st.chat_input("Say something") | |
Learn how to Build a basic LLM chat app | |
Mutate data | |
# Add rows to a dataframe after | |
# showing it. | |
>>> element = st.dataframe(df1) | |
>>> element.add_rows(df2) | |
# Add rows to a chart after | |
# showing it. | |
>>> element = st.line_chart(df1) | |
>>> element.add_rows(df2) | |
Display code | |
>>> with st.echo(): | |
>>> st.write("Code will be executed and printed") | |
Placeholders, help, and options | |
# Replace any single element. | |
>>> element = st.empty() | |
>>> element.line_chart(...) | |
>>> element.text_input(...) # Replaces previous. | |
# Insert out of order. | |
>>> elements = st.container() | |
>>> elements.line_chart(...) | |
>>> st.write("Hello") | |
>>> elements.text_input(...) # Appears above "Hello". | |
st.help(pandas.DataFrame) | |
st.get_option(key) | |
st.set_option(key, value) | |
st.set_page_config(layout="wide") | |
st.query_params[key] | |
st.query_params.get_all(key) | |
st.query_params.clear() | |
Connect to data sources | |
st.connection("pets_db", type="sql") | |
conn = st.connection("sql") | |
conn = st.connection("snowflake") | |
>>> class MyConnection(BaseConnection[myconn.MyConnection]): | |
>>> def _connect(self, **kwargs) -> MyConnection: | |
>>> return myconn.connect(**self._secrets, **kwargs) | |
>>> def query(self, query): | |
>>> return self._instance.query(query) | |
Optimize performance | |
Cache data objects | |
# E.g. Dataframe computation, storing downloaded data, etc. | |
>>> @st.cache_data | |
... def foo(bar): | |
... # Do something expensive and return data | |
... return data | |
# Executes foo | |
>>> d1 = foo(ref1) | |
# Does not execute foo | |
# Returns cached item by value, d1 == d2 | |
>>> d2 = foo(ref1) | |
# Different arg, so function foo executes | |
>>> d3 = foo(ref2) | |
# Clear all cached entries for this function | |
>>> foo.clear() | |
# Clear values from *all* in-memory or on-disk cached functions | |
>>> st.cache_data.clear() | |
Cache global resources | |
# E.g. TensorFlow session, database connection, etc. | |
>>> @st.cache_resource | |
... def foo(bar): | |
... # Create and return a non-data object | |
... return session | |
# Executes foo | |
>>> s1 = foo(ref1) | |
# Does not execute foo | |
# Returns cached item by reference, s1 == s2 | |
>>> s2 = foo(ref1) | |
# Different arg, so function foo executes | |
>>> s3 = foo(ref2) | |
# Clear all cached entries for this function | |
>>> foo.clear() | |
# Clear all global resources from cache | |
>>> st.cache_resource.clear() | |
Deprecated caching | |
>>> @st.cache | |
... def foo(bar): | |
... # Do something expensive in here... | |
... return data | |
>>> # Executes foo | |
>>> d1 = foo(ref1) | |
>>> # Does not execute foo | |
>>> # Returns cached item by reference, d1 == d2 | |
>>> d2 = foo(ref1) | |
>>> # Different arg, so function foo executes | |
>>> d3 = foo(ref2) | |
Display progress and status | |
# Show a spinner during a process | |
>>> with st.spinner(text="In progress"): | |
>>> time.sleep(3) | |
>>> st.success("Done") | |
# Show and update progress bar | |
>>> bar = st.progress(50) | |
>>> time.sleep(3) | |
>>> bar.progress(100) | |
>>> with st.status("Authenticating...") as s: | |
>>> time.sleep(2) | |
>>> st.write("Some long response.") | |
>>> s.update(label="Response") | |
st.balloons() | |
st.snow() | |
st.toast("Warming up...") | |
st.error("Error message") | |
st.warning("Warning message") | |
st.info("Info message") | |
st.success("Success message") | |
st.exception(e) | |
Personalize apps for users | |
# Show different content based on the user's email address. | |
>>> if st.user.email == "[email protected]": | |
>>> display_jane_content() | |
>>> elif st.user.email == "[email protected]": | |
>>> display_adam_content() | |
>>> else: | |
>>> st.write("Please contact us to get access!") | |
Previous: | |
Changelog | |
Next: | |
Streamlit Community Cloud | |
forum | |
Still have questions? | |
Our forums are full of helpful information and Streamlit experts. | |
Home | |
Contact Us | |
Community | |
Copyright Β© 2024, Streamlit Inc.Cookie policy | |
βββββ | |
Additional Streamlit features - Streamlit Docs | |
https://docs.streamlit.io/get-started/fundamentals/additional-features | |
βββββ | |
st.text_input - Streamlit Docs | |
https://docs.streamlit.io/library/api-reference/widgets/st.text_input | |
β¦ | |
st.text_area - Streamlit Docs | |
https://docs.streamlit.io/library/api-reference/widgets/st.text_area | |
βββββ | |
st.slider - Streamlit Docs | |
https://docs.streamlit.io/library/api-reference/widgets/st.slider | |
Bijvoorbeeld om de temperature voor de LLM in te stellen. | |
βββββ | |
st.radio - Streamlit Docs | |
https://docs.streamlit.io/library/api-reference/widgets/st.radio | |
Bijvoorbeeld om uit een paar mogelijkheden 1 keuze te maken. | |
Misschien enkele talen? | |
Maar bij een keuze uit heel veel opties, gebruik dan: | |
VOOR EEN KEUZE UIT HEEL VEEL TALEN: | |
st.selectbox - Streamlit Docs | |
https://docs.streamlit.io/library/api-reference/widgets/st.selectbox | |
!!!!! !!!! | |
Of eventueel: | |
st.select_slider - Streamlit Docs | |
https://docs.streamlit.io/library/api-reference/widgets/st.select_slider | |
β¦ | |
βββββ | |
Create a multipage app - Streamlit Docs | |
https://docs.streamlit.io/get-started/tutorials/create-a-multipage-app | |
βββββ | |
st.audio - Streamlit Docs | |
https://docs.streamlit.io/library/api-reference/media/st.audio | |
βββββ | |
st.image - Streamlit Docs | |
https://docs.streamlit.io/library/api-reference/media/st.image | |
βββββ | |
st.expander - Streamlit Docs | |
https://docs.streamlit.io/library/api-reference/layout/st.expander | |
Bijvoorbeeld: | |
Om uitklapbare toelichting te geven bij een item. Zoals een overzicht/uitleg bij een prompt/query, of een hele series voorbeeld vragen. | |
Dit zou ook op een apart tabblad gezet kunnen worden: | |
st.tabs - Streamlit Docs | |
https://docs.streamlit.io/library/api-reference/layout/st.tabs | |
βββββ | |
st.sidebar - Streamlit Docs | |
https://docs.streamlit.io/library/api-reference/layout/st.sidebar | |
βββββ | |
st.tabs - Streamlit Docs | |
https://docs.streamlit.io/library/api-reference/layout/st.tabs | |
!!!!! !!!!! | |
βββββ | |
st.status - Streamlit Docs | |
https://docs.streamlit.io/library/api-reference/status/st.status | |
Insert a status container to display output from long-running tasks. | |
βββββ | |