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
8c68566c80
commit
a9cf9661b4
13 files changed
+170
-11
No files matched your search
@@ -15,6 +15,9 @@
|
||||
let tavilyKey = $state('');
|
||||
let braveKey = $state('');
|
||||
let perplexityKey = $state('');
|
||||
let ccKey = $state('');
|
||||
let ccBaseUrl = $state('');
|
||||
let ccModel = $state('');
|
||||
|
||||
async function loadConfig() {
|
||||
try {
|
||||
@@ -23,6 +26,9 @@
|
||||
tavilyKey = config['web.tavily_api_key'] || '';
|
||||
braveKey = config['web.brave_api_key'] || '';
|
||||
perplexityKey = config['web.perplexity_api_key'] || '';
|
||||
ccKey = config['web.chat_completions_api_key'] || '';
|
||||
ccBaseUrl = config['web.chat_completions_base_url'] || '';
|
||||
ccModel = config['web.chat_completions_model'] || '';
|
||||
} catch {
|
||||
toast.error($t('admin.failedToLoadConfig'));
|
||||
} finally {
|
||||
@@ -110,6 +116,7 @@
|
||||
<option value="brave">Brave</option>
|
||||
<option value="perplexity">Perplexity</option>
|
||||
<option value="duckduckgo">DuckDuckGo</option>
|
||||
<option value="chat_completions">{$t('admin.webChatCompletions')}</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
@@ -190,6 +197,51 @@
|
||||
{$t('admin.webPerplexityHint')}
|
||||
</p>
|
||||
</div>
|
||||
{:else if provider === 'chat_completions'}
|
||||
<div class="mt-3 space-y-3">
|
||||
<div>
|
||||
<label class="block text-[13px] text-gray-700 dark:text-gray-300 mb-1"
|
||||
>{$t('admin.webCcBaseUrl')}</label
|
||||
>
|
||||
<input
|
||||
type="text"
|
||||
class="w-full text-[13px] bg-gray-50 dark:bg-white/4 border border-gray-200 dark:border-white/8 rounded-lg px-2.5 py-1.5 outline-none text-gray-700 dark:text-gray-300 placeholder:text-gray-400 dark:placeholder:text-gray-600"
|
||||
placeholder="https://api.perplexity.ai/v1"
|
||||
bind:value={ccBaseUrl}
|
||||
onblur={() => saveKey('web.chat_completions_base_url', ccBaseUrl)}
|
||||
disabled={saving}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label class="block text-[13px] text-gray-700 dark:text-gray-300 mb-1"
|
||||
>{$t('admin.webCcKey')}</label
|
||||
>
|
||||
<input
|
||||
type="password"
|
||||
class="w-full text-[13px] bg-gray-50 dark:bg-white/4 border border-gray-200 dark:border-white/8 rounded-lg px-2.5 py-1.5 outline-none text-gray-700 dark:text-gray-300 placeholder:text-gray-400 dark:placeholder:text-gray-600"
|
||||
placeholder="sk-..."
|
||||
bind:value={ccKey}
|
||||
onblur={() => saveKey('web.chat_completions_api_key', ccKey)}
|
||||
disabled={saving}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label class="block text-[13px] text-gray-700 dark:text-gray-300 mb-1"
|
||||
>{$t('admin.webCcModel')}</label
|
||||
>
|
||||
<input
|
||||
type="text"
|
||||
class="w-full text-[13px] bg-gray-50 dark:bg-white/4 border border-gray-200 dark:border-white/8 rounded-lg px-2.5 py-1.5 outline-none text-gray-700 dark:text-gray-300 placeholder:text-gray-400 dark:placeholder:text-gray-600"
|
||||
placeholder="sonar-pro"
|
||||
bind:value={ccModel}
|
||||
onblur={() => saveKey('web.chat_completions_model', ccModel)}
|
||||
disabled={saving}
|
||||
/>
|
||||
</div>
|
||||
<p class="text-[11px] text-gray-400 dark:text-gray-600">
|
||||
{$t('admin.webCcHint')}
|
||||
</p>
|
||||
</div>
|
||||
{/if}
|
||||
{/if}
|
||||
|
||||
|
||||
@@ -260,5 +260,10 @@
|
||||
"admin.webBraveHint": "API-Schlüssel unter brave.com/search/api erhalten",
|
||||
"admin.webDuckDuckGoNote": "DuckDuckGo benötigt keinen API-Schlüssel (Fallback).",
|
||||
"admin.webPerplexityKey": "Perplexity API-Schlüssel",
|
||||
"admin.webPerplexityHint": "API-Schlüssel unter perplexity.ai erhalten"
|
||||
"admin.webPerplexityHint": "API-Schlüssel unter perplexity.ai erhalten",
|
||||
"admin.webChatCompletions": "Chat Completions",
|
||||
"admin.webCcBaseUrl": "Basis-URL",
|
||||
"admin.webCcKey": "API-Schlüssel",
|
||||
"admin.webCcModel": "Modell",
|
||||
"admin.webCcHint": "Beliebiger OpenAI-kompatibler Endpunkt (z. B. Perplexity Sonar, LiteLLM-Proxy)"
|
||||
}
|
||||
@@ -261,5 +261,10 @@
|
||||
"admin.webBraveHint": "Get your API key at brave.com/search/api",
|
||||
"admin.webDuckDuckGoNote": "DuckDuckGo requires no API key (fallback).",
|
||||
"admin.webPerplexityKey": "Perplexity API Key",
|
||||
"admin.webPerplexityHint": "Get your API key at perplexity.ai"
|
||||
"admin.webPerplexityHint": "Get your API key at perplexity.ai",
|
||||
"admin.webChatCompletions": "Chat Completions",
|
||||
"admin.webCcBaseUrl": "Base URL",
|
||||
"admin.webCcKey": "API Key",
|
||||
"admin.webCcModel": "Model",
|
||||
"admin.webCcHint": "Use any OpenAI-compatible endpoint (e.g. Perplexity Sonar, LiteLLM proxy)"
|
||||
}
|
||||
@@ -260,5 +260,10 @@
|
||||
"admin.webBraveHint": "Obtén tu clave de API en brave.com/search/api",
|
||||
"admin.webDuckDuckGoNote": "DuckDuckGo no requiere clave de API (respaldo).",
|
||||
"admin.webPerplexityKey": "Clave de API de Perplexity",
|
||||
"admin.webPerplexityHint": "Obtén tu clave de API en perplexity.ai"
|
||||
"admin.webPerplexityHint": "Obtén tu clave de API en perplexity.ai",
|
||||
"admin.webChatCompletions": "Chat Completions",
|
||||
"admin.webCcBaseUrl": "URL base",
|
||||
"admin.webCcKey": "Clave de API",
|
||||
"admin.webCcModel": "Modelo",
|
||||
"admin.webCcHint": "Cualquier endpoint compatible con OpenAI (ej. Perplexity Sonar, proxy LiteLLM)"
|
||||
}
|
||||
@@ -260,5 +260,10 @@
|
||||
"admin.webBraveHint": "Obtenez votre clé API sur brave.com/search/api",
|
||||
"admin.webDuckDuckGoNote": "DuckDuckGo ne nécessite aucune clé API (solution de repli).",
|
||||
"admin.webPerplexityKey": "Clé API Perplexity",
|
||||
"admin.webPerplexityHint": "Obtenez votre clé API sur perplexity.ai"
|
||||
"admin.webPerplexityHint": "Obtenez votre clé API sur perplexity.ai",
|
||||
"admin.webChatCompletions": "Chat Completions",
|
||||
"admin.webCcBaseUrl": "URL de base",
|
||||
"admin.webCcKey": "Clé API",
|
||||
"admin.webCcModel": "Modèle",
|
||||
"admin.webCcHint": "Tout endpoint compatible OpenAI (ex. Perplexity Sonar, proxy LiteLLM)"
|
||||
}
|
||||
@@ -260,5 +260,10 @@
|
||||
"admin.webBraveHint": "brave.com/search/apiでAPIキーを取得してください",
|
||||
"admin.webDuckDuckGoNote": "DuckDuckGoはAPIキー不要です(フォールバック)。",
|
||||
"admin.webPerplexityKey": "Perplexity APIキー",
|
||||
"admin.webPerplexityHint": "perplexity.ai でAPIキーを取得"
|
||||
"admin.webPerplexityHint": "perplexity.ai でAPIキーを取得",
|
||||
"admin.webChatCompletions": "Chat Completions",
|
||||
"admin.webCcBaseUrl": "ベースURL",
|
||||
"admin.webCcKey": "APIキー",
|
||||
"admin.webCcModel": "モデル",
|
||||
"admin.webCcHint": "OpenAI互換エンドポイント(例: Perplexity Sonar、LiteLLMプロキシ)"
|
||||
}
|
||||
@@ -260,5 +260,10 @@
|
||||
"admin.webBraveHint": "brave.com/search/api에서 API 키를 받으세요",
|
||||
"admin.webDuckDuckGoNote": "DuckDuckGo는 API 키가 필요 없습니다(대체).",
|
||||
"admin.webPerplexityKey": "Perplexity API 키",
|
||||
"admin.webPerplexityHint": "perplexity.ai에서 API 키를 받으세요"
|
||||
"admin.webPerplexityHint": "perplexity.ai에서 API 키를 받으세요",
|
||||
"admin.webChatCompletions": "Chat Completions",
|
||||
"admin.webCcBaseUrl": "기본 URL",
|
||||
"admin.webCcKey": "API 키",
|
||||
"admin.webCcModel": "모델",
|
||||
"admin.webCcHint": "OpenAI 호환 엔드포인트 사용 (예: Perplexity Sonar, LiteLLM 프록시)"
|
||||
}
|
||||
@@ -260,5 +260,10 @@
|
||||
"admin.webBraveHint": "Obtenha sua chave de API em brave.com/search/api",
|
||||
"admin.webDuckDuckGoNote": "DuckDuckGo não exige chave de API (fallback).",
|
||||
"admin.webPerplexityKey": "Chave de API do Perplexity",
|
||||
"admin.webPerplexityHint": "Obtenha sua chave de API em perplexity.ai"
|
||||
"admin.webPerplexityHint": "Obtenha sua chave de API em perplexity.ai",
|
||||
"admin.webChatCompletions": "Chat Completions",
|
||||
"admin.webCcBaseUrl": "URL base",
|
||||
"admin.webCcKey": "Chave de API",
|
||||
"admin.webCcModel": "Modelo",
|
||||
"admin.webCcHint": "Qualquer endpoint compatível com OpenAI (ex. Perplexity Sonar, proxy LiteLLM)"
|
||||
}
|
||||
@@ -260,5 +260,10 @@
|
||||
"admin.webBraveHint": "Получите API-ключ на brave.com/search/api",
|
||||
"admin.webDuckDuckGoNote": "DuckDuckGo не требует API-ключа (резервный вариант).",
|
||||
"admin.webPerplexityKey": "API-ключ Perplexity",
|
||||
"admin.webPerplexityHint": "Получите API-ключ на perplexity.ai"
|
||||
"admin.webPerplexityHint": "Получите API-ключ на perplexity.ai",
|
||||
"admin.webChatCompletions": "Chat Completions",
|
||||
"admin.webCcBaseUrl": "Базовый URL",
|
||||
"admin.webCcKey": "API-ключ",
|
||||
"admin.webCcModel": "Модель",
|
||||
"admin.webCcHint": "Любой OpenAI-совместимый эндпоинт (напр. Perplexity Sonar, прокси LiteLLM)"
|
||||
}
|
||||
@@ -260,5 +260,10 @@
|
||||
"admin.webBraveHint": "在 brave.com/search/api 获取你的 API 密钥",
|
||||
"admin.webDuckDuckGoNote": "DuckDuckGo 不需要 API 密钥(备用)。",
|
||||
"admin.webPerplexityKey": "Perplexity API 密钥",
|
||||
"admin.webPerplexityHint": "在 perplexity.ai 获取你的 API 密钥"
|
||||
"admin.webPerplexityHint": "在 perplexity.ai 获取你的 API 密钥",
|
||||
"admin.webChatCompletions": "Chat Completions",
|
||||
"admin.webCcBaseUrl": "基础 URL",
|
||||
"admin.webCcKey": "API 密钥",
|
||||
"admin.webCcModel": "模型",
|
||||
"admin.webCcHint": "使用任何 OpenAI 兼容端点(如 Perplexity Sonar、LiteLLM 代理)"
|
||||
}
|
||||
@@ -260,5 +260,10 @@
|
||||
"admin.webBraveHint": "在 brave.com/search/api 取得你的 API 金鑰",
|
||||
"admin.webDuckDuckGoNote": "DuckDuckGo 不需要 API 金鑰(備用)。",
|
||||
"admin.webPerplexityKey": "Perplexity API 金鑰",
|
||||
"admin.webPerplexityHint": "在 perplexity.ai 取得你的 API 金鑰"
|
||||
"admin.webPerplexityHint": "在 perplexity.ai 取得你的 API 金鑰",
|
||||
"admin.webChatCompletions": "Chat Completions",
|
||||
"admin.webCcBaseUrl": "基礎 URL",
|
||||
"admin.webCcKey": "API 金鑰",
|
||||
"admin.webCcModel": "模型",
|
||||
"admin.webCcHint": "使用任何 OpenAI 相容端點(如 Perplexity Sonar、LiteLLM 代理)"
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
"""Chat Completions search provider: any OpenAI-compatible endpoint.
|
||||
|
||||
Send the search query as a user message to a /chat/completions endpoint
|
||||
and return the model's response. Works with Perplexity Sonar (web-grounded),
|
||||
LiteLLM proxy, or any model that returns search-grounded results.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import httpx
|
||||
|
||||
_SYSTEM_PROMPT = (
|
||||
"You are a web search assistant. Given the user's query, provide a concise, "
|
||||
"factual answer with relevant sources. Format each result as:\n"
|
||||
"**Title**\nURL\nSnippet\n\n"
|
||||
"Include up to 5 results. Cite your sources with URLs when possible."
|
||||
)
|
||||
|
||||
|
||||
async def search(
|
||||
query: str,
|
||||
api_key: str,
|
||||
base_url: str,
|
||||
model: str,
|
||||
) -> str:
|
||||
"""Search using an OpenAI-compatible chat completions endpoint."""
|
||||
async with httpx.AsyncClient(timeout=30) as client:
|
||||
resp = await client.post(
|
||||
f"{base_url.rstrip('/')}/chat/completions",
|
||||
json={
|
||||
"model": model,
|
||||
"messages": [
|
||||
{"role": "system", "content": _SYSTEM_PROMPT},
|
||||
{"role": "user", "content": query},
|
||||
],
|
||||
},
|
||||
headers={
|
||||
"Authorization": f"Bearer {api_key}",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
)
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
|
||||
content = data.get("choices", [{}])[0].get("message", {}).get("content", "")
|
||||
return content or "No results found."
|
||||
@@ -41,7 +41,7 @@ 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
|
||||
from cptr.utils.web import exa, perplexity, tavily, brave, duckduckgo, chat_completions
|
||||
|
||||
# Check if web access is disabled by admin
|
||||
enabled = await _get_config("web.enabled")
|
||||
@@ -54,6 +54,13 @@ async def web_search_handler(query: str) -> str:
|
||||
perplexity_key = await _get_key("PERPLEXITY_API_KEY", "web.perplexity_api_key")
|
||||
tavily_key = await _get_key("TAVILY_API_KEY", "web.tavily_api_key")
|
||||
brave_key = await _get_key("BRAVE_API_KEY", "web.brave_api_key")
|
||||
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", "")
|
||||
|
||||
# Explicit provider mode
|
||||
if provider != "auto":
|
||||
@@ -76,6 +83,10 @@ async def web_search_handler(query: str) -> str:
|
||||
return await brave.search(query, brave_key)
|
||||
elif provider == "duckduckgo":
|
||||
return await duckduckgo.search(query)
|
||||
elif provider == "chat_completions":
|
||||
if not cc_key or not cc_url or not cc_model:
|
||||
return "Error: Chat Completions search requires API key, base URL, and model."
|
||||
return await chat_completions.search(query, cc_key, cc_url, cc_model)
|
||||
else:
|
||||
return f"Error: unknown search provider '{provider}'."
|
||||
except Exception as e:
|
||||
|
||||
Reference in new issue
Block a user