Streamlit ChatGPT Chatbot
This repository contains a simple chatbot created with Streamlit and OpenAI's ChatGPT model.
Installation
To install the required packages, run the following command:
pip install -r requirements.txt
Usage
To run the app, use the following command:
streamlit run app.py
Then, enter a message in the text box and press Enter or Return to send the message. The chatbot will respond with a message generated by OpenAI's ChatGPT model.
The chat history will be displayed on the screen, and new messages will be added to the history as they are sent and received.
Code Explanation
The app.py
file contains the code for the chatbot. The openai
and streamlit
packages are imported at the beginning of the file.
The execute_openai
function is defined to execute the OpenAI command and add the result into the chat history. It takes the user's input from the st.session_state.input
variable, sends it to the OpenAI API using the openai.ChatCompletion.create
method, and adds the response to the chat history.
The print_history
function is defined to print the chat history to the screen. It iterates over the chat history stored in the st.session_state.history
variable and displays each message using the streamlit_chat.message
function.
Finally, the main Streamlit app is defined using the st.text_input
method to create a text box for the user to input messages. The key
parameter is set to "input"
to ensure that the input is stored in the st.session_state.input
variable, and the on_change
parameter is set to the execute_openai
function to execute the OpenAI command when the user presses Enter or Return.