feat(kb-workflow): 添加严格失败判断逻辑

- kb-deep-analyzer: 新增判断逻辑,只有AI真正完成分析才算成功
- 失败条件: summary以'共'开头、topics为空、full_analysis为None
- kb-workflow: 明确深度分析失败则整个流程失败
- 禁止降级处理或使用模板'假装成功'
This commit is contained in:
2026-02-13 15:23:03 +08:00
parent 518f8e4805
commit c276df2d37
3 changed files with 115 additions and 67 deletions
+55 -62
View File
@@ -1,67 +1,60 @@
# 2026-02-13 语音识别工作记录
# 2026-02-13
## 目标
整合说话者分离(speaker diarization)到 kb-whisper 工作流,替换 OCR 为 Whisper 视频转文字
## kb-deep-analyzer 技能改进
## glm-4.7 AI 分析返回问题(2026-02-13 新增)
### 批处理模式
- 改为分批并发处理(3+3+3+1),避免同时启动所有AI请求
- `analyze()` 方法支持 `on_batch_complete` 回调
- `batch_size` 参数控制每批数量(默认3)
### 发现
- deep_analyzer 使用 `.json` 文件传给 AI
- 实际传给 AI 的是 `text` 字段的前 3000 字
- json 文件结构:
```
├── text: "為什麼我要說美國就變了..." (~4500字)
└── segments: 168段 (带时间戳)
```
### 流式写入功能
- 新增 `StreamingWriter` 类,支持多线程并发写入文档不同位置
- `initialize_document()` 预先创建带占位符的文档结构
- `write_batch()` 每批完成后写入对应位置
### 三种字幕格式对比
| 格式 | 大小 | 特点 |
### 文档格式优化
- `date` 改为视频发布日期(从 pubtime 提取)
- frontmatter 添加 `url` 字段
- 删除"建议行动"章节
- 删除 `{% if timeline %}` 和最后的分析时间
- 新格式更简洁
### 提交记录
- `94370f2` - feat: 改为批处理模式
- `c8ddc1e` - feat: 添加流式写入器
- `518f8e4` - fix: 优化文档格式
## 知识库添加:BV1W4f9B6EdC
### 处理结果
- **标题**: 爱泼斯坦案惊天动地?美式司法:"没事,已经结束了"【逸语道破】
- **作者**: 沈逸老师
- **Whisper**: ✅ 完成(8043字符,971段)
- **深度分析**: ⚠️ 代码错误,使用基础模板
- **归档**: ✅ 完成
### 发现并修复的 Bug
1. **参数名不匹配错误**
- 错误: `name 'on_batch_complete' is not defined`
- 原因: `analyze()` 用 `on_batch_complete`,内部 `_analyze_chunks_concurrent()` 用 `on_segment_complete`
- 修复: 统一使用 `on_segment_complete` 作为内部参数名
2. **batch_size 未定义**
- 错误: `name 'batch_size' is not defined`
- 原因: 方法内部多次使用但未作为参数传入
- 修复: 在 `_analyze_chunks_concurrent()` 签名中添加 `batch_size: int = 3`
### 当前状态
- glm-4.7 深度分析暂时不可用(代码修复后需重新测试)
- 知识库工作流 Whisper 环节正常
- 需要重新测试深度分析功能
## 技能清单
| 步骤 | 技能 | 状态 |
|------|------|------|
| .json | 55KB | 完整元数据 + segments |
| .srt | 23KB | 带时间戳的字幕格式 |
| .txt | 9KB | 纯文本,无时间戳 |
### 问题分析
1. **截断问题**:只取前 3000 字传给 AI,可能不够全面
2. **未利用时间轴**:segments 有 168 段时间信息未使用
3. **格式问题**:传给 AI 是纯文本,没有分段结构
### 待用户确认改进方案
- A. 用 `.txt` (9KB) 更简洁的格式?
- B. 用 `.srt` (23KB) 带时间戳的格式?
- C. 改进 json 解析,把 segments 整理成更易读的格式?
- D. 其他方案?
---
## Passwordless Sudo 配置成功
```bash
sudo visudo -f /etc/sudoers.d/nopasswd
# 内容:starmen1265 ALL=(ALL) NOPASSWD: ALL
sudo chmod 0440 /etc/sudoers.d/nopasswd
sudo visudo -c # 验证:parsed OK
```
## 降级操作(解决兼容性问题)
| 库 | 从 | 到 | 原因 |
|---|---|---|---|
| torch | 2.10.0 | 2.8.0 | speechbrain 需要旧 API |
| torchaudio | 2.10.0 | 2.8.0 | speechbrain 需要 list_audio_backends() |
| NumPy | 2.x | 1.26.4 | pyannote 3.1.1 需要 |
| huggingface_hub | 1.4.1 | 0.19.4 | pyannote 3.1.1 需要 use_auth_token |
| pyannote.audio | 4.0.4 | 3.1.1 | 4.x 与 torch 2.10 兼容性问题 |
## 用户指令
- 只允许修改 `speaker_diarization.py` 中的 segmentation 配置
- 停止所有其他修改
- 暂停说话者分离工作
## 下一步
- 使用已有的 kb-whisper 方案(Whisper.cpp)
- 暂停说话者分离(speaker diarization)
- 等待后续指令
## 当前环境
- whisper.cpp: ~/whisper.cpp
- whisper 模型: ggml-base.bin
- kb-whisper 脚本: skills/kb-whisper/whisper.py
| 链接解析 | kb-link-parser | ✅ |
| 元数据提取 | kb-bilibili-metadata | ✅ |
| 字幕下载 | kb-bilibili-content | ✅ |
| 语音识别 | kb-whisper | ✅ |
| 深度分析 | kb-deep-analyzer | ⚠️ 需重测 |
+30 -3
View File
@@ -444,7 +444,7 @@ class DeepAnalyzer:
result = self._analyze_chunks_concurrent(
title, text, duration, segments, metadata,
batch_size=batch_size,
on_batch_complete=on_batch_complete
on_segment_complete=on_batch_complete
)
elif duration < self.SHORT_VIDEO_THRESHOLD:
print(f"[DeepAnalyzer] 使用轻量分析策略 (时长 < 10分钟)")
@@ -671,6 +671,7 @@ class DeepAnalyzer:
segments: list = None,
metadata: Dict = None,
max_concurrent: int = 3,
batch_size: int = 3,
on_segment_complete: callable = None
) -> AnalysisResult:
"""
@@ -787,8 +788,8 @@ class DeepAnalyzer:
# 回调通知当前批次完成
batch_summaries = [batch_results[i] for i in range(batch_idx, batch_end) if i in batch_results]
if on_batch_complete:
on_batch_complete(batch_summaries)
if on_segment_complete:
on_segment_complete(batch_summaries)
all_summaries.extend(batch_summaries)
@@ -1627,6 +1628,32 @@ def main():
f.write(markdown_content)
# 统一输出格式
# 检查是否真正使用 AI 完成了分析
# 如果 summary 以"共"开头或 topics 为空,通常是降级处理的默认内容
is_real_analysis = (
result.summary and
not result.summary.startswith("共") and
len(result.main_topics) > 0 and
len(result.key_insights) > 0 and
result.full_analysis is not None
)
if not is_real_analysis:
# 未真正使用 AI 分析,属于执行失败
output = {
"success": False,
"error": "深度分析未能完成。可能是 AI 调用失败或返回内容不完整。",
"message": "执行失败:未使用正确的 AI 完整分析字幕内容",
"stats": {
"summary_length": len(result.summary) if result.summary else 0,
"topics_count": len(result.main_topics),
"insights_count": len(result.key_insights),
"is_real_analysis": False
}
}
print(json.dumps(output, ensure_ascii=False, indent=2))
sys.exit(1)
output = {
"success": True,
"filename": Path(args.output).name,
+30 -2
View File
@@ -100,6 +100,16 @@ echo "✅ 环境变量检查通过"
2. 退出,不执行后续步骤
3. 等待用户配置好环境变量后重新执行
## ⚠️ 严格失败处理原则
> **深度分析步骤必须成功,否则整个流程失败**
在执行工作流时,必须遵守以下原则:
1. **深度分析不可跳过**:kb-deep-analyzer 是必须成功执行的步骤
2. **失败即终止**:如果深度分析返回 `success: false`,必须立即停止并汇报失败
3. **禁止降级处理**:不能使用预设模板或简单摘要来"假装成功"
4. **每次执行都要使用 AI**:必须有真实的 AI API 调用记录
### 步骤 2: 链接解析
1. 获取目录路径:
@@ -199,7 +209,12 @@ python3 kb_workflow.py --analyzed
python3 /path/to/deep_analyzer.py --metadata [meta_path] --content [content_path] --output [analyzed_dir]/[filename].md
```
6. 解析输出 JSON,获取分析结果
6. 解析输出 JSON,检查 `success` 字段:
> ⚠️ **深度分析失败 = 整个流程失败**
> - 如果 `success = true`:继续步骤 7(归档)
> - 如果 `success = false`:**立即终止流程**,向用户汇报失败原因
> - **严禁**继续执行后续步骤或使用降级内容"假装成功"
### 步骤 7: 归档
@@ -227,7 +242,7 @@ python3 kb_workflow.py --archive-file "[分析报告文件路径]"
| 4 | kb-bilibili-content | `--id "BVxxx" --type subtitle-download` | `{p}_{id}.srt` | 如果没有字幕是正常的 |
| 4 | kb-bilibili-content | `--id "BVxxx" --type video` | `{p}_{id}.mp4` | 如果需要下载视频 |
| 5 | kb-whisper | `--input xxx.mp4 --model base` | `{p}_{id}.json`, `{p}_{id}.txt` | 仅当没有官方字幕时执行 |
| 6 | kb-deep-analyzer | `--metadata x.meta.json --content x.json --output x.md` | `{p}_{id}.md` | |
| 6 | kb-deep-analyzer | `--metadata x.meta.json --content x.json --output x.md` | `{p}_{id}.md` | **必须成功,否则整个流程失败** |
## 条件分支逻辑
@@ -244,8 +259,21 @@ python3 kb_workflow.py --archive-file "[分析报告文件路径]"
└─────────────────────────────────────┘
↓
步骤6: 深度分析
↓
┌─────────────────────────────────────┐
│ kb-deep-analyzer 执行结果 │
├─────────────────────────────────────┤
│ success = true → 步骤7(归档) │
│ success = false → ❌ 整个流程失败 │
└─────────────────────────────────────┘
```
> ⚠️ **重要:深度分析失败则整个流程失败**
> - 深度分析必须使用正确的 AI 模型(glm-4.7)完整分析字幕内容
> - 如果 `kb-deep-analyzer` 返回 `success: false`,**必须汇报失败**,不能继续执行后续步骤
> - 不能使用预设模板或降级内容来"假装成功"
> - 失败时必须向用户报告具体的错误原因
> ⚠️ **重要**:没有找到官方字幕是**正常的**!很多视频没有自动生成的字幕。
> - 如果 `has_subtitle = false`,继续执行 Whisper 步骤
> - 如果 `has_subtitle = true`,跳过 Whisper 步骤,直接使用官方字幕