From 95da0734b63b97b339ff3680aa9e7609c105e309 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Tue, 24 Dec 2024 17:56:46 -0700 Subject: [PATCH] refac --- backend/open_webui/routers/retrieval.py | 2 +- backend/open_webui/utils/middleware.py | 26 ++++++++++++++++--------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/backend/open_webui/routers/retrieval.py b/backend/open_webui/routers/retrieval.py index e31d5c01d..4444581f5 100644 --- a/backend/open_webui/routers/retrieval.py +++ b/backend/open_webui/routers/retrieval.py @@ -1238,7 +1238,7 @@ def search_web(request: Request, engine: str, query: str) -> list[SearchResult]: @router.post("/process/web/search") -async def process_web_search( +def process_web_search( request: Request, form_data: SearchForm, user=Depends(get_verified_user) ): try: diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index 078c1e07e..9e55596da 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -9,6 +9,7 @@ import random import json import inspect from uuid import uuid4 +from concurrent.futures import ThreadPoolExecutor from fastapi import Request @@ -412,15 +413,22 @@ async def chat_web_search_handler( ) try: - results = await process_web_search( - request, - SearchForm( - **{ - "query": searchQuery, - } - ), - user, - ) + + # Offload process_web_search to a separate thread + loop = asyncio.get_running_loop() + with ThreadPoolExecutor() as executor: + results = await loop.run_in_executor( + executor, + lambda: process_web_search( + request, + SearchForm( + **{ + "query": searchQuery, + } + ), + user, + ), + ) if results: await event_emitter(