timfe commited on
Commit
613df31
·
1 Parent(s): b2f3ea1

added streamed message

Browse files
Files changed (1) hide show
  1. app.py +11 -2
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import streamlit as st
2
  from openai import OpenAI
3
  import glob
 
4
  import pickle
5
  from langchain_community.vectorstores import Chroma
6
  from langchain_core.output_parsers import StrOutputParser
@@ -153,12 +154,19 @@ if "messages" not in st.session_state:
153
  for msg in st.session_state.messages:
154
  st.chat_message(msg["role"]).write(msg["content"])
155
 
 
 
 
 
 
 
156
  if st.session_state.clicked:
157
  prompt = st.session_state['prompt']
158
  st.chat_message("user").write(prompt)
159
  with get_openai_callback() as cb:
160
  response = rag_chain.invoke(prompt)
161
- st.chat_message("assistant").write(response['answer'])
 
162
  with st.expander("Kontext ansehen"):
163
  for citation in response["context"]:
164
  st.write("[...] ", str(citation.page_content), " [...]")
@@ -173,7 +181,8 @@ if prompt := st.chat_input():
173
  st.chat_message("user").write(prompt)
174
  with get_openai_callback() as cb:
175
  response = rag_chain.invoke(prompt)
176
- st.chat_message("assistant").write(response['answer'])
 
177
  with st.expander("Kontext ansehen"):
178
  for citation in response["context"]:
179
  st.write("[...] ", str(citation.page_content), " [...]")
 
1
  import streamlit as st
2
  from openai import OpenAI
3
  import glob
4
+ import time
5
  import pickle
6
  from langchain_community.vectorstores import Chroma
7
  from langchain_core.output_parsers import StrOutputParser
 
154
  for msg in st.session_state.messages:
155
  st.chat_message(msg["role"]).write(msg["content"])
156
 
157
+ # Streamed response emulator
158
+ def response_generator(response):
159
+ for word in response.split():
160
+ yield word + " "
161
+ time.sleep(0.05)
162
+
163
  if st.session_state.clicked:
164
  prompt = st.session_state['prompt']
165
  st.chat_message("user").write(prompt)
166
  with get_openai_callback() as cb:
167
  response = rag_chain.invoke(prompt)
168
+ response_stream = response_generator(response['answer'])
169
+ st.chat_message("assistant").write_stream(response_stream)
170
  with st.expander("Kontext ansehen"):
171
  for citation in response["context"]:
172
  st.write("[...] ", str(citation.page_content), " [...]")
 
181
  st.chat_message("user").write(prompt)
182
  with get_openai_callback() as cb:
183
  response = rag_chain.invoke(prompt)
184
+ response_stream = response_generator(response['answer'])
185
+ st.chat_message("assistant").write_stream(response_stream)
186
  with st.expander("Kontext ansehen"):
187
  for citation in response["context"]:
188
  st.write("[...] ", str(citation.page_content), " [...]")