vsouza commited on
Commit
ce4ef7a
·
1 Parent(s): d35c922

adding session cache

Browse files
Files changed (2) hide show
  1. app.py +37 -24
  2. requirements.txt +1 -0
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import openai
2
  import streamlit as st
 
3
  import uuid
4
  import time
5
 
@@ -7,38 +8,47 @@ openai.api_key = st.secrets["OPENAI_API_KEY"]
7
  assistant_id = st.secrets["OPENAI_ASSISTANT_ID"]
8
  client = openai
9
 
 
 
 
 
 
 
 
 
 
10
  if "start_chat" not in st.session_state:
11
  st.session_state.start_chat = False
12
  if "session_id" not in st.session_state:
13
  st.session_state.session_id = str(uuid.uuid4())
14
- if "thread_id" not in st.session_state:
15
- st.session_state.thread_id = None
16
  if "messages" not in st.session_state:
17
  st.session_state.messages = []
18
 
19
- st.set_page_config(page_title="Assistant API Chat", page_icon=":speech_balloon:")
20
  st.title(":speech_balloon: Assistant API Chat")
21
 
22
- st.sidebar.caption(f"**Session ID**: \n {st.session_state.session_id}")
23
- st.sidebar.header("Configuration")
24
- thread_id = st.sidebar.text_input("Enter your Thread ID:")
25
-
26
- if st.sidebar.button("Start Chat"):
27
-
28
- st.session_state.start_chat = True
29
- if thread_id:
30
- st.session_state.thread_id = thread_id
31
- else:
32
- thread = client.beta.threads.create(
33
- metadata={
34
- 'session_id': st.session_state.session_id,
35
- }
36
- )
37
- st.session_state.thread_id = thread.id
 
 
38
 
39
- st.session_state.messages = client.beta.threads.messages.list(
40
- thread_id=st.session_state.thread_id
41
- )
 
42
 
43
  #def process_message(message):
44
  # message_content = message.content[0].text
@@ -47,8 +57,11 @@ if st.sidebar.button("Start Chat"):
47
  # full_response = message_content.value + '\n\n' + '\n'.join(citations)
48
  # return full_response
49
 
50
- if st.session_state.start_chat:
51
 
 
 
 
52
  st.caption(f"**Thread ID**: {st.session_state.thread_id}")
53
  for message in reversed(st.session_state.messages.data):
54
  with st.chat_message(message.role):
@@ -86,4 +99,4 @@ if st.session_state.start_chat:
86
  with st.chat_message("assistant"):
87
  st.markdown(message.content[0].text.value)
88
  else:
89
- st.write("Click on 'Start Chat' to start/continue a thread.")
 
1
  import openai
2
  import streamlit as st
3
+ import extra_streamlit_components as stx
4
  import uuid
5
  import time
6
 
 
8
  assistant_id = st.secrets["OPENAI_ASSISTANT_ID"]
9
  client = openai
10
 
11
+ st.set_page_config(page_title="Assistant API Chat", page_icon=":speech_balloon:")
12
+
13
+ @st.cache_resource(experimental_allow_widgets=True)
14
+ def get_manager():
15
+ return stx.CookieManager()
16
+ cookie_manager = get_manager()
17
+
18
+ st.session_state.thread_id = cookie_manager.get('thread_id')
19
+
20
  if "start_chat" not in st.session_state:
21
  st.session_state.start_chat = False
22
  if "session_id" not in st.session_state:
23
  st.session_state.session_id = str(uuid.uuid4())
 
 
24
  if "messages" not in st.session_state:
25
  st.session_state.messages = []
26
 
 
27
  st.title(":speech_balloon: Assistant API Chat")
28
 
29
+ with st.sidebar:
30
+ st.caption(f"**Session ID**: \n {st.session_state.session_id}")
31
+ st.header("Configuration")
32
+ thread_id = st.sidebar.text_input("Enter your Thread ID:", placeholder="Leave empty to start a new thread")
33
+ c1, c2 = st.columns(2)
34
+
35
+ if c1.button("Start Chat", use_container_width=True):
36
+ st.session_state.start_chat = True
37
+ if thread_id:
38
+ st.session_state.thread_id = thread_id
39
+ else:
40
+ thread = client.beta.threads.create(
41
+ metadata={
42
+ 'session_id': st.session_state.session_id,
43
+ }
44
+ )
45
+ st.session_state.thread_id = thread.id
46
+ cookie_manager.set('thread_id', st.session_state.thread_id)
47
 
48
+ if c2.button("Clear Chat", use_container_width=True):
49
+ st.session_state.start_chat = False
50
+ st.session_state.thread_id = None
51
+ cookie_manager.delete('thread_id')
52
 
53
  #def process_message(message):
54
  # message_content = message.content[0].text
 
57
  # full_response = message_content.value + '\n\n' + '\n'.join(citations)
58
  # return full_response
59
 
60
+ if st.session_state.thread_id:
61
 
62
+ st.session_state.messages = client.beta.threads.messages.list(
63
+ thread_id=st.session_state.thread_id
64
+ )
65
  st.caption(f"**Thread ID**: {st.session_state.thread_id}")
66
  for message in reversed(st.session_state.messages.data):
67
  with st.chat_message(message.role):
 
99
  with st.chat_message("assistant"):
100
  st.markdown(message.content[0].text.value)
101
  else:
102
+ st.write("Click on 'Start Chat' to start a new thread.")
requirements.txt CHANGED
@@ -1,2 +1,3 @@
 
1
  streamlit==1.28.2
2
  openai==1.2.4
 
1
+ extra_streamlit_components==0.1.60
2
  streamlit==1.28.2
3
  openai==1.2.4