Want to create interactive content? It’s easy in Genially!
Build a Simple Chatbot Interface With Streamlit and OpenAI's GPT-4 API
Hugh
Created on March 18, 2025
Start designing with a free template
Discover more than 1500 professional designs like these:
Transcript
Build a Simple Chatbot Interface With Streamlit and OpenAI's GPT-4 API
Start
10
Content Overview
Background Information
In this guide, you’ll learn how to create a simple web application using Streamlit that mimics the basic functionalities of ChatGPT. Objectives: - Gain hands-on experience with key Streamlit components (such as st.chat_message and st.chat_input)
- Set up an OpenAI account to obtain your API key, and integrate these elements into your app.
What is Streamlit?
What is OpenAI's API?
+info
Target Audience
This guide is designed for learners with a basic understanding of Python who are interested in web app development and quick prototyping.
10
10
Intro to Streamlit Chat Elements
st.chat_input
Check out this quick video for an introduction to these components:
Displays a chat input widget so the user can type in a message. The streamlit chat input widget is always pinned to the bottom of the screen.
st.chat_message
Inserts a chat message container so that messages can be displayed to the user. A chat message container can contain other streamlit elements, including charts, tables, text, and more.
10
10
Acquiring an OpenAI API Key
Follow the instructions in this video to create your API Key. Save this as we will be using it later.
10
10
Streamlit Environment Setup
1. Ensure you have the latest version of Python installed2. Open terminal and run 'pip install openai streamlit'Ensure the latest versions were installed:
- streamlit >= 1.43
- openai >= 1.66
Test Setup
10
10
Running a Streamlit App
Create new file named app.py and import openai and streamlit:
Confirm that your environment was setup correctly by running the app:
Expected Output
- In your terminal type 'streamlit run app.py'
- Navigate to localhost:8501 on your browser of choice
10
10
Creating Our Chatbot Interface
To implement our chatbot interface, add this block of code using st.chat_input and st.chat_message to your script, changing your OpenAI API Key to the one you got from slide 4
Expected Output
10
10
Optional Feature Additions
Congratulations! You successfully built a chatbot interface with Streamlit and OpenAI's API. Here are some ideas for other features to add, with links to helpful tutorials/documentation:
Implementing Conversation Memory: Consider using Streamlit’s st.session_state to store and display the full conversation history. This would allow your chatbot to remember previous interactions, creating a more coherent dialogue experience.
Enhancing File Upload Capabilities:Explore the additional parameters of st.chat_input, such as accept_file (set to True for a single file or "multiple" to accept several) and file_type to restrict file uploads by type. This new feature (released in the latest version 1.43.0) can allow users to share files along with text messages.
Exploring Additional Streamlit Features:Experiment with other Streamlit components (like interactive widgets or media elements) to enrich your app’s interactivity. Think about integrating features such as dynamic content updates, customizable layouts, or even voice input in future iterations.
10
10
Question 1
Recap Quiz!
Question 3
Question 2
10
10
Tutorial Complete!
What we accomplished: - Successfully built a Streamlit web app that mimics ChatGPT using OpenAI's latest API.
- Leveraged Streamlit’s st.chat_input and st.chat_message widgets to create a dynamic, conversational UI.
- Discussed optional enhancements such as conversation memory and file upload capabilities for future improvements.
10
10
If you have never heard of OpenAI, ChatGPT, or their API, refer to the first 3 minutes of this video:
OpenAI's GPT API
A cloud-based service that leverages advanced natural language processing to generate and interpret text in a human-like manner
OpenAI's application programming interface (API) enables developers to integrate complex text-based features into their apps with minimal effort and cost.
https://platform.openai.com/docs/overview
- Text Generation: Create coherent and contextually appropriate content from provided prompts.
- Conversational Interfaces: Develop interactive chatbots that engage in natural, flowing dialogues.
- Content Summarization: Automatically condense long texts into concise, informative summaries.
- Multilingual Support: Facilitate translation and language detection across a wide range of languages.
What is Streamlit?
Python library designed for fast setup of data analysis and AI/ML web apps
Streamlit allows developers to transform data scripts and model outputs into engaging web applications with minimal coding effort - only basic knowledge of Python is needed.
https://streamlit.io/
- Real-Time Updates: Automatically refresh app content as underlying data changes.
- Customizable Layouts: Design flexible, multi-page apps with embedded media and responsive layouts.
- Interactive Widgets: Easily integrate sliders, buttons, and text inputs for dynamic user interfaces.
- Data Visualization: Seamlessly work with libraries like Matplotlib and Plotly to create compelling visuals.
Example of a user query with the simple chatbot interface.