Sbnos commited on
Commit
d1601e3
·
verified ·
1 Parent(s): 398db91

Check streaming

Browse files

Last good version is the chatgpt

Files changed (1) hide show
  1. app.py +15 -5
app.py CHANGED
@@ -122,7 +122,7 @@ def app():
122
  }
123
  conversational_qa_chain = _inputs | _context | ANSWER_PROMPT | llm
124
 
125
- st.header("Hello Doctor!")
126
  for message in st.session_state.messages:
127
  with st.chat_message(message["role"]):
128
  st.write(message["content"])
@@ -140,19 +140,29 @@ def app():
140
  with st.spinner("Thinking..."):
141
  for _ in range(3): # Retry up to 3 times
142
  try:
143
- response = conversational_qa_chain.invoke(
144
  {
145
  "question": prompts2,
146
  "chat_history": chistory,
147
  }
148
  )
149
- st.write(response)
150
- message = {"role": "assistant", "content": response}
151
- st.session_state.messages.append(message)
 
 
152
  break
153
  except Exception as e:
154
  st.error(f"An error occurred: {e}")
155
  time.sleep(2) # Wait 2 seconds before retrying
156
 
 
 
 
 
 
 
 
 
157
  if __name__ == '__main__':
158
  app()
 
122
  }
123
  conversational_qa_chain = _inputs | _context | ANSWER_PROMPT | llm
124
 
125
+ st.header("Ask Away!")
126
  for message in st.session_state.messages:
127
  with st.chat_message(message["role"]):
128
  st.write(message["content"])
 
140
  with st.spinner("Thinking..."):
141
  for _ in range(3): # Retry up to 3 times
142
  try:
143
+ response_generator = stream_conversational_qa_chain(
144
  {
145
  "question": prompts2,
146
  "chat_history": chistory,
147
  }
148
  )
149
+ message_content = ""
150
+ for response_part in response_generator:
151
+ message_content += response_part
152
+ st.chat_message("assistant", message_content)
153
+ st.session_state.messages.append({"role": "assistant", "content": message_content})
154
  break
155
  except Exception as e:
156
  st.error(f"An error occurred: {e}")
157
  time.sleep(2) # Wait 2 seconds before retrying
158
 
159
+ def stream_conversational_qa_chain(inputs):
160
+ try:
161
+ response = conversational_qa_chain.invoke(inputs)
162
+ for part in response:
163
+ yield part
164
+ except Exception as e:
165
+ raise e
166
+
167
  if __name__ == '__main__':
168
  app()