|
|
""" |
|
|
id: research |
|
|
title: Web Research |
|
|
author: admin |
|
|
description: Search the web, fetch top results, and return short summaries. |
|
|
version: 0.1.0 |
|
|
license: Proprietary |
|
|
""" |
|
|
|
|
|
import re |
|
|
|
|
|
try: |
|
|
from bs4 import BeautifulSoup |
|
|
except Exception: |
|
|
BeautifulSoup = None |
|
|
|
|
|
web = None |
|
|
|
|
|
|
|
|
def _strip_html(s: str) -> str: |
|
|
return re.sub("<[^<]+?>", "", s or "") |
|
|
|
|
|
|
|
|
class Tools: |
|
|
def quick(self, query: str, max_results: int = 3, fetch_chars: int = 1200) -> dict: |
|
|
|
|
|
global web |
|
|
if web is None: |
|
|
from open_webui.utils.plugin import load_tool_module_by_id |
|
|
web, _ = load_tool_module_by_id("web_search") |
|
|
|
|
|
|
|
|
res = web.search(query=query, max_results=max_results) |
|
|
out = {"query": query, "hits": []} |
|
|
for hit in res.get("results", [])[:max_results]: |
|
|
url = hit.get("url") |
|
|
title = hit.get("title") |
|
|
try: |
|
|
from open_webui.utils.plugin import load_tool_module_by_id |
|
|
http_mod, _ = load_tool_module_by_id("http") |
|
|
page = http_mod.get(url=url) |
|
|
text = ( |
|
|
_strip_html(page.get("stdout", "")) |
|
|
if not BeautifulSoup |
|
|
else BeautifulSoup(page.get("stdout", ""), "html.parser").get_text( |
|
|
" ", strip=True |
|
|
) |
|
|
) |
|
|
out["hits"].append( |
|
|
{"title": title, "url": url, "preview": text[:fetch_chars]} |
|
|
) |
|
|
except Exception as e: |
|
|
out["hits"].append({"title": title, "url": url, "error": str(e)}) |
|
|
return out |
|
|
|