mirror of
https://github.com/open-webui/computer.git
synced 2026-11-22 21:50:43 -05:00
refac
This commit is contained in:
1 parent
5b8139c1a6
commit
03601a522c
4 files changed
+118
-1
No files matched your search
@@ -21,6 +21,7 @@
|
||||
let perplexityBaseUrl = $state('');
|
||||
let firecrawlSearchKey = $state('');
|
||||
let firecrawlSearchBaseUrl = $state('https://api.firecrawl.dev');
|
||||
let searxngBaseUrl = $state('');
|
||||
let ccKey = $state('');
|
||||
let ccBaseUrl = $state('');
|
||||
let ccModel = $state('');
|
||||
@@ -51,6 +52,7 @@
|
||||
firecrawlSearchKey = (config['web.firecrawl_api_key'] as string) || '';
|
||||
firecrawlSearchBaseUrl =
|
||||
(config['web.firecrawl_base_url'] as string) || 'https://api.firecrawl.dev';
|
||||
searxngBaseUrl = (config['web.searxng_base_url'] as string) || '';
|
||||
ccKey = (config['web.chat_completions_api_key'] as string) || '';
|
||||
ccBaseUrl = (config['web.chat_completions_base_url'] as string) || '';
|
||||
ccModel = (config['web.chat_completions_model'] as string) || '';
|
||||
@@ -87,6 +89,7 @@
|
||||
'web.perplexity_base_url': perplexityBaseUrl,
|
||||
'web.firecrawl_api_key': firecrawlSearchKey,
|
||||
'web.firecrawl_base_url': firecrawlSearchBaseUrl,
|
||||
'web.searxng_base_url': searxngBaseUrl,
|
||||
'web.chat_completions_api_key': ccKey,
|
||||
'web.chat_completions_base_url': ccBaseUrl,
|
||||
'web.chat_completions_model': ccModel,
|
||||
@@ -165,6 +168,7 @@
|
||||
<option value="tavily">Tavily</option>
|
||||
<option value="brave">Brave</option>
|
||||
<option value="firecrawl">{$t('admin.browserFirecrawl')}</option>
|
||||
<option value="searxng">SearXNG</option>
|
||||
<option value="perplexity">Perplexity</option>
|
||||
<option value="duckduckgo">DuckDuckGo</option>
|
||||
<option value="chat_completions">{$t('admin.webChatCompletions')}</option>
|
||||
@@ -257,6 +261,22 @@
|
||||
{$t('admin.browserFirecrawlBaseUrlHint')}
|
||||
</p>
|
||||
</div>
|
||||
{:else if searchProvider === 'searxng'}
|
||||
<div>
|
||||
<label class="text-xs text-gray-600 dark:text-gray-400" for="searxng-base-url"
|
||||
>{$t('admin.webSearxngBaseUrl')}</label
|
||||
>
|
||||
<input
|
||||
id="searxng-base-url"
|
||||
type="text"
|
||||
bind:value={searxngBaseUrl}
|
||||
placeholder="https://search.example.com"
|
||||
class="w-full mt-1 h-7 px-2 rounded-lg text-xs bg-gray-100 dark:bg-white/6 text-gray-700 dark:text-gray-300 border border-gray-200 dark:border-white/8 outline-none focus:border-blue-400 dark:focus:border-blue-500 transition-colors"
|
||||
/>
|
||||
<p class="text-[0.6875rem] text-gray-400 dark:text-gray-600 mt-0.5">
|
||||
{$t('admin.webSearxngHint')}
|
||||
</p>
|
||||
</div>
|
||||
{:else if searchProvider === 'perplexity'}
|
||||
<div>
|
||||
<label class="text-xs text-gray-600 dark:text-gray-400" for="pplx-key"
|
||||
|
||||
@@ -338,6 +338,8 @@
|
||||
"admin.webFirecrawlKey": "Firecrawl API Key",
|
||||
"admin.webFirecrawlBaseUrl": "Firecrawl Base URL",
|
||||
"admin.webFirecrawlHint": "Uses Firecrawl search. Leave base URL as-is unless you self-host Firecrawl.",
|
||||
"admin.webSearxngBaseUrl": "SearXNG Base URL",
|
||||
"admin.webSearxngHint": "Use a SearXNG instance with JSON output enabled.",
|
||||
"admin.webDuckDuckGoNote": "DuckDuckGo requires no API key (fallback).",
|
||||
"admin.webPerplexityKey": "Perplexity API Key",
|
||||
"admin.webPerplexityHint": "Get your API key at perplexity.ai",
|
||||
|
||||
@@ -6,7 +6,8 @@ Priority in auto mode:
|
||||
3. Tavily (TAVILY_API_KEY or web.tavily_api_key)
|
||||
4. Brave (BRAVE_API_KEY or web.brave_api_key)
|
||||
5. Firecrawl (FIRECRAWL_API_KEY or web.firecrawl_api_key)
|
||||
6. DuckDuckGo (zero-config fallback)
|
||||
6. SearXNG (SEARXNG_BASE_URL or web.searxng_base_url)
|
||||
7. DuckDuckGo (zero-config fallback)
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -50,6 +51,7 @@ async def web_search_handler(query: str) -> str:
|
||||
duckduckgo,
|
||||
chat_completions,
|
||||
firecrawl,
|
||||
searxng,
|
||||
)
|
||||
|
||||
# Check if web access is disabled by admin
|
||||
@@ -74,6 +76,9 @@ async def web_search_handler(query: str) -> str:
|
||||
)
|
||||
if not firecrawl_url:
|
||||
firecrawl_url = await _get_config("browser.firecrawl_base_url")
|
||||
searxng_url = (await _get_config("web.searxng_base_url")) or os.environ.get(
|
||||
"SEARXNG_BASE_URL", ""
|
||||
)
|
||||
cc_key = await _get_key("CHAT_COMPLETIONS_SEARCH_API_KEY", "web.chat_completions_api_key")
|
||||
cc_url = (await _get_config("web.chat_completions_base_url")) or os.environ.get(
|
||||
"CHAT_COMPLETIONS_SEARCH_BASE_URL", ""
|
||||
@@ -111,6 +116,10 @@ async def web_search_handler(query: str) -> str:
|
||||
if firecrawl_url:
|
||||
return await firecrawl.search(query, firecrawl_key, base_url=firecrawl_url)
|
||||
return await firecrawl.search(query, firecrawl_key)
|
||||
elif provider == "searxng":
|
||||
if not searxng_url:
|
||||
return "Error: SearXNG base URL not configured."
|
||||
return await searxng.search(query, searxng_url)
|
||||
elif provider == "duckduckgo":
|
||||
return await duckduckgo.search(query)
|
||||
elif provider == "chat_completions":
|
||||
@@ -139,6 +148,8 @@ async def web_search_handler(query: str) -> str:
|
||||
if firecrawl_key:
|
||||
_fc_kw = {"base_url": firecrawl_url} if firecrawl_url else {}
|
||||
providers.append(("firecrawl", lambda: firecrawl.search(query, firecrawl_key, **_fc_kw)))
|
||||
if searxng_url:
|
||||
providers.append(("searxng", lambda: searxng.search(query, searxng_url)))
|
||||
providers.append(("duckduckgo", lambda: duckduckgo.search(query)))
|
||||
|
||||
for name, fn in providers:
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
"""SearXNG search provider.
|
||||
|
||||
https://docs.searxng.org/dev/search_api.html
|
||||
Queries a configured SearXNG instance's JSON API.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import httpx
|
||||
|
||||
|
||||
def _text(value: object) -> str:
|
||||
if value is None:
|
||||
return ""
|
||||
if isinstance(value, str):
|
||||
return value.strip()
|
||||
return str(value).strip()
|
||||
|
||||
|
||||
def _score(item: dict[str, object]) -> float:
|
||||
try:
|
||||
return float(item.get("score") or 0)
|
||||
except (TypeError, ValueError):
|
||||
return 0
|
||||
|
||||
|
||||
async def search(query: str, base_url: str, count: int = 5) -> str:
|
||||
"""Search using a self-hosted SearXNG instance."""
|
||||
async with httpx.AsyncClient(timeout=15, follow_redirects=True) as client:
|
||||
resp = await client.get(
|
||||
f"{base_url.rstrip('/')}/search",
|
||||
params={"q": query, "format": "json"},
|
||||
headers={"User-Agent": "Mozilla/5.0 (compatible; cptr/1.0)"},
|
||||
)
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
|
||||
parts = []
|
||||
|
||||
for answer in data.get("answers") or []:
|
||||
answer_text = _text(answer)
|
||||
if answer_text:
|
||||
parts.append(f"> {answer_text}")
|
||||
|
||||
for box in (data.get("infoboxes") or [])[:count]:
|
||||
title = _text(box.get("infobox") or box.get("title"))
|
||||
content = _text(box.get("content"))
|
||||
attributes = []
|
||||
for attr in box.get("attributes") or []:
|
||||
label = _text(attr.get("label"))
|
||||
value = _text(attr.get("value"))
|
||||
if label and value:
|
||||
attributes.append(f"{label}: {value}")
|
||||
|
||||
box_parts = []
|
||||
if title:
|
||||
box_parts.append(f"**Infobox - {title}**")
|
||||
if content:
|
||||
box_parts.append(content)
|
||||
box_parts.extend(attributes)
|
||||
if box_parts:
|
||||
parts.append("\n".join(box_parts))
|
||||
|
||||
results = sorted(data.get("results") or [], key=_score, reverse=True)
|
||||
for item in results[:count]:
|
||||
title = _text(item.get("title"))
|
||||
url = _text(item.get("url"))
|
||||
content = _text(item.get("content"))
|
||||
result_parts = []
|
||||
if title:
|
||||
result_parts.append(f"**{title}**")
|
||||
if url:
|
||||
result_parts.append(url)
|
||||
if content:
|
||||
result_parts.append(content)
|
||||
if result_parts:
|
||||
parts.append("\n".join(result_parts))
|
||||
|
||||
suggestions = [_text(item) for item in data.get("suggestions") or []]
|
||||
suggestions = [item for item in suggestions if item]
|
||||
if suggestions:
|
||||
parts.append(f"Suggestions: {', '.join(suggestions)}")
|
||||
|
||||
return "\n\n".join(parts) if parts else "No results found."
|
||||
Reference in new issue
Block a user