- Remove 3 dead modules: academic/, wechat/, social/ (merged into engines/) - Remove 4 stale news scripts (tech_news.py, wallstreetcn.py, cls.py, rss.py) - scripts/core/base.py: canonical SearchResult + BaseEngine (was in engines/base.py) - scripts/engines/base.py: re-exports from core.base for compatibility - scripts/engines/news_engine.py: Bing News + RSS unified (was in scripts/news/rss.py) - scripts/engines/academic_engine.py: ArXiv + CrossRef unified (was in scripts/academic/arxiv.py) - scripts/cli.py: complete rewrite — 10 commands (search/web/news/academic/wechat/social/rss/baidu/health/sources) - scripts/unified_search.py: clean async Python API (was a stub) - ws.py: lightweight launcher - README.md + SKILL.md: updated to reflect actual 15+ engines - All EngineResult → SearchResult - All engines return list[SearchResult]
144 lines
4.8 KiB
Python
144 lines
4.8 KiB
Python
"""
|
|
Baidu Qianfan AI Search API engine.
|
|
|
|
Uses the official Baidu Qianfan (千帆) AI Search API to get
|
|
structured search results. Requires BAIDU_API_KEY environment variable
|
|
or .env file in the project root.
|
|
|
|
API docs: https://console.bce.baidu.com/qianfan/ais/console/apiKey
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import logging
|
|
import os
|
|
from pathlib import Path
|
|
from typing import Any, Optional
|
|
|
|
import httpx
|
|
|
|
from scripts.core.base import BaseEngine, SearchResult
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
API_URL = "https://qianfan.baidubce.com/v2/ai_search/web_search"
|
|
ENV_KEY = "BAIDU_API_KEY"
|
|
|
|
|
|
def _load_env_file() -> dict[str, str]:
|
|
"""Load .env file from project root (simple parser, no external deps)."""
|
|
env_path = Path(__file__).resolve().parent.parent.parent / ".env"
|
|
if not env_path.exists():
|
|
return {}
|
|
env_vars = {}
|
|
with open(env_path, "r", encoding="utf-8") as f:
|
|
for line in f:
|
|
line = line.strip()
|
|
if not line or line.startswith("#") or "=" not in line:
|
|
continue
|
|
key, _, val = line.partition("=")
|
|
env_vars[key.strip()] = val.strip().strip("\"'")
|
|
return env_vars
|
|
|
|
|
|
def _get_api_key() -> str:
|
|
"""Get API key: env var > .env file > config."""
|
|
key = os.getenv(ENV_KEY)
|
|
if key:
|
|
return key
|
|
env_from_file = _load_env_file()
|
|
return env_from_file.get(ENV_KEY, "")
|
|
|
|
|
|
class BaiduQianFanEngine(BaseEngine):
|
|
"""百度千帆 AI 搜索 API 引擎."""
|
|
|
|
name = "baidu_qianfan"
|
|
|
|
def __init__(self, config: Optional[dict] = None, timeout: float = 30.0):
|
|
super().__init__(timeout=timeout)
|
|
self.api_key = (config or {}).get("api_key", "") or _get_api_key()
|
|
if not self.api_key:
|
|
logger.warning(
|
|
"BAIDU_API_KEY not set. Set it in .env or as environment variable."
|
|
)
|
|
self._client = httpx.AsyncClient(
|
|
timeout=timeout,
|
|
headers={
|
|
"Authorization": f"Bearer {self.api_key}",
|
|
"X-Appbuilder-From": "openclaw",
|
|
"Content-Type": "application/json",
|
|
},
|
|
follow_redirects=True,
|
|
)
|
|
|
|
async def search(
|
|
self, query: str, max_results: int = 10, **kwargs
|
|
) -> list[SearchResult]:
|
|
"""Search via Baidu Qianfan AI Search API."""
|
|
if not self.api_key:
|
|
return [
|
|
SearchResult(
|
|
title="错误: BAIDU_API_KEY 未配置",
|
|
url="https://console.bce.baidu.com/qianfan/ais/console/apiKey",
|
|
snippet="请先设置 BAIDU_API_KEY 环境变量",
|
|
source=self.name,
|
|
rank=1,
|
|
category="web",
|
|
extra={"error": "missing_api_key"},
|
|
)
|
|
]
|
|
|
|
top_k = min(max_results, 50)
|
|
resource_type_filter = [{"type": "web", "top_k": top_k}]
|
|
|
|
search_filter: dict[str, Any] = {}
|
|
if kwargs.get("site"):
|
|
search_filter["match"] = {"site": [kwargs["site"]]}
|
|
|
|
request_body: dict[str, Any] = {
|
|
"messages": [{"content": query, "role": "user"}],
|
|
"edition": kwargs.get("edition", "standard"),
|
|
"search_source": "baidu_search_v2",
|
|
"resource_type_filter": resource_type_filter,
|
|
"search_recency_filter": kwargs.get("recency", "year"),
|
|
"safe_search": kwargs.get("safe_search", False),
|
|
}
|
|
if search_filter:
|
|
request_body["search_filter"] = search_filter
|
|
if kwargs.get("block_websites"):
|
|
request_body["block_websites"] = kwargs["block_websites"]
|
|
|
|
try:
|
|
response = await self._client.post(API_URL, json=request_body)
|
|
response.raise_for_status()
|
|
data = response.json()
|
|
|
|
if "code" in data:
|
|
logger.error(f"Baidu Qianfan API error: {data.get('message', '')}")
|
|
return []
|
|
|
|
results = []
|
|
for i, ref in enumerate(data.get("references", [])[:max_results]):
|
|
results.append(SearchResult(
|
|
title=ref.get("title", ""),
|
|
url=ref.get("url", ""),
|
|
snippet=ref.get("snippet", ""),
|
|
source=self.name,
|
|
rank=i + 1,
|
|
category="web",
|
|
extra={k: ref[k] for k in ("site", "date", "type", "source") if k in ref},
|
|
))
|
|
return results
|
|
|
|
except httpx.HTTPStatusError as e:
|
|
logger.error(f"Baidu Qianfan HTTP {e.response.status_code}: {e}")
|
|
return []
|
|
except httpx.RequestError as e:
|
|
logger.error(f"Baidu Qianfan request failed: {e}")
|
|
return []
|
|
|
|
async def close(self):
|
|
await self._client.aclose()
|