mirror of
https://github.com/open-webui/open-webui.git
synced 2025-12-13 12:55:19 +00:00
enh: optionally add user headers external websearch
Co-Authored-By: Classic298 <27028174+Classic298@users.noreply.github.com>
This commit is contained in:
parent
9578bac099
commit
25c7f101f2
2 changed files with 25 additions and 6 deletions
|
|
@ -2,27 +2,42 @@ import logging
|
||||||
from typing import Optional, List
|
from typing import Optional, List
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from open_webui.retrieval.web.main import SearchResult, get_filtered_results
|
|
||||||
|
from fastapi import Request
|
||||||
|
|
||||||
from open_webui.env import SRC_LOG_LEVELS
|
from open_webui.env import SRC_LOG_LEVELS
|
||||||
|
|
||||||
|
from open_webui.retrieval.web.main import SearchResult, get_filtered_results
|
||||||
|
from open_webui.utils.headers import include_user_info_headers
|
||||||
|
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
log.setLevel(SRC_LOG_LEVELS["RAG"])
|
log.setLevel(SRC_LOG_LEVELS["RAG"])
|
||||||
|
|
||||||
|
|
||||||
def search_external(
|
def search_external(
|
||||||
|
request: Request,
|
||||||
external_url: str,
|
external_url: str,
|
||||||
external_api_key: str,
|
external_api_key: str,
|
||||||
query: str,
|
query: str,
|
||||||
count: int,
|
count: int,
|
||||||
filter_list: Optional[List[str]] = None,
|
filter_list: Optional[List[str]] = None,
|
||||||
|
user=None,
|
||||||
) -> List[SearchResult]:
|
) -> List[SearchResult]:
|
||||||
try:
|
try:
|
||||||
|
headers = {
|
||||||
|
"User-Agent": "Open WebUI (https://github.com/open-webui/open-webui) RAG Bot",
|
||||||
|
"Authorization": f"Bearer {external_api_key}",
|
||||||
|
}
|
||||||
|
headers = include_user_info_headers(headers, user)
|
||||||
|
|
||||||
|
chat_id = getattr(request.state, "chat_id", None)
|
||||||
|
if chat_id:
|
||||||
|
headers["X-OpenWebUI-Chat-Id"] = str(chat_id)
|
||||||
|
|
||||||
response = requests.post(
|
response = requests.post(
|
||||||
external_url,
|
external_url,
|
||||||
headers={
|
headers=headers,
|
||||||
"User-Agent": "Open WebUI (https://github.com/open-webui/open-webui) RAG Bot",
|
|
||||||
"Authorization": f"Bearer {external_api_key}",
|
|
||||||
},
|
|
||||||
json={
|
json={
|
||||||
"query": query,
|
"query": query,
|
||||||
"count": count,
|
"count": count,
|
||||||
|
|
|
||||||
|
|
@ -1811,7 +1811,9 @@ def process_web(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def search_web(request: Request, engine: str, query: str) -> list[SearchResult]:
|
def search_web(
|
||||||
|
request: Request, engine: str, query: str, user=None
|
||||||
|
) -> list[SearchResult]:
|
||||||
"""Search the web using a search engine and return the results as a list of SearchResult objects.
|
"""Search the web using a search engine and return the results as a list of SearchResult objects.
|
||||||
Will look for a search engine API key in environment variables in the following order:
|
Will look for a search engine API key in environment variables in the following order:
|
||||||
- SEARXNG_QUERY_URL
|
- SEARXNG_QUERY_URL
|
||||||
|
|
@ -2069,11 +2071,13 @@ def search_web(request: Request, engine: str, query: str) -> list[SearchResult]:
|
||||||
)
|
)
|
||||||
elif engine == "external":
|
elif engine == "external":
|
||||||
return search_external(
|
return search_external(
|
||||||
|
request,
|
||||||
request.app.state.config.EXTERNAL_WEB_SEARCH_URL,
|
request.app.state.config.EXTERNAL_WEB_SEARCH_URL,
|
||||||
request.app.state.config.EXTERNAL_WEB_SEARCH_API_KEY,
|
request.app.state.config.EXTERNAL_WEB_SEARCH_API_KEY,
|
||||||
query,
|
query,
|
||||||
request.app.state.config.WEB_SEARCH_RESULT_COUNT,
|
request.app.state.config.WEB_SEARCH_RESULT_COUNT,
|
||||||
request.app.state.config.WEB_SEARCH_DOMAIN_FILTER_LIST,
|
request.app.state.config.WEB_SEARCH_DOMAIN_FILTER_LIST,
|
||||||
|
user=user,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
raise Exception("No search engine API key found in environment variables")
|
raise Exception("No search engine API key found in environment variables")
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue