mirror of
https://github.com/open-webui/open-webui
synced 2025-05-22 13:54:20 +00:00
improve stack trace of duckduckgo exception
* fix search_results out of scope * ddgs.text does already always return a list
This commit is contained in:
parent
66322727e3
commit
337c7caafa
@ -3,6 +3,7 @@ from typing import Optional
|
|||||||
|
|
||||||
from open_webui.retrieval.web.main import SearchResult, get_filtered_results
|
from open_webui.retrieval.web.main import SearchResult, get_filtered_results
|
||||||
from duckduckgo_search import DDGS
|
from duckduckgo_search import DDGS
|
||||||
|
from duckduckgo_search.exceptions import RatelimitException
|
||||||
from open_webui.env import SRC_LOG_LEVELS
|
from open_webui.env import SRC_LOG_LEVELS
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
@ -22,16 +23,15 @@ def search_duckduckgo(
|
|||||||
list[SearchResult]: A list of search results
|
list[SearchResult]: A list of search results
|
||||||
"""
|
"""
|
||||||
# Use the DDGS context manager to create a DDGS object
|
# Use the DDGS context manager to create a DDGS object
|
||||||
|
search_results = []
|
||||||
with DDGS() as ddgs:
|
with DDGS() as ddgs:
|
||||||
# Use the ddgs.text() method to perform the search
|
# Use the ddgs.text() method to perform the search
|
||||||
ddgs_gen = ddgs.text(
|
try:
|
||||||
|
search_results = ddgs.text(
|
||||||
query, safesearch="moderate", max_results=count, backend="api"
|
query, safesearch="moderate", max_results=count, backend="api"
|
||||||
)
|
)
|
||||||
# Check if there are search results
|
except RatelimitException as e:
|
||||||
if ddgs_gen:
|
log.error(f"RatelimitException: {e}")
|
||||||
# Convert the search results into a list
|
|
||||||
search_results = [r for r in ddgs_gen]
|
|
||||||
|
|
||||||
if filter_list:
|
if filter_list:
|
||||||
search_results = get_filtered_results(search_results, filter_list)
|
search_results = get_filtered_results(search_results, filter_list)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user