Add openai integration
This commit is contained in:
parent
bc9f41bc63
commit
07532752a6
1
.streamlit/secrets.toml
Normal file
1
.streamlit/secrets.toml
Normal file
@ -0,0 +1 @@
|
|||||||
|
OPENAI_API_KEY = "sk-oaiwrapper-QpaWSUO8TLerRMJVB7RhT3BlbkFJGSjSx21NfCI6qozxXw2Z"
|
29
app.py
29
app.py
@ -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})
|
|
Loading…
Reference in New Issue
Block a user