Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from openai import OpenAI
|
| 2 |
+
import streamlit as st
|
| 3 |
+
|
| 4 |
+
st.title("ChatGPT-like clone")
|
| 5 |
+
|
| 6 |
+
client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
|
| 7 |
+
|
| 8 |
+
if "openai_model" not in st.session_state:
|
| 9 |
+
st.session_state["openai_model"] = "gpt-3.5-turbo"
|
| 10 |
+
|
| 11 |
+
if "messages" not in st.session_state:
|
| 12 |
+
st.session_state.messages = []
|
| 13 |
+
|
| 14 |
+
for message in st.session_state.messages:
|
| 15 |
+
with st.chat_message(message["role"]):
|
| 16 |
+
st.markdown(message["content"])
|
| 17 |
+
|
| 18 |
+
if prompt := st.chat_input("What is up?"):
|
| 19 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 20 |
+
with st.chat_message("user"):
|
| 21 |
+
st.markdown(prompt)
|
| 22 |
+
|
| 23 |
+
with st.chat_message("assistant"):
|
| 24 |
+
stream = client.chat.completions.create(
|
| 25 |
+
model=st.session_state["openai_model"],
|
| 26 |
+
messages=[
|
| 27 |
+
{"role": m["role"], "content": m["content"]}
|
| 28 |
+
for m in st.session_state.messages
|
| 29 |
+
],
|
| 30 |
+
stream=True,
|
| 31 |
+
)
|
| 32 |
+
response = st.write_stream(stream)
|
| 33 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|