Spaces:
Sleeping
Sleeping
| from openai import OpenAI | |
| client = OpenAI(api_key='OPENAI_API_KEY') | |
| import streamlit as st | |
| # Add your OpenAI API key here | |
| # Streamlit title | |
| st.title("Data Science Instructor Chatbot") | |
| # System prompt | |
| system_prompt = """ | |
| You are a data science instructor. You help students with topics related to data science, machine learning, and artificial intelligence. | |
| You respond to questions patiently, with clear explanations and a teaching approach. | |
| """ | |
| # Session state to store messages | |
| if 'messages' not in st.session_state: | |
| st.session_state.messages = [] | |
| # Display messages | |
| for message in st.session_state.messages: | |
| with st.chat_message(message['role']): | |
| st.markdown(message['content']) | |
| # Get user input | |
| if prompt := st.chat_input("Type your question here..."): | |
| st.session_state.messages.append({"role": "user", "content": prompt}) | |
| with st.chat_message("user"): | |
| st.markdown(prompt) | |
| # OpenAI API call | |
| response = client.chat.completions.create(model="gpt-3.5-turbo", | |
| messages=[ | |
| {"role": "system", "content": system_prompt}, | |
| *st.session_state.messages | |
| ]) | |
| # Store and display the response | |
| response_message = response.choices[0].message.content | |
| st.session_state.messages.append({"role": "assistant", "content": response_message}) | |
| with st.chat_message("assistant"): | |
| st.markdown(response_message) | |