Spaces:
Runtime error
Runtime error
Commit ·
acda3f1
1
Parent(s): 12731c2
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.
266 unmodified lines267268269270271272273274275276266 unmodified linesst.session_state.pending_response = True# Retrieve web search context if enabledretrieved_context = ""if enable_search:retrieved_context = retrieve_context(user_input, max_results=max_results, max_chars_per_result=max_chars_per_result)with st.sidebar:st.markdown("### Retrieved Context")st.text_area("", value=retrieved_context or "No context found.", height=150)266 unmodified lines267268269270271272273274275276277278279280281282283284285286266 unmodified linesst.session_state.pending_response = True# Retrieve web search context asynchronously, with a timeout, if enabledretrieved_context = ""if enable_search:result_list = []def run_search():result = retrieve_context(user_input, max_results=max_results, max_chars_per_result=max_chars_per_result)result_list.append(result)search_thread = threading.Thread(target=run_search)search_thread.start()# Wait only up to 2 seconds for the search to returnsearch_thread.join(timeout=2)if result_list:retrieved_context = result_list[0]# Display whichever result (or lack thereof) in the sidebarwith st.sidebar:st.markdown("### Retrieved Context")st.text_area("", value=retrieved_context or "No context found.", height=150)
app.py+12-2