mirror of
https://github.com/open-webui/computer.git
synced 2026-11-23 14:10:45 -05:00
refac
This commit is contained in:
1 parent
a4e7eb4373
commit
0fddcba26d
4 files changed
+135
-13
No files matched your search
@@ -19,6 +19,8 @@
|
||||
let braveKey = $state('');
|
||||
let perplexityKey = $state('');
|
||||
let perplexityBaseUrl = $state('');
|
||||
let firecrawlSearchKey = $state('');
|
||||
let firecrawlSearchBaseUrl = $state('https://api.firecrawl.dev');
|
||||
let ccKey = $state('');
|
||||
let ccBaseUrl = $state('');
|
||||
let ccModel = $state('');
|
||||
@@ -46,6 +48,8 @@
|
||||
braveKey = (config['web.brave_api_key'] as string) || '';
|
||||
perplexityKey = (config['web.perplexity_api_key'] as string) || '';
|
||||
perplexityBaseUrl = (config['web.perplexity_base_url'] as string) || '';
|
||||
firecrawlSearchKey = (config['web.firecrawl_api_key'] as string) || '';
|
||||
firecrawlSearchBaseUrl = (config['web.firecrawl_base_url'] as string) || 'https://api.firecrawl.dev';
|
||||
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) || '';
|
||||
@@ -77,6 +81,8 @@
|
||||
'web.brave_api_key': braveKey,
|
||||
'web.perplexity_api_key': perplexityKey,
|
||||
'web.perplexity_base_url': perplexityBaseUrl,
|
||||
'web.firecrawl_api_key': firecrawlSearchKey,
|
||||
'web.firecrawl_base_url': firecrawlSearchBaseUrl,
|
||||
'web.chat_completions_api_key': ccKey,
|
||||
'web.chat_completions_base_url': ccBaseUrl,
|
||||
'web.chat_completions_model': ccModel,
|
||||
@@ -147,6 +153,7 @@
|
||||
<option value="exa">Exa</option>
|
||||
<option value="tavily">Tavily</option>
|
||||
<option value="brave">Brave</option>
|
||||
<option value="firecrawl">{$t('admin.browserFirecrawl')}</option>
|
||||
<option value="perplexity">Perplexity</option>
|
||||
<option value="duckduckgo">DuckDuckGo</option>
|
||||
<option value="chat_completions">{$t('admin.webChatCompletions')}</option>
|
||||
@@ -181,6 +188,19 @@
|
||||
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-[11px] text-gray-400 dark:text-gray-600 mt-0.5">{$t('admin.webBraveHint')}</p>
|
||||
</div>
|
||||
{:else if searchProvider === 'firecrawl'}
|
||||
<div>
|
||||
<label class="text-xs text-gray-600 dark:text-gray-400" for="web-fc-key">{$t('admin.webFirecrawlKey')}</label>
|
||||
<input id="web-fc-key" type="password" bind:value={firecrawlSearchKey} placeholder="fc-..."
|
||||
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-[11px] text-gray-400 dark:text-gray-600 mt-0.5">{$t('admin.webFirecrawlHint')}</p>
|
||||
</div>
|
||||
<div>
|
||||
<label class="text-xs text-gray-600 dark:text-gray-400" for="web-fc-url">{$t('admin.webFirecrawlBaseUrl')}</label>
|
||||
<input id="web-fc-url" type="text" bind:value={firecrawlSearchBaseUrl} placeholder="https://api.firecrawl.dev"
|
||||
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-[11px] text-gray-400 dark:text-gray-600 mt-0.5">{$t('admin.browserFirecrawlBaseUrlHint')}</p>
|
||||
</div>
|
||||
{:else if searchProvider === 'perplexity'}
|
||||
<div>
|
||||
<label class="text-xs text-gray-600 dark:text-gray-400" for="pplx-key">{$t('admin.webPerplexityKey')}</label>
|
||||
|
||||
@@ -304,6 +304,9 @@
|
||||
"admin.webTavilyHint": "Get your API key at tavily.com",
|
||||
"admin.webBraveKey": "Brave API Key",
|
||||
"admin.webBraveHint": "Get your API key at brave.com/search/api",
|
||||
"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.webDuckDuckGoNote": "DuckDuckGo requires no API key (fallback).",
|
||||
"admin.webPerplexityKey": "Perplexity API Key",
|
||||
"admin.webPerplexityHint": "Get your API key at perplexity.ai",
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
"""Firecrawl search provider.
|
||||
|
||||
https://docs.firecrawl.dev/api-reference/endpoint/search
|
||||
Uses the v2 search endpoint and returns web results formatted for LLM context.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import httpx
|
||||
|
||||
DEFAULT_BASE_URL = "https://api.firecrawl.dev"
|
||||
|
||||
|
||||
async def search(
|
||||
query: str,
|
||||
api_key: str,
|
||||
count: int = 5,
|
||||
base_url: str = DEFAULT_BASE_URL,
|
||||
) -> str:
|
||||
"""Search using Firecrawl's search API."""
|
||||
async with httpx.AsyncClient(timeout=30) as client:
|
||||
resp = await client.post(
|
||||
f"{base_url.rstrip('/')}/v2/search",
|
||||
json={
|
||||
"query": query,
|
||||
"limit": count,
|
||||
"sources": ["web"],
|
||||
},
|
||||
headers={
|
||||
"Authorization": f"Bearer {api_key}",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
)
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
|
||||
if not data.get("success", True):
|
||||
error = data.get("error", "Unknown error")
|
||||
return f"Firecrawl error: {error}"
|
||||
|
||||
results = []
|
||||
for item in data.get("data", {}).get("web", [])[:count]:
|
||||
title = item.get("title", "")
|
||||
url = item.get("url", "")
|
||||
description = item.get("description") or item.get("snippet", "")
|
||||
markdown = item.get("markdown", "")
|
||||
|
||||
parts = []
|
||||
if title:
|
||||
parts.append(f"**{title}**")
|
||||
if url:
|
||||
parts.append(url)
|
||||
if description:
|
||||
parts.append(description)
|
||||
if markdown:
|
||||
if len(markdown) > 1500:
|
||||
markdown = markdown[:1500] + "..."
|
||||
parts.append(markdown)
|
||||
|
||||
if parts:
|
||||
results.append("\n".join(parts))
|
||||
|
||||
warning = data.get("warning")
|
||||
if warning and results:
|
||||
results.append(f"Warning: {warning}")
|
||||
|
||||
return "\n\n".join(results) if results else "No results found."
|
||||
+45
-13
@@ -5,7 +5,8 @@ Priority in auto mode:
|
||||
2. Perplexity (PERPLEXITY_API_KEY or web.perplexity_api_key, optional PERPLEXITY_BASE_URL or web.perplexity_base_url)
|
||||
3. Tavily (TAVILY_API_KEY or web.tavily_api_key)
|
||||
4. Brave (BRAVE_API_KEY or web.brave_api_key)
|
||||
5. DuckDuckGo (zero-config fallback)
|
||||
5. Firecrawl (FIRECRAWL_API_KEY or web.firecrawl_api_key)
|
||||
6. DuckDuckGo (zero-config fallback)
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -41,7 +42,15 @@ async def _get_config(key: str) -> str:
|
||||
|
||||
async def web_search_handler(query: str) -> str:
|
||||
"""Search the web using the configured or best available provider."""
|
||||
from cptr.utils.web import exa, perplexity, tavily, brave, duckduckgo, chat_completions
|
||||
from cptr.utils.web import (
|
||||
exa,
|
||||
perplexity,
|
||||
tavily,
|
||||
brave,
|
||||
duckduckgo,
|
||||
chat_completions,
|
||||
firecrawl,
|
||||
)
|
||||
|
||||
# Check if web access is disabled by admin
|
||||
enabled = await _get_config("web.enabled")
|
||||
@@ -52,18 +61,26 @@ async def web_search_handler(query: str) -> str:
|
||||
|
||||
exa_key = await _get_key("EXA_API_KEY", "web.exa_api_key")
|
||||
perplexity_key = await _get_key("PERPLEXITY_API_KEY", "web.perplexity_api_key")
|
||||
perplexity_url = (
|
||||
await _get_config("web.perplexity_base_url")
|
||||
) or os.environ.get("PERPLEXITY_BASE_URL", "")
|
||||
perplexity_url = (await _get_config("web.perplexity_base_url")) or os.environ.get(
|
||||
"PERPLEXITY_BASE_URL", ""
|
||||
)
|
||||
tavily_key = await _get_key("TAVILY_API_KEY", "web.tavily_api_key")
|
||||
brave_key = await _get_key("BRAVE_API_KEY", "web.brave_api_key")
|
||||
firecrawl_key = await _get_key("FIRECRAWL_API_KEY", "web.firecrawl_api_key")
|
||||
if not firecrawl_key:
|
||||
firecrawl_key = await _get_config("browser.firecrawl_api_key")
|
||||
firecrawl_url = (await _get_config("web.firecrawl_base_url")) or os.environ.get(
|
||||
"FIRECRAWL_BASE_URL", ""
|
||||
)
|
||||
if not firecrawl_url:
|
||||
firecrawl_url = await _get_config("browser.firecrawl_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", "")
|
||||
cc_model = (
|
||||
await _get_config("web.chat_completions_model")
|
||||
) or os.environ.get("CHAT_COMPLETIONS_SEARCH_MODEL", "")
|
||||
cc_url = (await _get_config("web.chat_completions_base_url")) or os.environ.get(
|
||||
"CHAT_COMPLETIONS_SEARCH_BASE_URL", ""
|
||||
)
|
||||
cc_model = (await _get_config("web.chat_completions_model")) or os.environ.get(
|
||||
"CHAT_COMPLETIONS_SEARCH_MODEL", ""
|
||||
)
|
||||
|
||||
# Explicit provider mode
|
||||
if provider != "auto":
|
||||
@@ -75,7 +92,11 @@ async def web_search_handler(query: str) -> str:
|
||||
elif provider == "perplexity":
|
||||
if not perplexity_key:
|
||||
return "Error: Perplexity API key not configured."
|
||||
return await perplexity.search(query, perplexity_key, base_url=perplexity_url) if perplexity_url else await perplexity.search(query, perplexity_key)
|
||||
return (
|
||||
await perplexity.search(query, perplexity_key, base_url=perplexity_url)
|
||||
if perplexity_url
|
||||
else await perplexity.search(query, perplexity_key)
|
||||
)
|
||||
elif provider == "tavily":
|
||||
if not tavily_key:
|
||||
return "Error: Tavily API key not configured."
|
||||
@@ -84,6 +105,12 @@ async def web_search_handler(query: str) -> str:
|
||||
if not brave_key:
|
||||
return "Error: Brave API key not configured."
|
||||
return await brave.search(query, brave_key)
|
||||
elif provider == "firecrawl":
|
||||
if not firecrawl_key:
|
||||
return "Error: Firecrawl API key not configured."
|
||||
if firecrawl_url:
|
||||
return await firecrawl.search(query, firecrawl_key, base_url=firecrawl_url)
|
||||
return await firecrawl.search(query, firecrawl_key)
|
||||
elif provider == "duckduckgo":
|
||||
return await duckduckgo.search(query)
|
||||
elif provider == "chat_completions":
|
||||
@@ -102,11 +129,16 @@ async def web_search_handler(query: str) -> str:
|
||||
providers.append(("exa", lambda: exa.search(query, exa_key)))
|
||||
if perplexity_key:
|
||||
_pplx_kw = {"base_url": perplexity_url} if perplexity_url else {}
|
||||
providers.append(("perplexity", lambda: perplexity.search(query, perplexity_key, **_pplx_kw)))
|
||||
providers.append(
|
||||
("perplexity", lambda: perplexity.search(query, perplexity_key, **_pplx_kw))
|
||||
)
|
||||
if tavily_key:
|
||||
providers.append(("tavily", lambda: tavily.search(query, tavily_key)))
|
||||
if brave_key:
|
||||
providers.append(("brave", lambda: brave.search(query, brave_key)))
|
||||
if firecrawl_key:
|
||||
_fc_kw = {"base_url": firecrawl_url} if firecrawl_url else {}
|
||||
providers.append(("firecrawl", lambda: firecrawl.search(query, firecrawl_key, **_fc_kw)))
|
||||
providers.append(("duckduckgo", lambda: duckduckgo.search(query)))
|
||||
|
||||
for name, fn in providers:
|
||||
|
||||
Reference in new issue
Block a user