owl123 commited on
Commit
bc6f217
1 Parent(s): e1f92ce
Files changed (3) hide show
  1. app.py +34 -17
  2. icon_assistant.png +0 -0
  3. icon_user.png +0 -0
app.py CHANGED
@@ -2,24 +2,41 @@ import openai
2
  import streamlit as st
3
  import json
4
 
5
- openai.api_key = st.secrets["OPENAI_API_KEY"]
6
- if not openai.api_key:
7
- openai.api_key = st.text_input("Your openai api key", placeholder="Input your OpenAI API key here")
8
-
9
- if openai.api_key:
10
- user_prompt = st.text_input("Prompt", placeholder="Ask me anything")
11
 
12
- if user_prompt:
13
- response = openai.ChatCompletion.create(
 
 
 
14
  model="gpt-3.5-turbo",
15
- messages=[
16
- {"role": "system", "content": "You are a helpful assistant."},
17
- {"role": "user", "content": user_prompt},
18
- ],
19
  max_tokens=1000,
20
- temperature=0.7,
21
  )
22
- #print(response)
23
- json_object = json.loads(str(response))
24
- st.write(json_object['choices'][0]['message']['content'])
25
- st.write(json_object['usage'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import streamlit as st
3
  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,
 
 
 
15
  max_tokens=1000,
16
+ temperature=0.7
17
  )
18
+ return response
19
+
20
+ def format_exchanges(exchanges):
21
+ for i in range(len(exchanges)):
22
+ if (i%2) == 0:
23
+ icon, text, blank = st.columns([1,8,2])
24
+ else:
25
+ blank, text, icon = st.columns([2,8,1])
26
+ with icon:
27
+ st.image("icon_" + exchanges[i]["role"] + ".png", width=50)
28
+ with text:
29
+ st.markdown(exchanges[i]["content"])
30
+ st.divider()
31
+
32
+ openai.api_key = st.secrets["OPENAI_API_KEY"]
33
+
34
+ if openai.api_key:
35
+ st.text_input("Prompt", placeholder="Ask me anything", key="prompt")
36
+
37
+ if st.session_state.prompt:
38
+ response = exchange_with_chatbot(st.session_state.prompt)
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)
42
+
icon_assistant.png ADDED
icon_user.png ADDED