欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > [原创]openwebui解决searxng通过接口请求不成功问题

[原创]openwebui解决searxng通过接口请求不成功问题

2025/8/11 17:20:52 来源:https://blog.csdn.net/jxyk2007/article/details/145900238  浏览:    关键词:[原创]openwebui解决searxng通过接口请求不成功问题

openwebui 对接 searxng 时 无法查询到联网信息,使用bing搜索,每次返回json是正常的

神秘代码:

http://172.30.254.200:8080/search?q=北京市天气&format=json&language=zh&time_range=&safesearch=0&language=zh&locale=zh-Hans-CN&autocomplete=&favicon_resolver=&image_proxy=0&method=POST&safesearch=0&theme=simple&results_on_new_tab=0&doi_resolver=oadoi.org&simple_style=auto&center_alignment=0&advanced_search=0&query_in_title=0&infinite_scroll=0&search_on_category_select=1&hotkeys=default&url_formatting=pretty&disabled_plugins=&enabled_plugins=&tokens=&categories=general&disabled_engines="wikipedia__general\054currency__general\054wikidata__general\054duckduckgo__general\054google__general\054lingva__general\054qwant__general\054startpage__general\054dictzone__general\054mymemory translated__general\054brave__general"&enabled_engines=bing__general

官方教程是这样设置的  非常不稳定,经常搜索不到结果

 searxng.py 源码 调整前

import logging
from typing import Optionalimport requests
from open_webui.retrieval.web.main import SearchResult, get_filtered_results
from open_webui.env import SRC_LOG_LEVELSlog = logging.getLogger(__name__)
log.setLevel(SRC_LOG_LEVELS["RAG"])def search_searxng(query_url: str,query: str,count: int,filter_list: Optional[list[str]] = None,**kwargs,
) -> list[SearchResult]:"""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:query_url (str): The base URL of the SearXNG server.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.safesearch (int): Safe search filter for safer web results; 0 = off, 1 = moderate, 2 = strict. Defaults to 1 (moderate).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."""# Default values for optional parameters are provided as empty strings or None when not specified.language = kwargs.get("language", "en-US")safesearch = kwargs.get("safesearch", "1")time_range = kwargs.get("time_range", "")categories = "".join(kwargs.get("categories", []))params = {"q": query,"format": "json","pageno": 1,"safesearch": safesearch,"language": language,"time_range": time_range,"categories": categories,"theme": "simple","image_proxy": 0,}# Legacy query formatif "<query>" in query_url:# Strip all query parameters from the URLquery_url = query_url.split("?")[0]log.debug(f"searching {query_url}")response = requests.get(query_url,headers={"User-Agent": "Open WebUI (https://github.com/open-webui/open-webui) RAG Bot","Accept": "text/html","Accept-Encoding": "gzip, deflate","Accept-Language": "en-US,en;q=0.5","Connection": "keep-alive",},params=params,)response.raise_for_status()  # Raise an exception for HTTP errors.json_response = response.json()results = json_response.get("results", [])sorted_results = sorted(results, key=lambda x: x.get("score", 0), reverse=True)if filter_list:sorted_results = get_filtered_results(sorted_results, filter_list)return [SearchResult(link=result["url"], title=result.get("title"), snippet=result.get("content"))for result in sorted_results[:count]]

 调整后

import logging
from typing import Optionalimport requests
from open_webui.retrieval.web.main import SearchResult, get_filtered_results
from open_webui.env import SRC_LOG_LEVELSlog = logging.getLogger(__name__)
log.setLevel(SRC_LOG_LEVELS["RAG"])def search_searxng(query_url: str,query: str,count: int,filter_list: Optional[list[str]] = None,**kwargs,
) -> list[SearchResult]:"""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:query_url (str): The base URL of the SearXNG server.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.safesearch (int): Safe search filter for safer web results; 0 = off, 1 = moderate, 2 = strict. Defaults to 1 (moderate).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."""# Default values for optional parameters are provided as empty strings or None when not specified.language = kwargs.get("language", "zh")safesearch = kwargs.get("safesearch", "1")time_range = kwargs.get("time_range", "")categories = "".join(kwargs.get("categories", []))params = {"q": query,"format": "json","pageno": 1,"safesearch": safesearch,"language": language,"time_range": time_range,"categories": categories,"theme": "simple","image_proxy": 0,"locale":"zh-Hans-CN",        "disabled_engines":"wikipedia__general\054currency__general\054wikidata__general\054duckduckgo__general\054google__general\054lingva__general\054qwant__general\054startpage__general\054dictzone__general\054mymemory translated__general\054brave__general","enabled_engines":"bing__general"}# Legacy query formatif "<query>" in query_url:# Strip all query parameters from the URLquery_url = query_url.split("?")[0]log.debug(f"searching {query_url}")response = requests.get(query_url,headers={"User-Agent": "Open WebUI (https://github.com/open-webui/open-webui) RAG Bot","Accept": "text/html","Accept-Encoding": "gzip, deflate","Accept-Language": "en-US,en;q=0.5","Connection": "keep-alive",},params=params,)response.raise_for_status()  # Raise an exception for HTTP errors.json_response = response.json()results = json_response.get("results", [])sorted_results = sorted(results, key=lambda x: x.get("score", 0), reverse=True)if filter_list:sorted_results = get_filtered_results(sorted_results, filter_list)return [SearchResult(link=result["url"], title=result.get("title"), snippet=result.get("content"))for result in sorted_results[:count]]

 改完 看到请求参数

总结   openwebui 对接SearXNG 有bug 我修不来 ,提出的关键词就会被修改掉为什么呢?

接着搞,把搜索关键字写死

 出结果了

 

 实际搜索到网页是正确的,结果就 是不行,是模型问题还是openwebui问题?

搞不来了,放弃 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词