Add openai integration

This commit is contained in:
Shaquille Soekhlal 2024-06-23 14:32:39 +00:00
parent bc9f41bc63
commit 07532752a6
2 changed files with 8 additions and 22 deletions

1
.streamlit/secrets.toml Normal file
View File

@ -0,0 +1 @@
OPENAI_API_KEY = "sk-oaiwrapper-QpaWSUO8TLerRMJVB7RhT3BlbkFJGSjSx21NfCI6qozxXw2Z"

29
app.py
View File

@ -1,23 +1,14 @@
import streamlit as st import streamlit as st
import random from openai import OpenAI
import time
st.title("ChatGPT-like clone")
# Streamed response emulator # Set OpenAI API key from Streamlit secrets
def response_generator(): client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
response = random.choice(
[
"Hello there! How can I assist you today?",
"Hi, human! Is there anything I can help you with?",
"Do you need help?",
]
)
for word in response.split():
yield word + " "
time.sleep(0.05)
# Set a default model
st.title("Simple chat") if "openai_model" not in st.session_state:
st.session_state["openai_model"] = "gpt-4o"
# Initialize chat history # Initialize chat history
if "messages" not in st.session_state: if "messages" not in st.session_state:
@ -35,9 +26,3 @@ if prompt := st.chat_input("What is up?"):
# Display user message in chat message container # Display user message in chat message container
with st.chat_message("user"): with st.chat_message("user"):
st.markdown(prompt) st.markdown(prompt)
# Display assistant response in chat message container
with st.chat_message("assistant"):
response = st.write_stream(response_generator())
# Add assistant response to chat history
st.session_state.messages.append({"role": "assistant", "content": response})