Anne31415 commited on
Commit
eb0eb47
·
1 Parent(s): 8c67e77

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -8
app.py CHANGED
@@ -110,26 +110,57 @@ def page1():
110
  # Add question to chat history
111
  st.session_state.chat_history.append(("User", question))
112
  process_question(question)
 
113
 
114
  # Function to process the question
115
  def process_question(question):
 
 
 
116
  # Add message to the thread
117
- st.session_state.messages = client.beta.threads.messages.create(
118
  thread_id=st.session_state.thread.id,
119
  role="user",
120
  content=question
121
  )
122
-
123
- # Do a run to process the messages in the thread
124
  st.session_state.run = client.beta.threads.runs.create(
125
  thread_id=st.session_state.thread.id,
126
  assistant_id=st.session_state.assistant.id,
127
  )
128
-
129
- # Wait and rerun logic
130
- if st.session_state.retry_error < 3:
131
- time.sleep(1) # Wait 1 second before checking run status
132
- st.rerun()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
 
134
  # Example query buttons
135
  col3, col4 = st.columns(2) # Create two columns for buttons
 
110
  # Add question to chat history
111
  st.session_state.chat_history.append(("User", question))
112
  process_question(question)
113
+ display_chat_history()
114
 
115
  # Function to process the question
116
  def process_question(question):
117
+ # Add user question to chat history
118
+ st.session_state.chat_history.append(("User", question))
119
+
120
  # Add message to the thread
121
+ client.beta.threads.messages.create(
122
  thread_id=st.session_state.thread.id,
123
  role="user",
124
  content=question
125
  )
126
+
127
+ # Create a new run to process the messages in the thread
128
  st.session_state.run = client.beta.threads.runs.create(
129
  thread_id=st.session_state.thread.id,
130
  assistant_id=st.session_state.assistant.id,
131
  )
132
+
133
+ # Wait for the run to complete
134
+ while not hasattr(st.session_state.run, 'status') or st.session_state.run.status not in ["completed", "failed"]:
135
+ time.sleep(1)
136
+ st.session_state.run = client.beta.threads.runs.retrieve(
137
+ thread_id=st.session_state.thread.id,
138
+ run_id=st.session_state.run.id,
139
+ )
140
+
141
+ if st.session_state.run.status == "completed":
142
+ # Retrieve the list of messages
143
+ st.session_state.messages = client.beta.threads.messages.list(
144
+ thread_id=st.session_state.thread.id
145
+ )
146
+
147
+ # Find the last assistant message
148
+ last_assistant_message = next((message for message in reversed(st.session_state.messages.data) if message.role == "assistant"), None)
149
+ if last_assistant_message:
150
+ # Add assistant's response to chat history
151
+ st.session_state.chat_history.append(("Assistant", last_assistant_message.content[0].text.value))
152
+
153
+ # Display the chat history
154
+ display_chat_history()
155
+
156
+ def display_chat_history():
157
+ for role, message in st.session_state.chat_history:
158
+ if role == "User":
159
+ with st.chat_message("User"):
160
+ st.markdown(message)
161
+ elif role == "Assistant":
162
+ with st.chat_message("Assistant"):
163
+ st.markdown(message)
164
 
165
  # Example query buttons
166
  col3, col4 = st.columns(2) # Create two columns for buttons