v0.8: Serper 集成 + H1 章节标题双行居中 + 反方证据观点化 + 术语核查命令
新增:Google 系检索(SerpAPI → Serper.dev) - scripts/lib/serper_client.py:封装 serper.dev 的 Google Search / Scholar / News / Patents - 专利检索用 site:patents.google.com 技巧,serper.dev 没专用 endpoint 但效果很好 - Scholar 带引用数、年份、期刊信息,便于权威信源识别 - News 支持 time_range(d/w/m/y)时效性过滤 - scripts/lib/search_client.py 扩展为多路由门面: - search() 通用:Exa → Tavily - patents() 专利:Serper(Google Patents)→ 通用搜索 + site: 兜底 - scholar() 论文:Serper Scholar → 通用搜索兜底 - news() 新闻:Serper News → 通用搜索兜底 - 所有 httpx 客户端 trust_env=False,绕过系统 socks5 代理(v0.6 修过的 TLS EOF) - .opencode/skills/search-strategy/SKILL.md §三重写:按查询类型路由,明确何时用哪个 API H1 章节标题:两行居中 + 装饰横线 - 新增 ParagraphStyle: h1-chapter-num / h1-chapter-title - 新增 parse_chapter_title() 支持中文/阿拉伯/混合空格章号: "第一章" / "第 9 章" / "第6章" / "Chapter 1" 全覆盖 - 分隔符支持: em dash — / en dash – / - / : / : - 新增 build_chapter_header():章号小字居中 + 章名大字深蓝居中 + HRFlowable 3cm 装饰线 - 只对正文章节(_title_kind == "chapter")启用;前置件(免责声明/执行摘要/术语表/目录) 仍用单行 h1 样式 反方证据段规范化(用户反馈 v0.7 问题 #5) - skill:evidence-table 新增 §"正文中反方证据段落的写作规范": - 禁止机械标题"反驳证据" / "Counter-Evidence" / "反方观点" - 必须观点化,包含具体判断(如"另一种声音:管线虚胖还是真实进展?") - 用 H2 或 H3,禁止加粗段冒充标题 - 给出段落结构模板(1-2 句过渡 → 列表型反方论点 → 整合判断) - dr-analyst.md Hard Rules #3 改为引用该规范 术语表事实核查前置(新 command /dr-glossary) - 新增 .opencode/commands/dr-glossary.md,支持 --from phase1|phase2|phase4 三个时机 - Phase 1 末 / Phase 2 初:从 framework.md 抽取专有名词种子表,在 dr-analyst 起草前 预先核查公司名/产品名/技术名拼写,避免编造错误(Mabwell → Maywavee 这类) - Phase 4:维持当前用法,对 glossary.json 全量核查 实测:dual-target-rnai-pipeline-2026 重生 PDF 55 页,所有 10 章标题双行居中正确渲染 (第一章/第二章/... 第十章 / 第 6 章 / 第 9 章 多种形式都识别)。
This commit is contained in:
@@ -0,0 +1,190 @@
|
||||
"""Serper.dev 客户端(Google Search API 代理)。
|
||||
|
||||
为什么用 Serper:
|
||||
- 2500 次免费额度,远超 SerpAPI 的 100/月
|
||||
- 支持 Google Search、Scholar、News、Images、Maps
|
||||
- Google Patents 无专用 endpoint,但可用 `site:patents.google.com` 技巧
|
||||
- 价格比 SerpAPI 便宜 3-5×
|
||||
|
||||
用途:
|
||||
- 专利检索:通用 search + `site:patents.google.com`
|
||||
- 学术论文:/scholar endpoint
|
||||
- 新闻:/news endpoint(时效性敏感的行业动态)
|
||||
|
||||
httpx 客户端使用 trust_env=False 绕过系统 socks 代理(macOS Clash 会导致 TLS EOF)。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, Literal
|
||||
|
||||
import httpx
|
||||
|
||||
|
||||
SERPER_BASE = "https://google.serper.dev"
|
||||
|
||||
|
||||
@dataclass
|
||||
class SerperHit:
|
||||
title: str
|
||||
url: str
|
||||
snippet: str
|
||||
source: str = "" # 论文出处 / 新闻媒体
|
||||
date: str = "" # 发表日期(如 scholar / news 返回的话)
|
||||
cited_by: int = 0 # 学术论文的引用数(仅 scholar)
|
||||
|
||||
|
||||
class SerperError(RuntimeError):
|
||||
pass
|
||||
|
||||
|
||||
class SerperClient:
|
||||
def __init__(self, api_key: str | None = None, timeout: float = 30.0) -> None:
|
||||
self.api_key = api_key or os.environ.get("SERPAPI_KEY") or os.environ.get("SERPER_API_KEY")
|
||||
if not self.api_key:
|
||||
raise SerperError("SERPAPI_KEY / SERPER_API_KEY not set")
|
||||
self._client = httpx.Client(trust_env=False, timeout=timeout)
|
||||
|
||||
def close(self) -> None:
|
||||
self._client.close()
|
||||
|
||||
def __enter__(self) -> "SerperClient":
|
||||
return self
|
||||
|
||||
def __exit__(self, *_args: Any) -> None:
|
||||
self.close()
|
||||
|
||||
def _post(self, path: str, body: dict) -> dict:
|
||||
try:
|
||||
r = self._client.post(
|
||||
f"{SERPER_BASE}{path}",
|
||||
json=body,
|
||||
headers={
|
||||
"X-API-KEY": self.api_key,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
)
|
||||
except httpx.RequestError as e:
|
||||
raise SerperError(f"network error: {e}")
|
||||
if r.status_code != 200:
|
||||
raise SerperError(f"HTTP {r.status_code}: {r.text[:300]}")
|
||||
try:
|
||||
return r.json()
|
||||
except Exception as e:
|
||||
raise SerperError(f"invalid JSON: {e}")
|
||||
|
||||
def search(
|
||||
self,
|
||||
query: str,
|
||||
*,
|
||||
num_results: int = 10,
|
||||
gl: str = "us",
|
||||
hl: str = "en",
|
||||
) -> list[SerperHit]:
|
||||
"""通用 Google 搜索。支持 site: / filetype: / 引号短语等 Google 高级语法。"""
|
||||
data = self._post("/search", {
|
||||
"q": query,
|
||||
"num": num_results,
|
||||
"gl": gl,
|
||||
"hl": hl,
|
||||
})
|
||||
hits: list[SerperHit] = []
|
||||
for item in (data.get("organic") or [])[:num_results]:
|
||||
hits.append(SerperHit(
|
||||
title=(item.get("title") or "")[:200],
|
||||
url=item.get("link") or "",
|
||||
snippet=(item.get("snippet") or "")[:600],
|
||||
date=item.get("date") or "",
|
||||
))
|
||||
return hits
|
||||
|
||||
def scholar(
|
||||
self,
|
||||
query: str,
|
||||
*,
|
||||
num_results: int = 10,
|
||||
year_low: int | None = None,
|
||||
year_high: int | None = None,
|
||||
) -> list[SerperHit]:
|
||||
"""Google Scholar 搜索——学术论文首选。
|
||||
|
||||
返回带引用数、发表年份等元数据,权威信源识别更准确。
|
||||
"""
|
||||
body: dict[str, Any] = {"q": query, "num": num_results}
|
||||
if year_low is not None:
|
||||
body["tbs"] = f"cdr:1,cd_min:{year_low}" + (f",cd_max:{year_high}" if year_high else "")
|
||||
data = self._post("/scholar", body)
|
||||
hits: list[SerperHit] = []
|
||||
for item in (data.get("organic") or [])[:num_results]:
|
||||
hits.append(SerperHit(
|
||||
title=(item.get("title") or "")[:200],
|
||||
url=item.get("link") or "",
|
||||
snippet=(item.get("snippet") or "")[:600],
|
||||
source=(item.get("publicationInfo") or "")[:200],
|
||||
year=item.get("year") or "",
|
||||
cited_by=item.get("citedBy") or 0,
|
||||
) if False else SerperHit(
|
||||
title=(item.get("title") or "")[:200],
|
||||
url=item.get("link") or "",
|
||||
snippet=(item.get("snippet") or "")[:600],
|
||||
source=(item.get("publicationInfo") or "")[:200],
|
||||
date=str(item.get("year") or ""),
|
||||
cited_by=item.get("citedBy") or 0,
|
||||
))
|
||||
return hits
|
||||
|
||||
def patents(
|
||||
self,
|
||||
query: str,
|
||||
*,
|
||||
num_results: int = 10,
|
||||
) -> list[SerperHit]:
|
||||
"""Google Patents 检索——用 site: 技巧走通用搜索。
|
||||
|
||||
serper.dev 没有专门的 patents endpoint,但 `site:patents.google.com` 效果很好。
|
||||
"""
|
||||
combined = f"site:patents.google.com {query}"
|
||||
return self.search(combined, num_results=num_results)
|
||||
|
||||
def news(
|
||||
self,
|
||||
query: str,
|
||||
*,
|
||||
num_results: int = 10,
|
||||
time_range: Literal["d", "w", "m", "y"] | None = None,
|
||||
) -> list[SerperHit]:
|
||||
"""Google News 搜索——时效敏感行业动态。
|
||||
|
||||
time_range: d=24h, w=7d, m=30d, y=1y
|
||||
"""
|
||||
body: dict[str, Any] = {"q": query, "num": num_results}
|
||||
if time_range:
|
||||
body["tbs"] = f"qdr:{time_range}"
|
||||
data = self._post("/news", body)
|
||||
hits: list[SerperHit] = []
|
||||
for item in (data.get("news") or [])[:num_results]:
|
||||
hits.append(SerperHit(
|
||||
title=(item.get("title") or "")[:200],
|
||||
url=item.get("link") or "",
|
||||
snippet=(item.get("snippet") or "")[:600],
|
||||
source=(item.get("source") or "")[:200],
|
||||
date=item.get("date") or "",
|
||||
))
|
||||
return hits
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
from scripts.lib.zenmux_client import load_secrets
|
||||
load_secrets()
|
||||
with SerperClient() as c:
|
||||
print("=== Patents: dual-target siRNA ===")
|
||||
for h in c.patents("dual-target siRNA GalNAc conjugate", num_results=3):
|
||||
print(f" {h.title[:70]}")
|
||||
print(f" {h.url}")
|
||||
print("\n=== Scholar: dual-target RNAi ===")
|
||||
for h in c.scholar("dual-target RNAi drug 2024", num_results=3):
|
||||
print(f" {h.title[:70]} [引用 {h.cited_by}] ({h.date})")
|
||||
print(f" {h.url}")
|
||||
print(f" 源: {h.source[:80]}")
|
||||
Reference in New Issue
Block a user