diff --git a/cptr/frontend/src/lib/components/Admin/Settings.svelte b/cptr/frontend/src/lib/components/Admin/Settings.svelte index 2727056..bafd500 100644 --- a/cptr/frontend/src/lib/components/Admin/Settings.svelte +++ b/cptr/frontend/src/lib/components/Admin/Settings.svelte @@ -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 @@ + @@ -190,6 +197,51 @@ {$t('admin.webPerplexityHint')}

+ {:else if provider === 'chat_completions'} +
+
+ + saveKey('web.chat_completions_base_url', ccBaseUrl)} + disabled={saving} + /> +
+
+ + saveKey('web.chat_completions_api_key', ccKey)} + disabled={saving} + /> +
+
+ + saveKey('web.chat_completions_model', ccModel)} + disabled={saving} + /> +
+

+ {$t('admin.webCcHint')} +

+
{/if} {/if} diff --git a/cptr/frontend/src/lib/i18n/locales/de.json b/cptr/frontend/src/lib/i18n/locales/de.json index 766c61e..66bea88 100644 --- a/cptr/frontend/src/lib/i18n/locales/de.json +++ b/cptr/frontend/src/lib/i18n/locales/de.json @@ -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)" } diff --git a/cptr/frontend/src/lib/i18n/locales/en.json b/cptr/frontend/src/lib/i18n/locales/en.json index d3c3081..f4a2b9c 100644 --- a/cptr/frontend/src/lib/i18n/locales/en.json +++ b/cptr/frontend/src/lib/i18n/locales/en.json @@ -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)" } diff --git a/cptr/frontend/src/lib/i18n/locales/es.json b/cptr/frontend/src/lib/i18n/locales/es.json index 4e8b28d..d1a7b38 100644 --- a/cptr/frontend/src/lib/i18n/locales/es.json +++ b/cptr/frontend/src/lib/i18n/locales/es.json @@ -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)" } diff --git a/cptr/frontend/src/lib/i18n/locales/fr.json b/cptr/frontend/src/lib/i18n/locales/fr.json index c4e7061..163e6c1 100644 --- a/cptr/frontend/src/lib/i18n/locales/fr.json +++ b/cptr/frontend/src/lib/i18n/locales/fr.json @@ -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)" } diff --git a/cptr/frontend/src/lib/i18n/locales/ja.json b/cptr/frontend/src/lib/i18n/locales/ja.json index fc08b6a..2f57291 100644 --- a/cptr/frontend/src/lib/i18n/locales/ja.json +++ b/cptr/frontend/src/lib/i18n/locales/ja.json @@ -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プロキシ)" } diff --git a/cptr/frontend/src/lib/i18n/locales/ko.json b/cptr/frontend/src/lib/i18n/locales/ko.json index 9027111..d9215a5 100644 --- a/cptr/frontend/src/lib/i18n/locales/ko.json +++ b/cptr/frontend/src/lib/i18n/locales/ko.json @@ -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 프록시)" } diff --git a/cptr/frontend/src/lib/i18n/locales/pt-BR.json b/cptr/frontend/src/lib/i18n/locales/pt-BR.json index 0cbbd4d..ce2e890 100644 --- a/cptr/frontend/src/lib/i18n/locales/pt-BR.json +++ b/cptr/frontend/src/lib/i18n/locales/pt-BR.json @@ -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)" } diff --git a/cptr/frontend/src/lib/i18n/locales/ru.json b/cptr/frontend/src/lib/i18n/locales/ru.json index 3dae60f..b5e5ff5 100644 --- a/cptr/frontend/src/lib/i18n/locales/ru.json +++ b/cptr/frontend/src/lib/i18n/locales/ru.json @@ -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)" } diff --git a/cptr/frontend/src/lib/i18n/locales/zh-CN.json b/cptr/frontend/src/lib/i18n/locales/zh-CN.json index e032637..078e361 100644 --- a/cptr/frontend/src/lib/i18n/locales/zh-CN.json +++ b/cptr/frontend/src/lib/i18n/locales/zh-CN.json @@ -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 代理)" } diff --git a/cptr/frontend/src/lib/i18n/locales/zh-TW.json b/cptr/frontend/src/lib/i18n/locales/zh-TW.json index b02a9b5..13e5863 100644 --- a/cptr/frontend/src/lib/i18n/locales/zh-TW.json +++ b/cptr/frontend/src/lib/i18n/locales/zh-TW.json @@ -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 代理)" } diff --git a/cptr/utils/web/chat_completions.py b/cptr/utils/web/chat_completions.py new file mode 100644 index 0000000..1dcf8fb --- /dev/null +++ b/cptr/utils/web/chat_completions.py @@ -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." diff --git a/cptr/utils/web/search.py b/cptr/utils/web/search.py index b536344..9ddcfed 100644 --- a/cptr/utils/web/search.py +++ b/cptr/utils/web/search.py @@ -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: