OAIWrapper/app.py

29 lines
899 B
Python
Raw Permalink Normal View History

2024-06-23 13:53:36 +00:00
import streamlit as st
2024-06-23 14:32:39 +00:00
from openai import OpenAI
2024-06-23 13:53:36 +00:00
2024-06-23 14:32:39 +00:00
st.title("ChatGPT-like clone")
2024-06-23 14:18:59 +00:00
2024-06-23 14:32:39 +00:00
# Set OpenAI API key from Streamlit secrets
2024-06-23 14:38:11 +00:00
client = OpenAI(api_key="sk-oaiwrapper-QpaWSUO8TLerRMJVB7RhT3BlbkFJGSjSx21NfCI6qozxXw2Z")
2024-06-23 14:18:59 +00:00
2024-06-23 14:32:39 +00:00
# Set a default model
if "openai_model" not in st.session_state:
st.session_state["openai_model"] = "gpt-4o"
2024-06-23 13:58:54 +00:00
# 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"])
2024-06-23 14:08:42 +00:00
2024-06-23 14:18:59 +00:00
# Accept user input
2024-06-23 14:08:42 +00:00
if prompt := st.chat_input("What is up?"):
2024-06-23 14:18:59 +00:00
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
2024-06-23 14:08:42 +00:00
# Display user message in chat message container
with st.chat_message("user"):
st.markdown(prompt)