├── requirements.txt ├── .streamlit └── config.toml ├── README.md └── streamlit_app.py /requirements.txt: -------------------------------------------------------------------------------- 1 | streamlit 2 | openai 3 | -------------------------------------------------------------------------------- /.streamlit/config.toml: -------------------------------------------------------------------------------- 1 | [theme] 2 | primaryColor="#F63366" 3 | backgroundColor="#FFFFFF" 4 | secondaryBackgroundColor="#F0F2F6" 5 | textColor="#262730" 6 | font="sans serif" 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 🤖💬 OpenAI Chatbot 2 | 3 | A conversational chatbot built in Python using Streamlit and the OpenAI LLM model GPT 3.5. 4 | 5 | ## Demo App 6 | 7 | [![Streamlit App](https://static.streamlit.io/badges/streamlit_badge_black_white.svg)](https://openai-chatbot.streamlit.app/) 8 | 9 | ## Prerequisite libraries 10 | 11 | ``` 12 | streamlit 13 | openai 14 | ``` 15 | 16 | ## Get an OpenAI API key 17 | 18 | You can get your own OpenAI API key by following the following instructions: 19 | 1. Go to https://platform.openai.com/account/api-keys. 20 | 2. Click on the `+ Create new secret key` button. 21 | 3. Next, enter an identifier name (optional) and click on the `Create secret key` button. 22 | 23 | ## Further Reading 24 | 25 | - 🛠️ [Streamlit Documentation Tutorial on _**Build conversational apps**_](https://docs.streamlit.io/knowledge-base/tutorials/build-conversational-apps) 26 | - 📖 [Streamlit Documentation on _**Chat elements**_](https://docs.streamlit.io/library/api-reference/chat) 27 | -------------------------------------------------------------------------------- /streamlit_app.py: -------------------------------------------------------------------------------- 1 | # Code refactored from https://docs.streamlit.io/knowledge-base/tutorials/build-conversational-apps 2 | 3 | import openai 4 | import streamlit as st 5 | 6 | with st.sidebar: 7 | st.title('🤖💬 OpenAI Chatbot') 8 | if 'OPENAI_API_KEY' in st.secrets: 9 | st.success('API key already provided!', icon='✅') 10 | openai.api_key = st.secrets['OPENAI_API_KEY'] 11 | else: 12 | openai.api_key = st.text_input('Enter OpenAI API token:', type='password') 13 | if not (openai.api_key.startswith('sk-') and len(openai.api_key)==51): 14 | st.warning('Please enter your credentials!', icon='⚠️') 15 | else: 16 | st.success('Proceed to entering your prompt message!', icon='👉') 17 | 18 | if "messages" not in st.session_state: 19 | st.session_state.messages = [] 20 | 21 | for message in st.session_state.messages: 22 | with st.chat_message(message["role"]): 23 | st.markdown(message["content"]) 24 | 25 | if prompt := st.chat_input("What is up?"): 26 | st.session_state.messages.append({"role": "user", "content": prompt}) 27 | with st.chat_message("user"): 28 | st.markdown(prompt) 29 | with st.chat_message("assistant"): 30 | message_placeholder = st.empty() 31 | full_response = "" 32 | for response in openai.ChatCompletion.create( 33 | model="gpt-3.5-turbo", 34 | messages=[{"role": m["role"], "content": m["content"]} 35 | for m in st.session_state.messages], stream=True): 36 | full_response += response.choices[0].delta.get("content", "") 37 | message_placeholder.markdown(full_response + "▌") 38 | message_placeholder.markdown(full_response) 39 | st.session_state.messages.append({"role": "assistant", "content": full_response}) 40 | --------------------------------------------------------------------------------