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 lines267268269270270271272273274273275276277278279280281282283284285286266 unmodified lines st.session_state.pending_response = True # Retrieve web search context if enabled # Retrieve web search context asynchronously, with a timeout, if enabled retrieved_context = "" if enable_search: result_list = [] def run_search(): retrieved_context = retrieve_context(user_input, max_results=max_results, max_chars_per_result=max_chars_per_result) 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 return search_thread.join(timeout=2) if result_list: retrieved_context = result_list[0] # Display whichever result (or lack thereof) in the sidebar with st.sidebar: st.markdown("### Retrieved Context") st.text_area("", value=retrieved_context or "No context found.", height=150)app.py+12-2