Files
auto-ragas/rag_evaluation/docs/debug_ready.md
T
2026-01-16 16:01:39 +08:00

252 lines
5.7 KiB
Markdown

# 文件加载模块调试准备完成
## ✅ 已完成的准备工作
### 1. 文档准备
- ✅ 创建了详细的调试指南: `docs/debug_guide_loader.md`
- ✅ 创建了可执行的调试脚本: `debug_loader.py`
- ✅ 自动生成测试数据文件
### 2. 调试脚本功能
调试脚本 `debug_loader.py` 包含以下功能:
1. **文档加载测试**
- 加载文本文件 (.txt)
- 加载 Markdown 文件 (.md)
- 显示文档元数据和内容预览
2. **采样策略测试**
- 显示采样配置
- 执行文档采样
- 显示每个文档的采样率
3. **文档分块测试**
- 配置分块参数
- 执行文档分块
- 显示分块结果和元数据
### 3. 测试结果
运行 `python debug_loader.py` 的结果:
```
✓ 成功加载 2 个文档
- test.txt (110 字符)
- test.md (147 字符)
✓ 采样完成: 2/2 文档
- 采样率均为 1.00 (小文件)
✓ 分块完成: 4 个块
- 块大小: 100 字符
- 重叠大小: 20 字符
```
## 🎯 关键代码入口点
### 主要模块
1. **document_loader.py**
- `Document` 类 - 文档数据结构
- `DocumentLoader` - 基础加载器
- `TextLoader` - 文本加载器
- `MarkdownLoader` - Markdown 加载器
- `load_documents()` - 批量加载入口函数 ⭐
2. **sampling.py**
- `SamplingStrategy` - 采样策略
- `get_sampling_rate()` - 计算采样率
- `sample_documents()` - 执行采样 ⭐
- `DocumentChunker` - 文档分块器
- `chunk_document()` - 分块单个文档
- `chunk_documents()` - 批量分块 ⭐
### 关键入口函数
```python
# 1. 加载文档 (最常用的入口)
from rag_evaluation.testset.document_loader import load_documents
documents = load_documents(file_paths)
# 2. 采样文档
from rag_evaluation.testset.sampling import SamplingStrategy
from rag_evaluation.core.config import TestsetConfig
config = TestsetConfig()
sampler = SamplingStrategy(config)
sampled_docs = sampler.sample_documents(documents)
# 3. 分块文档
from rag_evaluation.testset.sampling import DocumentChunker
chunker = DocumentChunker(chunk_size=1000, chunk_overlap=200)
chunks = chunker.chunk_documents(documents)
```
## 🔍 调试建议
### 1. 从简单到复杂
建议按以下顺序调试:
1. **单个文件加载** - 测试 `TextLoader` 和 `MarkdownLoader`
2. **批量文件加载** - 测试 `load_documents()`
3. **采样策略** - 测试不同文件大小的采样
4. **文档分块** - 测试分块逻辑和边界
### 2. 使用断点调试
在以下位置设置断点:
```python
# document_loader.py
def load(self): # 第 42 行 (TextLoader)
with open(self.file_path, 'r', encoding='utf-8') as f:
content = f.read() # 在这里设置断点
# sampling.py
def get_sampling_rate(self, file_size_bytes: int): # 第 22 行
file_size_mb = calculate_file_size_mb(file_size_bytes) # 在这里设置断点
def chunk_document(self, document: Document): # 第 83 行
while start < len(content): # 在这里设置断点
```
### 3. 添加日志
在关键位置添加日志输出:
```python
import logging
logger = logging.getLogger(__name__)
# 在 load() 方法中
logger.debug(f"Loading file: {self.file_path}")
logger.info(f"Loaded {len(content)} characters")
# 在 sample_documents() 方法中
logger.debug(f"Sampling rate for {doc.metadata['source']}: {sampling_rate}")
# 在 chunk_document() 方法中
logger.debug(f"Creating chunk {chunk_index}: [{start}:{end}]")
```
### 4. 测试边界情况
测试以下场景:
- ✅ 空文件
- ✅ 超大文件 (>10MB)
- ✅ Unicode 字符
- ✅ 特殊字符文件名
- ✅ 不存在的文件
- ✅ 无权限的文件
## 📊 性能监控
### 使用 cProfile
```python
import cProfile
import pstats
profiler = cProfile.Profile()
profiler.enable()
# 执行加载
documents = load_documents(file_paths)
profiler.disable()
stats = pstats.Stats(profiler)
stats.sort_stats('cumulative')
stats.print_stats(10)
```
### 使用 time 模块
```python
import time
start = time.time()
documents = load_documents(file_paths)
elapsed = time.time() - start
print(f"加载耗时: {elapsed:.2f} 秒")
print(f"平均每个文件: {elapsed/len(file_paths):.2f} 秒")
```
## 🐛 常见问题
### 问题 1: 文件编码错误
**解决方案:**
```python
# 尝试不同编码
encodings = ['utf-8', 'gbk', 'gb2312', 'latin-1']
for encoding in encodings:
try:
with open(file_path, 'r', encoding=encoding) as f:
content = f.read()
break
except UnicodeDecodeError:
continue
```
### 问题 2: 文件路径问题
**解决方案:**
```python
from pathlib import Path
# 使用绝对路径
file_path = Path(file_path).absolute()
# 检查文件是否存在
if not file_path.exists():
raise FileNotFoundError(f"File not found: {file_path}")
```
### 问题 3: 分块边界错误
**解决方案:**
```python
# 确保最后一个块不会超出文档长度
end = min(start + self.chunk_size, len(content))
```
## 📝 下一步
完成文件加载模块调试后,可以继续:
1. **测评集生成器** (`testset/generator.py`)
- 集成 RAGAS TestsetGenerator
- 生成问题和答案
2. **评估执行引擎** (`evaluation/executor.py`)
- RAG 系统集成
- 指标计算
3. **报告生成** (`report/generator.py`)
- 结果聚合
- 报告生成
## 🎓 参考资料
- 调试指南: `docs/debug_guide_loader.md`
- 调试脚本: `debug_loader.py`
- 测试文件: `tests/test_document_loader.py`
- 测试文件: `tests/test_sampling.py`
- 配置文件: `core/config.py`
## 📞 需要帮助?
如果遇到问题,可以:
1. 查看测试文件了解预期行为
2. 运行单元测试: `pytest tests/test_document_loader.py -v`
3. 查看日志输出
4. 使用调试器逐步执行
祝调试顺利! 🚀