lring3223 commited on
Commit
999224a
1 Parent(s): ecf35de

Chat app template

Browse files
Files changed (3) hide show
  1. .gitignore +1 -0
  2. app.py +31 -2
  3. requirements.txt +1 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .streamlit/
app.py CHANGED
@@ -1,4 +1,33 @@
 
1
  import streamlit as st
2
 
3
- x = st.slider('Select a value')
4
- st.write(x, 'squared is', x * x)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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})
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ openai