Set filter_list as optional param in searxng.py

This commit is contained in:
Que Nguyen 2024-06-17 14:36:56 +07:00 committed by GitHub
parent 3cc0e3ecb6
commit 9c446d9fb4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,9 +1,9 @@
import logging import logging
import requests import requests
from typing import List from typing import List, Optional
from apps.rag.search.main import SearchResult from apps.rag.search.main import SearchResult, get_filtered_results
from config import SRC_LOG_LEVELS from config import SRC_LOG_LEVELS
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -11,7 +11,7 @@ log.setLevel(SRC_LOG_LEVELS["RAG"])
def search_searxng( def search_searxng(
query_url: str, query: str, count: int, whitelist:List[str], **kwargs query_url: str, query: str, count: int, filter_list: Optional[List[str]] = None, **kwargs
) -> List[SearchResult]: ) -> List[SearchResult]:
""" """
Search a SearXNG instance for a given query and return the results as a list of SearchResult objects. Search a SearXNG instance for a given query and return the results as a list of SearchResult objects.
@ -78,10 +78,11 @@ def search_searxng(
json_response = response.json() json_response = response.json()
results = json_response.get("results", []) results = json_response.get("results", [])
sorted_results = sorted(results, key=lambda x: x.get("score", 0), reverse=True) sorted_results = sorted(results, key=lambda x: x.get("score", 0), reverse=True)
filtered_results = filter_by_whitelist(sorted_results, whitelist) if filter_list:
sorted_results = get_filtered_results(sorted_results, whitelist)
return [ return [
SearchResult( SearchResult(
link=result["url"], title=result.get("title"), snippet=result.get("content") link=result["url"], title=result.get("title"), snippet=result.get("content")
) )
for result in filtered_results[:count] for result in sorted_results[:count]
] ]