import streamlit as st import os from openai import OpenAI class ChatBot: def __init__(self): self.client = OpenAI(api_key=os.environ["OPENAI_API_KEY"]) self.history = [{"role": "system", "content": "You are a helpful assistant."}] def generate_response(self, prompt: str) -> str: self.history.append({"role": "user", "content": prompt}) completion = self.client.chat.completions.create( model="gpt-4o", messages=self.history ) response = completion.choices[0].message.content self.history.append({"role": "assistant", "content": response}) return response def get_history(self) -> list: return self.history st.set_page_config(layout="wide") st.title("OpenAI GPT-4o 🤖") with st.sidebar: with st.expander("Instruction Manual"): st.markdown(""" ## OpenAI GPT-4o 🤖 Chatbot This Streamlit app allows you to chat with GPT-4o model. ### How to Use: 1. **Input**: Type your prompt into the chat input box labeled "What is up?". 2. **Response**: The app will display a response from GPT-4o. 3. **Chat History**: Previous conversations will be shown on the app. ### Credits: - **Developer**: [Yiqiao Yin](https://www.y-yin.io/) | [App URL](https://huggingface.co/spaces/eagle0504/meta-llama) | [LinkedIn](https://www.linkedin.com/in/yiqiaoyin/) | [YouTube](https://youtube.com/YiqiaoYin/) Enjoy chatting with Meta's Llama3 model! """) # Example: st.success("Example: Explain what is supervised learning.") st.success("Example: What is large language model?") st.success("Example: How to conduct an AI experiment?") st.success("Example: Write a tensorflow flow code with a 3-layer neural network model.") # Add a button to clear the session state if st.button("Clear Session"): st.session_state.messages = [] st.session_state["bot"] = None st.experimental_rerun() # 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 bot = ChatBot() if bot: st.session_state["bot"] = bot if prompt := st.chat_input("😉 Ask any question or feel free to use the examples provided in the left sidebar."): # 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}) # API Call response = bot.generate_response("What is the weather like today?") # 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})