当前位置: 首页 > news >正文

[原创]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问题?

搞不来了,放弃 

http://www.lryc.cn/news/544011.html

相关文章:

  • Jmeter聚合报告导出log文档,Jmeter聚合报告导出到CSV
  • mysqldump 参数详解
  • DeepSeek R1 简易指南:架构、本地部署和硬件要求
  • 基于 ‌MySQL 数据库‌对三级视图(用户视图、DBA视图、内部视图)的详细解释
  • [Web 信息收集] Web 信息收集 — 手动收集 IP 信息
  • 跨AWS账户共享SQS队列以实现消息传递
  • DeepSeek 202502 开源周合集
  • springai系列(二)从0开始搭建和接入azure-openai实现智能问答
  • Apache部署Vue操作手册(SSL部分)
  • 人类驾驶的人脑两种判断模式(反射和预判)-->自动驾驶两种AI模式
  • Docker和K8S中pod、services、container的介绍和关系
  • 【uniapp】在UniApp中实现持久化存储:安卓--生成写入数据为jsontxt
  • DeepSeek-R1本地部署保姆级教程
  • Python常见面试题的详解25
  • DeepSeek赋能大模型内容安全,网易易盾AIGC内容风控解决方案三大升级
  • 阿里开源正式开园文生视频、图生视频模型-通义万相 WanX2.1
  • 【Python爬虫(73)】用Python爬虫开启交通数据宝藏,畅行出行未来
  • 和鲸科技携手四川气象,以 AI 的力量赋能四川气象一体化平台建设
  • spring boot 2.7 + seata +微服务 降级失败问题修复
  • python-leetcode-最长公共子序列
  • centos 7 停更后如何升级kernel版本 —— 筑梦
  • WPF-3天快速WPF入门并达到企业级水准
  • 爬虫反爬:CSS位置偏移反爬案例分析与实战案例
  • Ubuntu20.04安装Redis
  • Ubuntu 22.04 安装Nvidia驱动加速deepseek
  • OkHttp、Retrofit、RxJava:一文讲清楚
  • 星环科技推出DeepSeek全场景解决方案:即开即用、企业级部署、端侧智能三位一体
  • Redis缓存一致性难题:如何让数据库和缓存不“打架”?
  • 动态部署Web应用程序与web.xml配置详解
  • 2025年软考报名费用是多少?全国费用汇总!