owl123
commited on
Commit
•
5d863e9
1
Parent(s):
d412f15
Fixed bug with longer exchanges
Browse files
app.py
CHANGED
@@ -4,11 +4,12 @@ import json
|
|
4 |
|
5 |
if 'exchanges' not in st.session_state:
|
6 |
st.session_state.exchanges = []
|
|
|
7 |
|
8 |
def exchange_with_chatbot(prompt):
|
9 |
st.session_state.exchanges.append({"role": "user", "content": prompt})
|
10 |
# limit to 10 exchanges to save cost
|
11 |
-
ex = st.session_state.exchanges if len(st.session_state.exchanges)<10 else st.session_state.exchanges[len(exchanges-10
|
12 |
response = openai.ChatCompletion.create(
|
13 |
model="gpt-3.5-turbo",
|
14 |
messages= ex,
|
@@ -19,10 +20,14 @@ def exchange_with_chatbot(prompt):
|
|
19 |
|
20 |
def format_exchanges(exchanges):
|
21 |
for i in range(len(exchanges)):
|
22 |
-
if
|
23 |
-
icon, text, blank = st.columns([1,8,
|
|
|
|
|
24 |
else:
|
25 |
-
|
|
|
|
|
26 |
with icon:
|
27 |
st.image("icon_" + exchanges[i]["role"] + ".png", width=50)
|
28 |
with text:
|
@@ -35,7 +40,11 @@ if openai.api_key:
|
|
35 |
st.text_input("Prompt", placeholder="Ask me anything", key="prompt")
|
36 |
|
37 |
if st.session_state.prompt:
|
38 |
-
|
|
|
|
|
|
|
|
|
39 |
json_object = json.loads(str(response))
|
40 |
st.session_state.exchanges.append({"role": "assistant", "content": json_object['choices'][0]['message']['content']})
|
41 |
format_exchanges(st.session_state.exchanges)
|
|
|
4 |
|
5 |
if 'exchanges' not in st.session_state:
|
6 |
st.session_state.exchanges = []
|
7 |
+
#st.session_state.exchanges = [{"role": "system", "content": "You are a chatbot."}]
|
8 |
|
9 |
def exchange_with_chatbot(prompt):
|
10 |
st.session_state.exchanges.append({"role": "user", "content": prompt})
|
11 |
# limit to 10 exchanges to save cost
|
12 |
+
ex = st.session_state.exchanges if len(st.session_state.exchanges)<10 else st.session_state.exchanges[len(st.session_state.exchanges)-10:]
|
13 |
response = openai.ChatCompletion.create(
|
14 |
model="gpt-3.5-turbo",
|
15 |
messages= ex,
|
|
|
20 |
|
21 |
def format_exchanges(exchanges):
|
22 |
for i in range(len(exchanges)):
|
23 |
+
if exchanges[i]["role"] == "user":
|
24 |
+
icon, text, blank = st.columns([1,8,1])
|
25 |
+
elif exchanges[i]["role"] == "assistant":
|
26 |
+
blank, text, icon = st.columns([1,8,1])
|
27 |
else:
|
28 |
+
st.markdown("*" + exchanges[i]["role"] + ":* " + exchanges[i]["content"])
|
29 |
+
continue
|
30 |
+
|
31 |
with icon:
|
32 |
st.image("icon_" + exchanges[i]["role"] + ".png", width=50)
|
33 |
with text:
|
|
|
40 |
st.text_input("Prompt", placeholder="Ask me anything", key="prompt")
|
41 |
|
42 |
if st.session_state.prompt:
|
43 |
+
try:
|
44 |
+
response = exchange_with_chatbot(st.session_state.prompt)
|
45 |
+
except Exception as e:
|
46 |
+
st.error(e)
|
47 |
+
st.stop()
|
48 |
json_object = json.loads(str(response))
|
49 |
st.session_state.exchanges.append({"role": "assistant", "content": json_object['choices'][0]['message']['content']})
|
50 |
format_exchanges(st.session_state.exchanges)
|