From 604da90fa8670900703a70605abfc576223f2bd7 Mon Sep 17 00:00:00 2001 From: Debanjum Date: Wed, 13 Nov 2024 16:46:09 -0800 Subject: [PATCH] Wrap try/catch around online search in research mode like other tools Previously when call to online search API etc. failed, it'd error out of response to query in research mode. Khoj should skip tool use that iteration but continue to try respond. --- src/khoj/routers/research.py | 45 ++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/src/khoj/routers/research.py b/src/khoj/routers/research.py index fdc10906..26150301 100644 --- a/src/khoj/routers/research.py +++ b/src/khoj/routers/research.py @@ -235,28 +235,29 @@ async def execute_information_collection( if iteration.onlineContext for subquery in iteration.onlineContext.keys() } - async for result in search_online( - this_iteration.query, - construct_tool_chat_history(previous_iterations, ConversationCommand.Online), - location, - user, - send_status_func, - [], - max_webpages_to_read=0, - query_images=query_images, - previous_subqueries=previous_subqueries, - agent=agent, - tracer=tracer, - ): - if isinstance(result, dict) and ChatEvent.STATUS in result: - yield result[ChatEvent.STATUS] - elif is_none_or_empty(result): - this_iteration.warning = ( - "Detected previously run online search queries. Skipping iteration. Try something different." - ) - else: - online_results: Dict[str, Dict] = result # type: ignore - this_iteration.onlineContext = online_results + try: + async for result in search_online( + this_iteration.query, + construct_tool_chat_history(previous_iterations, ConversationCommand.Online), + location, + user, + send_status_func, + [], + max_webpages_to_read=0, + query_images=query_images, + previous_subqueries=previous_subqueries, + agent=agent, + tracer=tracer, + ): + if isinstance(result, dict) and ChatEvent.STATUS in result: + yield result[ChatEvent.STATUS] + elif is_none_or_empty(result): + this_iteration.warning = "Detected previously run online search queries. Skipping iteration. Try something different." + else: + online_results: Dict[str, Dict] = result # type: ignore + this_iteration.onlineContext = online_results + except Exception as e: + logger.error(f"Error searching online: {e}", exc_info=True) elif this_iteration.tool == ConversationCommand.Webpage: try: