Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
open-webui
Commits
3729771f
Unverified
Commit
3729771f
authored
Jun 03, 2024
by
Timothy Jaeryang Baek
Committed by
GitHub
Jun 03, 2024
Browse files
Merge pull request #2760 from nirabo/fix/searxng
Fix/searxng
parents
ab58947a
08443b3c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
49 additions
and
14 deletions
+49
-14
backend/apps/rag/search/searxng.py
backend/apps/rag/search/searxng.py
+49
-14
No files found.
backend/apps/rag/search/searxng.py
View file @
3729771f
import
logging
import
logging
import
requests
import
requests
from
typing
import
List
from
apps.rag.search.main
import
SearchResult
from
apps.rag.search.main
import
SearchResult
from
config
import
SRC_LOG_LEVELS
from
config
import
SRC_LOG_LEVELS
...
@@ -9,20 +10,52 @@ log = logging.getLogger(__name__)
...
@@ -9,20 +10,52 @@ log = logging.getLogger(__name__)
log
.
setLevel
(
SRC_LOG_LEVELS
[
"RAG"
])
log
.
setLevel
(
SRC_LOG_LEVELS
[
"RAG"
])
def
search_searxng
(
query_url
:
str
,
query
:
str
,
count
:
int
)
->
list
[
SearchResult
]:
def
search_searxng
(
query_url
:
str
,
query
:
str
,
count
:
int
,
**
kwargs
)
->
List
[
SearchResult
]:
"""Search a SearXNG instance for a 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.
The function allows passing additional parameters such as language or time_range to tailor the search result.
Args:
Args:
query_url (str): The URL of the SearXNG instance to search. Must contain "<query>" as a placeholder
query_url (str): The base URL of the SearXNG server with a placeholder for the query "<query>".
query (str): The query to search for
query (str): The search term or question to find in the SearXNG database.
count (int): The maximum number of results to retrieve from the search.
Keyword Args:
language (str): Language filter for the search results; e.g., "en-US". Defaults to an empty string.
time_range (str): Time range for filtering results by date; e.g., "2023-04-05..today" or "all-time". Defaults to ''.
categories: (Optional[List[str]]): Specific categories within which the search should be performed, defaulting to an empty string if not provided.
Returns:
List[SearchResult]: A list of SearchResults sorted by relevance score in descending order.
Raise:
requests.exceptions.RequestException: If a request error occurs during the search process.
"""
"""
url
=
query_url
.
replace
(
"<query>"
,
query
)
if
"&format=json"
not
in
url
:
# Default values for optional parameters are provided as empty strings or None when not specified.
url
+=
"&format=json"
language
=
kwargs
.
get
(
'language'
,
'en-US'
)
log
.
debug
(
f
"searching
{
url
}
"
)
time_range
=
kwargs
.
get
(
'time_range'
,
''
)
categories
=
''
.
join
(
kwargs
.
get
(
'categories'
,
[]))
params
=
{
"q"
:
query
,
"format"
:
"json"
,
"pageno"
:
1
,
"results_per_page"
:
count
,
'language'
:
language
,
'time_range'
:
time_range
,
'engines'
:
''
,
'categories'
:
categories
,
'theme'
:
'simple'
,
'image_proxy'
:
0
r
=
requests
.
get
(
}
url
,
log
.
debug
(
f
"searching
{
query_url
}
"
)
response
=
requests
.
get
(
query_url
,
headers
=
{
headers
=
{
"User-Agent"
:
"Open WebUI (https://github.com/open-webui/open-webui) RAG Bot"
,
"User-Agent"
:
"Open WebUI (https://github.com/open-webui/open-webui) RAG Bot"
,
"Accept"
:
"text/html"
,
"Accept"
:
"text/html"
,
...
@@ -30,15 +63,17 @@ def search_searxng(query_url: str, query: str, count: int) -> list[SearchResult]
...
@@ -30,15 +63,17 @@ def search_searxng(query_url: str, query: str, count: int) -> list[SearchResult]
"Accept-Language"
:
"en-US,en;q=0.5"
,
"Accept-Language"
:
"en-US,en;q=0.5"
,
"Connection"
:
"keep-alive"
,
"Connection"
:
"keep-alive"
,
},
},
params
=
params
,
)
)
r
.
raise_for_status
()
json_response
=
r
.
json
()
response
.
raise_for_status
()
# Raise an exception for HTTP errors.
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
)
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
sorted_results
[:
count
]
for
result
in
sorted_results
]
]
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment