Spaces:
Running
on
Zero
Running
on
Zero
Improve responsiveness by asynchronously retrieving web search context
Browse files- Run web search lookup in a background thread to decouple it from prompt construction.
- Add a 2-second timeout using thread.join(timeout=2) to prevent delaying response generation.
- Fallback to using only the system prompt and user query if web search results aren't retrieved in time.
- Update sidebar UI to show the fetched context or a "No context found" message.
- Retain current streaming and chat history functionalities for model inference.
app.py
CHANGED
@@ -267,10 +267,20 @@ if user_input:
|
|
267 |
|
268 |
st.session_state.pending_response = True
|
269 |
|
270 |
-
# Retrieve web search context if enabled
|
271 |
retrieved_context = ""
|
272 |
if enable_search:
|
273 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
274 |
with st.sidebar:
|
275 |
st.markdown("### Retrieved Context")
|
276 |
st.text_area("", value=retrieved_context or "No context found.", height=150)
|
|
|
267 |
|
268 |
st.session_state.pending_response = True
|
269 |
|
270 |
+
# Retrieve web search context asynchronously, with a timeout, if enabled
|
271 |
retrieved_context = ""
|
272 |
if enable_search:
|
273 |
+
result_list = []
|
274 |
+
def run_search():
|
275 |
+
result = retrieve_context(user_input, max_results=max_results, max_chars_per_result=max_chars_per_result)
|
276 |
+
result_list.append(result)
|
277 |
+
search_thread = threading.Thread(target=run_search)
|
278 |
+
search_thread.start()
|
279 |
+
# Wait only up to 2 seconds for the search to return
|
280 |
+
search_thread.join(timeout=2)
|
281 |
+
if result_list:
|
282 |
+
retrieved_context = result_list[0]
|
283 |
+
# Display whichever result (or lack thereof) in the sidebar
|
284 |
with st.sidebar:
|
285 |
st.markdown("### Retrieved Context")
|
286 |
st.text_area("", value=retrieved_context or "No context found.", height=150)
|