File size: 1,383 Bytes
10d4187
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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)