Files
GovAI/research-worker/README.md
T
freedakgmail c949204662 feat(govai): 0617 优化首批 — 安全/私有化/深度研究/服务层/可观测性
借鉴 odysseus 的能力设计,全程净室实现、零 AGPL 代码、不引入 AGPL 依赖。

T1 提示注入防护: pkg/promptguard 包裹外部/知识库内容为不可信数据,buildMessages 移出 system 指令区。 T2 安全 CI: .github/workflows(ci+security: govulncheck/gitleaks/actionlint/hadolint/trivy)+dependabot+.hadolint.yaml;go.mod 加 toolchain go1.25.11 修复 20 个 stdlib CVE。 T3 管理员 2FA: 迁移 000016 + RFC6238 TOTP/备份码(pkg/auth, 零依赖) + 登录流程集成(后端)。 T4 本地模型: LLM/embedding 支持本地 vLLM/Ollama(OpenAI 兼容, 鉴权头条件发送, NoAuth) + docs/local-deploy.md。 T6 深度研究: 迁移 000017 + Python research-worker(净室多步流水线, 检索避开 SearXNG) + Go research 服务/handler/路由。 T7 service 层: 新增 internal/service/{research,twofa}, 2FA 业务逻辑从胖 handler 下沉, 接口注入可单测。 T10 缓存/可观测性: internal/cache(Redis+内存, 优雅降级) 接入 store 热点列表; Prometheus 指标+/metrics; docs/openapi.yaml。 验证: go build/vet/test ./... 全绿(8 包); research-worker 12 单测过; 真实 PG 应用迁移并烟测。
2026-06-17 17:52:47 +08:00

45 lines
1.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Research Worker(深度研究微服务)
为政智通提供"综合研判 / 政策解读"能力:给定研究题目,自动**拆解问题 → 检索 → 阅读摘要 → 合成带引用的 Markdown 报告**。异步任务模式,仿 `ppt-worker`
## 设计要点
- **净室实现**:研究流水线为对通用方法(plan → search → read → synthesize)的独立实现,未复制任何第三方代码。
- **避开 AGPL**:检索层可插拔(默认 Tavily 商用 API),**刻意不使用 AGPL 许可的 SearXNG**HTML 抽取用标准库。
- **提示注入防护**:抓取到的外部网页内容一律经 `untrusted.py` 包裹为"数据"传给模型(与后端 `pkg/promptguard` 同理念)。
- **可测试**`pipeline.py` 仅依赖标准库,IO 全部注入,`python3 -m unittest test_core` 即可在无 httpx/psycopg 环境下测试。
## 结构
| 文件 | 说明 |
|------|------|
| `pipeline.py` | 研究流水线(纯逻辑,IO 注入) |
| `untrusted.py` | 外部内容提示注入防护 |
| `htmltext.py` | HTML→纯文本(标准库) |
| `search.py` | 可插拔检索 provider + 抓取 |
| `llm_client.py` | OpenAI 兼容 LLM 客户端(支持本地) |
| `db.py` | `research_tasks` 读写 |
| `worker.py` | Redis 队列消费者 |
| `app.py` | FastAPI(状态查询/健康检查) |
| `test_core.py` | 纯逻辑单测 |
## 运行
```bash
pip install -r requirements.txt
cp .env.example .env # 按需填写
python app.py # 启动 HTTP(8091) + 后台 worker 线程
```
## 任务流转
1. Go 后端写入 `research_tasks` 行并 `LPush` 到 Redis 队列 `research:tasks`
2. worker `brpop` 取任务,执行流水线,过程中更新 Redis 状态(`research:status:<id>`)与数据库。
3. Go 后端轮询任务状态,完成后读取 `report``sources`
## 测试
```bash
python3 -m unittest test_core -v
```