mirror of
https://github.com/open-webui/open-webui.git
synced 2026-11-15 18:55:07 +03:00
29b4af6b98
Knowledge searches wrote the names and sources of the files they found to the server log at info level, and the SerpApi, Serply, Firecrawl, Yandex, Kagi, Mojeek and external web search engines wrote their whole results there, snippets included. Anyone who can read the logs could see what users looked up and what the model was given. These lines now log at debug, the same as the search queries, so they only show with GLOBAL_LOG_LEVEL=DEBUG. Fixes #34129
47 lines
1.2 KiB
Python
47 lines
1.2 KiB
Python
import logging
|
|
from typing import Optional
|
|
from urllib.parse import urlencode
|
|
|
|
import requests
|
|
from open_webui.retrieval.web.main import SearchResult, get_filtered_results
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
def search_serpapi(
|
|
api_key: str,
|
|
engine: str,
|
|
query: str,
|
|
count: int,
|
|
filter_list: Optional[list[str]] = None,
|
|
) -> list[SearchResult]:
|
|
"""Search using serpapi.com's API and return the results as a list of SearchResult objects.
|
|
|
|
Args:
|
|
api_key (str): A serpapi.com API key
|
|
query (str): The query to search for
|
|
"""
|
|
url = 'https://serpapi.com/search'
|
|
|
|
engine = engine or 'google'
|
|
|
|
payload = {'engine': engine, 'q': query, 'api_key': api_key}
|
|
|
|
url = f'{url}?{urlencode(payload)}'
|
|
response = requests.request('GET', url)
|
|
|
|
json_response = response.json()
|
|
log.debug('results from serpapi search: %s', json_response)
|
|
|
|
results = sorted(json_response.get('organic_results', []), key=lambda x: x.get('position', 0))
|
|
if filter_list:
|
|
results = get_filtered_results(results, filter_list)
|
|
return [
|
|
SearchResult(
|
|
link=result['link'],
|
|
title=result.get('title'),
|
|
snippet=result.get('snippet'),
|
|
)
|
|
for result in results[:count]
|
|
]
|